You are on page 1of 19

Advanced UVM

Understanding the Factory and Configuration

Tom Fitzpatrick
Verification Evangelist

info@verificationacademy.com | www.verificationacademy.com
Two Customization Mechanisms
• Factory
• Allows test to change the type of a desired component
or object
• Typically set up at start of simulation
• Configuration
• Allows parents to define properties for children
- Static (build-time) – Highest parent “wins”
- Dynamic (run_time) – Last set “wins”
• All UVM components get their own configuration
- Optionally use to configure their children
© 2013 Mentor Graphics Corporation, all rights reserved.
Create() vs. New()
class my_env extends uvm_env; new() hard-codes the type
virtual function void build_phase(uvm_phase phase);
…without
comp1 = new(“comp1”, this);
modifying the
comp2 = my_comp::type_id::create(“comp2”, this);
instantiating
endfunction
code!
class my_comp extends uvm_component;
`uvm_component_utils(my_comp) create() returns a
… constructed instance
endclass comp1 from the factory
class my_Xcomp
my_comp extends
extendsuvm_component;
my_comp; Factory lets you change
`uvm_component_utils(my_Xcomp)
`uvm_component_utils(my_comp) the type of the created
… comp2 component…
endclass

© 2013 Mentor Graphics Corporation, all rights reserved.


Registering with the Factory
• Objects are registered with the factory via macro
• `uvm_object_utils(<type>) ‘type_id’ is a wrapper
• `uvm_component_utils(<type>) created by the macro

class my_env extends uvm_env;


virtual function void build_phase(uvm_phase phase);
comp2 = my_comp::type_id::create(“comp2”, this);
endfunction class my_comp extends uvm_component;
`uvm_component_utils(my_comp) No “;”
No ‘;’

comp2
endclass

© 2013 Mentor Graphics Corporation, all rights reserved.


Registering with the Factory
Override type
• Static methods in wrapper returned by factory
class test extends uvm_test;
get_type() returns
function void build_phase(uvm_phase phase);
my_env e = new(“e”); the type “handle”
my_comp::type_id::set_type_override(my_Xcomp::get_type());
class my_env extends uvm_env;
virtual function void build_phase(uvm_phase phase);
comp2 = my_comp::type_id::create(“comp2”, this);
endfunction class my_Xcomp extends my_comp;
returns a constructed `uvm_component_utils(my_Xcomp)
instance … comp2
(no $cast needed) endclass

• set_inst_override(<type>, “<instance>”); // too


© 2013 Mentor Graphics Corporation, all rights reserved.
Overriding a Type
class test extends uvm_test;
function void build_phase(uvm_phase phase); Environments are
e = my_env::type_id::create(“e”, this); components

endfunction class my_env extends uvm_env;


U1 U2
endclass `uvm_component_utils(my_env)
shape u1,u2;// default square

function void build_phase(uvm_phase phase);


u1 = shape::type_id::create(“u1”,this);
u2 = shape::type_id::create(“u2”,this);


endfunction
© 2013 Mentor Graphics Corporation, all rights reserved.
Overriding a Type
class test extends uvm_test;
New Desired
function void build_phase(uvm_phase phase);
type
e = my_env::type_id::create(“e”, this); All Instances
shape::type_id::set_type_override( circle::get_type() ); Overridden

endfunction class my_env extends uvm_env;


U1 U2
endclass `uvm_component_utils(my_env)
shape u1,u2;// default square

function void build_phase(uvm_phase phase);


u1 = shape::type_id::create(“u1”,this);
u2 = shape::type_id::create(“u2”,this);


endfunction
© 2013 Mentor Graphics Corporation, all rights reserved.
Overriding an Instance
class test extends uvm_test; New Desired type
function void build_phase(uvm_phase phase);
Instance Name
e = my_env::type_id::create(“e”, this);
shape::type_id::set_type_override( circle::get_type() ); Instance
shape::type_id::set_inst_override( triangle::get_type(), “e.u2” ); Changed

endfunction class my_env extends uvm_env;


U1 U2
endclass `uvm_component_utils(my_env) U2
shape u1,u2;// default square

function void build_phase(uvm_phase phase);


u1 = shape::type_id::create(“u1”,this);
u2 = shape::type_id::create(“u2”,this);


endfunction
© 2013 Mentor Graphics Corporation, all rights reserved.
Using Parameterized Types
class test extends uvm_test;
function void build_phase(uvm_phase phase);
e = my_env::type_id::create(“e”, this);
shape::type_id::set_type_override( circle::get_type() );
shape::type_id::set_inst_override( triangle::get_type(), “e.u2” );

endfunction class my_env extends uvm_env;


