Tutorials

Learn More

In uvm_object, we discussed print, clone, copy, compare methods, etc. The compare() method compares two objects to return 1 in case of successful comparison. The uvm_comparer adds up policy for the comparison and counts the number of miscompares if any.

Variables in uvm_comparer class

Type

Variables

Description

enum

policy

It is an enum variable that compares the type UVM_DEEP, UVM_SHALLOW, or UVM_REFERENCE.

int unsigned

show_max

int

verbosity

Sets verbosity for message prints. Default verbosity = UVM_LOW

uvm_severity

sev

Sets severity for message prints. Default sev = UVM_INFO

bit

abstract

Provides a filtering mechanism for fields.

bit

physical

Provides a filtering mechanism for fields.

string

miscompares

Holds the last set of miscompares during a comparison. The miscompares string resets to an empty string on starting a comparison.

bit

check_type

Verifies types of two comparing objects having type given by uvm_object::get_type_name.

int unsigned

result

Stores number of miscompares for the given comparison. It is recommended to clear this variable before starting a new comparison otherwise, the end result might be incorrect.

Note: The physical and abstract bit setting distinguish objects between two different classes of fields.

Methods in uvm_comparer class

Methods

Description

virtual function bit compare_field

(string                   name,

uvm_bitstream_t  lhs, 

uvm_bitstream_t  rhs,

int                 size,

uvm_radix_enum radix = UVM_NORADIX)

Compares two integral values.

name: used to store and print miscompare.

lhs and rhs: used to compare left-hand and right-hand side objects

size: indicates number of bis to compare. (size <= 4096)

radix: used for reporting purposes. Default radix = HEX.

virtual function bit compare_field_int

(string name,

uvm_integral_t  lhs, uvm_integral_t  rhs,

int               size,

uvm_radix_enum radix = UVM_NORADIX)

It is the same as compare_field except arguments are small integers having size <= 64.

If compare_field calls compare_field_int automatically if the size argument has a value less than or equal to 64.

virtual function bit compare_field_real

(string  name,

real      lhs,

real      rhs)

It is the same as compare_field except arguments are real numbers

virtual function bit compare_string 

(string name,

string lhs,

string rhs)

It is the same as compare_field except arguments are string variables.

virtual function bit compare_object 

(string         name,

uvm_object lhs,

uvm_object rhs)

Compares two class objects using a policy variable having deep, shallow or reference comparison.

function void print_msg

(string msg)

Prints a message that appends error count to miscompares string. 

UVM Comparer example

class transaction extends uvm_object;
  rand bit[15:0] addr;
  rand bit[15:0] data;
  `uvm_object_utils_begin(transaction)
    `uvm_field_int(addr, UVM_PRINT);
    `uvm_field_int(data, UVM_PRINT);
  `uvm_object_utils_end
  
  function new(string name = "transaction");
    super.new(name);
  endfunction
endclass

class base_test extends uvm_test;
  transaction tr1, tr2;
  uvm_comparer comp;

  `uvm_component_utils(base_test)
  
  function new(string name = "base_test", uvm_component parent = null);
    super.new(name, parent);
  endfunction
  
  function void build_phase(uvm_phase phase);
    super.build_phase(phase);
    tr1 = transaction::type_id::create("tr1", this);
    tr2 = transaction::type_id::create("tr2", this);
    comp = new();
  endfunction
 
  task run_phase(uvm_phase phase);
    super.run_phase(phase);
    assert(tr1.randomize());
    assert(tr2.randomize());
    
    comp.verbosity = UVM_LOW;
    comp.sev = UVM_ERROR;
    comp.show_max = 100;
    
    `uvm_info(get_full_name(), "Comparing objects", UVM_LOW)
    comp.compare_object("tr_compare", tr1, tr2);
    tr2.copy(tr1);
    comp.compare_object("tr_compare", tr1, tr2);
    `uvm_info(get_full_name(), $sformatf("Comparing objects: result = %0d", comp.result), UVM_LOW)
    
    comp.compare_field_int("int_compare", 5'h2, 5'h4, 5);
    comp.compare_string("string_compare", "name", "names");
    
    `uvm_info(get_full_name(), $sformatf("Comparing objects: result = %0d", comp.result), UVM_LOW)
    
    comp.compare_field_int("int_compare", 5'h4, 5'h4, 5);
    comp.compare_string("string_compare", "name", "name");
  endtask
endclass

module tb_top;
  initial begin
    run_test("base_test");
  end
endmodule

Output:

UVM_INFO testbench.sv(33) @ 0: uvm_test_top [uvm_test_top] Comparing objects
UVM_ERROR /xcelium20.09/tools//methodology/UVM/CDNS-1.2/sv/src/base/uvm_comparer.svh(351) @ 0: reporter [MISCMP] Miscompare for tr_compare.addr: lhs = 'hf87e : rhs = 'h7781
UVM_ERROR /xcelium20.09/tools//methodology/UVM/CDNS-1.2/sv/src/base/uvm_comparer.svh(351) @ 0: reporter [MISCMP] Miscompare for tr_compare.data: lhs = 'h6139 : rhs = 'hf210
UVM_ERROR /xcelium20.09/tools//methodology/UVM/CDNS-1.2/sv/src/base/uvm_comparer.svh(382) @ 0: reporter [MISCMP] 2 Miscompare(s) for object tr2@1874 vs. tr1@1872
UVM_ERROR /xcelium20.09/tools//methodology/UVM/CDNS-1.2/sv/src/base/uvm_comparer.svh(382) @ 0: reporter [MISCMP] 2 Miscompare(s) for object tr2@1874 vs. tr1@1872
UVM_INFO testbench.sv(44) @ 0: uvm_test_top [uvm_test_top] Comparing objects: result = 2
UVM_ERROR /xcelium20.09/tools//methodology/UVM/CDNS-1.2/sv/src/base/uvm_comparer.svh(351) @ 0: reporter [MISCMP] Miscompare for int_compare: lhs = 'h2 : rhs = 'h4
UVM_ERROR /xcelium20.09/tools//methodology/UVM/CDNS-1.2/sv/src/base/uvm_comparer.svh(351) @ 0: reporter [MISCMP] Miscompare for string_compare: lhs = "name" : rhs = "names"
UVM_INFO testbench.sv(49) @ 0: uvm_test_top [uvm_test_top] Comparing objects: result = 4