Verilog Arrays
Array means an ordered list of variables. In Verilog, we can have an any dimension of array and can be of 2 types, i.e., scalar and vector. Arrays are only allowed for net, reg, integer and real data types. Array elements are located with the help of index value. These index value starts with 0 and thus the last index = size_of_array-1.
Scalar vs Vector Array
Scalar Array is an array of one 1-bit data type. Ex – reg a[3:0] - This is an array of size 4 and where each element can hold 1-bit data.
Vector Array is an array of vectors. As we have seen earlier vectors are nothing but a group of 1-bit data types combined to increase the size of data type. Ex – reg [3:0] a [7:0] - This is an array of size 8, where each element can hold 4-bit reg data.
Dimension of array
Dimension can be related to dimensions of real objects, where 1-D would mean that object will just have width, 2-D means it will have length and width and 3-D means it will have height, length, and width. But for real life objects dimensions are related. In Verilog, array can be of any dimension and would simply mean that how many index variables are required to access a particular element of the array.
Example
Memories
Memories are a vital part of hardware devices as it stores data. Memories have 2 important terms width and depth. Width represents the no. of bits each element can store, and depth is the number of elements a memory can store.
If we see memories in terms of array, then a 1-D vector/scalar array represents a memory.
Example
Accessing array elements
All array elements cannot be accessed at the same time. Thus, while reading or writing the data from array elements, we must access array elements one at a time, using loops. Generally, we use for loop for this purpose, but at times other types of loops can also be used.
As mentioned earlier, no. of index variable depends upon the dimension of array and no. of loops is equal to no. of index variable.