精通 VHDL Wait 语句:Wait On、Wait Until 与组合用法
上一教程探讨了 VHDL 中信号与变量的区别,强调信号作用域比变量大(变量局限于单个进程)。下一个合乎逻辑的问题是如何利用信号协调多个进程。
除了熟悉的 wait;(无限暂停)和 wait for(基于时间的暂停),VHDL 还提供两个强大的事件驱动同步语句:Wait On 和 Wait Until。它们也可与 wait for 组合,创建复杂的控制流。
Wait On
wait on 语句挂起进程,直到列表中任一信号值发生变化。
wait on <signal_name1>, <signal_name2>, ...;
Wait Until
wait until 语句暂停执行,直到指定的布尔条件变为真。
wait until <condition>;
组合 Wait 语句
三种构造可结合使用,实现细粒度时序行为:
wait on <signal_name1> until <condition> for <time_value>;
例如,下面一行将暂停 10 ns,或直到 signal1 变化且 signal2 等于 signal3:
wait on signal1 until signal2 = signal3 for 10 ns;
实际示例
下面完整测试平台演示如何用 wait on 和 wait until 实现进程间通信。
entity T07_WaitOnUntilTb is end entity;
architecture sim of T07_WaitOnUntilTb is
signal CountUp : integer := 0;
signal CountDown : integer := 10;
begin
process is
begin
CountUp <= CountUp + 1;
CountDown <= CountDown - 1;
wait for 10 ns;
end process;
process is
begin
wait on CountUp, CountDown;
report "CountUp=" & integer'image(CountUp) & " CountDown=" & integer'image(CountDown);
end process;
process is
begin
wait until CountUp = CountDown;
report "Jackpot!";
end process;
end architecture;
ModelSim 控制台输出展示事件序列:
VSIM 2> run # ** Note: CountUp=1 CountDown=9 # Time: 0 ns Iteration: 1 Instance: /t07_waitonuntiltb # ** Note: CountUp=2 CountDown=8 # Time: 10 ns Iteration: 1 Instance: /t07_waitonuntiltb # ** Note: CountUp=3 CountDown=7 # Time: 20 ns Iteration: 1 Instance: /t07_waitonuntiltb # ** Note: CountUp=4 CountDown=6 # Time: 30 ns Iteration: 1 Instance: /t07_waitonuntiltb # ** Note: CountUp=5 CountDown=5 # Time: 40 ns Iteration: 1 Instance: /t07_waitonuntiltb # ** Note: Jackpot! # Time: 40 ns Iteration: 1 Instance: /t07_waitonuntiltb # ** Note: CountUp=6 CountDown=4 # Time: 50 ns Iteration: 1 Instance: /t07_waitonuntiltb # ** Note: CountUp=7 CountDown=3 # Time: 60 ns Iteration: 1 Instance: /t07_waitonuntiltb # ** Note: CountUp=8 CountDown=2 # Time: 70 ns Iteration: 1 Instance: /t07_waitonuntiltb # ** Note: CountUp=9 CountDown=1 # Time: 80 ns Iteration: 1 Instance: /t07_waitonuntiltb # ** Note: CountUp=10 CountDown=0 # Time: 90 ns Iteration: 1 Instance: /t07_waitonuntiltb # ** Note: CountUp=11 CountDown=-1 # Time: 100 ns Iteration: 1 Instance: /t07_waitonuntiltb
分析
第一个进程每 10 ns 同步更新 CountUp 和 CountDown。因为信号赋值仅在 wait 语句后生效,两个计数器在每个仿真步骤同时更新。
第二个进程使用 wait on CountUp, CountDown;,挂起直到任一计数器变化。如输出所示,这在 0 ns(初始更新)触发,随后每当任一信号变化再次触发。
第三个进程采用 wait until CountUp = CountDown;。它在每次信号变化时唤醒,但仅当等式成立时继续执行。因此“Jackpot!”消息仅出现一次——在 40 ns 两计数器均达到 5 时。

要点
- Wait On 暂停直到任一指定信号变化。
- Wait Until 在信号变化时唤醒,但仅当条件求值为真才继续。
wait on、wait until和wait for三种构造可组合,实现精细控制。
进入下一教程深入了解 VHDL 同步机制。
VHDL