SystemVerilog enhances array
declarations in several ways. SystemVerilog supports fixed-size arrays, dynamic
arrays, and associative arrays. Fixed-size arrays can be multi-dimensional and
have fixed storage allocated for all the elements of the array. Dynamic arrays
also allocate storage for all the elements of the array, but the array size can
be changed dynamically. Dynamic and associative arrays are one-dimensional. Fixedsize and dynamic arrays are indexed using integer
expressions, while associative arrays can be indexed using arbitrary data
types. Associative arrays do not have any storage allocated until it is needed,
which makes them ideal for dealing with sparse data.
SystemVerilog uses the term “packed array”
to refer to the dimensions declared before the object name (what Verilog-2001
refers to as the vector width). The term “unpacked array” is used
to refer to the dimensions declared after the object name.
bit [7:0] c1;
// packed array
real u [7:0];
// unpacked array
SystemVerilog
enhances packed arrays by allowing multiple dimensions. SystemVerilog adds the
ability to procedurally change the size of one of the dimensions of an unpacked
array. Fixed-size unpacked arrays can be multi-dimensional and have fixed
storage allocated for all the elements of the array. Each dimension of an
unpacked array can be declared as having a fixed or un-fixed size. A dynamic
array allocates storage for elements at runtime along with option of changing
the size of one of its dimensions. An associative array allocates storage for
elements individually as they are written. Associative arrays can be indexed
using arbitrary data types. A queue type of array grows or shrinks to
accommodate the number elements written to the array at runtime.
SystemVerilog uses the term “packed array”
to refer to the dimensions declared before the object name (what Verilog-2001
refers to as the vector width). The term “unpacked array” is used
to refer to the dimensions declared after the object name.
bit [7:0] c1; // packed array
real u [7:0]; // unpacked array
If a packed array is
declared as signed, then the array viewed as a single vector shall be signed. The individual elements of the array are unsigned unless
they are of a named type declared as signed. A part-select of a packed
array shall be unsigned.
— Treatment as an integer in an expression, e.g., (A +
3)
If an unpacked array is declared as signed, then this applies to
the individual elements of the array, since the whole array cannot be viewed as
a single vector.
Dynamic arrays are
one-dimensional unpacked arrays A dynamic array is one-dimension
of an unpacked array whose size can be set or changed at runtime. The
space for a dynamic array doesn’t exist until the array is explicitly created
at runtime.
The prototype
of the new function is:
array_identifier = new[size] [(src_array)];
blocking_assignment ::= //
from Annex A.6.2
…
| hierarchical_dynamic_array_variable_identifier [ ] = dynamic_array_new
…
dynamic_array_new ::= //
from Annex A.2.4
new [ expression ] [ ( dynamic_array_variable_identifier expression ) ]
Syntax 4-1—Declaration of dynamic array new (excerpt from Annex A)
size [ expression ]:
The number of elements in the array. Must be a non-negative
integral expression.
src_array ( expression ):
Optional.
The name of an An array with which to initialize the new
array. If src_array it is not
specified, the elements of array_name the newly
allocated array are
initialized to their default value. src_array This
array identifier must be a
dynamic array of the same data type as array_name the array on the
left-hand side, but it need
not have the same size. If the size of src_array this array is less than size the size of the new array, the extra elements of array_name shall be initialized
to their default value. If the size of src_array this array is greater than size the size of the new array, the additional elements of src_array shall be ignored.
This
argument is useful when growing or shrinking an existing array. In this situation,
src_array the value of ( expression ) is array_name the same as the left-hand side, so the previous values of the array elements are preserved. For
example:
integer addr[]; //
Declare the dynamic array.
addr = new[100]; // Create a 100-element array.
...
// Double the
array size, preserving previous values.
addr = new[200](addr);
The new operator follows
the SystemVerilog precedence rules. Since both the square brackets [] and the parenthesis () have the same precedence, the arguments to
this operator are evaluated left to right: size [ expression ]first, and src_array ( expression ) second.
Note that unsized dimensions can occur in dynamic arrays and in formal
arguments of import DPI functions. If one of
dimensions of a formal is unsized,
then any size of the corresponding dimension of an actual is accepted.
Array elements in
associative arrays are allocated dynamically; an entry is created the first
time it is written. The associative array maintains the entries that have been
assigned values and their relative order according to the index data type.
Associative arrays elements are unpacked,
meaning that other than copying or comparing arrays, you must select an
individual element out of the array before using it in most expressions.
constant_primary ::= //
from Annex A.8.1
concatenation
::= // from Annex A.8.1
Syntax
4-72—Associative
array literal syntax (excerpt from Annex A)
array_method_call ::= // from Annex A.8.2
not in Annex A
...
| expression . array_method_name { attribute_instance } [ ( list_of_arguments ) ]
[ with ( expression ) ]21
array_method_name ::=
method_identifier | unique | and | or | xor
Syntax
4-9—array method call syntax (excerpt from
not in Annex A)
The optional with
clause accepts an expression enclosed
in parenthesis. In contrast, the with
clause used by the randomize method
(see Section 12.6) accepts a set of constraints
enclosed in braces.
The array dimensions are
numbered as defined in Section 23.4 23.7: The slowest varying is dimension 1.
Successively faster varying dimensions have sequentially higher dimension
numbers. If the dimension is not specified, the first dimension is used by
default