Sign in

Learn

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

Practice

Try the challenge first: Battery Charge Controller

Know this topic? Give it a go. The lesson is here if you need it.

Conditional Assignment

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

Diagnose the priority before writing syntax

A fan must follow this temperature contract:

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 true. Only otherwise does it check the higher thresholds.

Find the smallest revealing input

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. This remains one concurrent assignment and one output driver.

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

Order overlapping conditions deliberately

At 40 degrees, all three threshold conditions are true. 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. Later conditions handle only inputs not already selected. Synthesis may optimize the logic; the required priority must remain the same.

The final fallback covers temperatures below every threshold.

Keep the unconditional final alternative. 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 changed-spec design question, not an extra input in the shipped example.

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, predict the codes at 24, 25, 34, and 35 degrees. The waveform changes speed exactly at 25 and 35. At 40, speed_o remains 11, despite all three comparisons being true.

Cooling back through 30, 20, and 10 gives medium, slow, then off. There is 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, not only one ordinary input per design.
Loading editor...
No waveform is available for this example. You can continue with the lesson and example code.