Understanding SystemVerilog Dynamic Arrays

In this blog post, we will discuss dynamic arrays in SystemVerilog. Dynamic arrays are a powerful feature in SystemVerilog, allowing you to create and resize arrays at runtime. We will go through the basics of creating and manipulating dynamic arrays and show you some examples to help you get started.

What is a Dynamic Array?

A dynamic array is an unpacked array whose size can be set or changed at runtime. Dynamic arrays support all variable data types as element types, including arrays. Dynamic array dimensions are denoted in the array declaration by [].

Here's a basic example of a dynamic array declaration:

bit [7:0] byteArray[]; // Dynamic array of 8-bit vectors
real realMatrix[3][];  // Fixed-size unpacked array composed of 3 dynamic subarrays of real numbers

To set or change the size of a dynamic array and initialize its elements, use the new[] constructor. The size of the array can be determined using the size() built-in method, and you can clear all the elements using the delete() built-in method.

Creating and Initializing Dynamic Arrays

Using the new[] Constructor

The new[] constructor is used to set the size of a dynamic array and initialize its elements. It can be used in variable declaration assignments and blocking procedural assignments.

int matrix[][4][5] = new [6]; // matrix sized to length 6
int vector[][] = new [8];     // vector sized to length 8; dynamic subarrays remain unsized and uninitialized

In procedural contexts, you can use the new[] constructor in blocking assignments:

int myArray[3][][];
myArray[1]    = new [5]; // dynamic subarray myArray[1] sized to length 5
myArray[1][2] = new [3]; // dynamic subarray myArray[1][2] sized to length 3

When the new[] constructor does not specify an initialization expression, the elements are initialized to the default value for their type.

Initializing with an Array

You can use an optional initialization expression to initialize the dynamic array. The initialization array should be assignment compatible with the left-hand-side dynamic array.

int dest[], src[4] = '{1, 3, 5, 7};
dest = new [4](src); // set size and array element data values (1, 3, 5, 7)

The size argument does not need to match the size of the initialization array. When the initialization array's size is greater, it is truncated to match the size argument; when it is smaller, the initialized array is padded with default values to attain the specified size.

int source[4], result1[], result2[];
source = '{10, 20, 30, 40};
result1 = new[3](source); // result1's elements are {10, 20, 30}.
result2 = new[5](source); // result2's elements are {10, 20, 30, 40, 0}.

Manipulating Dynamic Arrays

Resizing a Dynamic Array

You can resize an existing dynamic array by using the new[] constructor with the array itself as the initialization expression.

integer values[];
values = new[50]; // Create a 50-element array
values = new[100](values); // Double the array size, preserving previous values

Using size() and delete() Methods

The size() method returns the current size of a dynamic array, or zero if the array has not been created. This is useful when you want to know the current size of the array for any processing or calculation purposes.

int currentSize = values.size;
values = new[values.size() * 2](values); // double values array size

The delete() method empties the array, resulting in a zero-sized array. This is helpful when you want to free up memory by deleting the contents of the array.

int tempArray[] = new[10];      // create a temporary array of size 10
// use tempArray
tempArray.delete;               // delete the array contents
$display("%d", tempArray.size); // prints 0

Conclusion

Dynamic arrays in SystemVerilog are a powerful feature that allows you to create and resize arrays at runtime. In this blog post, we have covered the basics of dynamic arrays, including creating and initializing them, resizing, and using the size() and delete() methods. With this knowledge, you should be able to start using dynamic arrays in your SystemVerilog designs effectively. Remember to keep practicing and experimenting with various use cases to get a better understanding of dynamic arrays in SystemVerilog.