Module 2: UVM Foundations | Beginner UVM

Factory Creation Patterns

Learn how to replace UVM objects and components without modifying reusable testbench code.

Learning objectives

By the end of this lesson, you will be able to:

  • Explain what a factory override does.
  • Use a type override to replace one UVM type with another.
  • Use an instance override to replace a specific factory-created object.
  • Understand the difference between type and instance overrides.
  • Use the factory to customize a test without modifying reusable components.
  • Recognize common factory override mistakes and debug them.

Before you start

In Lesson 17, you learned that UVM can create objects through the factory instead of constructing them directly with new().

For example:

packet pkt;


pkt = packet::type_id::create("pkt");

The important idea was:

type_id::create()
    ↓
 Factory
    ↓


requested type

In this lesson, we add one more possibility:

type_id::create()
    ↓
 Factory
    ↓


"Is there an override?"
↙          ↘
YES           NO
↓             ↓
replacement    requested type
Key idea: A factory override tells UVM: "When code asks for this type, create a different compatible type instead."

Step 1: Why use a factory override?

Imagine that a reusable sequence creates a normal packet:

packet pkt;


pkt = packet::type_id::create("pkt");

The sequence was designed to work with the base packet type. Now suppose one test wants to generate an error packet instead.

One option would be to edit the sequence:

error_packet pkt;


pkt = new("pkt");

But this destroys an important property of reusable verification code: the sequence is no longer independent of the specific test behavior.

The factory gives us a better solution.

Reusable sequence
   ↓


packet::type_id::create()
↓
Factory
↓
error_packet

The sequence remains unchanged. The test controls the substitution.

Professional mindset: Factory overrides allow a test to customize reusable verification code without modifying that reusable code.

Step 2: Build a base and derived type

Start with a base transaction:

class packet extends uvm_sequence_item;


rand bit [7:0] addr;
rand bit       write;

`uvm_object_utils_begin(packet)
    `uvm_field_int(addr,  UVM_DEFAULT)
`uvm_field_int(write, UVM_DEFAULT)
  `uvm_object_utils_end

function new(string name = "packet");
super.new(name);
endfunction

endclass

Now create a specialized transaction:

class error_packet extends packet;


rand bit error;

`uvm_object_utils_begin(error_packet)
    `uvm_field_int(error, UVM_DEFAULT)
`uvm_object_utils_end

function new(string name = "error_packet");
super.new(name);
endfunction

endclass

Notice that error_packet extends packet. This matters because the factory replacement must be type-compatible with the code receiving the returned object.

Important: A factory override is not a license to substitute any unrelated class. The replacement type must be compatible with the requested type.

Step 3: Create the base type through the factory

Our reusable code continues to request packet:

packet pkt;


pkt = packet::type_id::create("pkt");

Without an override, the factory creates a packet.

requested type:
packet


factory result:
packet

Now we can change the factory configuration without changing the creation code.

Step 4: Type override

A type override says:

"Whenever the factory is asked to create this type, use this replacement type instead."

For example, conceptually:

packet → error_packet

After the override is installed, code such as:

packet::type_id::create("pkt");

can produce an error_packet instead of a packet.

The creation site does not need to change.

// Reusable code


packet pkt;

pkt = packet::type_id::create("pkt");

The test can control the implementation through the factory.

Step 5: Installing a type override

UVM provides factory methods for installing overrides. One common pattern uses the factory singleton:

uvm_factory factory;


factory = uvm_factory::get();

A type override can then be registered using the factory API. For example:

factory.set_type_override_by_type(


packet::get_type(),
error_packet::get_type()
);

This means:

requested:
packet


replacement:
error_packet

Whenever the factory resolves a request for packet, the override can cause error_packet to be created.

Read the API as a sentence: set_type_override_by_type(base, replacement) means "replace this requested type with this replacement type."

Step 6: Verify what the factory created

Factory behavior should be observable. Do not assume that an override worked just because the test ran.

One useful technique is to inspect the object's actual type:

packet pkt;


pkt = packet::type_id::create("pkt");

`uvm_info("FACTORY",
$sformatf("Created type = %s", pkt.get_type_name()),
UVM_MEDIUM)

Before the override, you would expect the type name to indicate packet.

After the override, you should see the replacement type, such as error_packet.

Verification habit: When debugging factory behavior, inspect the actual type that was created instead of assuming the override is active.

Step 7: Instance override

A type override affects requests for a type broadly. Sometimes that is too aggressive.

Imagine that an environment creates two packets:

packet request_packet;


packet response_packet;

request_packet =
packet::type_id::create("request_packet", this);

response_packet =
packet::type_id::create("response_packet", this);

Suppose we want only request_packet to become an error_packet.

An instance override provides more precise control.

request_packet → error_packet


response_packet → packet
Key idea: An instance override targets a particular factory creation location, rather than every request for the type.

Step 8: Set an instance override

The factory provides an API for this purpose:

factory.set_inst_override_by_type(


packet::get_type(),
error_packet::get_type(),
"env.agent.request_packet"
);

The important part is the instance path:

env.agent.request_packet

This tells the factory that the replacement applies at that particular creation location.

The exact instance path depends on the hierarchy of your UVM testbench.

