Reusable Agent Configuration
Learn how to package agent settings into reusable configuration objects so the same UVM agent can support different interfaces, modes, and test scenarios without modifying its implementation.
Learning objectives
- Explain why reusable UVM agents should separate configuration from behavior.
- Design an agent configuration object containing mode, interface, and timing settings.
- Configure multiple agent instances independently through
uvm_config_db. - Validate required configuration and report invalid settings early.
- Explain how configuration objects improve reuse across tests and environments.
Step 1: Understand the concept
A reusable UVM agent should not hard-code assumptions about how every test will use it. One test may require an active agent that drives transactions, while another may need a passive agent that only monitors the DUT.
Reusable agent configuration solves this by collecting settings in a dedicated configuration object. The agent reads the configuration during construction or build-time setup and uses those settings to determine how its components should operate.
A typical agent configuration may contain:
- is_active: whether the agent drives traffic or only observes it.
- virtual interface: the DUT interface used by the driver and monitor.
- timeout_cycles: a protocol-specific timing limit.
- checks_enable: whether optional protocol checks are enabled.
- coverage_enable: whether the agent's coverage collector is active.
Step 2: Build a configuration object
Start by defining a configuration object that extends uvm_object. Keep related settings together instead of placing many unrelated values directly in the configuration database.
class agent_config extends uvm_object;
`uvm_object_utils(agent_config)
virtual bus_if vif;
uvm_active_passive_enum is_active = UVM_ACTIVE;
int unsigned timeout_cycles = 100;
bit checks_enable = 1;
bit coverage_enable = 1;
function new(string name = "agent_config");
super.new(name);
endfunction
endclass
This object gives the agent one well-defined configuration contract. A test can create different configuration objects for different agent instances without changing the agent's source code.
Why use a configuration object?
Passing individual values through many uvm_config_db entries quickly becomes difficult to maintain. A single configuration object makes the relationship between settings explicit.
uvm_config_db#(agent_config)::set(
this,
"env.agent0",
"cfg",
agent0_cfg
);
The agent can then retrieve one object and access all of its related settings.
Step 3: Consume the configuration inside the agent
The agent retrieves its configuration during build_phase. A missing required configuration should normally be treated as a configuration error rather than silently replaced with an unintended default.
class bus_agent extends uvm_agent;
`uvm_component_utils(bus_agent)
agent_config cfg;
bus_driver driver;
bus_monitor monitor;
bus_sequencer sequencer;
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
super.build_phase(phase);
if (!uvm_config_db#(agent_config)::get(
this, "", "cfg", cfg
))
`uvm_fatal("CFG",
"bus_agent requires an agent_config object")
monitor = bus_monitor::type_id::create("monitor", this);
if (cfg.is_active == UVM_ACTIVE) begin
driver = bus_driver::type_id::create("driver", this);
sequencer =
bus_sequencer::type_id::create("sequencer", this);
end
endfunction
endclass
Notice that the agent does not ask the test whether it should create a driver. It reads the configuration and makes that architectural decision itself.
Step 4: Configure active and passive instances
The same agent type can now be instantiated in different modes.
agent_config agent0_cfg;
agent_config agent1_cfg;
agent0_cfg = agent_config::type_id::create("agent0_cfg");
agent1_cfg = agent_config::type_id::create("agent1_cfg");
agent0_cfg.is_active = UVM_ACTIVE;
agent1_cfg.is_active = UVM_PASSIVE;
agent0_cfg.timeout_cycles = 50;
agent1_cfg.timeout_cycles = 200;
uvm_config_db#(agent_config)::set(
this, "env.agent0", "cfg", agent0_cfg
);
uvm_config_db#(agent_config)::set(
this, "env.agent1", "cfg", agent1_cfg
);
In this example, agent0 can drive stimulus while agent1 only monitors. Both instances use the same reusable agent implementation.
This is one of the major benefits of configuration objects: one implementation can support multiple roles through controlled configuration.
Step 5: Validate configuration early
Reusable configuration should be validated before the simulation reaches a confusing protocol failure.
function void validate();
if (vif == null)
`uvm_fatal("CFG", "Virtual interface is not configured")
if (timeout_cycles == 0)
`uvm_fatal("CFG", "timeout_cycles must be greater than zero")
endfunction
The test can call validate() after creating the configuration, or the agent can validate the configuration when it retrieves it.
Early validation changes a difficult runtime failure into a clear configuration diagnostic such as:
UVM_FATAL CFG: Virtual interface is not configured
That is much easier to diagnose than discovering several simulation cycles later that a driver has no interface handle.
Step 6: Separate shared and instance-specific settings
Not every setting should necessarily be identical across all agents. Decide which values belong to the environment and which belong to an individual agent instance.
| Setting | Typical ownership | Reason |
|---|---|---|
| Virtual interface | Agent instance | Different agents may connect to different interfaces. |
| Active/passive mode | Agent instance | One interface may drive while another only monitors. |
| Timeout | Agent instance or environment | Protocols or interfaces may have different timing requirements. |
| Global test mode | Environment/test | The same policy may apply to many components. |
Good configuration architecture makes ownership obvious. If a value has unclear ownership, configuration bugs become much harder to track.
Step 7: Build it in small pieces
- Create the configuration class. Start with the virtual interface and active/passive mode.
- Add sensible defaults. Defaults should represent safe, intentional behavior rather than hiding missing configuration.
- Pass the object through
uvm_config_db. Use a precise instance path whenever practical. - Retrieve the configuration in the agent. Treat missing required configuration as an error.
- Use the configuration to build the agent. For example, create the driver and sequencer only for an active agent.
- Validate the settings. Check interfaces, ranges, and incompatible combinations.
- Run multiple instances. Give each instance different settings and verify that the settings remain isolated.
Step 8: Debug configuration problems
When an agent behaves differently from what the test expects, make the resolved configuration visible.
`uvm_info("CFG",
$sformatf(
"Agent=%s mode=%s timeout=%0d checks=%0b coverage=%0b",
get_full_name(),
cfg.is_active == UVM_ACTIVE ? "ACTIVE" : "PASSIVE",
cfg.timeout_cycles,
cfg.checks_enable,
cfg.coverage_enable
),
UVM_MEDIUM)
Check the following in order:
- Was the configuration object created?
- Was it set at the expected hierarchy?
- Did the agent retrieve the intended object?
- Are the values inside the object correct?
- Did the agent interpret the settings correctly?
- Did multiple agent instances accidentally share mutable state?
Printing the resolved configuration at build time often reveals scope mistakes immediately.
Try it yourself
Build a reusable agent_config class and use it to configure two agent instances.
- Give
agent0an active mode with a timeout of50cycles. - Give
agent1a passive mode with a timeout of200cycles. - Assign a different virtual interface to each instance.
- Make the agent create its driver and sequencer only when configured as active.
- Add configuration validation for a missing interface and an invalid timeout.
- Print the final resolved configuration for each agent.
Challenge: add checks_enable and coverage_enable. Demonstrate that two instances of the same agent can independently enable and disable these features.
Key takeaways
- A reusable agent should separate configuration data from protocol implementation.
- A dedicated configuration object keeps related settings together and gives the agent a clear configuration contract.
uvm_config_dbcan distribute different configuration objects to different agent instances.- Active/passive mode is a practical example of how one reusable agent can support different verification roles.
- Validate required configuration early so missing interfaces and invalid settings produce useful diagnostics.
- Make resolved configuration observable during debug to expose hierarchy and value-selection problems.
- Good configuration architecture increases reuse without turning the testbench into a collection of hidden global settings.