如何在Verilog中声明和使用一维和二维字节数组?

发布于 2024-09-05 05:24:26 字数 299 浏览 5 评论 0原文

如何在Verilog中声明和使用一维和二维字节数组?

例如。如何做类似的事情

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;
    }
}

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

極樂鬼 2024-09-12 05:24:26

Verilog 以位为单位进行思考,因此 reg [7:0] a[0:3] 将为您提供一个 4x8 位数组(=4x1 字节数组)。您可以使用 a[0] 从中获取第一个字节。第二个字节的第三位是a[1][2]

对于二维字节数组,首先检查您的模拟器/编译器。旧版本(我相信 01 之前的版本)不支持此功能。然后 reg [7:0] a [0:3] [0:3] 将为您提供一个 2D 字节数组。例如,可以使用 a[2][0][7] 访问单个位。

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

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
注定孤独终老 2024-09-12 05:24:26

除了 Marty 的出色回答之外,SystemVerilog 规范还提供了 byte 数据类型。下面声明一个 4x8 位变量(4 个字节),为每个字节分配一个值,然后显示所有值:

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

打印出:

Address = 0, Data = 00000001
Address = 1, Data = 00000010
Address = 2, Data = 00000100
Address = 3, Data = 00001000

这在概念上类似于 Marty 的 reg [7:0] a [0:3] ;。但是,byte 是 2 态数据类型(0 和 1),而 reg 是 4 态数据类型 (01xz)。使用 byte 还需要您的工具链(模拟器、合成器等)支持此 SystemVerilog 语法。另请注意更紧凑的 foreach (b[i]) 循环语法。

SystemVerilog 规范支持多种多维数组类型。 LRM 可以比我更好地解释它们;请参阅IEEE Std 1800-2005,第 5 章。

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.

豆芽 2024-09-12 05:24:26

其实很简单,就像 C 编程一样,你只需要在声明时传递右侧的数组索引即可。但是,是的,4 个元素的语法类似于 [0:3]。

reg a[0:3]; 

这将创建一个一维的单个位数组。类似地,2D 数组可以这样创建:

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

现在在 C 中假设您创建一个 int 的 2D 数组,那么它将在内部创建一个 32 位的 2D 数组。但不幸的是,Verilog 是一个 HDL,所以它以位而不是一堆位来思考(尽管 Verilog 中有 int 数据类型),它可以允许您创建任意数量的位来存储在数组元素中(这不是对于 C 的情况,您不能在 C 中的 2D 数组的每个元素中存储 5 位)。因此,要创建一个 2D 数组,其中每个单独的元素都可以保存 5 位值,您应该这样写:

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文