How to select specific bits from integer and assign it to queue of byte

I have the following code:

   rand byte unsigned data[$];
   int burst_width, raw_data;

burst_width value is calculated on the fly.

I have to assign

raw_data[(burst_width*8-1):0]

to data.

I tried the follwong:

data = raw_data >> (burst_width*8);

But in system Verilog we can’t assign packed array to queue.

In reply to Sarit8r:

Use the streaming pack/unpack operator

module top;
  
  byte unsigned data[$];
  int burst_width, raw_data;
  
  initial begin
    raw_data = 'h01234567;
    $displayh("raw_data %h",raw_data);
    for(burst_width=1;burst_width<5;burst_width++) begin
      data = {>>{raw_data with [(burst_width*8)-1:0]}};
      $displayh("%0p",data);
    end
  end
endmodule
1 Like

In reply to Sarit8r:

If you want to convert an integer (raw_data) into a packed array of bytes (data), you need to handle the bit extraction and packing yourself. Since you are working with a dynamic array of bytes, you can do this in a loop. Here is the way to do it :

rand byte unsigned data[$];
int burst_width, raw_data;

// ... Assume burst_width is set somehow ...

// Calculate the size of the data array based on burst_width
data = new[burst_width];

// Pack raw_data into data
int i;
for (i = 0; i < burst_width; i++) begin
    data[i] = raw_data[8*i +: 8]; // Extract 8 bits at a time
end