|
|
|
|
|
|
|
|
|
|
|
Arrays
|
|
|
Vera supports one-dimensional and multi-dimensional arrays, which are lists of variables that are all of the same type and called with the same name. Arrays in Vera can be static or dynamic. There are four different types of arrays. |
|
|
|
|
|
- Fixed-size Arrays
- Dynamic Arrays
- Multi-dimensional Arrays
- Associative Arrays
|
|
|
|
|
|
We will be looking at each of this in detail below. |
|
|
|
|
|
Fixed Size Arrays
|
|
|
Fixed-size arrays are memory efficient and offer fast data access. The access time is constant regardless of the number of elements in an array. The size of fixed-size arrays is set at compile time. Accessing an array with an unknown bit (`x') in the index causes a simulation error. Also, writing to an array with an unknown in the index is ignored, and reading with an unknown in the index returns `X's. Concatenation is not supported in array initialization. An attempt to concatenate will result in a compilation error. |
|
|
|
|
|
Note: A bit field of an array element cannot be referenced directly. To reference a bit field of an array element, use a temporary variable. |
|
|
|
|
|
Example : Fixed Arrays
|
|
|
|
|
|
1 program fixed_size_array {
2 // Normal declaration
3 integer mem_int[16];
4 // Init the array with values
5 bit [7:0] mem_byte [8] = {0,1,2,3,4,8'bx,6,7};
6 // Below code will result in compile error
7 // Concatenation is not supported inside a raary init
8 //bit [7:0] mem_error [8] = {0,1,2,3,4,{1'bx,7'b0},6,7};
9 // Assign value
10 mem_int[1] = random();
11 // Print data
12 printf("value of mem_int at index 1 ->%x\n",mem_int[1]);
13 printf("value of mem_int at index 2 ->%x\n",mem_int[2]);
14 // Print a data in mem_byte
15 printf("value of mem_byte at index 8 ->%x\n",mem_byte[7]);
16 printf("value of mem_byte at index 6 ->%x\n",mem_byte[6]);
17 // Print the value with x in index
18 printf("value of mem_byte at index 4'b001x ->%x\n", mem_byte[4'b001x]);
19 }
You could download file fixed_size_array.vr here
|
|
|
|
|
|
Simulation : Fixed Arrays
|
|
|
|
|
|
value of mem_int at index 1 ->2fd9a2ac
value of mem_int at index 2 ->xxxxxxxx
value of mem_byte at index 8 ->07
value of mem_byte at index 6 ->06
value of mem_byte at index 4'b001x ->xx
|
|
|
|
|
|
Dynamic Arrays
|
|
|
Dynamic arrays are memory efficient and offer fast data access. The access time is constant regardless of the number of elements. The size of dynamic arrays can be allocated and resized at runtime. Dynamic arrays do not support multiple dimensions. |
|
|
|
|
|
Example : Dynamic Arrays
|
|
|
|
|
|
1 program dynamic_size_array {
2 // Dynamic size array
3 bit [7:0] mem_byte [*];
4 bit [7:0] mem_dyn [*];
5 // Init the first array
6 mem_byte = new[4];
7 mem_byte[0] = 0;
8 mem_byte[1] = 1;
9 mem_byte[2] = 2;
10 mem_byte[3] = 3;
11 // Set the size during run time
12 mem_dyn = new [100];
13 mem_dyn[50] = random();
14 printf("value of mem_dyn at index 50 ->%x\n",mem_dyn[50]);
15 // Resize the array and copy the values from another array
16 mem_dyn = new[4] (mem_byte);
17 // This will give error
18 printf("value of mem_dyn at index 50 ->%x\n",mem_dyn[50]);
19 }
You could download file dynamic_size_array.vr here
|
|
|
|
|
|
|
|
|
|
|
|
Simulation : Dynamic Arrays
|
|
|
|
|
|
value of mem_dyn at index 50 ->ac
Error: Out of bound dynamic array access (array size: 4, index value: 50)
at time 0 in file dynamic_size_array.vr line 18
|
|
|
|
|
|
Multi Dimension Arrays
|
|
|
Multi-dimensional Arrays Multi-dimensional arrays are fixed-size arrays with multiple dimensions. Multi-dimensional arrays can be passed as arguments to a task or function call, and must be of the same data type and size as the arguments declared in the task or function. Bit slicing of multi-dimensional arrays is not allowed. A compilation error is issued for such attempts. The maximum number of dimensions allowed is 16. |
|
|
|
|
|
|
|
|
Example : Multi Dimension Arrays
|
|
|
|
|
|
1 program multi_dimension_array {
2 integer matrix [2][5];
3 // Assign values
4 matrix[0][0] = 0;
5 matrix[0][1] = 1;
6 matrix[0][2] = 2;
7 matrix[0][3] = 3;
8 matrix[0][4] = 4;
9 matrix[1][0] = 5;
10 matrix[1][1] = 6;
11 matrix[1][2] = 7;
12 matrix[1][3] = 8;
13 matrix[1][4] = 9;
14 //Accessing the value
15 printf("value of matrix[1][4] %x\n",matrix[1][4]);
16 matrix[1][4] = matrix[0][0];
17 printf("value of matrix[1][4] %x\n",matrix[1][4]);
18 }
You could download file multi_dimension_array.vr here
|
|
|
|
|
|
Simulation : Multi Dimension Arrays
|
|
|
|
|
|
value of matrix[1][4] 00000009
value of matrix[1][4] 00000000
|
|
|
|
|
|
Associative Arrays
|
|
|
Associative arrays are very efficient for storing sparse data. The memory used is proportional to the number of elements stored rather then the range of the array indexes. Size specification is not required. For example, array indexes 1, 3, 5001, 9000 are used, only 4 elements are allocated rather than 9000. These arrays automatically accommodate array locations whose values are set. |
|
|
|
|
|
While associative arrays are designed to be fast, the time taken to access data increases slightly as the number of elements grows. The access time has a logarithmic relationship to the number of elements. If non-sparse indexes are used, either fixed or dynamic arrays are faster and more memory efficient that associative arrays. |
|
|
|
|
|
Associative arrays can be used to map strings to other values. This is done by declaring the array with an index type of string. |
|
|
|
|
|
Example : Associative Arrays
|
|
|
|
|
|
1 program associative_array {
2 bit [7:0] a_array [string];
3 // Store the values
4 a_array["Deepak"] = random();
5 a_array["Kumar"] = random();
6 a_array["Tala"] = random();
7 // Access the values
8 printf("Value stored at Deepak : %x\n",a_array["Deepak"]);
9 printf("Value stored at Kumar : %x\n",a_array["Kumar"]);
10 printf("Value stored at Tala : %x\n",a_array["Tala"]);
11 // We did not store this value, should return xx
12 printf("Value stored at ASIC : %x\n",a_array["ASIC"]);
13 }
You could download file associative_array.vr here
|
|
|
|
|
|
Simulation : Associative Arrays
|
|
|
|
|
|
Value stored at Deepak : ac
Value stored at Kumar : 1d
Value stored at Tala : bf
Value stored at ASIC : xx
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|