简体   繁体   English

在重置值的分配中确定参数值的大小

[英]Sizing a parameter value in an assignment of a reset value

The following code is correct as RESET_VAL is extended or truncated to WIDTH automagically:以下代码是正确的,因为RESET_VAL会自动扩展或截断为WIDTH

parameter RESET_VAL = 5;
reg [WIDTH-1:0] data;

always @(posedge clk or negedge nres) begin
    if (!nres) begin
        data <= RESET_VAL;
    end
    ...

However, a synthesis tool is throwing a warning:但是,综合工具发出警告:

Width of left hand side 'data' [6] doesn't match the width of right hand side [32] in assignment

Can anyone help me with the correct syntax for sizing the parameter value?任何人都可以帮助我使用正确的语法来调整parameter值的大小吗?

I would like to pass parameters through hierarchy, specifying their (32-bit) value without having to dive into submodules for determining the correct size of the parameters.我想通过层次结构传递参数,指定它们的(32 位)值,而不必深入到子模块以确定参数的正确大小。

The type of a parameter in Verilog/SV is inferred from its value. Verilog/SV 中的参数类型是根据其值推断的。
The post specifies an unsized number literal which is an integer in Verilog whose default is 32 bits.该帖子指定了一个未调整大小的数字文字,它是 Verilog 中的 integer,其默认值为 32 位。
Change the number to be 6 bits this way, to form a size 6-bit decimal value of five:这样把数字改成6位,组成一个大小为5的6位十进制值:

parameter RESET_VAL = 6'd5;  

Example:例子:

module testbench ();
  
  parameter RESET_VAL = 6'd5;
  
  initial
    $display("RESET_VAL = %b",RESET_VAL);
  
endmodule  

Produces:生产:

RESET_VAL = 000101

Alternate Answer: If you want to have a 32 bit parameter, and eliminate the warning at the assignment, use one of these:备选答案:如果您想要一个 32 位参数,并消除分配时的警告,请使用以下之一:

module testbench ();
  
  parameter RESET_VAL = 5;
  parameter WIDTH     = 6;
  
  initial begin
    $display("RESET_VAL = %b",WIDTH'(RESET_VAL));
    $display("RESET_VAL = %b",RESET_VAL[WIDTH - 1:0]);
  end
  
endmodule    

Referring to the alternate answer: Pass WIDTH in from the top of various modules to resize differently in different modules (answers the part of the question asked as additional question details in the comments).参考备用答案:从各个模块的顶部传递 WIDTH 以在不同的模块中调整不同的大小(回答问题的一部分作为评论中的附加问题详细信息)。

Normally the type of a parameter is based on the type being assigned to it.通常,参数的类型基于分配给它的类型。 The correct thing to do here is sizing the parameter to match the size of the variable it is to be used with.此处正确的做法是调整参数大小以匹配要使用的变量的大小。

parameter bit [WIDTH-1:0] RESET_VAL = 5;
logic [WIDTH-1:0] data;

always @(posedge clk or negedge nres) begin
    if (!nres) begin
        data <= RESET_VAL;
    end

RESET_VAL now always has a fixed type regardless of the value on the RHS or overridden, RESET_VAL 现在始终具有固定类型,无论 RHS 上的值或被覆盖,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM