How do I print a dynamic array in the convert2string method of transaction class?

Hi

I have a transaction class extending from ovm_sequence_item:

class my_trans extends ovm_sequence_item;

...
// Class members

rand bit first;
rand int info;
rand int delay;
rand byte wdata[];

...

`ovm_object_utils_begin(my_trans)
     `ovm_field_int (first, OVM_ALL_ON)
     `ovm_field_array_int(wdata, OVM_ALL_ON)
      ....
`ovm_object_utils_end

//Methods

function new(string name= "...");
...
endfunction

function string convert2string();
  string str1;
  str1 = {
       $sformatf("m_first :'h%h\n", first),
       ....
       $sformatf("wdata :%x\n", wdata),
         }
    return(str1);
endfunction

endclass

I use this function in my scoreboard when comparing two transactions, for debug purposes by calling txn.conver2string()

I get a compile time error saying illegal format specifier. Can someone help me understand how can I convert this dynamic array to string format and print it?

If you want your entire array on a single line, you can write

function string convert2string();
  string str1;
  str1 = {
       $sformatf("m_first :'h%h\n", first),
       ....
         }
  $swriteh(str, "%s wdata :%0p\n", str, wdata),
  return(str1);
endfunction

Otherwise you will need to use a foreach or for loop to iterate over each element and insert newlines where you want them.

In reply to dave_59:

Thanks! That works.

Hi All,
I have a dynamic array on data.
To change dynamic array to string variable for my debugging process.

 bit data[];  //Declaration
      string data2string= "";
      .....
      .....
      data = new[primitive]; // primitive will be change dynamically
   
      data2string = convert2string();         
   
      function string convert2string();
  string str1;
  str1 = {
       $sformatf("data:'h%h\n",data),
         }
  return(str1);
endfunction

But i am facing a compilation issue.

Thanks
Rajaraman

In reply to Rajaraman Rak7:

I can think of two choices:

You can use the streaming operator to pack data into a fixed sized variable

bit [MAX_DATA_SIZE-1:0] data_packed;
data_packed = {>>{data}};
str1 = $sformatf("data:'h%h\n",data_packed);

You might have to play around with the packing order and shift the results.

The other option is using a for-loop and packing 4-bits at a time, converting that to one hex character, and appending that to your str1.

for(int ii=0, ii<data.size(), ii+=4) begin
   bit [3:0] nibble;
   nibble = {>>{data[ii+:4]}};
   str1 = {str1,$sformatf("data:'h%h\n",data_packed)};
end

Again, you might have to play with the ordering as I didn’t put too much time into this.