如何在 VHDL 中使用等待和等待直到
在上一教程中,我们了解了信号和变量之间的主要区别。我们了解到,信号比变量具有更广泛的范围,变量只能在一个进程中访问。那么我们如何使用信号在多个进程之间进行通信呢?
我们已经学会使用wait;
无限等待,wait for
等待特定的时间。 VHDL中还有两种等待语句。
这篇博文是基本 VHDL 教程系列的一部分。
Wait On 语句将暂停进程,直到指定信号之一发生变化:wait on <signal_name1>, <signal_name2> ...;
Wait Until 语句将暂停,直到某个事件导致条件变为真:wait until <condition>;
其实Wait On、Wait Until和Wait For是可以组合的:wait on <signal_name1> until <condition> for <time_value>;
此示例将暂停 10 纳秒,或直到 signal1
更改和 signal2
等于 signal3
:wait on signal1 until signal2 = signal3 for 10 ns;
运动
在本视频教程中,我们将学习如何在 VHDL 中使用 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
分析
第一个过程递增 CountUp
计数器并递减 CountDown
柜台。它们同时更新。虽然两个信号的信号赋值在进程中不同的行,但赋值的信号值只有在程序遇到等待语句时才会生效。该过程在模拟开始时执行此操作,然后每 10 纳秒执行一次。
第二个进程的第一行是 wait on CountUp, CountDown;
.程序将在这一行等待,直到其中一个或两个信号发生变化。从打印输出中我们可以看出,这发生在 0 ns 仿真时间,此时计数器第一次更改,之后每次更改。
第三个进程的第一行是wait until CountUp = CountDown;
.每次两个信号中的一个发生变化时,程序都会唤醒,就像第一个进程一样。但只有当表达式的计算结果为 true
时它才会继续 , 否则它将重新进入睡眠状态。正如我们从打印输出中看到的那样,“大奖!”只打印一次,在 40 ns 时两个计数器的值相同。
外卖
- Wait On 将等到其中一个信号发生变化
- 如果其中一个信号发生变化,Wait Until 将唤醒,但仅当表达式为
true
时才会继续 - Wait On、Wait Until 和 Wait For 可以组合使用
转到下一个教程 »
VHDL