I kept getting the error “ Case statement must cover all possible values of expression”. How do I fix this?

58 views Asked by At

Part one of vhdl code for simon game

Part two of vhdl code for simon game

Part three of vhdl code for simon game

I got the case statement must cover all possible values of expression at line 35, "Case display counter is"

This is what I'm trying to accomplish

Please let me know if you see any other errors in the code.

Here is the typed version of the code below

library IEEE;

use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_ARITH.ALL;

use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity SimonGame is

Port ( clk : in STD_LOGIC;

       rst : in STD_LOGIC;

       btn1, btn2, btn3, btn4 : in STD_LOGIC;

       hex_0, hex_1, hex_2 : out STD_LOGIC_VECTOR(6 downto 0));

end SimonGame;


architecture Behavioral of SimonGame is

signal sequence : STD_LOGIC_VECTOR(2 downto 0);

signal count : INTEGER := 0;

signal display_counter : INTEGER := 0;

signal correct_input : BOOLEAN := TRUE;

type Digit_State is (DISPLAY_SEQUENCE, AWAIT_INPUT);

signal state : Digit_State := DISPLAY_SEQUENCE;

begin
process(clk, rst)
begin
    if rst = '1' then
        sequence <= "001"; -- Initial sequence
        count <= 0;
        display_counter <= 0;
        state <= DISPLAY_SEQUENCE;
    elsif rising_edge(clk) then
        case state is
            when DISPLAY_SEQUENCE =>
                -- Display the sequence on hex_0 and hex_1
                case display_counter is
                    when 0 =>
                        hex_0 <= "0000001";
                        hex_1 <= "0000000";
                    when 1 =>
                        hex_0 <= "0001000";
                        hex_1 <= "0000000";
                    when 2 =>
                        hex_0 <= "0000100";
                        hex_1 <= "0000000";
                    when 3 =>
                        hex_0 <= "0000010";
                        hex_1 <= "0000000";
                    when 4 =>
                        hex_0 <= "0001000";
                        hex_1 <= "0000000";
                    when 5 =>
                        hex_0 <= "0000001";
                        hex_1 <= "0000000";
                        state <= AWAIT_INPUT;
                end case;

                if display_counter < 5 then
                    display_counter <= display_counter + 1;
                else
                    display_counter <= 0;
                    count <= count + 1;
                    sequence <= sequence(0) & sequence(2 downto 1); -- Shift the sequence
                end if;

            when AWAIT_INPUT =>
                -- Check button input against the sequence
                if btn1 = '1' then
                    correct_input <= (sequence = "001");
                elsif btn4 = '1' then
                    correct_input <= (sequence = "010");
                elsif btn3 = '1' then
                    correct_input <= (sequence = "100");
                elsif btn2 = '1' then
                    correct_input <= (sequence = "010");
                else
                    correct_input <= FALSE;
                end if;

                if correct_input then
                    state <= DISPLAY_SEQUENCE;
                end if;
        end case;
    end if;
end process;
end Behavioral;
1

There are 1 answers

1
Ada On

A case statement must always cover all possible values that your variable can take. In your case 0 <= display_counter <= 5 is covered, but what if display_counter is smaller than 0 or larger than 5? You have to tell your program what to do in these cases. The general structure looks like this:

case my_variable is
    when value1 =>
        -- do something for value1
    when value2 =>
        -- do something for value2
    when others =>
        -- default action when my_variable doesn't match any defined case
end case;

So I think your problem will be resolved if you add when others with a corresponding action. Not doing anything is also a valid action, that could look something like this:

when others =>
        null;