如何在Verilog中声明和使用一维和二维字节数组?
如何在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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]
访问单个位。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 witha[0]
. The third bit of the 2nd byte isa[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 witha[2][0][7]
for example.除了 Marty 的出色回答之外,SystemVerilog 规范还提供了
byte
数据类型。下面声明一个 4x8 位变量(4 个字节),为每个字节分配一个值,然后显示所有值:打印出:
这在概念上类似于 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:This prints out:
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), butreg
is 4-state (01xz). Usingbyte
also requires your tool chain (simulator, synthesizer, etc.) to support this SystemVerilog syntax. Note also the more compactforeach (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.
其实很简单,就像 C 编程一样,你只需要在声明时传递右侧的数组索引即可。但是,是的,4 个元素的语法类似于 [0:3]。
这将创建一个一维的单个位数组。类似地,2D 数组可以这样创建:
现在在 C 中假设您创建一个 int 的 2D 数组,那么它将在内部创建一个 32 位的 2D 数组。但不幸的是,Verilog 是一个 HDL,所以它以位而不是一堆位来思考(尽管 Verilog 中有 int 数据类型),它可以允许您创建任意数量的位来存储在数组元素中(这不是对于 C 的情况,您不能在 C 中的 2D 数组的每个元素中存储 5 位)。因此,要创建一个 2D 数组,其中每个单独的元素都可以保存 5 位值,您应该这样写:
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.
This will create a 1D of array of single bit. Similarly 2D array can be created like this:
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: