library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;

PACKAGE alarm_package IS
	COMPONENT alarm
		port(
		clk, switch	: in std_logic;
		in_min	: in std_logic_vector(5 downto 0);
		in_hr	: in std_logic_vector(4 downto 0);
		al_sound : out std_logic
		);

	END COMPONENT;

END alarm_package;



library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;



entity alarm is
	port(
		clk, switch, snoozeb: in std_logic;
		in_min	: in std_logic_vector(5 downto 0);
		in_hr	: in std_logic_vector(4 downto 0);
		ina_min	: in std_logic_vector(5 downto 0);
		ina_hr	: in std_logic_vector(4 downto 0);
		al_sound : out std_logic
		);
end alarm;

architecture behaviour of alarm is


type state_type is (sound, snooze);
signal state: state_type;
signal next_state	:state_type;
signal temp, alm, n: std_logic_vector (5 downto 0);
signal k  : std_logic;
signal l: std_logic := '0';
begin


change		:process(switch, state, clk)
	
	begin
	
	if clk'EVENT and clk='1' then
		n <= in_min;
		IF switch = '0' then
			al_sound <= '0';
		elsif  switch = '1' then
		
			if (in_hr = ina_hr ) or (k = '1') then
				if (in_min = ina_min) or (k = '1') then
					
					state <= sound;
					case state is

				  		  when sound =>

						if ( snoozeb = '0') and (l = '1') then
							state <= snooze;
						elsif snoozeb = '1' then

							temp <= in_min + "001010";
							al_sound <= '0';
							state <= snooze;

						elsif (snoozeb = '0') and (l = '0') then
							al_sound <= '1';					
						end if;

					  when snooze =>	
										
						if temp > "111011" then
							alm <= temp - "111100";
						else
							alm <= temp;
						end if;
										
						if (n = alm) then
							l <= '0';
							state <= sound;
						else
							al_sound <= '0';
							l <= '1';
						end if;

					when others => NULL;				
		      end case; 
			k <= '1';
			end if;
	  	end if;
	end if;
  end if;
end process change;
end behaviour;


