UVM Reference
UVM Factory Overrides
The factory builds objects by type. An override makes it build a child class instead, with no change to the environment.
Syntax
// create through the factory
drv = my_driver::type_id::create("drv", this);
// in the test: replace the type everywhere
my_driver::type_id::set_type_override(err_driver::get_type());Rules
- Register each class with `
uvm_component_utilsor `uvm_object_utils. - Create with
type_id::create, never withnew. An object made withnewcannot be overridden. - The override class must extend the original class.
- A type override replaces every instance. An instance override replaces one path.
- Set the override before the object is created, usually in the test's
build_phase.
Example
import uvm_pkg::*;
`include "uvm_macros.svh"
class my_driver extends uvm_component;
`uvm_component_utils(my_driver)
function new(string name, uvm_component parent); super.new(name, parent); endfunction
virtual function string kind(); return "normal driver"; endfunction
function void build_phase(uvm_phase phase);
$display("%s: %s", get_full_name(), kind());
endfunction
endclass
class err_driver extends my_driver;
`uvm_component_utils(err_driver)
function new(string name, uvm_component parent); super.new(name, parent); endfunction
virtual function string kind(); return "error-injecting driver"; endfunction
endclass
class my_test extends uvm_test;
`uvm_component_utils(my_test)
my_driver a, b;
function new(string name, uvm_component parent); super.new(name, parent); endfunction
function void build_phase(uvm_phase phase);
a = my_driver::type_id::create("a", this);
my_driver::type_id::set_type_override(err_driver::get_type());
b = my_driver::type_id::create("b", this);
endfunction
endclass
module tb;
initial run_test("my_test");
endmoduleuvm_test_top.a: normal driver
uvm_test_top.b: error-injecting driverCommon Mistake
Building with new
drv = new("drv", this); // the factory is never askedAn override only takes effect when the factory builds the object. A direct new always builds the written type.
Fix: Use my_driver::type_id::create("drv", this).
Learn It Properly
These lessons teach it step by step, with practice problems.
Standard
IEEE 1800.2-2020, 8.3.1.