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

后端 未结 3 905
面向向阳花
面向向阳花 2021-01-30 05:39

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;          


        
相关标签:
3条回答
  • 2021-01-30 05:48

    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.

    0 讨论(0)
  • 2021-01-30 05:50

    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];
    
    0 讨论(0)
  • 2021-01-30 06:09

    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
    
    0 讨论(0)
提交回复
热议问题