如何根据之前的参数生成参数

问题描述 投票:0回答:2

我最近从 VHDL 切换到 SystemVerilog,并且正在转换一些代码。我想根据 3 个参数生成一个局部参数数组:

SZ
L
MAX

formula

module test #(
    parameter int SZ = 1024,
    parameter int L = 35,
    parameter int MAX = 4
)()
//...
localparam int n[MAX:0] = ;//...

for(genvar i = 0; i < max; i++) begin: gg
//n[i] and n[i+1] will be used here
//There is a second generate loop here that uses n[i+1] and therefore n[i+1] has to be parameter.

end

我尝试使用函数来生成 localparams,但出现错误,函数中的元素分配不是恒定的。我在 VHDL 中从未遇到过这个问题。

我能想到的唯一其他选择是在

for
generate
中创建参数,但是我将如何引用初始值?还有其他解决办法吗?

我使用的模拟器是 Verilator,但我也希望设计能够在 Xilinx Vivado 中运行。

我不想从外部脚本生成参数,因为我无法使用 Vivado 在同一项目中使用不同参数运行多个综合/实现的能力。这就是我以前在 VHDL 中所做的事情。

verilog system-verilog verilator
2个回答
1
投票

您可以使用函数来初始化参数,只需将整个数组的输出作为函数的结果即可。为此,您需要一个 typedef

typedef int array_type[MAX:0];

function array_type f();
  f[0]=SZ;
  for(int i=0;i<MAX;i++)
      f[i+1]=f[i]-((2*i)+1)*L)/2;
endfunction
localparam array_type n = f();

0
投票

我通过使用 32 位压缩数组使其工作。 Verilator 不支持带常量的解包 int。 Packed int 也不支持,所以我必须将类型更改为 pack of 32-bits。

typedef [MAX:0][31:0] array_type;

function array_type f();
  f[0]=SZ;
  for(int i=0;i<MAX;i++)
      f[i+1]=f[i]-((2*i)+1)*L)/2;
endfunction
localparam array_type n = f();
© www.soinside.com 2019 - 2024. All rights reserved.