Verilog:通用参数
Verilog: generic parameters
我有几个 verilog(不是 system-verilog)块,我想根据其他参数在其中生成它们。例如:
module some_module (in, out)
realtime p = 3.5; // the parameter which I want to change
initial begin
if ('global_parameter == 1)
p = 5.8;
if ('global_parameter == 2)
p = 4.4;
end
core_module #(p) core (in, out);
endmodule
此处,"global_parameter" 在头文件中定义,但在模拟时使用模拟器参数覆盖。我的核心模块使用 "p" 作为延迟值,如本例所示:
module core_module(in, out)
parameter p = 1;
always out <= #p in; // p should be constant
endmodule
因此,我希望核心模块的参数得到更新"p"。但是这种情况是不可模拟的。如果可能的话,你能给我推荐一个可能的解决方案吗?
谢谢
参数在整个设计中必须是 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 header 样式并生成块。这些特性在 IEEE Std 1364-1995
中不存在
我不知道有任何纯 Verilog 的解决方案。您可以尝试嵌入代码(例如 Perl 的 EP3, Ruby's eRuby/ruby_it, Python's prepro, etc.) As I mention in another answer to a somewhat similar question, here。嵌入式的一个挑战是需要 pre-compiled/processed,使它们更像 `define
然后是参数。
如果 SystemVerilog 是一个选项,您可以执行以下操作:(参考 IEEE Std 1800-2012 § 6.20 Constants;接近 § 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
我有几个 verilog(不是 system-verilog)块,我想根据其他参数在其中生成它们。例如:
module some_module (in, out)
realtime p = 3.5; // the parameter which I want to change
initial begin
if ('global_parameter == 1)
p = 5.8;
if ('global_parameter == 2)
p = 4.4;
end
core_module #(p) core (in, out);
endmodule
此处,"global_parameter" 在头文件中定义,但在模拟时使用模拟器参数覆盖。我的核心模块使用 "p" 作为延迟值,如本例所示:
module core_module(in, out)
parameter p = 1;
always out <= #p in; // p should be constant
endmodule
因此,我希望核心模块的参数得到更新"p"。但是这种情况是不可模拟的。如果可能的话,你能给我推荐一个可能的解决方案吗?
谢谢
参数在整个设计中必须是 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 header 样式并生成块。这些特性在 IEEE Std 1364-1995
中不存在我不知道有任何纯 Verilog 的解决方案。您可以尝试嵌入代码(例如 Perl 的 EP3, Ruby's eRuby/ruby_it, Python's prepro, etc.) As I mention in another answer to a somewhat similar question, here。嵌入式的一个挑战是需要 pre-compiled/processed,使它们更像 `define
然后是参数。
如果 SystemVerilog 是一个选项,您可以执行以下操作:(参考 IEEE Std 1800-2012 § 6.20 Constants;接近 § 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