我在使用verilog尝试写一个rom块时,遇到parameter定义数组报错的问题,发现所使用的vivado2019.2版本默认文件类型verilog并不支持verilog2001版本的这一新特性。
报错说仅SystemVerilog支持此特性。
然后我尝试使用SystemVerilog,果然不报错,代码也成功编译运行。此外,parameter定义的数组还支持通过变量地址访问。
询问师兄后得知,parameter一般不用来定义rom块,使用reg更常见。更改为reg定义数组后,出现新的问题:
这就涉及到打包/未打包(packed type /unpacked type)的概念:
解释
在 Verilog 中:
- 打包类型(packed type):如标准的位向量,允许在声明时指定位宽,例如
[7:0]
。这种类型的变量允许按位访问。 - 未打包类型(unpacked type):通常是数组或结构,其元素可以是多位的,但整个集合不被视为单一的位宽实体。
对于 reg
类型的数组,你需要单独初始化每个元素,而不能在声明时直接使用一个初始化列表。这是因为 reg
数组在 Verilog 中是未打包的,每个数组元素必须在代码执行阶段(如 initial
块)中单独赋值。
因此最终将问题代码修改为使用reg定义数组后,再使用initial块初始化赋值存储。并且可以通过变量地址访问。
reg signed [7:0] amplitude [0:32];
initial
begin
amplitude[0] = 8'b00000000 ;amplitude[1] = 8'b00011001 ;
amplitude[2] = 8'b00110001 ;amplitude[3] = 8'b01000111 ;
amplitude[4] = 8'b01011011 ;amplitude[5] = 8'b01101010 ;
amplitude[6] = 8'b01110110 ;amplitude[7] = 8'b01111110 ;
amplitude[8] = 8'b01111111 ;amplitude[9] = 8'b01111110 ;
amplitude[10] = 8'b01110110 ;amplitude[11] = 8'b01101010 ;
amplitude[12] = 8'b01011011 ;amplitude[13] = 8'b01000111 ;
amplitude[14] = 8'b00110001 ;amplitude[15] = 8'b00011001 ;
amplitude[16] = 8'b00000000 ;amplitude[17] = 8'b11100111 ;
amplitude[18] = 8'b11001111 ;amplitude[19] = 8'b10111001 ;
amplitude[20] = 8'b10100101 ;amplitude[21] = 8'b10010110 ;
amplitude[22] = 8'b10001010 ;amplitude[23] = 8'b10000010 ;
amplitude[24] = 8'b10000000 ;amplitude[25] = 8'b10000010 ;
amplitude[26] = 8'b10001010 ;amplitude[27] = 8'b10010110 ;
amplitude[28] = 8'b10100101 ;amplitude[29] = 8'b10111001 ;
amplitude[30] = 8'b11001111 ;amplitude[31] = 8'b11100111 ;
amplitude[32] = 8'b00000000 ;
end