Associative Arrays of Queues warning message - SystemVerilog queue index 6 is beyond the queue's writable range

Hi there,

For following code,

module tb;
  bit [31:0] assoc_q[int][$];
  int tag_id;
  
  initial begin
    tag_id = 7;
    assoc_q[tag_id][0] = 4;
    $display("assoc_q[%d][0]=%d",tag_id,assoc_q[tag_id][0]);
    assoc_q[tag_id][6] = 5;
    $display("assoc_q[%d][6]=%d",tag_id,assoc_q[tag_id][6]);
  end
endmodule

Following is the simulator o/p where I am getting WARNING message “SystemVerilog queue index 6 is beyond the queue’s writable range [0:1]”.
Can someone help me fix the code and/or suggest an alternate to get rid of this warning?

Thank you for your support in advance.

  • Shailesh

====
assoc_q[ 7][0]= 4
assoc_q[tag_id][6] = 5;
|
ncsim: *W,RTSVQI (./testbench.sv,12|19): SystemVerilog queue index 6 is beyond the queue’s writable range [0:1].
assoc_q[ 7][6]= 0
Simulation complete via implicit call to $finish(1) at time 0 FS + 1
./testbench.sv:4 program tb;

In reply to thakks:

You need to allocate space for queue elements before you can write to them. You do that with a push method, or array concatenation.

assoc_q[tag_id].push_back (4); // queue was empty, so pushes element 0
assoc_q[tag_id].push_back (0);
assoc_q[tag_id].push_back (0);
assoc_q[tag_id].push_back (0);
assoc_q[tag_id].push_back (0);
assoc_q[tag_id].push_back (0);
assoc_q[tag_id].push_back (5);

// or
assoc_q[tag_id] = {4,0,0,0,0,0,5};

Thanks Dave for quick response.

Lets say I know that I will be getting 512 elements in that queue, what is the best way to allocate space?

Also when I allocate a space, meaning I am writing a value to that element. When the actual value is received later, how to I overwrite it?

  • Shailesh

In reply to thakks:

The major difference between queues, associative, and dynamic arrays is how elements get allocated. Once they are allocated, you can access them the same with a valid index.

A queue works best when you expect to be adding and removing elements one at a time. Otherwise, consider using a dynamic array.