如何在 if-else 语句中使用 Verilog 定义

How can I use Verilog defines in an if-else statements

我有一个 Verilog 定义如下:

`define NUM_BANKS 4

如果想在下面的代码中使用它:

if (`NUM_BANKS > 1)
  do something ..
else
  do something else ..

Lint 工具抱怨此表达式将始终被计算为真。

Lint 是正确的,因为在您的表达式中,4 > 1 始终为真。所以如果这是你想要的,没有问题。

应用 `define 后,if 将始终计算 4 > 1。编译器给出错误,因为 if 始终不相关。

我建议用 parameter 替换您的 `define(如果您希望通过更高级别的模块或实例化来更改它),或者使用编译器指令(http://www.csee.umbc.edu/portal/help/VHDL/verilog/compiler.html 例如)

`define HASBANKS
`ifdef (HASBANKS)
...
`else
...
`endif