-- G. Gora
-- MSWSiS 2023


library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity BlinkingLed is
	port(
		clk : in std_logic;
		reset_n : in std_logic := '1';
		enable : in std_logic := '0';
		
		q : out std_logic
	);
end entity;


architecture BlinkingLed of BlinkingLed is

	signal cnt05hz : unsigned(27 downto 0) := (others => '0');
	
	signal clk05hz : std_logic := '0';
	
	
	constant CLK05HZ_MAX : unsigned(27 downto 0) := x"098967F";
	constant CLK05HZ_P : unsigned(27 downto 0) := x"04C4B3F";
	

begin

	process(clk, reset_n)
	begin
		if(reset_n = '0')then
			cnt05hz <= (others => '0');
		elsif rising_edge(clk) then
			if(enable = '1')then
				if(cnt05hz < CLK05HZ_MAX)then
					cnt05hz <= cnt05hz + 1;
				else
					cnt05hz <= (others => '0');
				end if;
			end if;
		end if;
	end process;
	

	clk05hz <= '1' when(cnt05hz > CLK05HZ_P) else '0';
	
	
	process(clk, reset_n)
	begin
		if(reset_n = '0')then
			q <= '0';
		elsif rising_edge(clk) then
			q <= clk05Hz;
		end if;
	end process;
	

end architecture;
