Hi Folks,
I would like to create an associative array where a string key points to another associative array or to a reference of one.
Similarly I would like to create an associative array where a string key points to a dynamic string array.
Does the language allow the above data types?
-ravi
*In reply to ravi.ck.kumar:*Yes. SystemVerilog supports arrays whose elements can be of any type. When dealing with dynamically sized array, you can think of it as arrays of arrays instead of multi-dimensional arrays. because an array element in each dimension can have a different number of elements.
You can declare arrays of arrays by using an intermediate typedef (preferred)
typedef int int_index_by_str_t[string];
int_index_by_str_t lookup[];
or
int lookup[][string];
In reply to dave_59:
Hi David,
Thanks for the pointers. Based on that I created a quick example…
module mytest();
typedef string AADD[*]; // create a hash type
AADD arrayOfAAs; // create an array of hashes
arrayOfAAs AAWC2[string]; // create a hash of ‘array-of-hashes’ <<— This Doesn’t.
AADD AAWC3[string]; // create a hash of hash. <<---- This Works!!
…
I get the following error. I would like to have a string to reference an array of hashes OR even
Error-[SE] Syntax error
Following verilog source has syntax error :
memory of unknown type
“a.sv”, 6: token is ‘;’
arrayOfAAs AAWC2[string]; // create a hash of ‘array-of-hashes’
*In reply to ravi.ck.kumar:*You need to use a type when declaring a variable.
typedef AADD arrayOfAAs[]; // create type of an array of hashes
arrayOfAAs AAWC2[string]; // create a hash of 'array-of-hashes'
or
AADD arrayOfAAs[]; // create an array of hashes
var type(arrayOfAAs) AAWC2[string]; // create a hash of 'array-of-hashes'
Also, I do not recommend ever using a wildcard associative array index type. Use an actual type. The wildcard index type is antiquated and cannot be used with a number of other systemverilog features.
In reply to dave_59:
The second typedef did the trick! Thanks much. Also true about using wildcards. Very little utility in the first place IMHO.
In reply to dave_59:
*In reply to ravi.ck.kumar:*You need to use a type when declaring a variable.
typedef AADD arrayOfAAs[]; // create type of an array of hashes
arrayOfAAs AAWC2[string]; // create a hash of 'array-of-hashes'
or
AADD arrayOfAAs[]; // create an array of hashes
var type(arrayOfAAs) AAWC2[string]; // create a hash of 'array-of-hashes'
Also, I do not recommend ever using a wildcard associative array index type. Use an actual type. The wildcard index type is antiquated and cannot be used with a number of other systemverilog features.
AADD arrayOfAAs[]; // create an array of hashes
type(arrayOfAAs) AAWC2[string]; // create a hash of 'array-of-hashes'
Is this mode of type declaration supported in system-verilog?
In reply to yourcheers:
Yes. See 6.23 Type operator of the 1800-2012 LRM. I forgot to add the var keyword in front of the type. I corrected my example.
In reply to dave_59:
hi Dave , can we create a typedef using some previous typedef like :
typedef enum bit [1:0] {OKAY, ERROR, RETRY, SPLIT} response
so i want to create an associative array using response as input and logic [31:0] as key
typedef resp_t address_driver_resp [logic [31:0]] ; // will this work ?
if yes , how do we register it in our transaction_item ?
In reply to DV@Engineer :
An associative array can have any element type, and any index type as long as equality is defined for the index type.
What do you mean by “register it”?
I am unclear by what you mean an array of hashes. Is this the same thing as using a key into an array of arrays?
People use many different terms for what SystemVerilog calls an associative array: map, dictionary, symbol table, or hash. However a hash usually refers to the lookup mechanism used to access an element, not necessarily an array. I tend to avoid it, but was just repeating terminology the previous post used.
Looks so counter intuitive.
int lookup[][string]
feels more like an associative array of dynamic arrays.
But it is the other way round.
Why is this done?
When declaring the 2-dimensional fixed sized array int A[10][20]
, 10 ius the first dimension and 20 is the second dimension. Selecting A[0]
gives you a 1-dimensional array of 20 elements.
Similarly selecting lookup[0]
should give you an associative array.