Lesson 13: The UVM Factory and uvm_config_db
The UVM factory decides which class is actually built when code calls type_id::create. A test can register an override, so that every fifo_item, or only the scoreboard at one path, is built as a subclass instead, without editing the code that creates it. uvm_config_db is the other half of reuse: a typed table of settings, such as the virtual interface or a sequence length, that a test or the top module sets for a path in the component tree and a component gets in its build_phase. Together they let one environment serve many tests unchanged.
Learning objectives
- Replace a class everywhere with a type override, or at one path with an instance override, and say which wins
- Set and get values with uvm_config_db, read the path rules, and know which set wins during build
- Print the component tree and the factory's overrides to see what was really built
- Make an agent passive through configuration, and choose a class at run time by name
Prerequisites
- Lesson 11: the FIFO environment, its components and phases
- Lesson 3: classes, inheritance and virtual methods
- The running example is the synchronous FIFO: 16 entries of 8 bits, one clock, flags full and empty and an occupancy count; writes when full are dropped, reads when empty are ignored, and read data appears one clock after the read. Its shared testbench code is in the site's repository under scripts/verification/fifo, and every example runs in AMD Vivado xsim 2025.2.
The factory and overrides
`uvm_component_utils and `uvm_object_utils register a class with the factory, and type_id::create asks the factory for an object of that type. Before building it, the factory checks for an instance override that matches the requested type at this path, then for a type override for the type, and builds the requested type only if neither exists. Overrides must be registered before the object is created, which for components means before super.build_phase in the test.
// A replacement item: the same fields, plus a constraint that only writes the corner data values.
class fifo_corner_item extends fifo_item;
`uvm_object_utils(fifo_corner_item)
constraint c_corners { txn.data inside {8'h00, 8'hFF, 8'h55, 8'hAA}; }
function new(string name = "fifo_corner_item"); super.new(name); endfunction
endclass
// A replacement scoreboard for one instance: the same checks, and it reports each value it saw written.
class fifo_counting_scoreboard extends fifo_scoreboard;
`uvm_component_utils(fifo_counting_scoreboard)
int seen[bit [7:0]];
function new(string name, uvm_component parent); super.new(name, parent); endfunction
function void write(fifo_item it);
super.write(it);
if (it.txn.wr_en && !it.txn.full) seen[it.txn.data]++;
endfunction
function void report_phase(uvm_phase phase);
string s;
super.report_phase(phase);
foreach (seen[v]) s = {s, $sformatf(" %0d:%0d", v, seen[v])};
`uvm_info("SEEN", {"data written (value:count):", s}, UVM_NONE)
endfunction
endclass
The analysis port calls write through a handle of the base class, fifo_scoreboard. For the subclass's write to run, write has to be virtual in the base class, so Lesson 11's scoreboard declares it virtual function void write. Without virtual, the override would be built but its write would never be called.
uvm_config_db
uvm_config_db#(T)::set(context, path, field, value) stores a value for every component whose full name matches the context's name plus the path, which may use wildcards. get(this, "", field, var) looks it up for the calling component and returns 1 if it found one. The top module sets the virtual interface for uvm_test_top.* and every driver and monitor gets it in build_phase. During build, a set made higher in the hierarchy wins over one made lower, so a test can always overrule its environment.
function void build_phase(uvm_phase phase);
if (!uvm_config_db#(virtual fifo_if)::get(this, "", "vif", vif)) `uvm_fatal("DRV", "no vif in uvm_config_db")
endfunction
// A sequence that reads its length from uvm_config_db, with a default if nobody set one.
class fifo_cfg_seq extends uvm_sequence #(fifo_item);
`uvm_object_utils(fifo_cfg_seq)
int n = 20;
function new(string name = "fifo_cfg_seq"); super.new(name); endfunction
task body();
void'(uvm_config_db#(int)::get(m_sequencer, "", "n_items", n));
`uvm_info("CFG", $sformatf("n_items = %0d", n), UVM_NONE)
repeat (n) begin
fifo_item it = fifo_item::type_id::create("it"); // through the factory: an override applies here
start_item(it);
if (!it.randomize()) `uvm_fatal("SEQ", "randomize failed")
finish_item(it);
end
endtask
endclass
Worked example: one test reconfigures the environment
The test registers a type override for fifo_item, an instance override for the scoreboard at env.scb and a sequence length of 40, then calls super.build_phase. At end_of_elaboration it prints the component tree and the factory's overrides.
class fifo_factory_test extends fifo_test;
`uvm_component_utils(fifo_factory_test)
function new(string name, uvm_component parent); super.new(name, parent); endfunction
function void build_phase(uvm_phase phase);
// Overrides and configuration go in before super.build_phase creates the environment.
fifo_item::type_id::set_type_override(fifo_corner_item::get_type());
fifo_scoreboard::type_id::set_inst_override(fifo_counting_scoreboard::get_type(), "env.scb", this);
uvm_config_db#(int)::set(this, "env.agt.sqr", "n_items", 40);
super.build_phase(phase);
endfunction
function void end_of_elaboration_phase(uvm_phase phase);
uvm_top.print_topology();
uvm_factory::get().print();
endfunction
task traffic();
fifo_cfg_seq s = fifo_cfg_seq::type_id::create("s");
s.start(env.agt.sqr);
endtask
endclass
UVM_INFO uvm_pkg @ 0: reporter [UVMTOP] UVM testbench topology:
Name Type Size Value
uvm_test_top fifo_factory_test - @341
env fifo_uvm_env - @358
agt fifo_agent - @367
drv fifo_driver - @551
rsp_port uvm_analysis_port - @570
seq_item_port uvm_seq_item_pull_port - @560
mon fifo_monitor - @580
ap uvm_analysis_port - @589
sqr uvm_sequencer - @414
rsp_export uvm_analysis_export - @423
seq_item_export uvm_seq_item_pull_imp - @541
arbitration_queue array 0 -
lock_queue array 0 -
num_last_reqs integral 32 'd1
num_last_rsps integral 32 'd1
cov fifo_coverage_sub - @395
analysis_imp uvm_analysis_imp - @404
scb fifo_counting_scoreboard - @376
imp uvm_analysis_imp - @385
UVM_INFO uvm_pkg @ 0: reporter [UVM/FACTORY/PRINT]
#### Factory Configuration (*)
Instance Overrides:
Requested Type Override Path Override Type
--------------- -------------------- ------------------------
fifo_scoreboard uvm_test_top.env.scb fifo_counting_scoreboard
Type Overrides:
Requested Type Override Type
--------------- --------------------
fifo_item fifo_corner_item
UVM_INFO fifo_factory_tb.sv(17) @ 0: uvm_test_top.env.agt.sqr@@s [CFG] n_items = 40
UVM_INFO fifo_uvm.svh(112) @ 445000: uvm_test_top.env.scb [SCB] 43 clocks checked, 0 errors
UVM_INFO fifo_factory_tb.sv(47) @ 445000: uvm_test_top.env.scb [SEEN] data written (value:count): 0:9 85:1 170:5 255:3
UVM_ERROR : 0
[CFG] 1
The tree shows fifo_counting_scoreboard at env.scb, and the factory lists both overrides. The sequence got n_items = 40 from uvm_config_db, and every one of its items was built as fifo_corner_item: the only data values written were 0, 85, 170 and 255. Lesson 11's environment file was not edited.
Common mistakes
- Calling new() instead of type_id::create: the factory is bypassed and no override can apply.
- Registering an override after super.build_phase has built the components it should have replaced.
- Overriding a method that is called through a base handle when the base method is not virtual: the override is built but its method never runs.
- A config_db set whose path does not match the getter's full name, so get returns 0. Always check get's return value and stop with a clear message, as the FIFO driver does.
- Expecting xsim to read UVM's command-line options such as +uvm_set_type_override: its UVM library is built without DPI and reads only +UVM_TESTNAME. Read your own plusarg and override by name instead (E4).
Quick check
1. A type override and an instance override both match a create call. Which one is built?
The instance override: the factory checks instance overrides for the path first.
2. Why must the overrides be registered before super.build_phase in the test?
super.build_phase builds the environment, which creates its children through the factory. An override registered afterwards comes too late for them.
3. How did the sequence get n_items = 40?
The test set it for env.agt.sqr, and the sequence's get used its sequencer as the context, so the lookup matched uvm_test_top.env.agt.sqr.
Exercises
Graded from easy to hard. Try each one before you open the solution; every solution was run in xsim 2025.2, and its output is shown.
E1 (easy). Set the virtual interface for the monitor only, and see what the driver does.
The driver's get finds nothing for its path, returns 0, and the driver stops the run with a fatal error in build_phase, before any time passes.
// E1: the top module set the interface for the monitor only, so the driver's get fails in build_phase.
class fifo_e1_ftest extends fifo_short_test;
`uvm_component_utils(fifo_e1_ftest)
function new(string name, uvm_component parent); super.new(name, parent); endfunction
endclass
if ($test$plusargs("E1")) uvm_config_db#(virtual fifo_if)::set(null, "uvm_test_top.env.agt.mon", "vif", vif);
else uvm_config_db#(virtual fifo_if)::set(null, "uvm_test_top.*", "vif", vif);
UVM_FATAL fifo_uvm.svh(41) @ 0: uvm_test_top.env.agt.drv [DRV] no vif in uvm_config_db
UVM_FATAL : 1
E2 (easy). The test sets n_items to 40 and the environment sets it to 10 for the same sequencer. Which value does the sequencer see?
40. During build, a set made higher in the component hierarchy wins over one made lower, so the test overrules its environment.
// E2: the test and the environment both set n_items for the same sequencer. Which one does the sequence get?
class fifo_cfg_env extends fifo_uvm_env;
`uvm_component_utils(fifo_cfg_env)
function new(string name, uvm_component parent); super.new(name, parent); endfunction
function void build_phase(uvm_phase phase);
uvm_config_db#(int)::set(this, "agt.sqr", "n_items", 10);
super.build_phase(phase);
endfunction
endclass
class fifo_e2_ftest extends fifo_short_test;
`uvm_component_utils(fifo_e2_ftest)
function new(string name, uvm_component parent); super.new(name, parent); endfunction
function void build_phase(uvm_phase phase);
fifo_uvm_env::type_id::set_type_override(fifo_cfg_env::get_type());
uvm_config_db#(int)::set(this, "env.agt.sqr", "n_items", 40);
super.build_phase(phase);
endfunction
task traffic();
int n;
void'(uvm_config_db#(int)::get(env.agt.sqr, "", "n_items", n));
`uvm_info("E2", $sformatf("the sequencer sees n_items = %0d", n), UVM_NONE)
endtask
endclass
UVM_INFO fifo_factory_ex_tb.sv(63) @ 0: uvm_test_top [E2] the sequencer sees n_items = 40
E3 (medium). Make the agent passive from the test: monitor only, no driver or sequencer.
A subclass of the agent reads is_active from uvm_config_db and builds the driver and sequencer only when active; a type override puts it in place. It does not call super.build_phase, which would build all three children.
// E3: an agent that builds its driver and sequencer only when it is active, made passive from the test.
class fifo_agent_ap extends fifo_agent;
`uvm_component_utils(fifo_agent_ap)
function new(string name, uvm_component parent); super.new(name, parent); endfunction
function void build_phase(uvm_phase phase);
// No super.build_phase: fifo_agent's own build would create all three children. Read is_active directly.
void'(uvm_config_db#(uvm_active_passive_enum)::get(this, "", "is_active", is_active));
mon = fifo_monitor::type_id::create("mon", this);
if (get_is_active() == UVM_ACTIVE) begin
sqr = uvm_sequencer#(fifo_item)::type_id::create("sqr", this);
drv = fifo_driver::type_id::create("drv", this);
end
endfunction
function void connect_phase(uvm_phase phase);
if (get_is_active() == UVM_ACTIVE) drv.seq_item_port.connect(sqr.seq_item_export);
endfunction
endclass
class fifo_e3_ftest extends fifo_test;
`uvm_component_utils(fifo_e3_ftest)
function new(string name, uvm_component parent); super.new(name, parent); endfunction
function void build_phase(uvm_phase phase);
fifo_agent::type_id::set_type_override(fifo_agent_ap::get_type());
uvm_config_db#(uvm_active_passive_enum)::set(this, "env.agt", "is_active", UVM_PASSIVE);
super.build_phase(phase);
endfunction
function void end_of_elaboration_phase(uvm_phase phase);
`uvm_info("E3", $sformatf("agent is %s; driver %s, sequencer %s", env.agt.get_is_active() == UVM_ACTIVE ? "active" : "passive",
env.agt.drv == null ? "not built" : "built", env.agt.sqr == null ? "not built" : "built"), UVM_NONE)
endfunction
task run_phase(uvm_phase phase); endtask
endclass
UVM_INFO fifo_factory_ex_tb.sv(95) @ 0: uvm_test_top [E3] agent is passive; driver not built, sequencer not built
E4 (hard). Choose the item class at run time, without recompiling.
Read a plusarg and override by name with set_type_override_by_name. The same build, run twice: 25 different data values without the plusarg, 4 with +ITEM=fifo_corner_item.
// E4: choose the item class at run time with a plusarg, +ITEM=fifo_corner_item, and an override by name. (xsim's
// UVM library is built without DPI, so it does not read UVM's own +uvm_set_type_override option.)
class fifo_e4_ftest extends fifo_short_test;
`uvm_component_utils(fifo_e4_ftest)
function new(string name, uvm_component parent); super.new(name, parent); endfunction
function void build_phase(uvm_phase phase);
string item;
if ($value$plusargs("ITEM=%s", item)) begin
uvm_factory::get().set_type_override_by_name("fifo_item", item);
`uvm_info("E4", {"fifo_item is replaced by ", item}, UVM_NONE)
end
fifo_scoreboard::type_id::set_inst_override(fifo_counting_scoreboard::get_type(), "env.scb", this);
super.build_phase(phase);
endfunction
endclass
UVM_INFO fifo_factory_ex_tb.sv(26) @ 645000: uvm_test_top.env.scb [SEEN] 25 different data values written
UVM_INFO fifo_factory_ex_tb.sv(109) @ 0: uvm_test_top [E4] fifo_item is replaced by fifo_corner_item
UVM_INFO fifo_factory_ex_tb.sv(26) @ 645000: uvm_test_top.env.scb [SEEN] 4 different data values written
Interview questions
- Q1. What is the difference between a type override and an instance override? Answer: a type override replaces a class everywhere it is created through the factory; an instance override replaces it only for components at a given path. When both match, the instance override wins.
- Q2. Your override is registered but the old class is still built. What do you check? Answer: that the object is created with type_id::create, not new(); that the override is registered before the build that creates it; and, if the class is built but its behavior has not changed, that the method you overrode is virtual in the base class.
- Q3. How does a driver get its virtual interface? Answer: the top module sets it in uvm_config_db for a path that covers the driver, and the driver gets it in build_phase, checking the return value and stopping with a fatal error if nothing was set.
Summary
- The factory builds what type_id::create asks for, unless an instance override or a type override says otherwise.
- uvm_config_db is a typed table of settings keyed by path; components get their settings in build_phase.
- A higher set wins during build, and overrides must be in place before the build that uses them.
- One test reconfigured the FIFO environment, items, scoreboard and sequence length, without editing it.
Related
Sources
Last verified
Run in Vivado xsim 2025.2 with its bundled UVM 1.2 on 27 November 2026: the factory test and all four exercise tests. Outputs are copied from those runs and kept in scripts/verification/results; lib/verificationCourse.test.js checks them against this page.