Verilog“如果”使用变量的语句
我在 genvar 中有以下 verilog 代码,尽管变量“j”不是 genvar 变量。当我检查语法(使用 Xilinx)时,我在“if”语句行上收到错误“生成 if 语句中的条件表达式非法”。将“j”更改为 genvar 变量并不能解决问题,如何正确读取“j”?感谢您的任何帮助。
genvar i;
generate
integer j=0;
for(i=0; (i<10); i=i+1) begin: gen_columns
if (j==0) begin
//some code
end
assign j=j+1;
end
endgenerate
I have the following verilog code within a genvar, although the variable 'j' is not a genvar variable. When I check the syntax (using Xilinx) I receive the error "Illegal condition expression in generate if statement" on the line with the 'if' statement. Changing 'j' to a genvar variable doesn't fix the issue, how can I make read 'j' properly? Thanks for any help.
genvar i;
generate
integer j=0;
for(i=0; (i<10); i=i+1) begin: gen_columns
if (j==0) begin
//some code
end
assign j=j+1;
end
endgenerate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当详细设计时(仿真开始之前),生成块需要解析为标准 verilog 模块项。在您的示例中,该工具尝试将
if (j==0)
计算为常量,但无法执行此操作。从你的例子中并不清楚你想要做什么。以下是一些对我有用的示例:
此代码迭代总线的位并根据位分配不同的值。输出为
3fe
。此代码为
j[0]
和j[1]
创建相同的逻辑,以便在它们为零时递增它们。输出是:我的第二种情况和您的示例之间的区别在于 if 语句和赋值放置在 Always 块中。当详细设计时,该工具将用两个always块替换generate块,一个带有
i=0
,一个带有i=1
。风格注释:虽然从多个always块更新变量是合法的verilog,但它不被认为是好的做法。它可能会合成为多驱动信号,并可能在模拟中引入竞争条件。在我的第二个示例中,如果分配为
j = j + 1
(没有[i]
),则j
将始终以多个方式分配块。The generate block needs to be resolved to standard verilog module items when the design is elaborated (before simulation starts). In your example, the tool is trying to evaluate
if (j==0)
as a constant, and can't do it.It's not clear from your example what you're trying to do. Here are a couple examples that work for me:
This code iterates over the bits of a bus and assigns a different value depending on the bit. The output is
3fe
.This code creates identical logic for
j[0]
andj[1]
to increment them if they are zero. The output is:The difference between my second case and your example is that the if statement and assignment are placed within an always block. When the design is elaborated, the tool will replace the generate block with two always blocks, one with
i=0
and one withi=1
.A style note: although it is legal verilog to update a variable from multiple always blocks, it is not considered good practice. It will likely synthesize as a multiply-driven signal and may introduce race conditions in simulation. In my second example, if the assignment were
j = j + 1
(without[i]
), thenj
would be assigned in multiple always blocks.从上面的代码片段中,我无法弄清楚您正在尝试建模的硬件类型。
For 循环:在 Verilog 中,for 循环主要用于
直接来自 Verilog LRM 的生成循环:
在您的代码中,j 的用途是什么?在 Verilog 中,分配变量然后将其用作同一程序块中的控制逻辑是一种不好的形式。最有可能的是,您需要在生成循环之外为 j 添加另一个程序块。我不知道您代码中 j 的用途,但也许类似于以下内容?
From your code snippet above, I could not figure out what type of hardware you are trying to model.
For-loops: In Verilog for-loops are used primarily for
Straight from the Verilog LRM for generate-loops:
In your code, what is the purpose of j? In Verilog it is bad form to assign a variable and then use it as control logic in the same procedural block. Most likely you need another procedural block for j outside of the generate-loop. I don't know the purpose of j in your code, but perhaps something like the following?