--Engineer: Summer Rutherford, Regita Soetandar --Description: reaction game using LED lights and switches library IEEE; use IEEE.STD_LOGIC_1164.ALL; -- Uncomment the following library declaration if using -- arithmetic functions with Signed or Unsigned values use IEEE.NUMERIC_STD.ALL; -- Uncomment the following library declaration if instantiating -- any Xilinx leaf cells in this code. --library UNISIM; --use UNISIM.VComponents.all; entity alternatingLED is Port ( Clk : in STD_LOGIC; --Button : in STD_LOGIC; response : in std_logic_vector (6 downto 0); Reset : in STD_LOGIC; LEDs : out STD_LOGIC_VECTOR (4 downto 0); Anode : out STD_LOGIC_VECTOR (3 downto 0); Seg : out STD_LOGIC_VECTOR (6 downto 0)); end alternatingLED; architecture Behavioral of alternatingLED is type state_type is (S0, S1, S2, S3, S4, S5, S6, S7); signal PS, NS : state_type; component CLKDivide Port ( CLKIn : in STD_LOGIC; D90 : in STD_LOGIC_VECTOR (31 downto 0); CLKOut : out STD_LOGIC); end component; signal D1, D2 : std_logic_vector(31 downto 0); signal newclk : std_logic := '0'; signal Middle : std_logic := '0'; signal LEDT : std_logic_vector(4 downto 0) := "00000"; signal Level : std_logic_vector(2 downto 0) := "000"; signal PushClk : std_logic := '0'; signal Clk400 : std_logic := '0'; signal Stop : std_logic := '0'; signal Final : std_logic := '0'; begin --400 Hz --> D1 <= 1E848 --400 Hz clock for the 7 segment display to display "COOL" --x"003D0900" ClkFinalDisplay : CLKDivide port map (CLKIn => Clk, D90 => x"0001E848", CLKOut => CLK400); ButtonClk : CLKDivide port map (CLKIn => Clk, D90 => x"003EF2C2", CLKOut => PushClk); --use for game --ButtonClk : CLKDivide port map (CLKIn => Clk, D90 => x"00000001", CLKOut => PushClk); --use for simulation --SlowClk is dependent on D1 and is the speed the LEDs run at SlowClk: CLKDivide port map (CLKIn => Clk, D90 => D1, CLKOut => newclk); --use for game --SlowClk: CLKDivide port map (CLKIn => Clk, D90 => x"00000002", CLKOut => newclk); --use for simulation --Shift is the process that uses the new clock and alternates the LEDs Shift : process (newclk) is -- variable Final: unsigned := "0"; variable Track: unsigned (3 downto 0) := "0010"; variable Count: unsigned (2 downto 0) := "001"; begin --without stop if Stop = '1' then LEDT <= "00000"; -- D1 <= x"00000000"; --if stop is being used, change to elsif rising_edge(newclk) --changed rising to falling elsif falling_edge(newclk) then -- if Stop = '1' then -- LEDT <= "00000"; if count = "001" then LEDT <= "00001"; elsif count = "010" then LEDT <= "00010"; elsif count = "011" then LEDT <= "00100"; elsif count = "100" then LEDT <= "01000"; elsif count = "101" then LEDT <= "10000"; --else -- LEDT <= "11111"; end if; -- Track controls when count should count up or down if Track < "0110" then count := count + 1; elsif Track = "1001" then Track := "0001"; count := "001"; --added "and Track < "1001"" elsif Track > "0101" and Track < "1001" then count := count - 1; else LEDT <= "00000"; end if; Track := Track + 1; -- This just makes the LEDs flash on and off when the player has won if Level = "111" then if Final = '1' then LEDT <= "11111"; else LEDT <= "00000"; end if; Final <= not Final; end if; LEDs <= LEDT; Middle <= LEDT(2); end if; end process Shift; sync_proc : process(NS, Reset) is begin if (Reset = '1') then PS <= S0; --changing rising to falling elsif falling_edge(PushClk) then PS <= NS; end if; end process sync_proc; --replaced response with button --added stop in sensitivity list comb_proc : process(PS, response, Middle) is begin -- Level <= "00000"; case PS is when S0 => if Middle = '1' and response = "0000001" then NS <= S1; else NS <= S0; end if; when S1 => if Middle = '1' and response = "0000011" then NS <= S2; Stop <= '1'; elsif Middle = '0' and response = "0000011" then NS <= S0; Stop <= '0'; -- elsif Middle = '1' and Reset = '1' then NS <= S0; Stop <= '0'; else NS <= S1; Stop <= '0'; end if; when S2 => if Middle = '1' and response = "0000111" then NS <= S3; Stop <= '1'; elsif Middle = '0' and response = "0000111" then NS <= S0; Stop <= '0'; -- elsif Middle = '1' and Reset = '1' then NS <= S0; Stop <= '0'; else NS <= S2; Stop <= '0'; end if; when S3 => if Middle = '1' and response = "0001111" then NS <= S4; Stop <= '1'; elsif Middle = '0' and response = "0001111" then NS <= S0; Stop <= '0'; -- elsif Middle = '1' and Reset = '1' then NS <= S0; Stop <= '0'; else NS <= S3; Stop <= '0'; end if; when S4 => if Middle = '1' and response = "0011111" then NS <= S5; Stop <= '1'; elsif Middle = '0' and response = "0011111" then NS <= S0; Stop <= '0'; -- elsif Middle = '1' and Reset = '1' then NS <= S0; Stop <= '0'; else NS <= S4; Stop <= '0'; end if; when S5 => if Middle = '1' and response = "0111111" then NS <= S6; Stop <= '1'; elsif Middle = '0' and response = "0111111" then NS <= S0; Stop <= '0'; -- elsif Middle = '1' and Reset = '1' then NS <= S0; Stop <= '0'; else NS <= S5; Stop <= '0'; end if; when S6 => if Middle = '1' and response = "1111111" then NS <= S7; Stop <= '1'; elsif Middle = '0' and response = "1111111" then NS <= S0; Stop <= '0'; -- elsif Middle = '1' and Reset = '1' then NS <= S0; Stop <= '0'; else NS <= S6; Stop <= '0'; end if; when S7 => if Reset = '1' then NS <= S0; Stop <= '0'; else NS <= S7; Stop <= '0'; end if; -- when others => -- NS <= S0; Stop <= '0'; end case; end process comb_proc; with PS select Level <= "000" when S0, "001" when S1, "010" when S2, "011" when S3, "100" when S4, "101" when S5, "110" when S6, "111" when S7, "000" when others; -- Display for 8 states and a final state that displays "COOL" Display : process (Level, Reset, Clk400) is variable count : unsigned (1 downto 0) := "00"; begin if Reset = '1' then Anode <= "1110"; Seg <= "1001111"; elsif Level = "000" then Anode <= "1110"; Seg <= "1001111"; elsif Level = "001" then Anode <= "1110"; Seg <= "0010010"; elsif Level = "010" then Anode <= "1110"; Seg <= "0000110"; elsif Level = "011" then Anode <= "1110"; Seg <= "1001100"; elsif Level = "100" then Anode <= "1110"; Seg <= "0100100"; elsif Level = "101" then Anode <= "1110"; Seg <= "0100000"; elsif Level = "110" then Anode <= "1110"; Seg <= "0001111"; elsif Level = "111" then if rising_edge(Clk400) then if count = "00" then Anode <= "1110"; Seg <= "1110001"; elsif count = "01" then Anode <= "1101"; Seg <= "0000001"; elsif count = "10" then Anode <= "1011"; Seg <= "0000001"; elsif count = "11" then Anode <= "0111"; Seg <= "0110001"; end if; count := count + 1; end if; end if; end process Display; -- Speed is the process to determine which divider number goes into the clkdivider -- It is dependent on Level Speed : process(Level) is begin --if Level = "000" then -- D1 <= x"02FAf080"; --initial freq values --current freq vals if Level = "000" then D1 <= x"023c3460"; --37500000 --37500000 elsif Level = "001" then D1 <= x"01E84800"; --32500000 --32000000 elsif Level = "010" then D1 <= x"01945BA0"; --25000000 --26500000 elsif Level = "011" then D1 <= x"01406F40"; --12500000 --21000000 elsif Level = "100" then D1 <= x"00989680"; --7500000 --15500000 00EC82E0 > 10000000 elsif Level = "101" then D1 <= x"0044AA20"; --5000000 --10000000 00989680 > 4500000 elsif Level = "110" then D1 <= x"002DC6C0"; --4000000 --4500000 0044AA20 > 3000000 002DC6C0 else D1 <= x"01406F40"; --12500000 --21000000 -- D1 <= x"03B9ACA0"; -- D2 <= x"017d7840"; -- D2 <= x"001e8480"; end if; end process Speed; end Behavioral;