Topa
Topa

Reputation: 100

How do I generate parameters dependent on previous parameters

I recently switched from VHDL to SystemVerilog, and I am converting some of my codes. I want to generate an array of local parameters based on 3 parameters: 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

I tried using a function to generate localparams, but I get an error that element assignment in function is not constant. I never had this issue in VHDL.

The only other option I can think of is to create the params inside the for generate, but how would I reference the initial value? Is there any other solution?

The simulator I am using is Verilator, but I also want the design to work in Xilinx Vivado.

I do not want to generate the parameters from an external script because I lose the ability to use Vivado's ability to run multiple synthesis/implementation in the same project with different parameters. That was what I used to do in VHDL.

Upvotes: 2

Views: 799

Answers (2)

Topa
Topa

Reputation: 100

I got it working by using packed array of 32-bits. Verilator doesn't support unpacked int with constants. Packed int is also not supported so I had to change the type to 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();

Upvotes: 0

dave_59
dave_59

Reputation: 42616

You can use a function to initialize a parameter, you just have to have the output of the entire array as the result of the function. To do that, you need a 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();

Upvotes: 1

Related Questions