一、概述
在验证环境的创建过程build_phase中,除了组件的实例化,配置也是必不可少的。为了验证环境的复用性,通过外部的参数配置,使得环境在创建时可以根据不同参数来选择创建的组件类型、组件实例数目、组件之间的连接以及组件的运行模式等。在更细致的环境调节中有更多的变量需要配置,例如for-loop的阈值、字符串名称、随机变量的生成比重等。比起重新编译来调节变量,如果在仿真中可以通过变量设置来修改环境,那么就更灵活了,而UVM的config机制正提供了这样的便利。
UVM提供了
uvm_config_db
配置类以及几种方便的变量设置方法来实现仿真时的环境控制,常见的
uvm_config_db
类的使用方式包括:
- 传递virtual interface到环境中
- 设置单一变量值,例如int、string、enum等
- 传递配置对象(config object)到环境
uvm_config_db #(T)::set(uvm_component cntxt, string inst_name, string field_name, T value);
uvm_config_db #(T)::get(uvm_component cntxt, string inst_name, string field_name, T value);
二、interface传递
interface
传递可以很好地解决了连接硬件世界和软件世界。而在之前SV验证模块中,虽然SV可以通过层次化的
interface
的索引来完成了传递,但是这样不利于软件环境的封装和复用。UVM的
uvm_comfig_db
使得接口的传递和获取彻底分离开来。
在实现接口传递的过程中需要注意:
- 接口传递应该发生在
之前。保证了在进入run_test()
之前,build_phase
已经被传递到virtual interface
中。uvm_config_db
- 应该把
与interface
的声明区分开来,在传递过程中的类型应当为virtual interface
,即实际接口的句柄。virtual interface
interface intf1;
logic enable = 0;
endinterface
class comp1 extends uvm_component;
`uvm_component_utils(comp1)
virtual intf1 vif;
...
function void build_phase(uvm_phase phase);
if(!uvm_config_db #(virtual intf1)::get(this, "", "vif", vif)) begin //this指代当前组件comp1,“vif”指的是comp1的一个变量
`uvm_error("GETVIF", "no virtual interface is assigned")
end
`uvm_info("SETVIF", $sformatf("vif.enable is %b before set", vif.enable), UVM_LOW)
vif.enable = 1;
`uvm_info("SETVIF", $sformatf("vif.enable is %b after set", vif.enable), UVM_LOW)
endfunction
endclass
class test1 extends uvm_test;
`uvm_component_utils(test1)
comp1 c1;
...
endclass
intf1 intf();
initial begin
//第一个参数时传递一个句柄,第二个参数时路径,第三个参数时c1.vif这个变量,第四个参数是那个例化的需要传递的intf
uvm_config_db #(virtual intf1)::set(uvm_root::get(), "uvm_test_top.c1", "vif", intf);
run_test("test1");
end
uvm_config_db #(virtual intf1)::get(this, "", "vif", vif)
的含义如下图所示

uvm_config_db #(virtual intf1)::set(uvm_root::get(), "uvm_test_top.c1", "vif", intf)
的含义如下图所示
三、变量设置
在各个test中,可以在build_phase对底层组件的变量加以配置,进而在环境例化之前完成配置,使得环境可以按照预期运行。
class comp1 extends uvm_component;
`uvm_component_utils(comp1)
int vall = 1;
string str1 = "null";
...
function void build_phase(uvm_phase phase);
`uvm_info("SETVIF", $sformatf("vall is %d before set", vall), UVM_LOW)
`uvm_info("SETVIF", $sformatf("str1 is %s before set", str1), UVM_LOW)
uvm_config_db#(int)::get(this, "", "vall", vall);
uvm_config_db#(string)::get(this, "", "str1", str1);
`uvm_info("SETVIF", $sformatf("vall is %d after set", vall), UVM_LOW)
`uvm_info("SETVIF", $sformatf("str1 is %s after set", str1), UVM_LOW)
endfunction
endclass
class test1 extends uvm_test;
`uvm_component_utils(test1)
comp1 c1;
...
function void build_phase(uvm_phase phase);
uvm_config_db#(int)::set(this, "c1", "vall", 100);
uvm_config_db#(string)::set(this, "c1", "str1", "comp1");
c1 = comp1::type_id::create("c1", this);
endfunction
endclass
四、object传递
在test配置中,需要配置的参数不只是数量多,而且可能还分属于不同的组件。那么如果对这么多层次中的变量做出类似上面的变量设置,那会需要更多的代码,容易出错还不易于复用,甚至底层组件的变量被删除后,也无法通过
uvm_config_db::set()
得知配置是否成功。然而如果将每个组件中的变量加以整合,首先放置到一个
uvm_object
中,再对中心化的配置对象进行传递,将会有利于整体环境的修改。
class comp1 extends uvm_component;
`uvm_component_utils(comp1)
config1 cfg;
...
function void build_phase(uvm_phase phase);
uvm_object tmp;
uvm_config_db#(uvm_object)::get(this, "", "cfg", tmp);
void'($cast(cfg, tmp));
`uvm_info("SETVIF", $sformatf("cfg.vall is %d after get", cfg.vall), UVM_LOW)
`uvm_info("SETVIF", $sformatf("cfg.str1 is %s after get", cfg.str1), UVM_LOW)
endfunction
endclass
class test1 extends uvm_test;
`uvm_component_utils(test1)
comp1 c1, c2;
config1 cfg1, cfg2;
...
function void build_phase(uvm_phase phase);
cfg1 = config1::type_id::create("cfg1");
cfg2 = config1::type_id::create("cfg2");
cfg1.vall = 30;
cfg1.str1 = "c1";
cfg2.vall = 50;
cfg2.str1 = "c2";
uvm_config_db#(uvm_object)::set(this, "c1", "cfg", cfg1);
uvm_config_db#(uvm_object)::set(this, "c2", "cfg", cfg2);
c1 = comp1::type_id::create("c1", this);
c2 = comp1::type_id::create("c2", this);
endfunction
endclass
五、总结
- 在使用
时,通过层次和变量名,将这些信息放置到uvm_config_db::set()
唯一的全局变量uvm_pkg
。全局变量uvm_pkg::uvm_resources
用来储存和释放配置资源信息,uvm_resources
是uvm_resources
类的全局唯一实例,该实例中有两个uvm_resource_pool
数组用来存放配置信息,这两个数组中一个由层次名字所以,一个由类型索引,通过这两个关联数组可以存放通过层次配置的信息。同时,底层的组件也可以通过层次或者类型来取得来自高层的配置信息。而在使用resource
方法时,通过传递的参数构成索引层次,然后在uvm_config_db::get()
已有的配置信息池中索引该配置,如果索引到返回1,否则返回0。uvm_resources
- 在使用
和set()
方法时,传递的参数类型应当上下保持一致,对于get()
等实例的传递,如果类型不一致,应首先通过uvm_object
完成类型转换,再对类型转换后的对象进行操作。$cast()
-
和set()
方法传递的参数可以使用通配符get()
来表示任意的层次。*
- 在
环境中如果使用module
,则传递的第一个参数uvm_config_db::set()
参数一般用来表示当前的层次。如果当前层次为最高层,可以设置为uvm_component cntxt
,也可以设置为null
来表示uvm_root::get()
的全局顶层实例。uvm_root
- 在使用配置变量时,应确保先进行
,在获得了正确的配置值以后再使用。uvm_config_db::get()
- 应该尽量确保
方法在相关配置组件创建前调用,只有先完成配置,相关组件在例化前才可以得到配置值继而正确地例化。uvm_config_db::set()
- 在
方法第一个参数使用当前层次的前提下,对于同一个组件的同一个变量,如果有多个高层组件对该变量进行设置,那么较高层组件的配置会覆盖较低层的配置,但是如果是同一层次组件对该变量进行多次配置时,后面的配置会覆盖前面的配置。uvm_config_db::set()
- 在使用
方法时,添加便于调试的语句,例如通过UVM报告信息得知uvm_config_db::get()
方法中的配置变量是否从uvm_config_db::get()
获取到。uvm_config_db