aboutsummaryrefslogtreecommitdiff
path: root/src/stopwatch.vhd
blob: 01300fc837c34b11b4eeefef12c2944fa0bd7aa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

entity Watch is
	port(
		clk, sysReset, watchRunning, watchReset: in std_logic;
		mins, secs: out std_logic_vector(5 downto 0));
end Watch;

architecture Behavioral of Watch is
begin
	process(clk, sysReset)
		variable subsec: std_logic_vector(26 downto 0) := (others => '0'); -- floor(log2(100_000_000))
		-- = 26 (bits needed to count to 1 second for 100Mhz clock)
		variable minute, second: std_logic_vector(5 downto 0) := (others => '0');
	begin
		if sysReset = '1' then
			subsec := (others => '0');
			second := (others => '0');
			minute := (others => '0');
		elsif rising_edge(clk) then
			if watchRunning = '1' then
				subsec := (subsec + 1);

				-- if subsec is 100_000_000 (second complete)
				if subsec = "101111101011110000100000000" then
					subsec := (others => '0');
					second := (second + 1);

					-- if second is 60 (minute complete)
					if second = "111100" then
						second := (others => '0');
						minute := (minute + 1);
	
						-- if minute is 60 (hour complete)
						if minute = "111100" then
							minute := (others => '0');
						end if;
					end if;
				end if;
			elsif watchReset = '1' then
				subsec := (others => '0');
				second := (others => '0');
				minute := (others => '0');
			end if;
		end if;

		mins <= minute;
		secs <= second;
	end process;
end Behavioral;