📖 Scenario: You are designing a simple digital circuit that outputs a specific 4-bit value based on a 2-bit input selector. This is a common task in digital design where you want to choose one of several values depending on a control signal.
🎯 Goal: Build a VHDL code snippet that uses a with-select statement to assign a 4-bit output signal based on a 2-bit input selector signal.
📋 What You'll Learn
Create a 2-bit input signal called sel.
Create a 4-bit output signal called out_val.
Use a with sel select statement to assign out_val based on sel.
Assign these exact values for out_val: "0001" for sel = "00", "0010" for sel = "01", "0100" for sel = "10", and "1000" for sel = "11".
💡 Why This Matters
🌍 Real World
Selected assignment with <code>with-select</code> is used in digital circuits to choose one output from many inputs based on control signals, like multiplexers.
💼 Career
Understanding <code>with-select</code> statements is essential for hardware engineers and FPGA designers to write clear and efficient VHDL code.
Progress0 / 4 steps
1
Create input and output signals
Declare a 2-bit input signal called sel and a 4-bit output signal called out_val inside an architecture.
VHDL
Hint
Remember, input and output signals are declared in the entity port list.
2
Add with-select statement
Inside the architecture body, write a with sel select statement to assign out_val based on sel.
VHDL
Hint
Use the syntax: with sel select out_val <= value when condition, ...;
3
Assign exact values for each selector case
Complete the with sel select statement by assigning out_val the exact values: "0001" for sel = "00", "0010" for sel = "01", "0100" for sel = "10", and "1000" for sel = "11".
VHDL
Hint
Make sure each when condition matches the exact selector value and output.
4
Test output with a print statement (simulation)
Add a simple process to print the value of out_val for each sel value in a testbench style process. Print the output using report statements for sel values "00", "01", "10", and "11".
VHDL
Hint
Use a process with a loop to assign sel values and print out_val using report.
Practice
(1/5)
1. What is the main purpose of a with-select statement in VHDL?
easy
A. To create a loop that repeats a set of statements
B. To declare a new signal in the architecture
C. To assign a value to a signal based on the value of another signal
D. To define a new data type
Solution
Step 1: Understand the role of with-select
The with-select statement chooses a value for a signal depending on another signal's value.
Step 2: Compare with other options
Loops, declarations, and data types are unrelated to with-select.
Final Answer:
To assign a value to a signal based on the value of another signal -> Option C
Quick Check:
Selected assignment = conditional signal assignment [OK]
Hint: Remember: with-select picks signal values based on another signal [OK]
Common Mistakes:
Confusing with loops or process statements
Thinking it declares signals
Mixing with data type definitions
2. Which of the following is the correct syntax for a with-select assignment in VHDL?
easy
A. with sel select out = "00" when "00", "01" when "01", others => "11";
B. with sel select out <= "00" when "00", "01" when "01", others => "11";
C. with sel select out <= "00" when => "00", "01" when => "01", others => "11";
D. with sel select out <= "00" when "00" "01" when "01", others => "11";
Solution
Step 1: Check the syntax format
The correct syntax uses commas between choices and when others => for default.
Step 2: Identify the correct arrow symbol
The assignment uses <= and the choices use => after when.
Final Answer:
with sel select out <= "00" when "00", "01" when "01", others => "11"; -> Option B
Quick Check:
Correct syntax uses commas and 'others =>' [OK]
Hint: Look for commas between choices and 'others =>' for default [OK]
Common Mistakes:
Using wrong arrow symbols
Missing commas between choices
Omitting 'others =>' clause
3. Given the following VHDL code:
with sel select
out <= "00" when "00",
"01" when "01",
"10" when "10",
others => "11";
sel = "10";
What will be the value of out?
medium
A. "10"
B. "01"
C. "00"
D. "11"
Solution
Step 1: Match sel value with choices
The value of sel is "10", which matches the third choice.
Step 2: Determine assigned value
When sel is "10", out is assigned "10".
Final Answer:
"10" -> Option A
Quick Check:
sel = "10" means out = "10" [OK]
Hint: Match sel value exactly to get out value [OK]
Common Mistakes:
Choosing default 'others' instead of exact match
Confusing when clauses
Ignoring quotes around values
4. Identify the error in this with-select statement:
with sel select
out <= "00" when "00",
"01" when "01",
"10" when "10";
medium
A. Missing semicolon at the end
B. Incorrect use of commas between choices
C. Wrong assignment operator used
D. Missing when others => clause
Solution
Step 1: Check completeness of choices
The with-select must cover all possible values, including unexpected ones.
Step 2: Identify missing default case
There is no when others => clause to cover unmatched values.
Final Answer:
Missing when others => clause -> Option D
Quick Check:
Always include 'when others =>' in with-select [OK]
Hint: Always add 'when others =>' to cover all cases [OK]
Common Mistakes:
Forgetting the default 'others' clause
Thinking semicolon is missing
Misusing commas or arrows
5. You want to assign a 2-bit output signal out based on a 2-bit input sel using with-select. The mapping is: - "00" -> "11" - "01" -> "10" - "10" -> "01" For any other value, out should be "00".
Which of the following with-select statements correctly implements this?
hard
A. with sel select
out <= "11" when "00",
"10" when "01",
"01" when "10",
others => "00";
B. with sel select
out <= "00" when "00",
"01" when "01",
"10" when "10",
others => "11";
C. with sel select
out <= "11" when "00",
"10" when "01",
"01" when "10";
D. with sel select
out <= "11" when "00",
"10" when "01",
"01" when "10",
others => "11";
Solution
Step 1: Match each sel value to correct out
"00" maps to "11", "01" to "10", "10" to "01" as given.
Step 2: Check the default others value
For any other sel, out should be "00".
Step 3: Verify options
with sel select
out <= "11" when "00",
"10" when "01",
"01" when "10",
others => "00"; matches all mappings and default correctly.
Final Answer:
with sel select
out <= "11" when "00",
"10" when "01",
"01" when "10",
others => "00"; -> Option A
Quick Check:
Correct mapping + others = "00" [OK]
Hint: Check each mapping and default carefully in with-select [OK]
Common Mistakes:
Missing 'others' clause
Swapping output values
Assigning wrong default value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.