----------------------------------------------------------------------------------
-- Company: 
-- Engineer: 
-- 
-- Create Date: 06/22/2025 01:39:33 PM
-- Design Name: 
-- Module Name: CodeGenerator - Behavioral
-- Project Name: 
-- Target Devices: 
-- Tool Versions: 
-- Description: 
-- 
-- Dependencies: 
-- 
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
-- 
----------------------------------------------------------------------------------


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity CodeGenerator is
    Port ( clk : in STD_LOGIC;
           reset : in STD_LOGIC;
           r : in STD_LOGIC;
           g : in STD_LOGIC;
           b : in STD_LOGIC;
           opened : out STD_LOGIC;
           locked : out STD_LOGIC);
end CodeGenerator;

architecture Behavioral of CodeGenerator is
type statetype is (idle, red1, green, blue, red2);
signal state: statetype;

begin
--state register
    process (clk, reset) begin
    if (reset = '1') then state <= idle;
    elsif rising_edge(clk) then 
    case (state) is
        when idle =>
            if (r ='1') then state <= red1;
            end if;
        when red1 =>
            if (b ='1') then state <= blue;
            elsif (r = '1') then state <= red1;
            elsif (g = '1') then state <= idle;
            end if;    
        when blue =>
            if (g ='1') then state <= green;
            elsif (b = '1') then state <= blue;
            elsif (r = '1') then state <= idle;
            end if;
       when green =>
            if (r ='1') then state <= red2;
            elsif (g = '1') then state <= green;
            elsif (b = '1') then state <= idle;
            end if;
       when red2 =>
           state <= red2;
       when others =>
           state <= idle;
    end case;
    end if;
    end process;

opened <= '1' when state = red2 else '0';  --LED1 is used to show open
locked <= '0' when state = red2 else '1';  --LED0 is used to show locked

end Behavioral;
