In the previous tutorial we used a conditional expression with the Wait Until statement. The expression ensured that the process was only triggered when the two counter signals where equal. But what if we wanted the program in a process to take different actions based on different inputs?
The If-Then-Elsif-Else statements can be used to create branches in our program. Depending on the value of a variable, or the outcome of an expression, the program can take different paths.
This blog post is part of the Basic VHDL Tutorials series.
The basic syntax is:
if <condition> then elsif <condition> then else end if;
else are optional, and
elsif may be used multiple times. The
<condition> can be a boolean
false, or it can be an expression which evaluates to
Example expression which is
MyCounter is less than 10:
MyCounter < 10
|<=||less than or equal|
|>=||greater than or equal|
|not a||true if a is false|
|a and b||true if a and b are true|
|a or b||true if a or b are true|
|a nand b||true if a or b is false|
|a nor b||true if a and b are false|
|a xor b||true if exactly one of a or b are true|
|a xnor b||true if a and b are equal|
In this video tutorial we will learn how to use If-Then-Elsif-Else statements in VHDL:
The final code we created in this tutorial:
entity T08_IfTb is end entity; architecture sim of T08_IfTb 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 if CountUp > CountDown then report "CountUp is larger"; elsif CountUp < CountDown then report "CountDown is larger"; else report "They are equal"; end if; wait on CountUp, CountDown; end process; end architecture;
The output to the simulator console when we pressed the run button in ModelSim:
VSIM 2> run # ** Note: CountDown is larger # Time: 0 ns Iteration: 0 Instance: /t08_iftb # ** Note: CountDown is larger # Time: 0 ns Iteration: 1 Instance: /t08_iftb # ** Note: CountDown is larger # Time: 10 ns Iteration: 1 Instance: /t08_iftb # ** Note: CountDown is larger # Time: 20 ns Iteration: 1 Instance: /t08_iftb # ** Note: CountDown is larger # Time: 30 ns Iteration: 1 Instance: /t08_iftb # ** Note: They are equal # Time: 40 ns Iteration: 1 Instance: /t08_iftb # ** Note: Countup is larger # Time: 50 ns Iteration: 1 Instance: /t08_iftb # ** Note: Countup is larger # Time: 60 ns Iteration: 1 Instance: /t08_iftb ...
CountDown an initial value of 10, and
CountUp a value of 0. The first process changes both counter values at the exact same time, every 10 ns. When this happens, the second process is triggered because the program will always be waiting at the
wait on CountUp, CountDown; line. The program will always be waiting there because the If-Then-Elsif-Else and the report statements consume zero simulation time.
The If-Then-Elsif-Else statement will cause the program to take one of the three branches we created. The two first branches cover the cases where the two counters have different values. We could have dropped the single
else, and used
elsif CountUp = CountDown then which would have had the same result. But it is good design practice to cover all branches, and the
else clause covers all intentional and unforeseen cases.
As we can see from the printout, the second process takes one of the three branches every time the counters change.
- If-Then may be used alone or in combination with Elsif and Else.
- Expressions may contain relational and logical comparisons and mathematical calculations.