Tutorials
Learn More
Compare method in UVM
The compare method returns 1 if comparison matches for the current object when it is compared with the R.H.S. argument object. It does a deep comparison.
compare method example
`include "uvm_macros.svh"
import uvm_pkg::*;
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();
endfunction
endclass
class my_test extends uvm_test;
`uvm_component_utils(my_test)
my_object obj_A, obj_B;
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_A = my_object::type_id::create("obj_A", this);
obj_B = my_object::type_id::create("obj_B", this);
assert(obj_A.randomize());
assert(obj_B.randomize());
`uvm_info(get_full_name(), $sformatf("obj_A = \n%s and obj_B = \n%s", obj_A.sprint(), obj_B.sprint()), UVM_LOW);
my_compare(obj_A, obj_B);
obj_B.copy(obj_A);
`uvm_info(get_full_name(), "obj_B is copied to obj_A", UVM_LOW);
my_compare(obj_A, obj_B);
endfunction
function void my_compare(my_object obj_A, obj_B);
if(obj_B.compare(obj_A)) begin
`uvm_info(get_full_name(), "Comparison matched", UVM_LOW);
end
else begin
`uvm_info(get_full_name(), "Comparison did not match", UVM_LOW);
end
endfunction
endclass
module tb_top;
initial begin
run_test("my_test");
end
endmodule
Output:
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [uvm_test_top] obj_A =
--------------------------------------------
Name Type Size Value
--------------------------------------------
obj_A my_object - @349
value integral 32 'h1f135537
names string 0 ""
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
--------------------------------------------
and obj_B =
--------------------------------------------
Name Type Size Value
--------------------------------------------
obj_B my_object - @351
value integral 32 'he96c3deb
names string 0 ""
colors color_type 32 RED
data sa(integral) 4 -
[0] integral 8 'hcb
[1] integral 8 'h49
[2] integral 8 'hcc
[3] integral 8 'h49
addr integral 8 'h6d
tmp temp_class - @352
tmp_addr integral 8 'h66
tmp_data integral 8 'h27
--------------------------------------------
UVM_INFO /apps/vcsmx/vcs/Q-2020.03-SP1-1//etc/uvm-1.2/src/base/uvm_comparer.svh(351) @ 0: reporter [MISCMP] Miscompare for obj_B.value: lhs = 'he96c3deb : rhs = 'h1f135537
UVM_INFO /apps/vcsmx/vcs/Q-2020.03-SP1-1//etc/uvm-1.2/src/base/uvm_comparer.svh(382) @ 0: reporter [MISCMP] 1 Miscompare(s) for object obj_A@349 vs. obj_B@351
UVM_INFO testbench.sv(69) @ 0: uvm_test_top [uvm_test_top] Comparison did not match
UVM_INFO testbench.sv(60) @ 0: uvm_test_top [uvm_test_top] obj_B is copied to obj_A
UVM_INFO testbench.sv(66) @ 0: uvm_test_top [uvm_test_top] Comparison matched
do_compare method
The do_compare()
callback method is a user-defined hook that is called by the compare()
method. The derived class should override The uvm_object_utils_begin, `uvm_object_utils_end
and `uvm_field_*
macros are not used.
do_compare method example
`include "uvm_macros.svh"
import uvm_pkg::*;
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_copy(uvm_object local_h);
temp_class tmp_h;
super.do_copy(local_h);
// $cast is necessary: uvm_object local_h do not have temp_class properties (tmp_addr and tmp_data)
// temp_class is extended from uvm_object, so child class temp_class handle is required for copying each property.
$cast(tmp_h, local_h);
tmp_addr = tmp_h.tmp_addr;
tmp_data = tmp_h.tmp_data;
endfunction
function bit do_compare(uvm_object local_h, uvm_comparer comparer);
temp_class tmp_h;
$cast(tmp_h, local_h);
return (super.do_compare(tmp_h, comparer) &
tmp_addr == tmp_h.tmp_addr &
tmp_data == tmp_h.tmp_data);
endfunction
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(my_object)
function new(string name = "my_object");
super.new(name);
tmp = new();
endfunction
function void do_copy(uvm_object local_h);
my_object my_obj;
super.do_copy(local_h);
$cast(my_obj, local_h);
value = my_obj.value;
names = my_obj.names;
colors= my_obj.colors;
data = my_obj.data;
addr = my_obj.addr;
tmp.copy(my_obj.tmp);
endfunction
function bit do_compare(uvm_object local_h, uvm_comparer comparer);
my_object my_obj;
$cast(my_obj, local_h);
return (super.do_compare(my_obj, comparer) &
value == my_obj.value &
names == my_obj.names &
colors == my_obj.colors &
data == my_obj.data &
addr == my_obj.addr &
tmp.do_compare(my_obj.tmp, comparer));
endfunction
endclass
class my_test extends uvm_test;
`uvm_component_utils(my_test)
my_object obj_A, obj_B;
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_A = my_object::type_id::create("obj_A", this);
obj_B = my_object::type_id::create("obj_B", this);
assert(obj_A.randomize());
assert(obj_B.randomize());
`uvm_info(get_full_name(), "Before copying", UVM_LOW);
obj_print(obj_A);
obj_print(obj_B);
my_compare(obj_A, obj_B);
obj_B.copy(obj_A);
`uvm_info(get_full_name(), "After copying", UVM_LOW);
obj_print(obj_B);
my_compare(obj_A, obj_B);
endfunction
function void obj_print(uvm_object obj);
my_object my_obj;
string out;
$cast(my_obj, obj);
$display("-----------------------------");
$display("---------- %s ----------", my_obj.get_name);
$display("-----------------------------");
$sformat(out, "value = %0h, names = %s, addr = %0h", my_obj.value, my_obj.names, my_obj.addr);
`uvm_info(get_full_name(), $sformatf("%s", out), UVM_LOW);
foreach(my_obj.data[i]) `uvm_info(get_full_name(), $sformatf("data[%0d] = %h", i, my_obj.data[i]), UVM_LOW);
$sformat(out, "tmp_addr = %0h, tmp_data = %0h", my_obj.tmp.tmp_addr, my_obj.tmp.tmp_data);
`uvm_info(get_full_name(), $sformatf("tmp: %s", out), UVM_LOW);
endfunction
function void my_compare(my_object obj_A, obj_B);
if(obj_B.compare(obj_A)) begin
`uvm_info(get_full_name(), "Comparison matched", UVM_LOW);
end
else begin
`uvm_info(get_full_name(), "Comparison did not match", UVM_LOW);
end
endfunction
endclass
module tb_top;
initial begin
run_test("my_test");
end
endmodule
Output:
UVM_INFO testbench.sv(89) @ 0: uvm_test_top [uvm_test_top] Before copying
-----------------------------
---------- obj_A ----------
-----------------------------
UVM_INFO testbench.sv(107) @ 0: uvm_test_top [uvm_test_top] value = 1f135537, names = , addr = 2f
UVM_INFO testbench.sv(108) @ 0: uvm_test_top [uvm_test_top] data[0] = 9f
UVM_INFO testbench.sv(108) @ 0: uvm_test_top [uvm_test_top] data[1] = 33
UVM_INFO testbench.sv(108) @ 0: uvm_test_top [uvm_test_top] data[2] = 12
UVM_INFO testbench.sv(108) @ 0: uvm_test_top [uvm_test_top] data[3] = 9c
UVM_INFO testbench.sv(110) @ 0: uvm_test_top [uvm_test_top] tmp: tmp_addr = 39, tmp_data = bd
-----------------------------
---------- obj_B ----------
-----------------------------
UVM_INFO testbench.sv(107) @ 0: uvm_test_top [uvm_test_top] value = e96c3deb, names = , addr = 6d
UVM_INFO testbench.sv(108) @ 0: uvm_test_top [uvm_test_top] data[0] = cb
UVM_INFO testbench.sv(108) @ 0: uvm_test_top [uvm_test_top] data[1] = 49
UVM_INFO testbench.sv(108) @ 0: uvm_test_top [uvm_test_top] data[2] = cc
UVM_INFO testbench.sv(108) @ 0: uvm_test_top [uvm_test_top] data[3] = 49
UVM_INFO testbench.sv(110) @ 0: uvm_test_top [uvm_test_top] tmp: tmp_addr = 66, tmp_data = 27
UVM_INFO testbench.sv(118) @ 0: uvm_test_top [uvm_test_top] Comparison did not match
UVM_INFO testbench.sv(94) @ 0: uvm_test_top [uvm_test_top] After copying
-----------------------------
---------- obj_B ----------
-----------------------------
UVM_INFO testbench.sv(107) @ 0: uvm_test_top [uvm_test_top] value = 1f135537, names = , addr = 2f
UVM_INFO testbench.sv(108) @ 0: uvm_test_top [uvm_test_top] data[0] = 9f
UVM_INFO testbench.sv(108) @ 0: uvm_test_top [uvm_test_top] data[1] = 33
UVM_INFO testbench.sv(108) @ 0: uvm_test_top [uvm_test_top] data[2] = 12
UVM_INFO testbench.sv(108) @ 0: uvm_test_top [uvm_test_top] data[3] = 9c
UVM_INFO testbench.sv(110) @ 0: uvm_test_top [uvm_test_top] tmp: tmp_addr = 39, tmp_data = bd
UVM_INFO testbench.sv(115) @ 0: uvm_test_top [uvm_test_top] Comparison matched
UVM Tutorials