UVM object pack and unpack - vineethkumarv/uvm-repos GitHub Wiki

UVM object pack and unpack

Pack and unpack are a part of UVM automation macros that are used to pack class variables to convert into bitstream.

Method Description
pack Performs a bit-wise concatenation of object's properties into an array of type bit.
pack_bytes Performs a bit-wise concatenation of object's properties into an array of type byte.
pack_ints Performs a bit-wise concatenation of the object's properties into an array of type int.
do_pack The do_pack method is the user-definable hook called by the pack methods.
unpack Extracts property values from an array of type bit and stores into correct class variable.
unpack_bytes Extracts property values from an array of type byte and stores into correct class variable.
unpack_ints Extracts property values from an array of type int and stores into correct class variable.
do_unpack The do_unpack method is the user-definable hook called by the pack methods.

code:

class test extends uvm_test;
`uvm_component_utils(test)
function  new(string name= "test",uvm_component parent=null);
  super.new(name,parent);
endfunction
my_object obj;
bit bits[];
byte unsigned bytes[];
int unsigned ints[];
my_object unp_obj;
virtual function void build_phase(uvm_phase phase);
obj=my_object::type_id::create("my_object",this);
obj.randomize();
obj.print();
$display("before pack values of byte are %p and stream bytes is %p",obj.data,bytes);
obj.pack(bits);
obj.pack_bytes(bytes);
$display("after pack values of byte are %p and stream bytes is %p",obj.data,bytes);
obj.pack_ints(ints);
`uvm_info(get_type_name(),$sformatf("pack bitdata=%p",bits),UVM_LOW);
`uvm_info(get_type_name(),$sformatf("pack bytedata=%p",bytes),UVM_LOW);
`uvm_info(get_type_name(),$sformatf("pack intdata=%p",ints),UVM_LOW);
$display("----------------------------------------------------------");
unp_obj=my_object::type_id::create("my_object",this);
unp_obj.unpack(bits);
unp_obj.unpack_bytes(bytes);
unp_obj.unpack_ints(ints);
//unp_obj.print();
`uvm_info(get_type_name(),$sformatf("pack bitdata=%p",bits),UVM_LOW);
`uvm_info(get_type_name(),$sformatf("pack bytedata=%p",bytes),UVM_LOW);
`uvm_info(get_type_name(),$sformatf("pack intdata=%p",ints),UVM_LOW);
endfunction  
endclass

output:

image

             Fig. Output of the code

In the above code obj is the handle of the my_object class that extends uvm_object.All the properties in the class are packed into bitstream in the bits array and all the class properties are packed into bytestream in bytes array and into intstream by ints array. The main objective of the pack is to convert all the object properties into the required stream,for example in the code the array bytes stores all the values of the object properties in the form of byte datatype,you can see in the output that the byte type class property is only data and the size is 5 bytes but after packing the bytestream bytes has 10 values of byte type,this is how the pack works.And unpack helps us to distribute the values to the respective class variables.

Note
1.do_pack and do_unpack are functions that implement all the packing and unpacking in single function call. 2.unpack actions are done by the receiver side like instance of same one class can unpack the values packed usong the instance o another class.

Why pack and unpack?

In bus protocol,you will be having a large amount of data on particular bus transaction and hence it becomes very tough to extract specific field value from that bus transactions. So we use classes on higher level,so that by using different fields,it becomes easy for you to control the value of those fields.
But in real hardware,everything is in bits,so you need to convert those class fields into valid bit patterns and transmit them along the bus.for that we use pack at transmitter and unpack at receiver.