Sign in

Learn

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

Practice

Know this already? Skip to the exercise: Battery Charge Controller

Conditional Assignment

A conditional assignment chooses which value drives an output. It describes selection hardware that responds to its inputs rather than a stored decision, and it builds on concurrent assignments.

A priority problem

A fan must follow this temperature rule:

Temperature in degrees CelsiusSpeed code
Below 1800, off
18 through 2401, slow
25 through 3410, medium
35 or higher11, fast

A candidate checks temperature >= 18 first and immediately selects slow when that is true, and only otherwise does it check the higher thresholds.

Quick check

Find the input that breaks it

Question 1 of 1

degrees Celsius

Select between two alternatives

A simple sensor selector illustrates the syntax:

shown_o <= main_i when ok_i = '1' else backup_i;

Read when ... else as the condition selecting the preceding value or the following alternative. It remains one concurrent assignment and one output driver.

Both inputs can exist at the same time, and the selector is a multiplexer, often shortened to mux. No clock is needed for this combinational decision.

Overlapping conditions and priority

At 40 degrees all three threshold conditions are true, so the required highest speed must take priority.

speed_o <= "11" when temp_i >= 35 else
           "10" when temp_i >= 25 else
           "01" when temp_i >= 18 else
           "00";

The first true condition supplies the result, and later conditions handle only the inputs not already selected. Synthesis may optimize the logic, but the required priority must remain the same. The final fallback covers temperatures below every threshold.

Keep the unconditional final alternative, because an incomplete combinational assignment can retain its previous output and infer storage.

Suppose a maintenance input must force the fan off even at 40 degrees. Would you check it before or after the temperature choices? The override must win over every matching threshold. This is a design question about a changed spec; the shipped example has no such input.

Reuse the pattern

The shared-pin recipe chooses between driving a value and releasing the pin:

line_io <= value_i when enable_i = '1' else 'Z';

Here Z means the driver releases the shared connection. Real tri-state support depends on where that connection exists in the target device.

A comparison can also drive a flag:

over_o <= '1' when total_o > LIMIT else '0';

The conditional form converts the comparison's Boolean result into a logic value.

Verify the boundary and the overlap

Open Fan Speed Selector in the example panel on the right. Its fan_speed source implements the required threshold order.

Before reading each output, work out the codes at 24, 25, 34, and 35 degrees. The waveform changes speed exactly at 25 and 35, and at 40 speed_o remains 11 despite all three comparisons being true.

Cooling back through 30, 20, and 10 gives medium, slow, then off, with no memory of the previous higher speed.

Key Takeaways

  • A conditional assignment selects an output from live alternatives.
  • Overlapping conditions require deliberate priority, with the first true condition winning.
  • Test boundaries and overlapping conditions rather than a single ordinary input per design.
Loading editor...
No waveform is available for this example. You can continue with the lesson and example code.