|
|
|
|
|
|
|
|
|
|
|
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. |
|
|
|
|
|
Lets take a example of memory controller verification, where the testcases normally does random write/read, and to have a fixed size array would cost system resource (say you want 512MB memory model, then it would take 512MB if you use verilog 2D array), so instead of regural 2D array, use a associative array, which stored the data for only address which has been written. |
|
|
|
|
|
Another application of a sparce array is for building out of order checkers, where each transcation contains one unique tag and transcation generated and served are out of order. |
|
|
|
|
|
Syntax |
|
|
|
|
|
data_type array_id [ index_type ];
|
|
|
|
|
|
Here |
|
|
|
|
|
- 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.
|
|
|
|
|
|
Associative arrays index can be integer, string, class, and *. To make working with associatuve arrays, there are number of methods. |
|
|
|
|
|
Associative arrays methods
|
|
|
To work with associative arrays, SystemVerilog provides following methods |
|
|
|
|
|
- exists() : The exists() function checks if an element exists at the specified index within the given array. It returns 1 if the element exists, otherwise it returns 0.
- first() : The first() method assigns to the given index variable the value of the first (smallest) index in the associative array. It returns 0 if the array is empty, and 1 otherwise.
- last() : The last() method assigns to the given index variable the value of the last (largest) index in the associative array. It returns 0 if the array is empty, and 1 otherwise.
- next() : The next() method finds the entry whose index is greater than the given index. If there is a next entry, the index variable is assigned the index of the next entry, and the function returns 1. Otherwise, index is unchanged, and the function returns 0.
- prev() : The prev() function finds the entry whose index is smaller than the given index. If there is a previous entry, the index variable is assigned the index of the previous entry, and the function returns 1. Otherwise, the index is unchanged, and the function returns 0.
- num() : The num() method returns the number of entries in the associative array. If the array is empty, it returns 0.
- delete() : If the index is specified, then the delete() method removes the entry at the specified index. If the entry to be deleted does not exist, the method issues no warning.
|
|
|
|
|
|
Integer or int index
|
|
|
While using integer in associatuve arrays, following rules need to be kept in mind. |
|
|
|
|
|
- A 4-state index containing X or Z is invalid.
- Indices smaller than integer are sign extended to 32 bits.
- The ordering is signed numerical.
- Indices can be any integral expression.
- Indices are signed.
- Example: int array_name [ integer ];
|
|
|
|
|
|
|
|
|
|
|
|
String index
|
|
|
While using string in associative arrays, following rules need to be kept in mind. |
|
|
|
|
|
- An empty string "" index is valid.
- The ordering is lexicographical (lesser to greater).
- Indices can be strings or string literals of any length.
- Example: int array_name [ string ];
|
|
|
|
|
|
Class index
|
|
|
While using class in associative arrays, following rules need to be kept in mind. |
|
|
|
|
|
- A null index is valid.
- The ordering is deterministic but arbitrary.
- Indices can be objects of that particular type or derived from that type.
- Example: int array_name [ some_Class ];
|
|
|
|
|
|
Wild Character index
|
|
|
While using wild characters in associative arrays, following rules need to be kept in mind. |
|
|
|
|
|
- A 4-state Index containing X or Z is invalid.
- Indices are unsigned.
- Indexing expressions are self-determined; signed indices are not sign extended.
- The ordering is numerical (smallest to largest).
- A string literal index is auto-cast to a bit-vector of equivalent size.
- The array can be indexed by any integral data type.
- Example: int array_name [*];
|
|
|
|
|
|
Example - Associative Arrays
|
|
|
|
|
|
1 module integer_associative_array ();
2
3 integer as_mem [integer];
4
5 integer i;
6
7 initial begin
8 // Add element array
9 as_mem[100] = 101;
10 $display ("value stored in 100 is %d", 101);
11 as_mem[1] = 100;
12 $display ("value stored in 1 is %d", 100);
13 as_mem[50] = 99;
14 $display ("value stored in 50 is %d", 99);
15 as_mem[256] = 77;
16 $display ("value stored in 256 is %d", 77);
17 // Print the size of array
18 $display ("size of array is %d", as_mem.num());
19 // Check if index 2 exists
20 $display ("index 2 exists %d", as_mem.exists(2));
21 // Check if index 100 exists
22 $display ("index 100 exists %d", as_mem.exists(100));
23 // Value stored in first index
24 if (as_mem.first(i)) begin
25 $display ("value at first index %d value %d", i, as_mem[i]);
26 end
27 // Value stored in last index
28 if (as_mem.last(i)) begin
29 $display ("value at last index %d value %d", i, as_mem[i]);
30 end
31 // Delete the first index
32 as_mem.delete(100);
33 $display ("Deleted index 100");
34 // Value stored in first index
35 if (as_mem.first(i)) begin
36 $display ("value at first index %d value %d", i, as_mem[i]);
37 end
38 #1 $finish;
39 end
40
41 endmodule
You could download file integer_associative_array.sv here
|
|
|
|
|
|
Simulation Output - Associative Arrays
|
|
|
|
|
|
value stored in 100 is 101
value stored in 1 is 100
value stored in 50 is 99
value stored in 256 is 77
size of array is 4
index 2 exists 0
index 100 exists 1
value at first index 1 value 100
value at last index 256 value 77
Deleted index 100
value at first index 1 value 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|