Aller au contenu

Conditionnal structures

if, elsif, else

This kind of conditionnal structure is always inside an explicit process :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
signal A : std_logic_vector(2 downto 0);
signal S : std_logic_vector(2 downto 0);
--
process(A)
begin
    if (A = "000") then
        S <= "000";
    elsif (A = "001") then
        S <= "001";
    elsif (A = "001") then
        S <= "011";
    elsif (A = "010") then
        S <= "011";
    elsif (A = "011") then
        S <= "010";
    elsif (A = "100") then
        S <= "110";
    elsif (A = "101") then
        S <= "111";
    elsif (A = "110") then
        S <= "110";
    elsif (A = "111") then
        S <= "100";
    else
        S<="000";
    end if;
end process;

Warning

Never forget the else at the end when in a combinatorial process

case

This kind of conditionnal structure is always inside an explicit process as well :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Process(A)
Begin
    Case A is
        when "000"  => S <= "000";
        when "001"  => S <= "001";
        when "001"  => S <= "011";
        when "010"  => S <= "011";
        when "011"  => S <= "010";
        when "100"  => S <= "110";
        when "101"  => S <= "111";
        when "110"  => S <= "110";
        when "111"  => S <= "100";
        when others => S <= "000";
    end case;
end process ;

Warning

Never forget the when others at the end when in a combinatorial process

Conditionnal assignment when else

This kind of conditionnal structure is described as implicit process :

1
2
3
4
5
6
7
8
9
S <= "000" when(A = "000")else
     "001" when(A = "001")else
     "011" when(A = "010")else
     "010" when(A = "011")else
     "110" when(A = "100")else
     "111" when(A = "101")else
     "110" when(A = "110")else
     "100" when(A = "111")else
     "000";

This type of assignment is very convenient and readable for the assignment of a binary signal.

1
W <= '1' when(A = "01110" or B = '1' and C = '1')else '0';

Warning

Never forget the else at the end.

Conditionnal assignment with select

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
with A select S <=
    "000" when "000",
    "001" when "001",
    "011" when "010",
    "010" when "011",
    "110" when "100",
    "111" when "101",
    "110" when "110",
    "100" when "111",
    "000" when others;

Warning

Never forget the when others at the end.