SystemVerilog Associative Arrays: Basics and Beyond
Associative arrays are an integral part of SystemVerilog and are widely used in various applications. In this blog post, we will dive deep into associative arrays, learn their basics, and explore some practical examples to fully understand how they work.
Introduction to Associative Arrays
What are Associative Arrays?
Associative arrays are a collection of elements indexed by a unique key, which could be a string, integer, or any other data type. These arrays do not have any fixed size and can grow dynamically as needed.
Comparison between Dynamic Arrays and Associative Arrays
Dynamic arrays are indexed using consecutive integers, while associative arrays use unique keys as indices. Dynamic arrays have a size limit and need to be resized, whereas associative arrays can grow as required.
Declaration of Associative Arrays
Associative arrays can be declared using the [*]
or [type]
syntax. Here's an example:
real myArray1[*]; // Associative array of real data type (unspecified index)
logic [7:0] myArray2[string]; // Associative array of 8-bit logic, indexed by string
event myArray3[myClass]; // Associative array of event indexed by class myClass
Accessing Invalid Indices and Allocating Associative Array Elements
Handling Invalid Indices
If you try to access an invalid index in an associative array, SystemVerilog does not throw an error but returns a default value for the indexed data type. For example, an invalid index for an associative array of integers will return 0.
Allocating Elements in Associative Arrays
Adding elements to an associative array is simple. Just assign a value to an index:
myArray["apple"] = 5; // Adding an element to the associative array
Exploring Built-in Associative Array Methods
Num() and size()
These methods return the number of elements in an associative array:
int count = myArray.size();
Delete()
This method removes an element from the associative array:
myArray.delete("apple"); // Deleting an element from the associative array
Exists()
This method checks if an element exists in the associative array:
if (myArray.exists("apple")) begin
// do something
end
First(), Last(), Next(), and Prev()
These methods allow you to traverse the associative array. Here's an example:
string key;
int value;
for (key = myArray.first(); key != ""; key = myArray.next(key)) begin
value = myArray[key];
// do something with the key-value pair
end
Associative Array Assignments, Arguments, and Literals
Assigning Associative Arrays
To copy an associative array to another, simply assign it:
int myNewArray[string];
myNewArray = myArray; // Copying associative arrays
Passing Associative Arrays as Arguments
To pass associative arrays as arguments to tasks or functions, use the input
and output
keywords:
function int SumElements(input int arrayArg[string]);
int sum = 0;
string key;
for (key = arrayArg.first(); key != ""; key = arrayArg.next(key)) begin
sum += arrayArg[key];
end
return sum;
endfunction
task main;
int total = SumElements(myArray);
endtask
Associative Array Literals
You can declare and initialize an associative array using the {}
syntax:
int myLiteralArray[string] = '{"apple": 5, "banana": 3};
Practical Examples and Common Use Cases
Example: Simple Gradebook System
In this example, we will create a simple gradebook system using associative arrays to store student grades.
module Gradebook;
string students[] = {"Alice", "Bob", "Charlie", "Diana"};
int grades[string];
initial begin
initializeGrades(students, grades);
printGrades(grades);
end
function void initializeGrades(input string studentList[], output int grades[string]);
foreach (studentList[i]) begin
// Assign random grades between 60 and 100 to each student
grades[studentList[i]] = $urandom_range(60, 100);
end
endfunction
function void printGrades(input int grades[string]);
string key;
for (key = grades.first(); key != ""; key = grades.next(key)) begin
$display("%s: %d", key, grades[key]);
end
endfunction
endmodule
In this example, we use an associative array grades
to store the grade of each student. The initializeGrades
function assigns random grades to each student in the associative array. The printGrades
function displays the grade of each student.
In conclusion, associative arrays in SystemVerilog offer a powerful and versatile data structure for managing and organizing data in various applications. Through the use of practical examples, we have demonstrated the ease and effectiveness of associative arrays in real-life scenarios. By mastering the usage of associative arrays, you can greatly improve the efficiency, readability, and performance of your SystemVerilog code, enabling you to tackle more complex problems with ease.