The 'with' in streaming dynamically sized data is giving error (11.4.14.4 of LRM(1800-2017))

Hello,

I am trying to achieve the following task. A loop generates the following values which are placed in an associative array.

00000000000000000000000000000000
00000000000000000000000000000010
00000000000000000000000000000011
00000000000000000000000000000100
00000000000000000000000000000101
00000000000000000000000000000110
00000000000000000000000000001110
00000000000000000000000000011110
00000000000000000000000000111110
00000000000000000000000001111110

I am trying to get some bits on the right side according to a variable. Sometimes it is 2, other times 4, then 8 maybe.
For example in the last one if I have variable value of 4, I should be able to get 1110. If I have variable value of 2, I should get 10.

What I have done so far:


bit [4:0] stream; // to store five of the sliced bits
int array[string]; //the associative array
string symbol; //its index
logic [31:0] code; // The above data is stored in this register for every iteration. Then discarded and new value stored
//rest of the code here to generate more 'code' i.e. the contents of 'array'
array[symbol] = code; //placed inside array
//a loop to iterate over the contents of the array
{<<{stream}} = code; //extracting right-side five bits from array
//Some logic to put these extracted bits in a new array
$display("Bits are %b", stream); //displaying the extracted bits for each iteration of the loop

This worked fine when I was learning about streaming operators, but I run into errors when I want to dynamically change the width of the extraction. I tried this:


bit stream[];
int array[string]; 
string symbol; 
logic [31:0] code;
//rest of the code here to generate the contents of 'array'
array[symbol] = code;
//a loop to iterate over the contents of the array
stream = {<<{code with [j-:0]}}; //extracting right-side bits from 'code', with variable being 'j'
$display("Bits are %b", stream); //displaying the extracted bits for each iteration of the loop


I get this error: syntax error, unexpected with-then-[, expecting ‘,’ or ‘}’

Then I tried this:


logic stream[];
int array[string]; 
string symbol; 
logic [31:0] code;
//rest of the code here to generate the contents of 'array'
array[symbol] = code;
//a loop to iterate over the contents of the array
stream = new[j]; //a dynamic array produced with each iteration
{<<{stream}} = code; 
$display("Bits are %b", stream); //displaying the extracted bits for each iteration of the loop


This gives the error: Node VARREF ‘code’ has no expected width?? Missing Visitor func?


How can I achieve my task?

In reply to Dev_Engine:

It would really help to give us enough code to execute the streaming operator as a complete example.

There are two problems your use of the with construct. It only works to select elements of unpacked arrays, and you have the range backwards.
Here is an example of a complete example

module top;
  int j;
  bit stream[];
  bit [31:0] code;
  initial begin
    j = 4;
    code = 32'b00000000000000000000000001111110;
    stream = {<<{code}};                // first do packed to unpacked array conversion
    stream = {>>{stream with [0+:j]}};  // then extract the bits you want
    $display("Bits are %p", stream); 
  end
endmodule

This prints

# Bits are '{0, 1, 1, 1}

Note that %p prints stream[0] first.

In reply to dave_59:

Thank you Dave. It is my simulator that does not support ‘with’ while using streaming operators. I tried your code separately and it gave the same error.
Is there some other way to achieve what I want to do?

In reply to Dev_Engine:

stream = new[j];
foreach(stream[i]) stream[i] = code[i];

In reply to dave_59:

In reply to Dev_Engine:

stream = new[j];
foreach(stream[i]) stream[i] = code[i];

I tried this for the entire dataset in a loop with this

stream = new[j](stream);

But it didn’t work as previous values weren’t appended to the latest ones. So I used an associative array and it worked fine.

Thanks again!