U1
endclass `uvm_component_utils(my_env) U2
shape u1,u2;// default square

function void build_phase(uvm_phase phase);


u1 = shape::type_id::create(“u1”,this);
u2 = shape::type_id::create(“u2”,this);


endfunction
© 2013 Mentor Graphics Corporation, all rights reserved.
Using Parameterized Types
class test extends uvm_test; class red #(int SIDES=3)
function void build_phase(uvm_phase phase); extends uvm_component;
e = my_env::type_id::create(“e”, this); `uvm_component_param_utils(red#(SIDES))
shape::type_id::set_type_override( circle::get_type() );
shape::type_id::set_inst_override( triangle::get_type(), “e.u2” );

endfunction class my_env extends uvm_env;


U1 U3
endclass `uvm_component_utils(my_env) U2
shape u1,u2;// default square
red #(4) u3;
function void build_phase(uvm_phase phase); Parameterized
u1 = shape::type_id::create(“u1”,this); type
u2 = shape::type_id::create(“u2”,this);
u3 = red#(4)::type_id::create(“u3”,this);

endfunction
© 2013 Mentor Graphics Corporation, all rights reserved.
Using Parameterized Types
class test extends uvm_test;
function void build_phase(uvm_phase phase);
e = my_env::type_id::create(“e”, this);
shape::type_id::set_type_override( circle::get_type() );
shape::type_id::set_inst_override( triangle::get_type(), “e.u2” );
red#(4)::type_id::set_type_override( blue#(4)::get_type() );
endfunction class my_env extends uvm_env;
U1
endclass `uvm_component_utils(my_env) U2
shape u1,u2;// default square
red #(4) u3;
function void build_phase(uvm_phase phase);
u1 = shape::type_id::create(“u1”,this);
u2 = shape::type_id::create(“u2”,this);
u3 = red#(4)::type_id::create(“u3”,this);

endfunction
© 2013 Mentor Graphics Corporation, all rights reserved.
Tests are Components, too!
• run_test() creates the test from the factory
class test extends uvm_test;
module top; `uvm_component_utils(test)
... …
endclass
Register the test
initial with the factory
begin: blk
...
run_test();
end

endmodule: top
Command line: vsim +UVM_TESTNAME=test
© 2013 Mentor Graphics Corporation, all rights reserved.
Tests are Components, too!
• Always call run_test() with null argument
class test extends uvm_test;
module top; `uvm_component_utils(test)
... …
endclass class test2 extends uvm_test;
`uvm_component_utils(test2)
initial …
begin: blk endclass
...
run_test();
end

endmodule: top
Command line: vsim +UVM_TESTNAME=test2
© 2013 Mentor Graphics Corporation, all rights reserved.
Use the Factory for Objects, too
class test extends uvm_test;
`uvm_component_utils(test)

virtual function void build_phase(phase);
e = my_env::type_id::create(“env”, this);
my_seq::type_id::set_type_override(my_seq2::get_type());
endfunction
endclass class my_env extends uvm_env;
virtual function void run_phase(uvm_phase phase);
rseq = my_seq::type_id::create(“rseq”);
endfunction
class my_seq2 extends my_seq;
`uvm_object_utils(my_seq2)

endclass

© 2013 Mentor Graphics Corporation, all rights reserved.


UVM Configuration Database
Usually, a component will get
• Explicitly typed its configuration and use that
to configure its children
• Tied to hierarchical scopes
test
test set e.y = 4
set e.a.d.x = 4 Path Value
env
env {test .e.a.d.x} 4
get y;
set a.d.x = 3 set a.y = y
{test.e .a.d.x} 3
agent {test.e.a .d.x} 2 agent
set d.x = 2; get y;
{test.e.a.d .x} 1
set d.y = y;
driver Highest Write
get x = 12
4
3 Wins driver
get y = 4

© 2013 Mentor Graphics Corporation, all rights reserved.


uvm_config_db
• Similar functionality to set/get_config_*()
• No casting on get()
• Linked to component hierarchy
uvm_config_db #(<type>)::set(this, “<inst>”,“<field>”,
value );
uvm_config_db #(<type>)::get(this, “<inst>”,“<field>”,
value );
top.env.agent
set(this,”drv”,”vif”,vif); top.env.agent.drv
top.env.agent.drv top.env.agent.drv
get(this,””,”vif”,vif);

© 2013 Mentor Graphics Corporation, all rights reserved.


UVM Features – uvm_config_db
• For passing into Test:
ahb_if AHB(); // AHB Interface
initial begin
uvm_config_db #(virtual ahb_if)::set(null, “uvm_test_top”, “AHB”, AHB);

• For passing inside UVM:


ahb_agent_config ahb_cfg;
env_config env_cfg;
function void build_phase(uvm_phase);
ahb_cfg = ahb_agent_config::type_id::create(“ahb_cfg”);
if(!uvm_config_db #(virtual ahb_if)::get(this, “”, “AHB”, ahb_cfg.AHB))
begin `uvm_error(…) end
env_cfg = env_config::type_id::create(“env_cfg”);
env_cfg.ahb_cfg = ahb_cfg;
uvm_config_db #(env_config)::set(this, “*”, “config”, env_cfg);
… Supports pattern matching
glob-style or regular expressions
© 2013 Mentor Graphics Corporation, all rights reserved.
Summary
• Use `uvm_object/component_utils macro to register
with the factory
• Always call <type>::type_id::create()
• Register tests with the factory
• Call run_test() with null argument
• Specify which test via the command line
• Use Config DB at build-time
• Components get their config before configuring children
• Highest set wins
• Use Config DB at run_time
• Last set wins
• Up to the “getter” to decide when it’s legal
© 2013 Mentor Graphics Corporation, all rights reserved.
Advanced UVM
Understanding the Factory and Configuration

Tom Fitzpatrick
Verification Evangelist

info@verificationacademy.com | www.verificationacademy.com

You might also like