Electrical – Replacing if-­‐statements with a case statement

vhdl

I would like to know how to replace an if statement with a case statement.

The if statement is

architecture super_mux_v1 of mux_case is
begin
    process (X,SEL) is
    begin
        if (SEL = "000") then
            Y <= X(0);
        elsif (SEL = "001") then
            Y <= X(1);
        elsif (SEL = "010") then
            Y <= X(2);
        elsif (SEL = "011") then
            Y <= X(3);
        elsif (SEL = "100") then
            Y <= X(4);
        elsif (SEL = "101") then
            Y <= X(5);
        elsif (SEL = "110") then
            Y <= X(6);
        else
            Y <= X(7);
        end if;
    end process;
end super_mux_v1;

My solution can be found below but I am getting some errors (case statement)

architecture super_mux_v1 of mux_case is
begin
    process (X,SEL) is
    begin
        case SEL is
        when "000" => Y <= X(0);
        when "001" => Y <= X(1);
        when "010" => Y <= X(2);
        when "011" => Y <= X(3);
        when "100" => Y <= X(4);
        when "101" => Y <= X(5);
        when "110" => Y <= X(6);
        end case;
    end process;
end super_mux_v1;

When I start compilation it comes up with an error saying

enter image description here

Best Answer

Like the VHDL reference guide says (for instance here)

All possible choices must be included, unless the others clause is used as the last choice

In your if version you used it, since you wrote else .... In your switch one, however, you didn't.

Try writing

    case SEL is
    when "000" => Y <= X(0);
    when "001" => Y <= X(1);
    when "010" => Y <= X(2);
    when "011" => Y <= X(3);
    when "100" => Y <= X(4);
    when "101" => Y <= X(5);
    when "110" => Y <= X(6);
    when others => Y <= X(7);
    end case;

This should fix your problems

EDIT: There is another problem in your code: process (X,SEL) is should be process (X,SEL) (without is)