Verilog Testbench常量exp和pram编译和模拟错误

时间:2015-06-14 19:40:02

标签: verilog simulation hdl modelsim

源代码:

module SingleOneBit(N,T);

parameter integer w; //width or number of inputs N

input wire [w-1:0] N;
output wire T;   

wire[w*(w-1):0] N1; //for anding all possible combinations of 2 bits 
wire R; // for oring all tha ands. If R = 1 then N contians more than one bit with value 1
wire RS; //ors all the bits of the input. Used for checking if there is one bit with value 1 or all 0s
wire R1; // not of R;


buf(R, 0); //initialy R should be 0;
buf(RS, 0); //initialy RS should be 0;

genvar i, j;

generate

for(i = 0; i<w; i=i+1) begin

or(RS,N[i],RS);

for(j = i+1; j<w; j=j+1) begin

and(N1[(i*w)+j], N[i], N[j]);
or(R,N1[(i*w)+j],R);

end    
end

endgenerate

not(R1, R);
and(T,RS,R1);  

endmodule 

Testbench代码:

`include "C:/Users/Muaz Aljarhi/Google Drive/Muaz/Hardware_Designs/Verilog Course/SingleOneBit.v"

module SingleOneBit_tb();

integer n = 5;

reg[n-1:0] N;
wire T;

SingleOneBit sob(.N(N),.T(T));
defparam sob.w = n;

initial begin

$monitor("N = %b, T = %b",N,T);  



end  

endmodule 

编译此Verilog Testbench代码会产生以下错误:

  

**错误:C:/ Users / Muaz Aljarhi / Google Drive / Muaz / Hardware_Designs / Verilog Course / SingleOneBit_tb.v(7):范围   必须以常数表达为界。   **错误:C:/ Users / Muaz Aljarhi / Google Drive / Muaz / Hardware_Designs / Verilog Course / SingleOneBit_tb.v(11):   defparam的右侧必须是不变的。

如何声明可在测试平台内更改的变量或常量表达式?我尝试使用参数,但参数不是可以更改的变量。提前致谢

编辑:我是否必须使用可能不同的输入reg变量来声明模块的不同实例化,还是有另一种方式?

我也试过这个:

SingleOneBit sob(.N(N[0]),.T(T));
defparam sob.w = 32'd5;

但是使用modelsim模拟以下内容:

# ** Error: (vopt-2293) The parameter 'w' in the instance ('/SingleOneBit_tb/sob') of ('SingleOneBit') is declared without a value,
# 
# and the instantiation does not provide a value for this parameter.

如何避免在模拟时出现此错误?再次感谢。

2 个答案:

答案 0 :(得分:2)

由于参数是编译时(技术上精化时间)常量,因此在执行过程中不能让它们发生变化。因此,sob.w设置为n的第一个错误无效,因为n在精化阶段(编译整个设计的一部分,之前)无法确定为固定值可以进行模拟)。

第二个错误是声明SingleOneBit模块sob没有定义hte w参数的结果。稍后使用defparam对其进行定义时,需要提供一些默认值w。您可以通过更改w的声明以包含默认值来在模块本身中执行此操作:

parameter integer w = 32'd5;

由于您似乎想要测试此模块的各种宽度,我没有看到一种方法不声明此模块的多个宽度。当然,您可以使用generate语句紧凑地生成各种宽度的模块;但我非常确定在模拟过程中没有任何改变参数的方法。

编辑: 我忘了提到defparam构造可能会从语言中删除(IEE1800-2009和IEEE1800-2012都将其列为将来可能会被删除的内容),所以你应该避免使用它来允许你的代码与未来的工具兼容。

答案 1 :(得分:1)

编译器在模块实例化时需要一些参数值。 在&#34; SingleOneBit&#34;中提供默认参数值像这样:

parameter integer w= 12345;

稍后您可以使用&#34; defparam&#34;。

进行更改

或者您可以在实例化表达式中设置参数,如下所示:

SingleOneBit #(12345) sob(.N(N[0]),.T(T));