Sie sind auf Seite 1von 4

UVM CallBacks

Developer and User perspectives


Sirajudheen Mubarak,WaferSpace,siraj.mubarak@gmail.com
What is CallBack ?
In computer programming, a callback is a piece of executable code that is passed as an argument to other code,
which is expected to call back(execute) the argument at some convenient time .
The environment developer must insert calls to specific callback methods/tasks at points where the user might need to
take specific action. The developer should think and come up with the user's requirement so that the user can do
some operation on the transactions which are in progress. The callback users can then create their derived callback
class and attach to one or more desired objects.The use model of callbacks is broken into two parts, the developer
side and the user's side .
$UVM_HOME says..
The callback class defines an interface consisting of one or more function or task prototypes. The signatures of each
method have no restrictions. The component developer knows best the intended semantic of multiple registered
callbacks. Thus the algorithm for traversal the callback queue should reside in the callback class itself. We could
provide convenience macros that implement the most common traversal methods, such as sequential in-order
execution.
Developer:
The developer must make the space where the callback to be executed so that the user can come and modify the
transaction object which are in progress. The CallBack function should have the referance to the the transaction object
and other required details like is_drop(if the user need to drop the transaction?) , insert(user can insert new packet
before or after the ongoing transaction) etc.
Using our example from Hierarchy Information Functions on page 46, we may want to add a callback to the city class
which gets executed before it prints its parent/child information. To do this we would do something like:
typedef class packet;
virtual class link_layer_cbs extends uvm_callback;
function new(string name="link_layer_cbs");
super.new(name);
endfunction
pure virtual function void on_enter_link_layer(ref packet pkt);
pure virtual task exit_link_layer(ref packet pkt);
endclass
Developer should register the callback type with the class type which uses the call back here it is link_layer.
UVM will check the type of the callback when the user adds it.Without registering callbacks, user will get a warning
from UVM that the callback type was not registered with the object type they are trying to add it to.
class link_layer extends uvm_component;
`uvm_register_cb(link_layer, link_layer_cbs )
...
endclass
Now the developer need to add the virtual functions for adding callBack or directly add the callback by using the uvm
macro `uvm_do_callbacks() as below. There are set of uvm macros availabe for adding,registering,doing callbacks.

`uvm_do_callbacks_exit_on(T,CB,METHOD,VAL)
Calls the given METHOD of all callbacks of type CB registered with the calling object (i.e. this object), which is or is
based on type T, returning upon the first callback returning the bit value given by VAL.
This macro executes all of the callbacks associated with the calling object (i.e. this object). The macro takes three
arguments:
CB is the class type of the callback objects to execute. The class type must have a function signature that
matches the METHOD argument.
T is the type associated with the callback. Typically, an instance of type T is passed as one the arguments in
the METHOD call.
METHOD is the method call to invoke, with all required arguments as if they were invoked directly.
VAL, if 1, says return upon the first callback invocation that returns 1. If 0, says return upon the first callback
invocation that returns 0.
`uvm_do_callbacks(T,CB,METHOD)
Calls the given METHOD of all callbacks of type CB registered with the calling object (i.e. this object), which is or is
based on type T.
This macro executes all of the callbacks associated with the calling object (i.e. this object). The macro takes three
arguments:
CB is the class type of the callback objects to execute. The class type must have a function signature that
matches the METHOD argument.
T is the type associated with the callback. Typically, an instance of type T is passed as one the arguments in
the METHOD call.
METHOD is the method call to invoke, with all required arguments as if they were invoked directly.
`uvm_register_cb(T,CB)
Registers the given CB callback type with the given T object type. If a type-callback pair is not registered then a
warning is issued if an attempt is made to use the pair (add, delete, etc.). The registration will typically occur in the
component that executes the given type of callback
Class link_layer extends uvm_component;
`uvm_register_cb(link_layer, link_layer_cbs)
virtual function bit on_enter_link_layer(packet pkt);
`uvm_do_callbacks_exit_on(
link_layer,
//
link_layer_cbs,
//
on_enter_link_layer(this,tr), //
1
// return after the
)
endfunction

this type
callback type
callback call
first invocation

virtual task on_exit_link_layer(ref packet pkt);


`uvm_do_callbacks(
bus_driver,
bus_driver_cb,
trans_executed(this,tr)
)
endtask
virtual task main_phase(uvm_phase phse);
protocol_layer_port.get(pkt);
uvm_report_info(get_type_name(),"Packet Entered to Link Layer",UVM_LOW);
if (!on_enter_link_layer(pkt))

begin
uvm_report_info("pkt dropped", "user callback indicated DROPPED\n");
return;
end
drive_pkt_to_next_layer(pkt); // user task
on_exit_link_layer(pkt);
uvm_report_info(get_type_name(),"pkt is Exiting Link Layer",UVM_LOW);
endtask
endclass
End-User Use Model
The end-user simply needs to extend the callback base class, overriding any or all of the prototypes provided in the
developer-supplied callback interface. Then, register an instance of the callback class with any object designed to
use the base callback type.
class my_ll_cbs extends link_layer_cbs;
// override the callback methodes here.
Endclass
To avoid compilation issue due to the order of compilation, define the type of callback
typedef uvm_callbacks #(link_layer,link_layer_cbs) link_layer_cbs_t;

ADD/DELETE
INTEFACE

add
add_by_name
delete
delete_by_name
ITERATOR
INTERFACE
get_first
get_last
get_next
get_prev
DEBUG
display

uvm_callbacks methods
Registers the given callback object, cb, with the givenobj handle.
Eg: uvm_callbacks#(my_comp)::add(comp_a, cb);

Registers the given callback object, cb, with one or more uvm_components.
Deletes the given callback object, cb, from the queue associated with the given obj handle.
Eg: uvm_callbacks#(my_comp, my_callback)::delete(comp_a,cb);

Removes the given callback object, cb, associated with one or more uvm_component
callback queues.
This set of functions provide an iterator interface for callback queues.
Returns the first enabled callback of type CB which resides in the queue for obj.
Returns the last enabled callback of type CB which resides in the queue for obj.
Returns the next enabled callback of type CB which resides in the queue for obj, using itr as
the starting point.
Returns the previous enabled callback of type CB which resides in the queue for obj, using
itr as the starting point.
This function displays callback information for obj.

Attaching the Callback


To register the callbacks with the link_layer, we get the global callback pool that is typed to our specific link_layercallback combination. We associate (register) the callback objects with link_layer using the callback pool's
add() method
// Test buils_phase
virtual fucntion void build_phase(uvm_phase phase)
// Create the environment components
link_layer link = link_layer::type_id::create("link",this);
// Create the callback
my_ll_cbs ll_cb = new("cb1");
// Add callback to the link_layer
link_layer_cbs_t::add(link,ll_cb);
endfunction

Guidelines for Using Callbacks


=======================
Developer Guidelines
=============
Create an abstract (virtual) class for the callbacks with pure virtual methods for the interface.
Pass a reference of the object making the callback call.
Make sure that the packet is not modified without the user's or developer's knowledge as we use referance of
the transaction packet between the callbacks and the normal flow of the env.
Provide a typedef for the uvm_callbacks#(T,CB) class with the name T_cb.

User Guidelines
=============
Create a callback implementation class which implements all of the pure virtual methods in the callback class.
Allocate a new callback object for each callback you add.

Sirajudheen Mubarak.E
siraj.mubarak@gmail.com