Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare and use 1D and 2D byte arrays in Verilog?

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;     } } 
like image 877
Ursa Major Avatar asked Jun 10 '10 03:06

Ursa Major


People also ask

How do you declare a 2D array in Verilog?

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.

How do you declare a 1D and 2D array?

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.

How do you declare and initialize 1D array and 2D array?

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.

How do you declare an array in Verilog?

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]


2 Answers

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 
like image 116
Marty Avatar answered Sep 20 '22 20:09

Marty


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.

like image 39
toolic Avatar answered Sep 17 '22 20:09

toolic