library ieee;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity D_TIME is
port(	clk: in std_logic;
		mode1:in std_logic_vector(1 downto 0);
		mode2: in std_logic_vector(1 downto 0);
		increase: in std_logic;
		
		hour: out std_logic_vector(4 downto 0);
		min: out std_logic_vector(5 downto 0);
		sec: out std_logic_vector(5 downto 0);
		dotAmPm: out std_logic;
		hur_carry: out std_logic:='0');
end D_TIME;

architecture behav of D_TIME is 
signal inc_min:std_logic;
signal inc_hur: std_logic;

begin
process(increase, mode1, mode2)
begin
	if increase= '1' and mode1="00" then
		inc_hur <= not(mode2(1)) and mode2(0);
		inc_min <= mode2(1) and not(mode2(0));
	else
		inc_hur <= '0';
		inc_min <= '0';
	end if;
end process;

process(clk, inc_hur, inc_min)
variable v_secc: std_logic_vector(6 downto 0):="0000000";
variable v_sec: std_logic_vector(5 downto 0):="000000";
variable v_min: std_logic_vector(5 downto 0):="000000";
variable v_hur: std_logic_vector(4 downto 0):="00000";
variable v_hur24: std_logic_vector(4 downto 0):="00000";

--Need to change this code to make 12 Hrs time system.
--And need to incorporate Dot Matrix

begin
	if rising_edge(clk) then
		v_secc:=v_secc+1;
		hur_carry<='0';
		if(v_secc="1100100") then	--100
			v_sec:=v_sec+1;
			v_secc:="0000000";
			if(v_sec="111100") then		--60
				v_min:=v_min+1;
				v_sec:="000000";
				if(v_min="111100") then	  --60
					v_hur:=v_hur+1;
					v_min:="000000";
					--testfor12
					
					--if(v_hur="11000") then	--24
					--	v_hur:="00000";
					--	hur_carry<='1';
					--end if;
					
					v_hur24:=v_hur24+1;
					if (v_hur="01101" and v_hur24="01101") then	--13
						v_hur:="00001";
						hur_carry<='0';
						--dotAmPm<='1';
					elsif (v_hur="01100" and v_hur24="11000") then	--12, 24
						v_hur:="00000";	
						v_hur24:="00000";	
						hur_carry<='1';
						--dotAmPm<='0';
					end if;
										
					--testfor12
				end if;
			end if;
		end if;
		

		if(inc_min='1') then
			if(v_min="111011")then	--59
				v_min:="000000";
				----------------
				v_hur:=v_hur+1;
				v_hur24:=v_hur24+1;
				if (v_hur="01101" and v_hur24="01101") then	--13, 13
					v_hur:="00001";
					v_hur24:="01110";		--14
					hur_carry<='0';
					--dotAmPm<='1';
				elsif (v_hur="01100" and v_hur24="11000") then	--12, 24
					v_hur:="00000";	
					v_hur24:="00000";	
					hur_carry<='1';
					--dotAmPm<='0';
				end if;
				-----------------
			else
				v_min:=v_min+1;
			end if;
		elsif(inc_hur='1') then
			
			--testfor change in inc
			
			--if(v_hur="10111") then	--23
			--	v_hur:="00000";
			--else
			--	v_hur:=v_hur+1;
			--end if;
			
			if (v_hur="01100" and v_hur24="01100") then	--12
				v_hur:="00001";
				v_hur24:="01101";	--13
				hur_carry<='0';
			elsif (v_hur="01011" and v_hur24="10111") then	--11, 23
				v_hur:="00000";	
				v_hur24:="00000";	
				hur_carry<='1';
			else
				v_hur:=v_hur+1;
				v_hur24:=v_hur24+1;
			end if;	
			--testfor change in inc
			
		end if;
	
		hour<=v_hur;
		min<=v_min;
		sec<=v_sec;
		
		--dotAmPm<='0'; --TEST
		if (v_hur24 > "01011") then
			dotAmPm<='1';	--PM
		else
			dotAmPm<='0';	--AM
		end if;
				
	end if;
end process;
end behav;