Sign in

Learn

0/8
0/6
0/6
0/6

Practice

Conditional Assignment

Every assignment in the previous lesson computed exactly one expression. But many wires need to choose: show the main sensor or the backup, run the fan fast or slow, drive the pin or let it go. Conditional assignment is the concurrent statement that chooses, and the hardware it describes is a multiplexer: a circuit that is always selecting, never branching.

Choosing Between Two Values

A monitoring panel shows the main sensor while it reports healthy, and falls back to the backup otherwise:

shown <= main when ok = '1' else backup;

VHDL spells it when/else. It is still an ordinary concurrent assignment: always on, one driver, order in the file irrelevant.

Resist the software reading. An if in a program picks which code runs; here nothing runs. main and backup are both live wires carrying values at all times, and the condition steers which of them is connected through to shown. That is a 2:1 multiplexer, with ok on the select pin. Nothing is skipped, because there is nothing to skip: all three inputs are just wires feeding one selecting circuit.

Chains and Priority

Stack more conditions and you get a chain. This is the fan controller from this lesson's example. Pick a speed from the temperature:

speed <= "11" when temp >= 35 else  -- fast
           "10" when temp >= 25 else  -- medium
           "01" when temp >= 18 else  -- slow
           "00";                        -- off

The conditions use the relational operators from Operators and Expressions, and they overlap on purpose: at 40 degrees, all three are true. The chain resolves the overlap by priority: conditions are considered in writing order, and the first true one wins. So 40 reads "fast" and the later conditions never get a say. In hardware, the chain is a cascade of 2:1 multiplexers with the first condition's mux sitting closest to the output, able to override everything behind it.

That makes ordering a design decision: put the most demanding condition first, and each later condition only handles what the earlier ones let through.

CAUTION

Common Mistake: ordering overlapping conditions weakest first. Start this chain with temp >= 18 and every temperature from 18 up (including 40) matches it immediately: the fan never leaves "slow". If your chain's later branches seem dead, check whether an earlier condition swallows them.

There Is Always an Else

The final else is the wire's value when nothing matched. Never leave it off. A selector with a missing alternative isn't a multiplexer anymore, because to "assign nothing" the circuit would have to remember its previous value. Remembering is not something a plain wire can do (that accident is called a latch, and Avoiding Unintended Latches deals with it properly).

Make the unconditional final else a habit.

A Pattern You Have Already Used

You've used this statement before it had a name. In Port Directions you drove a shared pin with the tri-state recipe, taken on faith. Here it is again, exactly as that lesson's line_driver example wrote it:

-- drive on your turn, let go otherwise
line <= value when enable = '1' else 'Z';

Here's why it works: this is a two-way conditional assignment, the same form as the backup-sensor selector at the top of this lesson. Its two alternatives are value and 'Z', and 'Z', "not driving", is simply one more value the wire type can carry. While enable is '1', the selector routes your value onto the pin; otherwise it routes the disconnect value, and the component lets go. There is no special tri-state statement to learn: releasing a wire is choosing to drive 'Z'. Building and arbitrating whole shared buses hands-on comes in Combinational Building Blocks.

That wasn't the only recipe you've been carrying. Numeric Types and Constants handed you a comparison-to-flag shape as-is, and Arithmetic and Relational Operators promised the mechanism would arrive here. Here it is once more, as the arithmetic lesson wrote it:

over <= '1' when total > LIMIT else '0';

The form is one more two-way select, the same shape as the backup-sensor selector at the top of this lesson. The alternatives are the constants '1' and '0'. The comparison (a true/false answer, not a wire level) sits on the select pin, steering one of the two constants onto the wire. That is the whole trick for connecting a truth value to a wire.

In the Example

The example panel on the right runs the fan controller through a heat-up and cool-down sweep. Watch speed against temp: it steps up exactly at 18, 25, and 35 (the boundaries in the chain). Then it steps back down as the sweep cools through 30, 20, and 10, one speed band at a time. The 40-degree step is the priority proof: three true conditions, and the output reads 11 because the first one wins.

Key Takeaways

  • A conditional assignment is a multiplexer that is always selecting: every alternative is a live circuit, and the condition steers one of them onto the wire.
  • Chains resolve overlapping conditions by priority (first true condition in writing order wins), so put the most demanding condition first, and always end with an unconditional fallback.
  • The tri-state recipe is a plain two-way select whose second alternative is 'Z': letting go of a wire is just another value to choose.
Loading editor...
Waveform not yet available. Run sync-content --examples to generate.