Associative arrays

Jan. 14, 2024 || Item:2.7.Associative arrays

Dynamic arrays are useful for dealing with contiguous collections of variables whose number changes dynamically. When the size of the collection is unknown or the data space is sparse, an associative array is a better option. Associative arrays do not have any storage allocated until it is used, and the index expression is not restricted to integral expressions, but can be of any type.

An associative array implements a lookup table of the elements of its declared type. The data type to be used as an index serves as the lookup key, and imposes an ordering.

The syntax to declare an associative array is:

data_type array_id [ index_type ];

where:

- data_type: is the data type of the array elements. Can be any type allowed for fixed-size arrays.

- array_id: is the name of the array being declared.

- index_type: is the data-type to be used as an index, or *. If * is specified, then the array is indexed by any integral expression of arbitrary size. An index type restricts the indexing expressions to a particular type.

Examples of associative array declarations are:

integer i_array[*];         // associative array of integer (unspecified index)

bit [20:0] array_b[string]; // associative array of 21-bit vector, indexed by string

event ev_array[myClass];    // associative array of event indexed by class myClass

Array elements in associative arrays are allocated dynamically; an entry is created the first time it is written. The associative array maintains the entries that have been assigned values and their relative order according to the index data type. Associative array elements are unpacked, meaning that other than copying or comparing arrays, you must select an individual element out of the array before using it in most expressions.

1. Wildcard index type

Example:

int array_name [*];

Associative arrays that specify a wildcard index type have the following properties:

- The array can be indexed by any integral data type. Since the indices can be of different sizes, the same numerical value can have multiple representations, each of a different size. SystemVerilog resolves this ambiguity by detecting the number of leading zeros and computing a unique length and representation for every value.

- Non-integral index types are illegal and result in a type check error.

- A 4-state Index containing X or Z is invalid.

- Indices are unsigned.

- Indexing expressions are self-determined; signed indices are not sign extended.

- A string literal index is auto-cast to a bit-vector of equivalent size.

- The ordering is numerical (smallest to largest).

2. String index

Example:

int array_name [ string ];

Associative arrays that specify a string index have the following properties:

- Indices can be strings or string literals of any length. Other types are illegal and shall result in a type check error.

- An empty string “” index is valid.

- The ordering is lexicographical (lesser to greater).

3. Class index

Example:

int array_name [ some_Class ];

Associative arrays that specify a class index have the following properties:

- Indices can be objects of that particular type or derived from that type. Any other type is illegal and shall result in a type check error.

- A null index is valid.

- The ordering is deterministic but arbitrary.

4. Integer (or int) index

Example:

int array_name [ integer ];

Associative arrays that specify an integer index have the following properties:

- Indices can be any integral expression.

- Indices are signed.

- A 4-state index containing X or Z is invalid.

- Indices smaller than integer are sign extended to 32 bits. — Indices larger than integer are truncated to 32 bits.

- The ordering is signed numerical.

5. Signed packed array

Example:

typedef bit signed [4:1] Nibble;

int array_name [ Nibble ];

Associative arrays that specify a signed packed array index have the following properties:

- Indices can be any integral expression.

- Indices are signed.

- Indices smaller than the size of the index type are sign extended.

- Indices larger than the size of the index type are truncated to the size of the index type.

- The ordering is signed numerical.

6. Unsigned packed array or packed struct

Example:

typedef bit [4:1] Nibble;

int array_name [ Nibble ];

Associative arrays that specify an unsigned packed array index have the following properties:

- Indices can be any integral expression.

- Indices are unsigned.

- A 4-state Index containing X or Z is invalid.

- Indices smaller than the size of the index type are zero filled.

- Indices larger than the size of the index type are truncated to the size of the index type.

- The ordering is numerical.

If an invalid index (i.e., 4-state expression has X’s) is used during a read operation or an attempt is made to read a non-existent entry then a warning is issued and the default initial value for the array type is returned, as shown in the table below:

Table 2-7

If an invalid index is used during a write operation, the write is ignored and a warning is issued.

7. Other user defined types

Example:

typedef struct {real R; int I[*];} Unpkt;

int array_name [ Unpkt ];

In general, associative arrays that specify an index of any type have the following properties:

- Declared Indices must have the equality operator defined for its type to be legal. This includes all of the dynamically sized types as legal Index types

- An Index expression that is or contains X or Z in any of its elements is invalid.

- An Index expression that is or contains an empty value or null for any of it elements does not make the Index invalid.

- If the relational operator is defined for the Index type, the ordering is as defined in the preceding sections. If not, the relative ordering of any two entries in such an associative array can vary, even between succes- sive runs of the same tool. However, the relative ordering must remain the same within the same simulation run while no Indices have been added or deleted.



Comments:

Leave a comment: