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

214,153

Solution 1

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

Solution 2

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.

Solution 3

It is simple actually, like C programming you just need to pass the array indices on the right hand side while declaration. But yeah the syntax will be like [0:3] for 4 elements.

reg a[0:3]; 

This will create a 1D of array of single bit. Similarly 2D array can be created like this:

reg [0:3][0:2];

Now in C suppose you create a 2D array of int, then it will internally create a 2D array of 32 bits. But unfortunately Verilog is an HDL, so it thinks in bits rather then bunch of bits (though int datatype is there in Verilog), it can allow you to create any number of bits to be stored inside an element of array (which is not the case with C, you can't store 5-bits in every element of 2D array in C). So to create a 2D array, in which every individual element can hold 5 bit value, you should write this:

reg [0:4] a [0:3][0:2];
Share:
214,153
Ursa Major
Author by

Ursa Major

I hope to help people and make this world a better place for our present and future generations.

Updated on December 24, 2021

Comments

  • Ursa Major
    Ursa Major over 2 years

    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;
        }
    }
    
  • Rajesh Shashi Kumar
    Rajesh Shashi Kumar almost 5 years
    Are unpacked arrays non-synthesizable? Why do you say b[i][j] can be a problem?
  • Rock
    Rock almost 5 years
    This example is a little confusing. I would suggest putting different index number for i and j. Right now they are both 0-3. Suppose you have reg [7:0] b [0:2] [0:3]; it is hard to tell b[i]'s size.
  • Kaiser Keister
    Kaiser Keister over 4 years
    @RajeshS They are synthesizable, but only with SystemVerilog