How to Use With Select Statement in VHDL: Syntax and Example
In VHDL, the
with select statement assigns values to a signal based on the value of a selector expression. It works like a switch-case, where you specify the selector and list possible values with their corresponding outputs, ending with others for default.Syntax
The with select statement assigns a value to a signal depending on the value of a selector expression. It has these parts:
- Selector: The expression you check (like a variable or signal).
- Choices: Possible values the selector can have.
- Assignments: The value assigned to the target signal for each choice.
- Others: A default assignment if none of the choices match.
vhdl
with selector_signal select
target_signal <= value1 when choice1,
value2 when choice2,
-- more choices
default_value when others;Example
This example shows how to use with select to assign a 2-bit output based on a 2-bit input selector.
vhdl
library ieee;
use ieee.std_logic_1164.all;
entity with_select_example is
port(
sel : in std_logic_vector(1 downto 0);
out_signal : out std_logic_vector(1 downto 0)
);
end with_select_example;
architecture Behavioral of with_select_example is
begin
with sel select
out_signal <= "00" when "00",
"01" when "01",
"10" when "10",
"11" when others;
end Behavioral;Common Pitfalls
Common mistakes when using with select include:
- Not covering all possible selector values, which can cause simulation warnings or mismatches.
- Forgetting the
othersclause, which acts as a default case. - Using incompatible types between selector and choices.
- Trying to use
with selectinside a process (it must be outside processes as a concurrent statement).
vhdl
wrong_way:
with sel select
out_signal <= "00" when "00",
"01" when "01"; -- Missing others clause
right_way:
with sel select
out_signal <= "00" when "00",
"01" when "01",
"10" when "10",
"11" when others;Quick Reference
| Part | Description |
|---|---|
| with selector_signal select | Start statement with selector expression |
| target_signal <= | Signal to assign value to |
| value when choice | Assign value when selector equals choice |
| default_value when others | Default assignment for unmatched choices |
Key Takeaways
Use
with select for simple conditional signal assignments based on a selector.Always include the
others clause to cover all possible selector values.with select is a concurrent statement and cannot be used inside processes.Ensure selector and choice values have compatible types.
It works like a clean and readable switch-case for signals in VHDL.