System Verilog: Dynamic Arrays

Dynamic array is one of the aggregate data types in system verilog. It is an unpacked array whose size can be set or changed at run time.

In verilog, dimension of the array can be set during declaration and it cannot be changed during run time. But in most of our verification scenarios, array dimensions need to be changed during runtime like packet length or size of the read or write data variations in runtime etc. In these scenarios, the only option if using verilog is to declare an array with maximum possible size.

These limitations has been overcome in system verilog by the introduction of dynamic array. Dynamic arrays support all variable data types as element types,including arrays.

Dynamic array initialization and resizing
The constructor new[] is used to initialize dynamic arrays. Other built-in method for dynamic array operations are size() and delete().The size() method returns the size of the array and delete() clears all the elements yielding an empty array. The default size of an uninitialized dynamic array is 0.

The size argument need not match the size of the initialization array. When the initialization size is greater, it is truncated to match the size argument; If it is smaller, the initialized array is appended with the default values to attain the specified size. Resizing can be done by preserving the previous values. The following code shows setting and resetting size of dynamic array, resize by preserving previous values, and delete method usage.

Dynamic array assignments
If the target of the assignment is a queue or dynamic array, resizing will be done to have the same number of elements as the source expression. But if a dynamic array is assigned to a fixed-size array, the size of the source will not be determined until run time. Run-time error will occur, if the size is different from fixed-size array.

Arrays as arguments to subroutines
If dynamic array is used as formal argument, it can accept dynamic array, queue, or fixed-size array of a compatible type as actual argument.

In the below example, formal argument of mytask is a dynamic array of int,

But if fixed array is used as formal and dynamic array is used as actual argument, then dynamic array should be of compatible type and has to be initialized to the same size as fixed array used as formal argument of subroutine.

Here, formal argument of mytask is a fixed size array of 10 int elements.

3 Comments

  1. Pingback: System Verilog: Associative Arrays | VLSI Pro

Leave a Reply

Your email address will not be published. Required fields are marked *