在整个设计中,参数必须是parameter 类型。您不能将变量作为参数传递。
您可以使用生成块来控制实例化:
module core_module#(parameter realtime P=1)(input in, output out);
always @(in) out <= #(P) in; // p should be constant
endmodule
module some_module (input in, output out);
generate
if (GLOBAL_PARAMETER == 1) begin : cfg_1
core_module #(5.8) core (in, out);
end
else if (GLOBAL_PARAMETER == 2) begin : cfg_2
core_module #(4.4) core (in, out);
end
else begin : cfg_dflt
core_module #(3.5) core (in, out); // default
end
endgenerate
endmodule
也可以单行计算参数:
module some_module (input in, output out);
parameter P = (GLOBAL_PARAMETER == 1) ? 5.8 : (GLOBAL_PARAMETER == 2) ? 4.4 : 3.5;
core_module #(P) core (in, out); // default
endmodule
或者(由于您无法合成),您可以将延迟值设为变量,然后通过分层引用强制该值。示例:
module core_module (input in, output out);
realtime p = 1;
always @(in) out <= #(p) in; // this p is a variable
endmodule
module some_module (input in, output out);
realtime p = 3.5;
core_module core (in, out);
initial begin
if (GLOBAL_PARAMETER == 1)
p = 5.8;
else if (GLOBAL_PARAMETER == 2)
p = 4.4;
force core.p = p; // force hierarchical variable assignment
end
endmodule
注意:所有示例都与 IEEE Std 1364-2001 和 IEEE Std 1364-2005 兼容,使用 ANSI 标头样式并生成块。 IEEE Std 1364-1995 中不存在这些功能
我不知道有任何干净的仅 Verilog 解决方案。您可以尝试嵌入代码(例如 Perl 的 EP3、Ruby 的 eRuby/ruby_it、Python 的 prepro 等)。正如我在另一个类似问题的答案中提到的,here。嵌入式的一个挑战是需要预编译/处理,使它们更像`define 然后是参数。
如果 SystemVerilog 是一个选项,您可以执行以下操作:(请参阅 IEEE Std 1800-2012 § 6.20 常量;靠近 § 6.20.2 末尾的示例)
module some_module (input in, output out);
parameter realtime P [3] = '{3.5, 5.8, 4.4};
core_module #(P[GLOBAL_PARAMETER]) core (in, out);
endmodule