uvm_object类是所有uvm层次类的基类,如uvm_report_object、uvm_component、uvm_transaction、uvm_sequence_item、uvm_sequence等。它在定义一组方法(如create, copy, print, clone, compare, record等)方面起着重要作用。
我们在UVM factory章节已经看到了“create”方法是如何工作的,“create”方法为对象分配内存空间并返回相同类型的句柄。
`uvm_object_utils或`uvm_abject_utils_begin`uvm_object_utils_end宏用于在uvm工厂中注册uvm_object和其他派生类型,如uvm_transaction、uvm_sequece_items。
UVM字段宏(field macros)提供了?create, copy, print, clone, compare, record等方法的实现。工厂中有不同的`uvm_field_*宏可用于各种数据类型变量的注册。`uvm_field_*宏至少接受两个参数作为arg(与`uvm_field_*宏兼容的变量的实例名)和flag(用作添加相应数据方法的控制机制)。
new()方法很重要,可以用相应的类名作为扩充来定义。
注:在编译期间,这些UVM自动化宏扩展了相应宏可用的完整代码。
Syntax without?field macros:
`uvm_object_utils(<class Type>)
Syntax with?field macros:
`uvm_object_utils_begin(<class_type>)
`uvm_field_*(<arg>, <flag>)
`uvm_object_utils_end
以下字段宏通常用于标量/动态类属性的数据方法[data methods](copy, compare, pack, unpack, print, clone,?等)。可以从UVM参考文件中研究完整列表。?
`uvm_field_*宏实现标量属性的数据方法。
`uvm_field_sarray*宏实现一维静态数组属性的数据方法。
uvm_field_array*宏实现一维动态数组属性的数据方法。
也可以指定打印的进制。默认是16进制HEX。
typedef enum{RED, GREEN, BLUE} color_type;
class my_object extends uvm_object;
int o_var;
string o_name;
color_type colors;
byte data[4];
bit [7:0] addr;
`uvm_object_utils(my_object)
function new(string name = "my_object");
super.new(name);
endfunction
endclass
typedef enum{RED, GREEN, BLUE} color_type;
class my_object extends uvm_object;
rand int value;
string names;
rand color_type colors;
rand byte data[4];
rand bit [7:0] addr;
`uvm_object_utils_begin(my_object)
`uvm_field_int(value, UVM_ALL_ON)
`uvm_field_string(names, UVM_ALL_ON)
`uvm_field_enum(color_type, colors, UVM_ALL_ON)
`uvm_field_sarray_int(data, UVM_ALL_ON)
`uvm_field_int(addr, UVM_ALL_ON)
`uvm_object_utils_end
function new(string name = "my_object");
super.new(name);
endfunction
endclass
打印方法用于以格式良好的方式深度打印UVM对象类的属性。需要根据类属性的数据类型使用适当的`uvm_field_*宏。
注意:sprint()方法与print()方法相同,不同的是sprint()方法以字符串格式打印对象。
typedef enum{RED, GREEN, BLUE} color_type;
class temp_class extends uvm_object;
rand bit [7:0] tmp_addr;
rand bit [7:0] tmp_data;
function new(string name = "temp_class");
super.new(name);
endfunction
`uvm_object_utils_begin(temp_class)
`uvm_field_int(tmp_addr, UVM_ALL_ON)
`uvm_field_int(tmp_data, UVM_ALL_ON)
`uvm_object_utils_end
endclass
class my_object extends uvm_object;
rand int value;
string names;
rand color_type colors;
rand byte data[4];
rand bit [7:0] addr;
rand temp_class tmp;
`uvm_object_utils_begin(my_object)
`uvm_field_int(value, UVM_ALL_ON)
`uvm_field_string(names, UVM_ALL_ON)
`uvm_field_enum(color_type, colors, UVM_ALL_ON)
`uvm_field_sarray_int(data, UVM_ALL_ON)
`uvm_field_int(addr, UVM_ALL_ON)
`uvm_field_object(tmp, UVM_ALL_ON)
`uvm_object_utils_end
function new(string name = "my_object");
super.new(name);
tmp = new();
this.names = "UVM";
endfunction
endclass
class my_test extends uvm_test;
`uvm_component_utils(my_test)
my_object obj;
bit packed_data_bits[];
byte unsigned packed_data_bytes[];
int unsigned packed_data_ints[];
my_object unpack_obj;
function new(string name = "my_test", uvm_component parent = null);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
super.build_phase(phase);
obj = my_object::type_id::create("obj", this);
assert(obj.randomize());
obj.print();
// or
//`uvm_info(get_full_name(), $sformatf("obj = \n%s", obj.sprint()), UVM_LOW);
endfunction
endclass
module tb_top;
initial begin
run_test("my_test");
end
endmodule
Output:
UVM_INFO @ 0: reporter [RNTST] Running test my_test...
--------------------------------------------
Name Type Size Value
--------------------------------------------
obj my_object - @349
value integral 32 'h1f135537
names string 3 UVM
colors color_type 32 GREEN
data sa(integral) 4 -
[0] integral 8 'h9f
[1] integral 8 'h33
[2] integral 8 'h12
[3] integral 8 'h9c
addr integral 8 'h2f
tmp temp_class - @350
tmp_addr integral 8 'h39
tmp_data integral 8 'hbd
--------------------------------------------
如果print方法与`uvm_object_utils一起使用,则不会打印类属性。
typedef enum{RED, GREEN, BLUE} color_type;
class my_object extends uvm_object;
rand int o_var;
string o_name;
rand color_type colors;
rand byte data[4];
rand bit [7:0] addr;
`uvm_object_utils(my_object)
function new(string name = "my_object");
super.new(name);
endfunction
endclass
class my_test extends uvm_test;
`uvm_component_utils(my_test)
my_object obj;
function new(string name = "my_test", uvm_component parent = null);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
super.build_phase(phase);
obj = my_object::type_id::create("obj", this);
assert(obj.randomize());
obj.print();
endfunction
function void end_of_elaboration_phase(uvm_phase phase);
super.end_of_elaboration_phase(phase);
uvm_top.print_topology();
endfunction
endclass
module tb_top;
initial begin
run_test("my_test");
end
endmodule
Output:
UVM_INFO @ 0: reporter [RNTST] Running test my_test...
----------------------------
Name Type Size Value
----------------------------
obj my_object - @1872
----------------------------
UVM_INFO /xcelium20.09/tools//methodology/UVM/CDNS-1.2/sv/src/base/uvm_root.svh(605) @ 0: reporter [UVMTOP] UVM testbench topology:
----------------------------------
Name Type Size Value
----------------------------------
uvm_test_top my_test - @1805
----------------------------------
UVM自动化宏主要涉及许多影响仿真器性能的附加代码。因此,不建议使用。相反,do_print()回调方法是一个用户定义的钩子,由print()或sprint()方法调用。用户必须调用在do_print()实现的printer API来添加要打印的信息。
typedef enum{RED, GREEN, BLUE} color_type;
class temp_class extends uvm_object;
rand bit [7:0] tmp_addr;
rand bit [7:0] tmp_data;
function new(string name = "temp_class");
super.new(name);
endfunction
`uvm_object_utils(temp_class)
function void do_print(uvm_printer printer);
super.do_print(printer);
printer.print_field_int("tmp_addr", tmp_addr, $bits(tmp_addr), UVM_HEX);
printer.print_field_int("tmp_data", tmp_data, $bits(tmp_data), UVM_HEX);
endfunction
endclass
class my_object extends uvm_object;
rand int value;
string names = "UVM";
rand color_type colors;
rand byte data[4];
rand bit [7:0] addr;
rand temp_class tmp;
`uvm_object_utils(my_object)
function new(string name = "my_object");
super.new(name);
tmp = new();
endfunction
function void do_print(uvm_printer printer);
super.do_print(printer);
printer.print_field_int("value", value, $bits(value), UVM_HEX);
printer.print_string("names", names);
printer.print_string("colors", colors.name);
foreach(data[i])
printer.print_field_int($sformatf("data[%0d]", i), data[i], $bits(data[i]), UVM_HEX);
printer.print_field_int("addr", addr, $bits(addr), UVM_HEX);
printer.print_object("tmp", tmp);
endfunction
endclass
class my_test extends uvm_test;
`uvm_component_utils(my_test)
my_object obj;
function new(string name = "my_test", uvm_component parent = null);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
super.build_phase(phase);
obj = my_object::type_id::create("obj", this);
assert(obj.randomize());
obj.print();
endfunction
endclass
module tb_top;
initial begin
run_test("my_test");
end
endmodule
Output:
UVM_INFO @ 0: reporter [RNTST] Running test my_test...
------------------------------------------
Name Type Size Value
------------------------------------------
obj my_object - @1876
value integral 32 'ha4a4f87e
names string 3 UVM
colors string 3 RED
data[0] integral 8 'hc6
data[1] integral 8 'h4c
data[2] integral 8 'hf
data[3] integral 8 'h89
addr integral 8 'h53
tmp temp_class - @1878
tmp_addr integral 8 'hea
tmp_data integral 8 'hdf
------------------------------------------