Reputation: 51
In systemverilog - is it possible to create an associative array of dynamic arrays?
Specifically - I need a map from id's (integers) of a certain type of request, to arrays of bytes (the response to the request), however the size of each array of bytes is only known during runtime.
If it is not possible, is there a way to have instead an associative array of pointers or pointer like objects? Or any other ideas for a solution to these types of data structures?
I know I could create a wrapper class for the arrays, but that seems a little cumbersome for such a basic need...
Thanks
Upvotes: 3
Views: 3416
Reputation: 13967
It is possible to have an associative array of dynamic arrays (or a dynamic array of dynamic arrays etc), eg:
byte AA_OF_DA_OF_BYTE [*][];
The trouble is that once you get more than one dimension of your dynamic array, the System-Verilog language struggles a bit and you have to start writing more code:
module ASSOC_OF_DYN;
//
// here's your associative array of dynamic arrays
//
byte AA_OF_DA_OF_BYTE [*][];
//
// iterate over the associative array to fill it full
//
// eg 16 possible dynamic arrays...
int unsigned NO_AI = 16;
// ...of up to 256 bytes
int unsigned MAX_DA_SIZE = 256;
// this array is indexed by consequtive unsigned ints, but you can index by
// whatever you like
initial begin : FILL
for (int AI = 0; AI < NO_AI; AI++) begin : AI_LOOP
// pick a random size for the dynamoc array...
automatic int unsigned DA_SIZE = $urandom_range(0, MAX_DA_SIZE-1);
// ...and allocate the AIth dynamic array
AA_OF_DA_OF_BYTE[AI] = new[DA_SIZE];
// fill the dynamic array - this could be done some other way
for (int DI = 0; DI < DA_SIZE; DI++)
AA_OF_DA_OF_BYTE[AI][DI] = $urandom_range(0, 255); // because it is a byte
end : AI_LOOP
end : FILL
//
// display the filled array
//
final begin : DISPLAY
for (int AI = 0; AI < NO_AI; AI++)
$display("AA_OF_DA_OF_BYTE[%d]= %p", AI, AA_OF_DA_OF_BYTE[AI]);
end : DISPLAY
endmodule
https://www.edaplayground.com/x/kZM
Upvotes: 4