How to initialize parameterized array parameter using loop/generate in verilog?

会有一股神秘感。 提交于 2019-12-12 03:17:34

问题


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

{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.


回答1:


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).

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

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.



来源:https://stackoverflow.com/questions/32546844/how-to-initialize-parameterized-array-parameter-using-loop-generate-in-verilog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!