SystemVerilog -- 2.9 Data Types ~ SystemVerilog Associative Array
SystemVerilog Associative Array
当集合的大小未知或数据空间稀疏时,关联数组是更好的选择。关联数组在使用之前不会分配任何存储,并且索引表达式不限于整数表达式,而是可以是任何类型。
关联数组实现其声明类型的元素的查找表。要用作索引的数据类型用作查找键并强制排序。
Syntax
// value Array_Name [key];
data_type array_identifier [index_type];
Initialization Example
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 atring 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
模拟日志
ncsim> run
array1 = '{1:22, 6:34}
array2 = '{"Joey":60, "Ross":100}
array3 = '{"Apples":"Orange", "Pears":"44"}
ncsim: *W,RNQUIE: Simulation is complete.
Associative Array Methods
Function | Decription |
---|---|
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 dose, 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 the given index variable the value of the last index; |
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 |
Associative Array Methods Example
module tb;
int fruits_10 [string];
initial begin
fruits_10 = '{"apple" : 4,
"orange" : 10,
"plum" : 9,
"guava" : 1 };
// size() : Print the number of items in the given dynamic array
$display ("fruits_10.size() = %0d", fruits_10.size());
// num() : Another function to print number of items in given array
$display ("fruits_10.num() = %0d", fruits_10.num());
// exists() : Check if a particular key exists in this dynamic array
if (fruits_10.exists ("orange"))
$display ("Found %0d orange !", fruits_10["orange"]);
if (fruits_10.exists ("apricots"))
$display ("Sorry, season for apricots is over ...");
// Note : String indices are taken in alphabetical order
// first() : Get the first element in the array
begin
string f;
// This function returns true if it succeeded and first key is stored in the provided string "f"
if (fruits_10.first(f))
$display ("fruits_10.first[%s] = %0d", f, fruits_10[f]);
end
// last() : Get the last element in the array
begin
string f;
if (fruits_10.last(f))
$display ("fruits_10.last[%s] = %0d", f, fruits_10[f]);
end
// prev() : Get the previous element in the array
begin
string f = "orange";
if (fruits_10.prev(f))
$display ("fruits_10.last[%s] = %0d", f, fruits_10[f]);
end
// next() : Get the next item in the array
begin
string f = "orange";
if (fruits_10.next(f))
$display ("fruits_10.last[%s] = %0d", f, fruits_10[f]);
end
end
endmodule
模拟日志
ncsim> run
fruits_10.size() = 4
fruits_10.num() = 4
Found 10 orange !
Sorry, season for apricots is over ...
fruits_10.frist [apple] = 4
fruits_10.last [plum] = 9
fruits_10.prev [guava] = 1
fruits_10.next [plum] = 9
ncsim: *W,RNQUIE: Simulation is complete.
Dynamic array of Associative arrays
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 indynamic array
foreach (fruits[i][fruit])
$display ("fruits[%0d][%s] = %0d", i, fruit, fruits[i][fruit]);
end
endmodule
模拟日志
ncsim> run
fruits[0][apple] = 1
fruits[0][grape] = 2
fruits[1][cherry] = 4
fruits[1][melon] = 3
ncsim: *W,RNQUIE: Simulation is complete.
Dynamic array within each index of an Associative array
// 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
模拟日志
ncsim> run
fruits[apple][0] = 4
fruits[apple][1] = 5
ncsim: *W,RNQUIE: Simulation is complete.