Arrays in system verilog :
An array is a collection of variables, all of the same type, and accessed using the same name plus one or more indices.
reg [7:0] r1 [1:256]; // [7:0] is the vector width, [1:256] is the array size
SystemVerilog uses the term packed array to refer to the dimensions declared before the object name (what Verilog 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
* System verilog enhances the arrays by allowing multiple dimentions.
System verilog have following type arrays
- Fixed Size Arrays
- Dynamic Arrays
- Queues
- Associative arrays
1. Fixed Size Arrays : (data_type name [constant];)
Fixed size arrays are fast and static.In fixed size arrays
- Multiple dimentions supported
- Out of bound writes are ignored. Out of bound returns x even for 2 state data types
- Arrays data are stored in 32 bit words
Examples :
int twoD1[0:7][0:23]; // 2D array
int twoD2[8][24]; // same as above
twoD1 = twoD2; // Array copy
if (twoD1==twoD2)... // Array compare
bit [3:0][7:0] bytes [0:2]; // 3 entries of packed 4 bytes
2. Dynamic Arrays (data_type name [ ]) :
Dynamic arrays are fast and variable size is possible with a call to new () function. In dynamic size array :
- Similar to fixed size arrays but size can be given in the run time
- Dynamic arrays can have only single dimention and it can not be packed
- Out of bound error in dynamic arrays causes run time error.
Examples :
int d[], b[]; // Two dynamic arrays
d = new[5]; // Make array with 5 elements
foreach (d[j]) // Initialize
d[j] = j;
b = d; // Copy a dynamic array
b[0] = 5;
$display(d[0],b[0]); // See both values (0 & 5)
d = new[20](d); // Expand and copy
d = new[100]; // Allocate 100 new integers
// Old values are lost
d.delete(); // Delete all elements
3. Queues ( data_type name [$]; ) :
In queues size is flexible. It can change easily
- Variable size array with automatic sizing, single dimension
- Many searching, sorting, and insertion methods
- Constant time to read, write, and insert at front & back
- Out of bounds access causes run-time error
Examples :
int q[$] = {0,1,3,6};
int j = 2, b[$] = {4,5};
q.insert(2, j); // {0,1,2,3,6} Insert before s[2]
q.insert(4, b); // {0,1,2,3,4,5,6} Insert whole queue
q.delete(1); // {0,2,3,4,5,6} // {0,2,3,4,5,6} Delete element #1
q.push_front(7); // {7,0,2,3,4,5,6} Insert at front
j = q.pop_back(); // {7,0,2,3,4,5} j = 6
q.push_back(8); // {7,0,2,3,4,5,8} Insert at back
j = q.pop_front(); // {0,2,3,4,5,8} j = 7
$display(q.size); // “6”
Traversing :
foreach (q[i]) $display(q[i]);
4. Associative Arrays ( DataType name [*]; ) :
Associative arrays are generally used for sparse memories. Associative arrays are :
- Dynamically allocated, non-contiguous elements
- Accessed with integer, or string index, single dimension
- Great for sparse arrays with wide ranging index
- Array functions: exists, first, last, next, prev
int aa[*], i;
// Printing Full Array
foreach (aa[i])
$display (i, aa[i]);
Go to next section for more details..
|