How to declare and use 1D and 2D byte arrays in Verilog?
eg. how to do something like
byte a_2D[3][3]; byte a_1D[3]; // using 1D for (int i=0; i< 3; i++) { a_1D[i] = (byte)i; } // using 2D for (int i=0; i< 3; i++) { for (int j=0; j< 3; j++) { a_2D[i][j] = (byte)i*j; } }
For a 2D array of bytes, first check your simulator/compiler. Older versions (pre '01, I believe) won't support this. Then reg [7:0] a [0:3] [0:3] will give you a 2D array of bytes. A single bit can be accessed with a[2][0][7] for example.
In order to create a 1D or 2D Array you need to specify the type of object that you are going to be storing in the array when you create it. When you create an array you type the name of the object followed by two brackets, which represent the array part of the creation.
Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces. Ex: int a[2][3]={0,0,0,1,1,1}; initializes the elements of the first row to zero and the second row to one. The initialization is done row by row.
An array is a collection of the same types of variables and accessed using the same name plus one or more indices. Each array dimension is declared by having the min and max indices within the square brackets. Array indices can be written in either direction: array_name[least_significant_index:most_significant_index]
Verilog thinks in bits, so reg [7:0] a[0:3]
will give you a 4x8 bit array (=4x1 byte array). You get the first byte out of this with a[0]
. The third bit of the 2nd byte is a[1][2]
.
For a 2D array of bytes, first check your simulator/compiler. Older versions (pre '01, I believe) won't support this. Then reg [7:0] a [0:3] [0:3]
will give you a 2D array of bytes. A single bit can be accessed with a[2][0][7]
for example.
reg [7:0] a [0:3]; reg [7:0] b [0:3] [0:3]; reg [7:0] c; reg d; initial begin for (int i=0; i<=3; i++) begin a[i] = i[7:0]; end c = a[0]; d = a[1][2]; // using 2D for (int i=0; i<=3; i++) for (int j=0; j<=3; j++) b[i][j] = i*j; // watch this if you're building hardware end
In addition to Marty's excellent Answer, the SystemVerilog specification offers the byte
data type. The following declares a 4x8-bit variable (4 bytes), assigns each byte a value, then displays all values:
module tb; byte b [4]; initial begin foreach (b[i]) b[i] = 1 << i; foreach (b[i]) $display("Address = %0d, Data = %b", i, b[i]); $finish; end endmodule
This prints out:
Address = 0, Data = 00000001 Address = 1, Data = 00000010 Address = 2, Data = 00000100 Address = 3, Data = 00001000
This is similar in concept to Marty's reg [7:0] a [0:3];
. However, byte
is a 2-state data type (0 and 1), but reg
is 4-state (01xz). Using byte
also requires your tool chain (simulator, synthesizer, etc.) to support this SystemVerilog syntax. Note also the more compact foreach (b[i])
loop syntax.
The SystemVerilog specification supports a wide variety of multi-dimensional array types. The LRM can explain them better than I can; refer to IEEE Std 1800-2005, chapter 5.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With