0
0
VHDLprogramming~5 mins

Selected assignment (with-select) in VHDL

Choose your learning style9 modes available
Introduction

Selected assignment lets you choose a value for a signal based on different conditions in a clear and simple way.

When you want to set a signal to different values depending on another signal's value.
When you have multiple options and want to pick one cleanly without many if-else statements.
When designing hardware logic that depends on a control signal to select outputs.
When you want your code to be easy to read and maintain by clearly showing choices.
Syntax
VHDL
with <selector_signal> select
    <target_signal> <= <value1> when <choice1>,
                      <value2> when <choice2>,
                      ...
                      <default_value> when others;

The selector_signal is the signal you check to decide the value.

The target_signal is the signal you assign a value to based on the selector.

Examples
This example sets out to different 4-bit values depending on the 2-bit sel signal.
VHDL
with sel select
  out <= "0001" when "00",
         "0010" when "01",
         "0100" when "10",
         "1000" when others;
Here, led turns on only if mode is "on"; otherwise, it is off.
VHDL
with mode select
  led <= '1' when "on",
         '0' when others;
Sample Program

This VHDL code uses selected assignment to set the 4-bit output out based on the 2-bit input sel. It picks one of four values depending on sel.

VHDL
library ieee;
use ieee.std_logic_1164.all;

entity selected_assign is
  port(
    sel : in std_logic_vector(1 downto 0);
    out : out std_logic_vector(3 downto 0)
  );
end selected_assign;

architecture behavior of selected_assign is
begin
  with sel select
    out <= "0001" when "00",
           "0010" when "01",
           "0100" when "10",
           "1000" when others;
end behavior;
OutputSuccess
Important Notes

Always include a when others clause to cover unexpected values.

Selected assignment is combinational logic; it updates output immediately when selector changes.

Summary

Selected assignment chooses a value based on a selector signal.

It is cleaner than many if-else statements for multiple choices.

Always use when others to handle all cases.