如何在Verilog中使用循环/生成来初始化参数化数组参数? [英] How to initialize parameterized array parameter using loop/generate in verilog?

查看:751
本文介绍了如何在Verilog中使用循环/生成来初始化参数化数组参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要初始化一个参数化的数组参数,如下所示:

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

如果n = 4,则此分配工作正常.当n = 8时,它应初始化为

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}

我想这样初始化它:

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

现在我应该用什么来进行初始化?我可以用generate来做吗? log2是一个函数.

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

推荐答案

首先,您正在使用SystemVerilog,它是Verilog的超级集合和后继者. Verilog不支持数组参数(矢量是可以的),并且Verilog无法分配整个未打包的数组('{}是SystemVerilog).

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,您可以使用以下方法自动缩放STATE的值:

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

注意:大多数编码风格指南建议对参数使用大写字母,对变量使用小写字母;这使得可读性更强.这就是为什么我在答案中将nstate更改为NSTATE的原因.

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.

这篇关于如何在Verilog中使用循环/生成来初始化参数化数组参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