-----------------------------------------------------------------------------------------------------
--File: clk10ms
--Author: 	Robert Ross (c) 2004
--Purpose: 	generates a 10ms clk used to drive the stopwatch and the rest of the counters
--Inputs:	
--	clkIn: 25.125MHz onboard clock
--	reset: reset button
-----------------------------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;

entity clk100Hz is port(
	clk: in std_logic;					--25.125MHz
	startStop: in std_logic;			--startStop button
	buttonMode: in std_logic;			--button to select the mode
end clk100Hz;

architecture arch of clk100Hz is
	signal count10ms: integer range 0 to 251250;	--should give 10ms delay
	signal clk10ms: std_logic;
	signal dbCount: integer range 0 to 200;
	signal dbCount2: integer range 0 to 200;
	signal modeSelect: std_logic_vector(0 to 2);
	signal settingMode: std_logic; -- when 0 not in setting mode when 1 in setting mode
	signal reset: std_logic; --when 1 reset the circuit
begin
	countMain:process(clk)
	begin
		if (clk'event and clk = '1') then
			if (reset = '1') then	--synchronous reset
				--reset all counts
				modeSelect <= "001";--in clock mode
				settingMode <= '1';	--in setting mode
			else
				if (count10ms = 251250) then
					count10ms <= 0;
					clk10ms <= NOT clk10ms;
				else
					count10ms <= count10ms + 1;
				end if;
			end if;
		end if;			
	end process
	
	selectMode:process(clk10ms)
	begin
		if (clk10ms'event and clk10ms = '1') then
			reset <= '0'; --inital value
			if (buttonMode = '1') then --button is pressed
				if (stopStart = '1') then --stopStart button pressed
					if (dbCount2 /= 200) then
						dbCount2 <= dbCount2 + 1;
					end if;
					if (dbCount /= 200) then	--2 second debounce
						dbCount <= dbCount + 1;
					end if;
					if ((dbCount = 199) and (modeSelect /= "100")) then
						settingMode <= NOT settingMode; --invert setting
					end if;
					if ((dbCount = 200) and (dbCount2 = 200)) then
						reset <= '1';	--global reset of whole device
					end if;
			else
				if ((dbCount /= 200) AND (dbCount > 3)) then
					if (settingMode = '1') then
						--rotate through digits
					else
						modeSelect <= rol modeSelect; --rotate left through modes
					end if;
				end if;
				dbCount <= 0;
			end if;
		end if;
	end process;
	
end arch;

