Case statement in process in VHDL - Time & Space Complexity
We want to see how the time to run a VHDL process with a case statement changes as input size changes.
Specifically, how does the number of operations grow when the case statement handles more options?
Analyze the time complexity of the following code snippet.
process(sel, data_in)
begin
case sel is
when 0 => output <= data_in(0);
when 1 => output <= data_in(1);
when 2 => output <= data_in(2);
when others => output <= '0';
end case;
end process;
This process selects one bit from data_in based on sel using a case statement.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The case statement checks sel against each possible value.
- How many times: It compares sel to each case option once per process activation.
As the number of case options grows, the process checks more conditions before deciding.
| Input Size (number of case options) | Approx. Operations |
|---|---|
| 4 | 4 comparisons |
| 10 | 10 comparisons |
| 100 | 100 comparisons |
Pattern observation: The number of comparisons grows directly with the number of case options.
Time Complexity: O(n)
This means the time to decide grows linearly with the number of case options.
[X] Wrong: "The case statement always runs in constant time no matter how many options it has."
[OK] Correct: Each option must be checked until a match is found, so more options mean more checks.
Understanding how case statements scale helps you reason about hardware design efficiency and timing.
"What if we replaced the case statement with a parallel if-elsif chain? How would the time complexity change?"
Practice
case statement inside a VHDL process?Solution
Step 1: Understand the role of case statement
Thecasestatement chooses different actions depending on the value of a signal.Step 2: Compare with other options
Declaring signals, looping, or assigning defaults are not the main purpose ofcase.Final Answer:
To select actions based on the value of a signal -> Option AQuick Check:
Case statement = select actions by signal value [OK]
- Thinking case declares signals
- Confusing case with loops
- Assuming case assigns default values
sel?Solution
Step 1: Recall VHDL case syntax
The correct syntax to start a case statement iscase signal_name is.Step 2: Check each option
case sel is matches the correct syntax. Options A, B, and D are incorrect VHDL syntax.Final Answer:
case sel is -> Option DQuick Check:
Correct case syntax = "case signal is" [OK]
- Using 'switch' instead of 'case'
- Adding parentheses or 'then' incorrectly
- Confusing case with if statement
out_signal when sel = "10"?
process(sel)
begin
case sel is
when "00" => out_signal <= '0';
when "01" => out_signal <= '1';
when "10" => out_signal <= '0';
when others => out_signal <= '1';
end case;
end process;Solution
Step 1: Find matching case branch for sel = "10"
The case statement has a branchwhen "10" => out_signal <= '0';which matches the input.Step 2: Determine assigned value
Forsel = "10",out_signalis assigned '0'.Final Answer:
'0' -> Option CQuick Check:
sel "10" sets out_signal to '0' [OK]
- Choosing default 'others' branch incorrectly
- Confusing '0' and '1' assignments
- Assuming syntax error without checking code
process(sel)
begin
case sel is
when "00" => out_signal <= '0';
when "01" => out_signal <= '1';
when others => out_signal <= '0';
end case;
end process;Solution
Step 1: Check syntax and completeness
The case statement covers "00", "01", and useswhen othersto cover all other values.Step 2: Verify syntax correctness
Semicolons and keywords are correctly placed; no syntax errors found.Final Answer:
No error, code is correct -> Option AQuick Check:
Proper use of 'others' covers missing cases [OK]
- Thinking all cases must be listed explicitly
- Assuming missing branch causes error without 'others'
- Misplacing semicolons
out_signal to '1' only when sel is "01" or "11", and '0' otherwise. Which case statement inside the process correctly implements this?Solution
Step 1: Identify required conditions
Setout_signalto '1' only forsel = "01"orsel = "11", else '0'.Step 2: Check each option for correctness
case sel is when "01" | "11" => out_signal <= '1'; when others => out_signal <= '0'; end case; uses invalid syntax with '|' for multiple choices in VHDL case. case sel is when "01" => out_signal <= '1'; when "11" => out_signal <= '1'; when others => out_signal <= '0'; end case; lists both cases separately, which is correct. case sel is when "01" => out_signal <= '1'; when others => out_signal <= '0'; end case; misses "11" case. case sel is when "01" => out_signal <= '1'; when "10" => out_signal <= '1'; when others => out_signal <= '0'; end case; incorrectly uses "10" instead of "11".Final Answer:
Option B with separate when clauses for "01" and "11" -> Option BQuick Check:
Separate when lines for each value = correct VHDL [OK]
- Using '|' to combine cases (invalid in VHDL)
- Missing one required case
- Confusing similar bit patterns
Ready to prove it?
Pass the test and this pattern moves unknown