Common mistake: An instance override only works when the factory creation request and instance path match the intended hierarchy. Incorrect paths are a common reason for an override appearing not to work.

Step 9: Type override vs instance override

These two patterns solve different problems.

Pattern Meaning Use when...
Type override Replace a type broadly You want requests for a type to use another implementation.
Instance override Replace a specific creation location Only one particular object/component should be replaced.

A useful mental shortcut is:

Type override
↓


"Replace this TYPE."

Instance override
↓
"Replace this INSTANCE."

Step 10: Components can use the factory too

Factory overrides are not limited to sequence items. UVM components can also participate in factory creation.

For example:

class my_driver extends uvm_driver #(packet);


`uvm_component_utils(my_driver)

function new(string name, uvm_component parent);
super.new(name, parent);
endfunction

endclass

A testbench component can create the driver through the factory:

drv = my_driver::type_id::create("drv", this);

This allows a test to replace the driver implementation without modifying the environment's construction code.

For example:

my_driver
↓


special_driver

This becomes particularly useful when a special test needs a driver with additional behavior, error injection, logging, or protocol experimentation.

Step 11: A practical override scenario

Consider a reusable verification environment for a bus. The normal environment contains a standard driver:

env


└── agent
├── driver
├── monitor
└── sequencer

Most tests should use the normal driver.

But one negative test needs to inject protocol errors.

Instead of modifying the normal driver:

normal_driver

create a specialized driver:

error_injecting_driver extends normal_driver

Then use the factory to substitute it for the appropriate test.

normal_driver
  ↓


Factory override
↓
error_injecting_driver
This is where the factory becomes valuable: the reusable environment remains unchanged while the test gains specialized behavior.

Step 12: Factory debugging

Factory problems can be confusing because the code may look correct while the factory creates a different type than expected.

When debugging an override, check these items in order:

  1. Is the class registered?
    Verify the appropriate UVM registration macro is present.
  2. Is the object created through the factory?
    Look for type_id::create() rather than direct new().
  3. Is the override installed before creation?
    The factory must know about the override when the object is created.
  4. Is the replacement type compatible?
    The replacement must be appropriate for the requested type.
  5. Is the instance path correct?
    This matters especially for instance overrides.
  6. What type was actually created?
    Use type information and factory debugging facilities to verify the result.
Debugging rule: Never debug a factory override only by looking at the override call. Trace the entire path: registration → create() → override → actual type.

Step 13: Common mistakes

Mistake 1: Using new() after installing an override

This bypasses the factory:

pkt = new("pkt");

If you expect a factory override to affect the creation, use factory-based creation:

pkt = packet::type_id::create("pkt");

Mistake 2: Registering only the base type

The replacement type must also be registered appropriately for factory use.

Mistake 3: Using an instance override when a type override is needed

If every instance of a type should be replaced, a type override is usually the clearer choice.

Mistake 4: Using a type override when only one instance should change

A broad type override can unexpectedly change multiple objects. Use an instance override when the replacement must be limited to a specific hierarchy location.

Mistake 5: Forgetting the creation path

An override cannot affect an object that was never created through the factory.

Quick check

Question 1

A test wants every request for packet to create error_packet. Which override concept fits best?

Show answer

A type override, because the replacement should apply broadly to requests for the type.

Question 2

An environment creates two packet objects, but only one should become an error_packet. Which override concept is appropriate?

Show answer

An instance override, because only a particular creation location should be replaced.

Question 3

Will a factory override affect this code?

packet pkt;


pkt = new("pkt");
Show answer

No. The object is created directly with new(), so the factory is bypassed for that creation.

Question 4

Why must the replacement type be compatible with the requested type?

Show answer

The code requesting the original type still expects an object that can be used as that type. An incompatible replacement would break the type relationship expected by the receiving code.

Try it yourself

Build a small factory override experiment.

  1. Create a packet class extending uvm_sequence_item.
  2. Create an error_packet class extending packet.
  3. Register both classes with the factory.
  4. Create the object using packet::type_id::create().
  5. Install a type override from packet to error_packet.
  6. Create another packet through the factory.
  7. Print the actual type name of both objects.

Your goal is to observe that the same creation request can produce a different implementation after the factory override is installed.

Challenge: Modify the experiment so that two creation locations exist. Use an instance override so that only one of them creates error_packet.

Professional tip

Do not use factory overrides just because UVM has them.

Use them when they improve reuse, configurability, or test-specific behavior. A simple testbench does not become better merely because it contains more factory overrides.

Key takeaways

  • A factory override changes what the factory creates without changing the reusable creation code.
  • A type override replaces a requested type broadly.
  • An instance override targets a specific factory creation location.
  • Factory overrides require factory-based creation; direct new() bypasses the factory.
  • Replacement types must be compatible with the requested type.
  • Factory overrides are especially useful for test-specific behavior such as error injection and specialized components.
  • Always verify the actual type created when debugging factory behavior.

What's next?

You now know how the factory can substitute one implementation for another.

The next step is to learn how UVM environments use configuration objects to pass reusable settings between tests and components.

Next lesson: Configuration Objects — design reusable configuration data that controls your UVM environment.