ch2-Associative Arrays

2019-10-24

SystemVerilog Associative Arrays

​ When size of a 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 look-up 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.

Syntax

1
2
3
// Value     Array_Name          [ key ];
data_type array_identifier [ index_type ];

Initialization Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module tb;

int array1 [int]; // An integer array with integer index
int array2 [string]; // An integer array with string index
string array3 [string]; // A string array with string index

initial begin
// Initialize each dynamic array with some values
array1 = '{ 1 : 22,
6 : 34 };

array2 = '{ "Ross" : 100,
"Joey" : 60 };

array3 = '{ "Apples" : "Oranges",
"Pears" : "44" };

// Print each array
$display ("array1 = %p", array1);
$display ("array2 = %p", array2);
$display ("array3 = %p", array3);
end
endmodule

Associative Array Methods

Function Description
function int num (); Returns the number of entries in the associative array
function int size (); Also returns the number of entries, if empty 0 is returned
function void delete ( [input index] ); index when specified deletes the entry at that index, else the whole array is deleted
function int exists (input index); Checks whether an element exists at specified index; returns 1 if it does, else 0
function int first (ref index); Assigns to the given index variable the value of the first index; returns 0 for empty array
function int last (ref index); Assigns to given index variable the value of the last index; returns 0 for empty array
function int next (ref index); Finds the smallest index whose value is greater than the given index
function int prev (ref index); Finds the largest index whose value is smaller than the given index

Creating a dynamic array of associative arrays

dynamic array的每一个元素都是associative array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module tb;
// Create an associative array with key of type string and value of type int
// for each index in a dynamic array
int fruits [] [string];

initial begin
// Create a dynamic array with size 2
fruits = new [2];

// Initialize the associative array inside each dynamic array index
fruits [0] = '{ "apple" : 1, "grape" : 2 };
fruits [1] = '{ "melon" : 3, "cherry" : 4 };

// Iterate through each index of dynamic array
foreach (fruits[i])
// Iterate through each key of the current index in dynamic array
foreach (fruits[i][fruit])
$display ("fruits[%0d][%s] = %0d", i, fruit, fruits[i][fruit]);

end
endmodule

Embedding a dynamic array within each index of an associative array

每个associative array的元素都是一个dynamic array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Create a new typedef that represents a dynamic array
typedef int int_da [];

module tb;
// Create an associative array where key is a string
// and value is a dynamic array
int_da fruits [string];

initial begin
// For key "apple", create a dynamic array that can hold 2 items
fruits ["apple"] = new [2];

// Initialize the dynamic array with some values
fruits ["apple"] = '{ 4, 5};

// Iterate through each key, where key represented by str1
foreach (fruits[str1])
// Iterate through each item inside the current dynamic array ie.fruits[str1]
foreach (fruits[str1][i])
$display ("fruits[%s][%0d] = %0d", str1, i, fruits[str1][i]);

end
endmodule

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!