SystemVerilog not recognizing constant: Error: Range must be bounded by constant expression

SystemVerilog not recognizing constant: Error: Range must be bounded by constant expression

在这个简短的例子中,我想简化信号宽度的写法。对于一个信号,确实没有必要这样做,但在我的实际代码中,我有很多这样的信号,用更长的样式声明它们是不合适的。

你能告诉我为什么 signal_2 会出错吗?

module sample #(parameter BYTE_WIDTH = 4);
    const int BIT_WIDTH = BYTE_WIDTH * 8;

    logic [BYTE_WIDTH * 8 -1 : 0] signal_1; // works
    logic [BIT_WIDTH -1 : 0] signal_2; // ** Error: Range must be bounded by constant expressions.
endmodule

一个 const 变量在 运行 时间 被赋值。为时已晚:变量 signal_2 的宽度需要在 编译时 固定。所以,你需要的是 localparam,它(像 parameter)在编译时是固定的,但(不像 parameter)不能从外部覆盖:

module sample #(parameter BYTE_WIDTH = 4);
    localparam BIT_WIDTH = BYTE_WIDTH * 8;

    logic [BYTE_WIDTH * 8 -1 : 0] signal_1; 
    logic [BIT_WIDTH -1 : 0] signal_2; 
endmodule

https://www.edaplayground.com/x/2fKU