[UVM] Issue in Pack and un_pack methods usage

Hi,

I have below snippet of code.I have used pack method to pack the bytes and unpack method to unpack.

Issue is i am not getting that data same as i packed.

Please help me by taking a look in below code.

Thanks
Bharath

In reply to bharath123:

The size of your packed bytes should have given you an indication that something is wrong. You get 17 bytes. Everything is getting packed twice. This is because when you used the field automation macros, you didn’t specify UVM_NOPACK when you intended to implement your own packing scheme. If you do this, you also need to implement the do_unpack(…) function:


     function void do_unpack(uvm_packer packer);
       sa = packer.unpack_field_int($bits(sa));
       // ...
     endfunction:do_unpack

In reply to Tudor Timi:

Hello Tudor,

I actually i missed to add uvm_unpack method.

Can you elaborate on reason it is printing size as 17 Bytes?

Thanks

In reply to bharath123:

When you use the field automation macros with UVM_ALL_ON, that also include packing. This means that the pack(…) function will pack all of the fields you have in the order in which you “called” the macros. You’re getting 4 bytes for color (because per default enums are 32 bits), 1 byte for da and sa and 5 more bytes for data.

Because data is a dynamic array, UVM doesn’t know how much to unpack, so data doesn’t get unpacked at all. You need to pack information about the size of data. This is done by setting the use_metadata field in the packer. In your case it should be the uvm_default_packer.

In reply to Tudor Timi:

I have made certain updates in my code and found the data i had packed and unpacked are mismatched.

can you help me to fiure out the issue.

please find the link at below path:

Best Regards

In reply to bharath123:

With Removing packing of enum member of class,I got proper result

Why we are not able to pack the enum data type?
Please find the updated code at below path:

Thanks
Bharath

In reply to bharath123:

If you pack the fields in a certain order you have to also unpack them in a certain order. Also, if you use the sized macros (like pack_enumN), you have to use the counterpart when unpacking:


     function void do_pack(uvm_packer packer);
       super.do_pack(packer);
       packer.pack_field_int(sa,$bits(sa));
       packer.pack_field_int(da,$bits(da));
       packer.pack_field_int(data_size,$bits(data_size));
       `uvm_pack_enumN(color,8);
       foreach(data[i]) begin
         packer.pack_field_int(data[i],8);
       end
     endfunction:do_pack
    
     function void do_unpack(uvm_packer packer);
       super.do_pack(packer);
       sa=packer.unpack_field_int($bits(sa));
       da=packer.unpack_field_int($bits(da));
       data_size=packer.unpack_field_int($bits(data_size));
       `uvm_unpack_enumN(color, 8, color_t);
       data=new[data_size];
       foreach(data[i])
         data[i]=packer.unpack_field_int(8);
     endfunction:do_unpack

No idea why you’d want to pack color after data_size, but whatever. I’ve packed color in 8 bits so it’s easy to analyze the packed bytes, but you can use 1 bit there as well. Best would be to change your enum definition to:


typedef enum bit {GOOD_COLOR,BAD_COLOR} color_t;

This way you don’t need the pack/unpack_enumN macros and you could just use pack/unpack_enum.