Day 5: uvm_config_db and the Factory — Configuring and Swapping Components
A 30-day SystemVerilog/UVM verification series. Days 1–3 built the skeleton and put it in motion. Day 4 wired the plumbing. Today we add the two controls that turn a one-off testbench into a reusable one.
By now your FIFO env works. But look at what's still hardcoded. The virtual interface is grabbed with a literal "vif" string and an assumed path. The driver class is fixed — you can't swap in an error-injecting driver for a stress test without editing the agent. The scoreboard's expected model can't be told to stand down when you're debugging something else.
Today's two mechanisms fix all of that. uvm_config_db is how values travel from the test down to components. The factory is how types get substituted per-test without touching env or agent code. They're taught together because they compose: the config object itself is created via the factory, so a test can substitute a richer config class as easily as it substitutes a driver.
1. uvm_config_db: The Path Rules
The API is two calls:
// Set — usually from the test, or from the top module for vifs
uvm_config_db#(T)::set(cntxt, inst_name, field_name, value);
// Get — inside a component's build_phase
if (!uvm_config_db#(T)::get(this, "", "field_name", var))
`uvm_fatal("CFG", "not found")
The first two arguments concatenate into the lookup scope. Get the mental model right and you'll never chase a "why is my get returning 0" again:
cntxt | inst_name | Effective scope |
|---|---|---|
null | "uvm_test_top.env.agent" | Absolute path |
this (in test) | "env.agent" | Relative to the test |
this (in test) | "*" | Every component under the test |
this (in agent) | "" | The agent itself |
null as context with the absolute path "uvm_test_top". That's the standard way to hand virtual interfaces to the test hierarchy before run_test() is called.
// In the top module
initial begin
uvm_config_db#(virtual fifo_if)::set(null, "uvm_test_top", "vif", fifo_vif);
run_test();
end
// In the driver's build_phase
if (!uvm_config_db#(virtual fifo_if)::get(this, "", "vif", vif))
`uvm_fatal("NOVIF", {"no vif for ", get_full_name()})
2. The Classic Path Bug
Here is the config_db mistake that costs people the most time:
// In test
uvm_config_db#(virtual fifo_if)::set(this, "env.agent.drv", "vif", vif);
// In driver build_phase
if (!uvm_config_db#(virtual fifo_if)::get(this, "", "vif", vif)) // 0! fails
The set targeted the path ...agent.drv. The get, called from inside the driver with this and "", resolves to the driver's own full name. If the driver was actually created as "driver" (not "drv"), the paths don't match and the get returns 0.
Two fixes, both fine:
- Wildcard the set side:
set(this, "*", "vif", vif). Any descendant whose get asks for"vif"finds it. This is what most tests do for scalar values. - Match names exactly: use the same string in the create call and in the set path. This is necessary when you're targeting a specific instance.
3. Config Objects
For more than one or two parameters, bundle them into an object:
class fifo_cfg extends uvm_object;
`uvm_object_utils(fifo_cfg)
rand int unsigned depth = 16;
rand bit enable_scoreboard = 1;
rand int unsigned num_writes = 100;
function new(string name = "fifo_cfg");
super.new(name);
endfunction
endclass
In the test's build_phase:
fifo_cfg cfg = fifo_cfg::type_id::create("cfg"); // factory create
if (!cfg.randomize()) `uvm_fatal("CFG", "rand failed")
cfg.depth = 32;
uvm_config_db#(fifo_cfg)::set(this, "*", "cfg", cfg);
In the scoreboard's build_phase:
if (!uvm_config_db#(fifo_cfg)::get(this, "", "cfg", cfg))
`uvm_fatal("NOCFG", "no cfg")
if (!cfg.enable_scoreboard)
`uvm_info("SB", "checking disabled", UVM_LOW)
type_id::create() for the config object? Because it makes the config overridable too. A future test can override fifo_cfg with a fifo_err_cfg subclass that adds extra knobs, and every component that gets the config receives the subclass. The test and the components don't change.
4. The Factory: type_id::create() vs new()
This is the single most important rule in UVM:
// Wrong in UVM:
drv = new("drv", this);
// Right:
drv = fifo_driver::type_id::create("drv", this);
new() constructs exactly the class you name. The factory never sees it, so no override can ever touch it. type_id::create() routes the request through the factory, which checks for overrides and returns the (possibly substituted) type.
`uvm_component_utils (components) or `uvm_object_utils (objects). No registration, no factory knowledge, no override. This is why "I set the override and nothing happened" almost always ends with a missing macro.
5. Overrides
5.1 Type override — swap everywhere
class fifo_err_test extends fifo_base_test;
`uvm_component_utils(fifo_err_test)
virtual function void build_phase(uvm_phase phase);
// Set BEFORE super.build_phase creates the env
fifo_driver::type_id::set_type_override(err_driver::get_type());
super.build_phase(phase);
endfunction
endclass
After this call, every request for fifo_driver returns an err_driver. The agent's fifo_driver::type_id::create("drv", this) produces the err driver without the agent knowing anything changed.
5.2 Instance override — swap at one path
fifo_driver::type_id::set_inst_override(
err_driver::get_type(),
"env.agent_b.drv"); // full path from the test
Only env.agent_b.drv becomes an err_driver. env.agent_a.drv stays as fifo_driver. The path must match the instance's get_full_name(), which is exactly what the create call produced.
5.3 Timing and precedence
- Overrides must be set before the
create()that would be affected. The factory applies them at construction time, not retroactively. - Instance override beats type override for the matching path.
- Overrides are recursive: if A→B and B→C, a request for A returns C.
- The last
set_*_overridefor the same target wins.
6. Putting It Together: a Configurable FIFO Test
Here is the full pattern, end to end. Notice that the agent never mentions err_driver. The test decides, the factory delivers.
class fifo_base_test extends uvm_test;
`uvm_component_utils(fifo_base_test)
fifo_env env;
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
fifo_cfg cfg = fifo_cfg::type_id::create("cfg");
if (!cfg.randomize()) `uvm_fatal("CFG", "rand failed")
cfg.depth = 32;
uvm_config_db#(fifo_cfg)::set(this, "*", "cfg", cfg);
env = fifo_env::type_id::create("env", this);
endfunction
endclass
class fifo_err_test extends fifo_base_test;
`uvm_component_utils(fifo_err_test)
virtual function void build_phase(uvm_phase phase);
fifo_driver::type_id::set_type_override(err_driver::get_type());
super.build_phase(phase);
endfunction
endclass
And the agent — unchanged across both tests:
class fifo_agent extends uvm_agent;
`uvm_component_utils(fifo_agent)
fifo_driver drv;
fifo_monitor mon;
fifo_sequencer sqr;
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
mon = fifo_monitor::type_id::create("mon", this);
if (get_is_active() == UVM_ACTIVE) begin
drv = fifo_driver::type_id::create("drv", this); // override applies here
sqr = fifo_sequencer::type_id::create("sqr", this);
end
endfunction
virtual function void connect_phase(uvm_phase phase);
if (get_is_active() == UVM_ACTIVE)
drv.seq_item_port.connect(sqr.seq_item_export);
endfunction
endclass
7. Common Pitfalls
| Symptom | Cause |
|---|---|
| Override silently does nothing | Class not registered with `` `uvm_*_utils `` |
| Override silently does nothing | set_type_override called after the affected create() |
| Override silently does nothing | new() used instead of type_id::create() |
get returns 0 / null | Path mismatch between set and get |
get returns 0 | set called in connect_phase (too late — build already ran) |
| Wrong instance overridden | Instance path uses a create name that differs from the override string |
8. What Day 6 Will Add
Tomorrow: Agents and Environment Layering. Active vs passive agents (get_is_active()), how env composition scales to multiple agents, where the scoreboard belongs in the tree, and the VIP reuse patterns that make a real testbench feel like a library rather than a monolith. Day 5 gave you the knobs — Day 6 shows what to build with them.
9. Quiz
Ten questions on config_db paths, the factory, and overrides. One at a time, options shuffled per page load, Submit reveals all answers with explanations.
Coming Up Next: Day 6 — Agents and Environment Layering
- Active vs passive agents — what
get_is_active()controls, and why a passive agent still needs a monitor but no driver or sequencer. - Env composition — building envs from envs, and where scoreboard, coverage, and virtual sequencer live in the tree.
- VIP reuse — how a well-written agent becomes a black box you drop into any env.
- Env-level config — how config objects flow to sub-envs without every level re-setting them.
Series: 30 days of SystemVerilog/UVM verification. Day 1 built the skeleton. Day 2 wired the lifecycle. Day 3 put it in motion. Day 4 wired the plumbing. Day 5 installed the knobs.
No comments:
Post a Comment