始终连接 block/case 语句 - Verilog

wire in always block/case statement - Verilog

以下是使用 case 语句和 always @(*) 块的示例代码。我不明白 always 块是如何触发的,也不知道为什么它在 x 被声明为 wire 时仍然有效。

wire [2:0] x = 0;
always @(*)
begin
case (1'b1)
  x[0]: $display("Bit 0 : %0d",x[0]);
  x[1]: $display("Bit 1 : %0d",x[1]);
  x[2]: $display("Bit 2 : %0d",x[2]);
  default: $display("In default case");
endcase
end

感谢任何帮助。

谢谢。

我们知道,reg可以由wire驱动,我们绝对可以在任何程序块中使用wire作为赋值的右侧。

在这里,您的代码检查 x 的哪一位是 1'b1(当然将 优先级 赋予第零位)。假设 x 更改为 3'b010。然后显示Bit 1等。现在,如果 x=3'b011 则显示 Bit 0,因为首先检查第零位。

如您所见,没有赋值x,程序块只读取它的值。而且,系统任务$display也读取了x的值。

此块的信号值没有变化。因此,这段代码工作正常。如果碰巧我们有类似 x[0] = ~x[0] 而不是 $display 的东西,那么这段代码将提供编译问题。

可以在 this and this 链接中找到更多信息。

这里,这个always块没有给x赋值,只是检查x的值。所以这是合法使用电线。

所以,关于你的问题always @(*)如何被触发的部分的解释如下:

"Nets and variables that appear on the right-hand side of assignments, in subroutine calls, in case and conditional expressions, as an index variable on the left-hand side of assignments, or as variables in case item expressions shall all be included in always @(*)."

参考:IEEE Std 1800-2012 Sec 9.4.2.2

作为@sharvil111 回答的扩展,如果你的代码是这样的

always @(*)
begin

case (sel)
x[0]: $display("Bit 0 : %0d",x[0]);
x[1]: $display("Bit 1 : %0d",x[1]);
x[2]: $display("Bit 2 : %0d",x[2]);
default: $display("In default case");

endcase
end

只要 sel 信号或 x 发生变化,程序块就会被触发,即它等同于 总是@(sel 或 x).