Dynamic Array

Next: Dynamic Array Methods

Why Dynamic Arrays

Verilog allows one-dimensional arrays of variables all along and Verilog-2001 allows multi-dimensional ones too. SystemVerilog classifies an array as 'packed' or 'unpacked' depending on how it is declared. If the array upper and lower bounds are declared between the variable type and the variable name, such as

reg [7:0] a_reg_array;
the array is called packed. If the array bounds are declared after the variable name, it is called an unpacked array, such as,
int int_array [7:0];
Of course, an array may have both packed and unpacked parts.
reg [7:0] reg_array [3:0][7:0];

Whether you are declaring packed or unpacked arrays of whatever dimensions, one thing that remains common is they are all static declarations. Once an array is declared this way, the tool that parses the declaration will statically allocate memories for the array and there is noway that you can alter that afterwards. As a result, the size of an array, for example, can not be changed once it is declared.

There are occasions, however, when you would want to declare an array whose size can not be pre-determined. A temporary buffer for variable rate incoming data stream, a list that has variable number of elements are few examples of problems that need array size that needs to be changed dynamically. Using a very large array with the assumption that it can hold the largest data (yet whose size is unknown) is neither safe nor efficient. So, the question is how can we declare an array whose size is dynamically alterable?

SystemVerilog dynamic array type addresses this need. In a sense, dynamic arrays are equivalent of malloc library function in C that allows one to dynamically alter the size of an array (or pointer).

Declaring a Dynamic Array

A dynamic array lets you keep the number of elements in the array unspecified at the declaration time. You can define the number of elements it holds during run time. Moreover, once declared, the number of elements can be altered at a later point of time too.

However, these benefits come at a price. There are some limitations on dynamic arrays. These limitations are:

  1. The dynamic part of the array must be of unpacked nature. A packed array can not be dynamic (can you guess why?)
  2. The unpacked (and dynamic) part of the array must be one dimensional. More than one dimension is not allowed for dynamic arrays.

With this rules and regulations, here is the syntax for declaration for dynamic array:

data_type array_name [];
Note the empty square brackets ([]) that indicate that the array is dynamic. Here are some examples of dynamic array declaration (similar to static arrays declared above):
reg [7:0]      a_reg_array []; // dynamic array of 8 bit reg
reg [7:0][3:0] b_reg_array []; // dynamic array of two packed dimension
int            int_array   []; // dynamic array of integers
Several things are noteworthy above.
  1. The variable a_reg_array is dynamic only in its unpacked part. The packed part is still static (and 8 bit wide) showing mixing static packed and dynamic unpacked parts is okay.
  2. The variable b_reg_array even has two dimensions in its packed part. It is only the unpacked part that is one-dimensional.
  3. The variable int_array shows a case whose packed part is not an array.

Working with Dynamic Arrays

Once you have declared a dynamic array, there are few built-in operator and methods that you can use that help you to use the array effectively. These are

  • The new[] operator.
  • The size() built-in method.
  • The delete() built-in method.
We discuss them on the next page.

Next: Dynamic Array Methods


Verification Management
Join Verification Management Group

Shop Amazon - Contract Cell Phones & Service Plans

Book of the Month