如何在verilog中使用循环/生成初始化参数化数组参数?

时间:2020-12-24 16:41:38

I want to initialize a parameterized array parameter as follow:

我想初始化参数化数组参数如下:

parameter n = 4;
parameter [(log2(n)-1):0] state [(n-1):0] = '{2'h3, 2'h2, 2'h1, 2'h0}; // for n=4

This assignment works fine if n=4. When n=8, it should initialize as

如果n=4,这个赋值是有效的。当n=8时,初始化为

{3'h7, 3'h6, 3'h5, 3'h4, 3'h3, 3'h2, 3'h1, 3'h0}

I want to initialize it like this:

我想这样初始化它:

for(i=0,i<n,i=i+1)
    state[i] = i;

Now what should I use to do this initialization? Can I do it with generate? Here log2 is a function.

现在我该用什么来初始化呢?我可以用generate吗?log2是一个函数。

1 个解决方案

#1


0  

First off, you are using SystemVerilog, the super-set and successor of Verilog. Verilog does not support arrayed parameters (vectors are okay) and Verilog cannot assign a whole unpacked array (the '{} is SystemVerilog).

首先,您使用的是SystemVerilog,它是Verilog的超集和继承者。Verilog不支持排列参数(向量是ok的),Verilog也不能分配一个完整的未填充数组(“{}是SystemVerilog”)。

With SystemVerilog you can auto scale the values of STATE with the following:

使用SystemVerilog,您可以自动缩放状态值,如下所示:

parameter N = 4;
parameter [(log2(N)-1):0] STATE [N] = state_val();

typedef [(log2(N)-1):0] state_t [N];
function state_t state_val();
  for(int i=0; i<N; i++)
    state_value[i] = i;
endfunction : state_val

Note: Most coding style guidelines recommend using uppercase for parameters and lowercase for variables; this allows easier readability. This is why I changed n and state to N and STATE in my answer.

注意:大多数编码风格指南建议参数使用大写字母,变量使用小写字母;这使得更容易可读性。这就是为什么我把n和状态变换为n,在我的答案中是状态。

#1


0  

First off, you are using SystemVerilog, the super-set and successor of Verilog. Verilog does not support arrayed parameters (vectors are okay) and Verilog cannot assign a whole unpacked array (the '{} is SystemVerilog).

首先,您使用的是SystemVerilog,它是Verilog的超集和继承者。Verilog不支持排列参数(向量是ok的),Verilog也不能分配一个完整的未填充数组(“{}是SystemVerilog”)。

With SystemVerilog you can auto scale the values of STATE with the following:

使用SystemVerilog,您可以自动缩放状态值,如下所示:

parameter N = 4;
parameter [(log2(N)-1):0] STATE [N] = state_val();

typedef [(log2(N)-1):0] state_t [N];
function state_t state_val();
  for(int i=0; i<N; i++)
    state_value[i] = i;
endfunction : state_val

Note: Most coding style guidelines recommend using uppercase for parameters and lowercase for variables; this allows easier readability. This is why I changed n and state to N and STATE in my answer.

注意:大多数编码风格指南建议参数使用大写字母,变量使用小写字母;这使得更容易可读性。这就是为什么我把n和状态变换为n,在我的答案中是状态。