Wednesday, May 4, 2016

Using uvm_object Hook Methods to Implement User Specific Functionality

It is sometimes necessary to modify or add functionality to existing built-in methods in uvm objects. Numerous methods in the uvm object class have build-in hooks called do_<method name> allowing user to extend existing implementations of the <method name>. These hooks are implemented as empty methods in the uvm_object class type and can be overloaded in a derived class with user specific functionality. Each hook is called automatically by its respective default method call.

function bit  uvm_object::compare (uvm_object rhs, uvm_comparer comparer=null);
.
// UVM implementation here
.
dc = do_compare(rhs, comparer); // call to hook
.
.

But what happens when the default implementation cannot be used due to a corner case or if there is a special implementation requirement? Since the default methods are non-virtual they cannot be overloaded in derived classes.
One way to work around this is to create a new unrelated method, lets call it  "my_compare", and call this instead of the default method call. Using this type of workaround should be considered only as a last resort as it can hinder reuse of existing or future code. For example, if the class is an inheritance of a sequence item and factory replaced, a "my_compare" implementation might limit an existing scoreboard receiving and checking these items with the default compare method.
Fortunately, the uvm framework has considered this use case where the default implementation should be circumvented while allowing the user defined code to run from the hooked method.

class my_sequence_item extends uvm_sequence_item;

  rand logic [31:0]  var1;
  rand logic [7:0]   var2;

  `uvm_object_utils_begin (my_sequence_item)
    `uvm_field_int( var1, UVM_ALL_ON | UVM_NOCOMPARE)
    `uvm_field_int( var2, UVM_ALL_ON | UVM_NOCOMPARE)
  `uvm_object_utils_end

  extern virtual function bit  do_compare(uvm_object rhs,uvm_comparer comparer);

endclass: my_sequence_item
//------------------------------------------------------------------------//
function bit my_sequence_item::do_compare(uvm_object rhs,uvm_comparer comparer);
.
// implementation here
.
endfunction: do_compare

Using the compare function as our example, class members var1 and var2 in the code snippet above are registered to be ignored when calling the default compare method. The  do_compare hook is still executed by the default method call executing the user specific code. This ability allows a call to the default compare method without any need to know that the inherited object has a specialized functionality implemented or that the compare method implementation is circumvented.

No comments:

Post a Comment