Conditional assignment (when-else) in VHDL - Time & Space Complexity
We want to understand how the time to run a conditional assignment in VHDL changes as inputs change.
Specifically, does the number of conditions affect how long it takes to decide the output?
Analyze the time complexity of the following code snippet.
signal a, b, c, d : std_logic;
signal sel : std_logic_vector(1 downto 0);
signal out_signal : std_logic;
out_signal <= a when sel = "00" else
b when sel = "01" else
c when sel = "10" else
d;
This code chooses one signal to assign to out_signal based on the value of sel.
- Primary operation: Checking conditions in sequence to find a match.
- How many times: At most, it checks each condition once in order until one matches.
As the number of conditions grows, the checks increase linearly.
| Input Size (number of conditions) | Approx. Operations (condition checks) |
|---|---|
| 2 | Up to 2 checks |
| 5 | Up to 5 checks |
| 10 | Up to 10 checks |
Pattern observation: The number of checks grows directly with the number of conditions.
Time Complexity: O(n)
This means the time to decide the output grows in a straight line with the number of conditions.
[X] Wrong: "The conditional assignment runs in constant time no matter how many conditions there are."
[OK] Correct: Because each condition is checked in order, more conditions mean more checks, so time grows with the number of conditions.
Understanding how condition checks add up helps you reason about hardware design speed and efficiency, a useful skill in many design tasks.
"What if we replaced the when-else chain with a case statement? How would the time complexity change?"
Practice
when-else statement do?Solution
Step 1: Understand the purpose of when-else
The when-else statement is used to assign values conditionally to signals in VHDL.Step 2: Compare with other options
Loops, signal declarations, and process blocks serve different purposes unrelated to conditional assignment.Final Answer:
Assigns a value to a signal based on a condition -> Option AQuick Check:
when-else = conditional assignment [OK]
- Confusing when-else with loops
- Thinking it declares signals
- Mixing it with process blocks
when-else in VHDL?Solution
Step 1: Recall VHDL assignment operator
In VHDL, signal assignment uses the '<=' operator, not ':=' or '='.Step 2: Check the full syntax
The correct syntax is: signal_name <= value1 when condition else value2;Final Answer:
signal_name <= value1 when condition else value2; -> Option BQuick Check:
Use '<=' for signal assignment with when-else [OK]
- Using ':=' instead of '<=' for signals
- Using '=' which is for variables
- Confusing arrow operators
signal out_signal : std_logic; out_signal <= '1' when input_signal = '1' else '0';
What will be the value of
out_signal if input_signal is '0'?Solution
Step 1: Analyze the condition
The assignment says out_signal is '1' when input_signal = '1', else '0'.Step 2: Apply input value
Since input_signal is '0', the condition is false, so out_signal gets '0'.Final Answer:
'0' -> Option AQuick Check:
Condition false -> else value '0' assigned [OK]
- Assigning '1' when input is '0'
- Confusing high impedance 'Z' with '0'
- Thinking output is undefined
signal result : std_logic; result <= '1' when input = 1 else '0';
Solution
Step 1: Check the condition type
The input signal is likely std_logic, so it should be compared to a character like '1', not integer 1.Step 2: Identify correct comparison
Correct comparison is input = '1' (with single quotes) for std_logic.Final Answer:
Input comparison uses integer instead of character -> Option DQuick Check:
Compare std_logic with character '1' not integer 1 [OK]
- Comparing std_logic to integer without quotes
- Confusing assignment operators
- Assuming signal names are restricted
y based on a 2-bit input sel using conditional assignment. If sel is "00", assign "0001"; if "01", assign "0010"; else assign "0000". Which is the correct VHDL code?Solution
Step 1: Recall syntax for multiple conditions
In VHDL, chain conditional assignments as: value1 when cond1 else value2 when cond2 else value3. No parentheses needed.Step 2: Compare options
y <= "0001" when sel = "00" else "0010" when sel = "01" else "0000"; matches the correct chained syntax.Final Answer:
y <= "0001" when sel = "00" else "0010" when sel = "01" else "0000"; -> Option CQuick Check:
Chained when-else without parentheses [OK]
- Using := for signal assignment
- Adding unnecessary parentheses around conditions
- Using single quotes for std_logic_vector literals
Ready to prove it?
Pass the test and this pattern moves unknown
