Factory Type Overrides
Use the UVM factory to replace reusable component or object implementations by type, without modifying the environment that creates them.
Learning objectives
- Explain why the UVM factory exists and what problem type overrides solve.
- Distinguish factory overrides from
uvm_config_dbconfiguration. - Register UVM components and objects so they can participate in factory creation.
- Use
set_type_override_by_type()to replace one registered type with another. - Understand why the replacement type must be compatible with the requested type.
- Recognize the importance of
type_id::create()for factory-based construction. - Apply a type override from a test without modifying the reusable environment.
- Verify that an override actually took effect.
- Identify common reasons a factory type override appears not to work.
- Understand the boundary between type overrides and the instance-specific overrides introduced in Lesson 45.
Step 1: What problem does the factory solve?
Consider a reusable environment containing a protocol driver:
protocol_env
|
+-- protocol_agent
|
+-- protocol_driver
The environment creates the driver because the agent needs one:
driver = protocol_driver::type_id::create(
"driver",
this
);
Now suppose one test needs a special driver that injects errors:
normal test:
protocol_driver
error-injection test:
error_injecting_driver
Without the factory, the reusable agent would need test-specific conditional logic such as:
if (some_test_specific_condition)
driver = error_injecting_driver::create(...);
else
driver = protocol_driver::create(...);
That couples the reusable agent to a particular test scenario.
The factory provides another approach:
Reusable agent
|
| asks factory for protocol_driver
v
UVM factory
|
+---- normal configuration -> protocol_driver
|
+---- test override -------> error_injecting_driver
Step 2: Configuration versus factory override
Lesson 43 dealt with configuration scope and precedence. Factory overrides solve a different problem.
| Mechanism | Changes | Example |
|---|---|---|
uvm_config_db |
Settings and policy | timeout, VIF, active/passive mode |
| Factory type override | Implementation type | normal driver → error-injecting driver |
For example:
Configuration:
timeout_cycles = 500
Factory:
protocol_driver -> error_injecting_driver
The configuration changes how the selected implementation behaves. The factory determines which implementation is created in the first place.
Step 3: Understand factory-based construction
A factory override only matters when the requested object or component is created through the factory.
Preferred:
driver = protocol_driver::type_id::create(
"driver",
this
);
Not factory-controlled:
driver = new("driver", this);
The second form directly constructs the requested class. A factory override cannot substitute the implementation when construction bypasses the factory.
Step 4: Register the base and replacement types
The factory needs to know the types involved.
A component class normally registers itself with:
class protocol_driver extends uvm_driver #(protocol_item);
`uvm_component_utils(protocol_driver)
function new(
string name = "protocol_driver",
uvm_component parent = null
);
super.new(name, parent);
endfunction
endclass
The replacement component is registered too:
class error_injecting_driver extends protocol_driver;
`uvm_component_utils(error_injecting_driver)
function new(
string name = "error_injecting_driver",
uvm_component parent = null
);
super.new(name, parent);
endfunction
endclass
The inheritance relationship is important because the agent expects a protocol_driver.
protocol_driver
^
|
|
error_injecting_driver
The replacement must be usable wherever the original requested type is expected.
Step 5: Create the component through the factory
The reusable agent should request the base type:
class protocol_agent extends uvm_agent;
`uvm_component_utils(protocol_agent)
protocol_driver driver;
function void build_phase(uvm_phase phase);
super.build_phase(phase);
if (is_active == UVM_ACTIVE) begin
driver = protocol_driver::type_id::create(
"driver",
this
);
end
endfunction
endclass
The agent does not need to know about error_injecting_driver.
That is an important reusable-environment property:
Agent knows:
"I need a protocol_driver."
Agent does NOT need to know:
"Which test wants to replace it?" Step 6: Apply a type override
A test can configure the factory before the component is created:
uvm_factory factory;
factory = uvm_factory::get();
factory.set_type_override_by_type(
protocol_driver::get_type(),
error_injecting_driver::get_type()
);
A more common form uses the global factory directly:
uvm_factory::get().set_type_override_by_type(
protocol_driver::get_type(),
error_injecting_driver::get_type()
);
Conceptually, this tells the factory:
When someone requests:
protocol_driver
Create:
error_injecting_driver
The agent source code remains unchanged.
Step 7: Put the override in the test
The test is usually the right place to express test-specific implementation substitution.
class error_injection_test extends uvm_test;
`uvm_component_utils(error_injection_test)
function new(
string name = "error_injection_test",
uvm_component parent = null
);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
uvm_factory::get().set_type_override_by_type(
protocol_driver::get_type(),
error_injecting_driver::get_type()
);
super.build_phase(phase);
endfunction
endclass
The important requirement is that the override be established before the affected factory creation occurs.
For a normal UVM testbench, placing the override in the test's build_phase() before calling super.build_phase(phase) is a useful pattern when the environment is constructed by the superclass build flow.
Step 8: See what actually happens
Without an override:
protocol_agent
|
v
protocol_driver::type_id::create()
|
v
protocol_driver
With the type override:
protocol_agent
|
v
protocol_driver::type_id::create()
|
v
factory lookup
|
v
error_injecting_driver
The requested type is still protocol_driver. The factory resolves that request to the replacement type.
This distinction is central to understanding UVM factory behavior.
Step 9: Verify the replacement at run time
A useful first check is to report the actual component type:
`uvm_info(
"FACTORY",
$sformatf(
"Created driver type = %s",
driver.get_type_name()
),
UVM_MEDIUM
)
For the overridden test, the expected output should identify the replacement type:
Created driver type = error_injecting_driver
This is better than assuming that the override worked because the simulation started successfully.
Step 10: Understand why inheritance matters
Suppose the agent declares:
protocol_driver driver;
and the factory creates:
error_injecting_driver
This works because the replacement derives from the requested type:
class error_injecting_driver extends protocol_driver;
The object stored in the handle is therefore substitutable:
protocol_driver handle;
handle = error_injecting_driver::type_id::create(...);
The agent can continue using the base-class interface while the derived implementation changes the behavior.
Step 11: Override objects as well as components
The factory is not limited to UVM components. UVM objects such as sequence items and sequences can also participate in factory creation when registered and constructed through the factory.
For example:
class protocol_item extends uvm_sequence_item;
`uvm_object_utils(protocol_item)
rand bit [31:0] address;
rand bit [31:0] data;
function new(string name = "protocol_item");
super.new(name);
endfunction
endclass
A specialized item can derive from it:
class error_protocol_item extends protocol_item;
`uvm_object_utils(error_protocol_item)
function new(string name = "error_protocol_item");
super.new(name);
endfunction
endclass
Then a factory-created request can be redirected to the specialized type.
This is useful when a reusable sequence or component requests a base transaction type while a particular test wants a specialized implementation.
Step 12: Know what a type override affects
A type override is broad by design.
protocol_driver
|
+-- factory type override
|
v
error_injecting_driver
Any matching factory creation request for the original type can be affected.
This is useful when every instance should change:
agent_a -> error_injecting_driver
agent_b -> error_injecting_driver
agent_c -> error_injecting_driver
But it is the wrong mechanism when only one particular instance should change:
agent_a -> normal driver
agent_b -> error-injecting driver
agent_c -> normal driver
That situation leads to the instance override mechanism covered in Lesson 45.
Step 13: Understand type override versus instance override
| Requirement | Factory mechanism |
|---|---|
Replace every matching protocol_driver |
Type override |
Replace only env.agent_b.driver |
Instance override |
| Change timeout for one agent | Configuration |
| Change implementation for a test | Factory override |
The choice should follow the intended scope of the behavior change.
Step 14: Debug a type override that did not work
Suppose the test expects:
error_injecting_driver
but the environment still creates:
protocol_driver
Use this sequence:
Override did not appear
|
v
Is the replacement registered?
|
v
Is the requested type registered?
|
v
Does the environment use type_id::create()?
|
v
Was the override established before creation?
|
v
Is the override registered for the correct base type?
|
v
Is another override replacing it?
|
v
What does the factory report?
|
v
What type was actually constructed? Step 15: Inspect the factory
When factory behavior becomes complicated, inspect the factory's registered information.
uvm_factory::get().print();
This can help expose registered types and active overrides.
Factory debugging should answer two separate questions:
1. Is the type known to the factory?
2. Is the requested type currently mapped
to a replacement?
Do not assume that successful compilation means the override is active.
Step 16: Understand override timing
Factory decisions occur when the affected object or component is created.
Therefore, this is too late:
build_phase:
create driver
run_phase:
set type override
The driver already exists by the time the override is established.
The useful mental model is:
Register types
|
v
Install override
|
v
Factory create()
|
v
Factory resolves requested type
|
v
Replacement constructed
Factory overrides are therefore normally established before the relevant build-time construction occurs.
Step 17: Avoid hiding test intent inside reusable components
A reusable agent should not contain knowledge such as:
if (test_name == "error_test")
create error_injecting_driver;
Instead:
Reusable agent:
requests protocol_driver
Test:
installs desired factory override
Factory:
performs substitution
This keeps the environment generic and makes the test's architectural intent visible where the test is defined.
Step 18: Recognize when not to use a type override
Factory overrides are powerful, but they should not become the default mechanism for every behavioral difference.
| Need | Better mechanism |
|---|---|
| Different timeout | Configuration object |
| Different VIF | Configuration |
| Enable/disable checking | Configuration or test policy |
| Replace implementation class | Factory override |
| Replace one specific instance | Instance factory override |
| Change runtime transaction behavior dynamically | Runtime control/sequence/TLM mechanism |
Common mistakes
- Using
newinstead oftype_id::create(). Direct construction bypasses the factory. - Forgetting to register the replacement. The factory must know the replacement type.
- Replacing an incompatible type. The derived replacement must be usable through the requested base type.
- Installing the override too late. The affected object may already have been constructed.
- Assuming a type override affects only one instance. Type overrides are broad; Lesson 45 covers instance-specific substitution.
- Using factory overrides to change ordinary settings. Timeout, VIF, and enable flags normally belong in configuration.
- Putting test-specific factory logic inside the reusable agent. Keep implementation substitution in the test or appropriate configuration layer.
- Not checking the actual constructed type. Verify the override rather than assuming it worked.
- Ignoring another active override. Multiple overrides can make factory behavior difficult to reason about.
- Assuming successful compilation proves factory behavior. Compilation only proves the code is legal; it does not prove the intended override was selected.
Practical implementation pattern
A compact reusable pattern looks like this:
// Base implementation
class protocol_driver extends uvm_driver #(protocol_item);
`uvm_component_utils(protocol_driver)
function new(
string name = "protocol_driver",
uvm_component parent = null
);
super.new(name, parent);
endfunction
endclass
// Replacement implementation
class error_injecting_driver extends protocol_driver;
`uvm_component_utils(error_injecting_driver)
function new(
string name = "error_injecting_driver",
uvm_component parent = null
);
super.new(name, parent);
endfunction
endclass
// Reusable agent
class protocol_agent extends uvm_agent;
`uvm_component_utils(protocol_agent)
protocol_driver driver;
function void build_phase(uvm_phase phase);
super.build_phase(phase);
if (is_active == UVM_ACTIVE) begin
driver = protocol_driver::type_id::create(
"driver",
this
);
end
endfunction
endclass
// Test-specific substitution
class error_test extends uvm_test;
`uvm_component_utils(error_test)
function void build_phase(uvm_phase phase);
uvm_factory::get().set_type_override_by_type(
protocol_driver::get_type(),
error_injecting_driver::get_type()
);
super.build_phase(phase);
endfunction
endclass
The reusable environment has no dependency on the replacement class. The test supplies the substitution policy.
Exercise: replace a driver
Create:
base_driver
debug_driver extends base_driver
Build an agent that creates base_driver using:
base_driver::type_id::create()
Then create a test that installs a type override:
base_driver -> debug_driver
Verify the actual type using get_type_name().
Expected behavior: the agent source remains unchanged, but the test constructs debug_driver.
Debugging exercise: the override is ignored
You have:
class special_driver extends protocol_driver;
`uvm_component_utils(special_driver)
endclass
The test installs:
uvm_factory::get().set_type_override_by_type(
protocol_driver::get_type(),
special_driver::get_type()
);
But the agent contains:
driver = new("driver", this);
The observed type remains protocol_driver.
Explain why.
Answer: the agent bypasses the factory by directly constructing the object. Change the creation to:
driver = protocol_driver::type_id::create(
"driver",
this
); Challenge: build a test-specific driver
Create a reusable protocol environment with:
protocol_env
|
+-- agent_a
| +-- driver
|
+-- agent_b
+-- driver
Implement:
- A normal
protocol_driver. - An
error_injecting_driverderived from it. - Factory registration for both.
- Factory-based creation inside the reusable agent.
- A test that type-overrides
protocol_driver. - A diagnostic report showing the actual driver type.
Then answer:
What happens to agent_a?
What happens to agent_b?
Does the type override affect both?
What would you change if only agent_b
should use error_injecting_driver?
The final question is the bridge to Lesson 45.
Knowledge check
-
What does a factory type override change?
It changes which registered implementation type is created when the overridden type is requested through the factory. -
Why use the factory?
It allows reusable environments to request base types while tests can substitute compatible implementations without modifying the environment source. -
Why is
type_id::create()important?
It routes construction through the UVM factory, allowing factory overrides to affect the result. -
Why must the replacement be compatible?
The reusable component expects the interface and behavior exposed by the requested base type. -
Where should a test-specific override normally be established?
Before the affected components or objects are created, commonly during test build-time setup. -
Does a type override normally target one instance?
No. A type override is broad; an instance override is used when only a particular hierarchy instance should change. -
Should timeout values normally use the factory?
No. Ordinary policy and settings belong in configuration. -
How can you verify an override?
Report the actual constructed type and inspect the factory when necessary. -
What is the most common reason an override appears ineffective?
The affected component was not created through the factory, or the override was installed after construction.
Key takeaways
- The UVM factory separates reusable component code from test-specific implementation choices.
- A type override replaces a requested registered type with a compatible replacement type.
- Factory-based construction, such as
type_id::create(), is required for the override to take effect. - Base and replacement types must be registered appropriately.
- The reusable agent should request the base type rather than knowing about every possible replacement.
- Type overrides are broad: they are appropriate when all matching instances should use the replacement.
- Configuration and factory overrides solve different problems: policy versus implementation.
- Factory overrides must be established before the affected construction occurs.
- Always verify the actual constructed type when debugging factory behavior.
- If only one hierarchy instance should change, use the instance override mechanism covered next.
What comes next?
The configuration and factory sequence now becomes:
Lesson 41
Configuration Database Patterns
|
v
Lesson 42
Configuration Object Design
|
v
Lesson 43
Configuration Scope and Precedence
|
v
Lesson 44
Factory Type Overrides
|
v
Lesson 45
Factory Instance Overrides
|
v
Lesson 46
Factory Debugging
Lesson 44 answered:
"How can I replace an implementation type
throughout the relevant factory scope?"
Lesson 45 narrows that problem:
"How can I replace the implementation
for one particular hierarchy instance?"
You will use the same factory architecture, but with instance-specific matching rather than a broad type-level substitution.