1 assign
assign 语句用于为 wire 类型的信号提供连续赋值。它建立了一个驱动源,根据右侧表达式的值持续驱动 wire 信号。
module Example;
wire a, b, c;
assign c = a & b;
endmodule
2 force
force 用于强制将一个信号的值设定为某个特定值,会覆盖该信号原有的任何驱动,包括 assign或其他硬件逻辑产生的驱动。
module Example;
wire a, b, c;
assign c = a & b;
initial begin
#10 force c = 1'b1;
#10 release c;
end
endmodule
上述代码中,在 #10 时刻,c 会被强制为 1,无论 a 和 b 的值如何,以及之前 assign 语句的驱动情况。
force 操作直接将信号强制为指定的值,并且这种强制会覆盖所有其他驱动源,包括正常的 assign逻辑、其他 force 操作(如果之前已经有 force 操作,新的 force 操作会覆盖旧的)。在 force操作生效期间,原有的逻辑被暂停,直到使用 release 操作取消强制。
持续时间由 force 操作的开始时间和 release 操作的时间决定。如果没有 release 操作,信号会一直被强制为指定的值,可能导致非预期行为。
必须使用 release 操作来取消 force 操作,让信号恢复到正常的驱动逻辑。
force和release使用示例
module testbench;
wire external_signal;
processor dut (
.ext_signal(external_signal)
);
initial begin
// 正常的测试
// 模拟外部信号错误
#20 force external_signal = 1'b0;
// 观察处理器的错误处理
#10 release external_signal;
// 继续正常测试
end
endmodule
module test;
logic a, b, c, d;
wire e;
and and1 (e, a, b, c);
initial begin
$monitor("%d d=%b,e=%b", $stime, d, e);
assign d = a & b & c;
a = 1;
b = 0;
c = 1;
#10;
force d = (a | b | c);
force e = (a | b | c);
#10;
release d;
release e;
#10 $finish;
end
endmodule
Results:
0 d=0,e=0
10 d=1,e=1
20 d=0,e=0
参考:IEEE Standard for SystemVerilog 10.6节