-----------------------------------------------------------------------------------------------------
--File: stopWatch
--Author: 	Robert Ross (c) 2004
--Purpose: 	generates a 10ms clk used to drive the stopwatch and the rest of the counters
--Inputs:	
--	clkIn: 100Hz clock (T = 10ms)
--	startStop: toggles start/stop
--	resetGlobal: global reset for board
--	resetCount: resets the count when in stop mode to 00:00:00:00
----------------------------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;

entity stopWatch is port(
	clkIn: in std_logic;					--100Hz
	startStop: in std_logic;				--toggles start/stop
	resetGlobal: in std_logic;				--global reset for board
	resetCount: in std_logic;				--resets the count when in stop mode to 00:00:00:00
	modeActiveL: in std_logic;				--if 1 this is active and the buttons apply, if 0 is inactive and startStop doesn't work
	msecLSD: buffer integer range 0 to 9;
	msecMSD: buffer integer range 0 to 9;
	secLSD: buffer integer range 0 to 9;
	secMSD: buffer integer range 0 to 9;
	minLSD: buffer integer range 0 to 9;
	minMSD: buffer integer range 0 to 9;
	hourLSD: buffer integer range 0 to 9;
	hourMSD: buffer integer range 0 to 9);			--will only get up to 2, but display function requires 0 to 9
end stopWatch;

architecture arch of stopWatch is
--	signal counting: std_logic;					--is 1 when the stopwatch is counting, 0 when it is stopped
--	signal oldStartStop: std_logic;
--	signal dbCount: integer range 0 to 3;
begin
countUp:process(clkIn)
begin
	if (clkIn'event and clkIn = '1') then
		if (resetGlobal = '1' OR (resetCount = '1' AND startStop = '0' AND modeActiveL = '1')) then
			msecLSD <= 0;
			msecMSD <= 0;
			secLSD <= 0;
			secMSD <= 0;
			minLSD <= 0;
			minMSD <= 0;
			hourLSD <= 0;
			hourMSD <= 0;
		else
			if (startStop = '1') then
--			if (counting = '1') then
				if (msecLSD = 9) then
					msecLSD <= 0;

					if (msecMSD = 9) then
						msecMSD <= 0;

						if (secLSD = 9) then
							secLSD <= 0;

							if (secMSD = 5) then
								secMSD <= 0;

								if (minLSD = 9) then
									minLSD <= 0;

									if (minMSD = 5) then
										minMSD <= 0;

										if (hourLSD = 9) then
											hourLSD <= 0;

											if (hourMSD = 9) then
												hourMSD <= 0;
											else
												hourMSD <= hourMSD + 1;
											end if;
										else
											hourLSD <= hourLSD + 1;
										end if;
									else
										minMSD <= minMSD + 1;
									end if;
								else
									minLSD <= minLSD + 1;
								end if;
							else
								secMSD <= secMSD + 1;
							end if;
						else
							secLSD <= secLSD + 1;
						end if;
					else
						msecMSD <= msecMSD + 1;
					end if;
				else
					msecLSD <= msecLSD + 1;	
				end if;
			end if;
		end if;
	end if;
end process;

--startStopSettings:process(clkIn)
--begin
--	if (clkIn'event and clkIn = '1') then
--		if ((counting /= '1') AND (counting /= '0')) then
--			counting <= '0';
--		end if;
--		if (modeActiveL = '1') then			--if the clock or timer is being used the start/stop will not work
--			oldStartStop <= startStop;
--			if ((startStop = '1') AND (startStop /= oldStartStop)) then
--				counting <= NOT counting;	--toggle between start and stop
--			end if;
--			if (resetGlobal = '1' OR (resetCount = '1' AND counting = '0' AND modeActiveL = '1')) then
--				counting <= '0';
--			end if;
--		end if;
--	end if;
--end process;	

end;
