
-------librerias-------------------
-----------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;
------------------------------------

entity cronometro is
	port(clk, reset: in std_logic;
		 puladorlap: in std_logic;
		 display, display1, display2, display3, display4, display5: out std_logic_vector (7 downto 0));
	
end cronometro;

 architecture arq of cronometro is
	signal contador: std_logic_vector(16 downto 0);
	signal contadorlap:std_logic_vector(25 downto 0);
	signal C0, C1, M0, M1, H0, H1: std_logic_vector (3 downto 0):="0000";
	signal C0sig, C1sig, M0sig, M1sig, H0sig, H1si: std_logic_vector (3 downto 0):="0000";
	
 begin


-----------------sincronizacion----------------------------------
-----------------------------------------------------------------
sincronizacion:process(clk, reset)
	if(reset='1') then
		C0 <= "0000";
		C1 <= "0000";
		M0 <= "0000";
		M1 <= "0000";
		H0 <= "0000";
		H1 <= "0000";
	
	elsif (clk' event and clk = '1') then
		C0 <= C0sig;
		C1 <= C1sig;
		M0 <= M0sig;
		M1 <= M1sig;
		H0 <= H0sig;
		H1 <= C0sig;
	end if;
	
end process sincronizacion;
------------------------------------------------------------------		
	
divisor100hz:process(clk)
		begin
				if (clk' event and clk='1') then
					if (contador < "11110100001001000") then
						contador <= contador+'1';
												
					else
						c0sig <= c0+1;							-- aumenta las decimas de segundo
						contador <= "00000000000000000";	-- reinicia contador
						
						if (c0sig="1010") then					--condicion limite 9 para aumentar decima de segundos
							c0sig <= "0000";					--decenas
							c1 <= c1+'1';
						end if;	
						
								if(c1="1010") then			--condicion limite 9 para aumentar segundos
								   M1 <= M1+'1';
								   c1 <= "0000";
								end if;
								
									if (M0="1010") then
										M0 <= "0000";
										M1 <= M1+'1';
									end if;
									
									if (M1="0110") then
										M1 <= "0000";
										H0 <= H0+'1';
									end if;	
										
								if (H0="1010") then
									H0 <= "0000";
									H1 <= H1+'1';
								end if;
								
						if (H1="0110") then
							H1 <= "0000";
						end if;							
						
					end if;
			end if;
		end process;
		
		
-------salidas----------------------------

	process (C0,C1,M0,M1,H0,H1)
	begin
	
	
	if (puladorlap='1') then


--iniciara conteo por 10 seg 0.1Hz al presionar pulsador Lap--
	lap10seg:loop
			if (clk' event and clk='1') then
				contadorlap=contadorlap+'1';
			
				exit lap10seg when contadorlap > "11101110011010110010100000";
				contadorlap="00000000000000000000000000";
	end loop;				
--------------------------------------------------------		
	else
		case C0 is

		  when "0000" => display  <= "00000010";
          when "0001" => display  <= "10011110";
          when "0010" => display  <= "00100100";
          when "0011" => display  <= "00001100";
          when "0100" => display  <= "10011000";
          when "0101" => display  <= "01001000";
          when "0110" => display  <= "01000000";       
          when "0111" => display  <= "00011110";
          when "1000" => display  <= "00000000";
          when others => display  <= "00011000";
         end case;
		
			case c1 is

			when "0000" => display1  <= "00000010";
			when "0001" => display1  <= "10011110";
			when "0010" => display1  <= "00100100";
			when "0011" => display1  <= "00001100";
			when "0100" => display1  <= "10011000";
			when "0101" => display1  <= "01001000";
			when "0110" => display1  <= "01000000";       
			when "0111" => display1  <= "00011110";
			when "1000" => display1  <= "00000000";
			when others => display1  <= "00011000";
			end case;
         
				case M0 is

				when "0000" => display2  <= "00000010";
				when "0001" => display2  <= "10011110";
				when "0010" => display2  <= "00100100";
				when "0011" => display2  <= "00001100";
				when "0100" => display2  <= "10011000";
				when "0101" => display2  <= "01001000";
				when "0110" => display2  <= "01000000";       
				when "0111" => display2  <= "00011110";
				when "1000" => display2  <= "00000000";
				when others => display2  <= "00011000";
				end case;
         
					case m1 is

					when "0000" => display3  <= "00000010";
					when "0001" => display3  <= "10011110";
					when "0010" => display3  <= "00100100";
					when "0011" => display3  <= "00001100";
					when "0100" => display3  <= "10011000";
					when "0101" => display3  <= "01001000";
					when "0110" => display3  <= "01000000";       
					when "0111" => display3  <= "00011110";
					when "1000" => display3  <= "00000000";
					when others => display3  <= "00011000";
					end case;
         
				case H0 is

				when "0000" => display4  <= "00000010";
				when "0001" => display4  <= "10011110";
				when "0010" => display4  <= "00100100";
				when "0011" => display4  <= "00001100";
				when "0100" => display4  <= "10011000";
				when "0101" => display4  <= "01001000";
				when "0110" => display4  <= "01000000";       
				when "0111" => display4  <= "00011110";
				when "1000" => display4  <= "00000000";
				when others => display4  <= "00011000";
				end case;
         
         case h1 is

		  when "0000" => display5  <= "00000010";
          when "0001" => display5  <= "10011110";
          when "0010" => display5  <= "00100100";
          when "0011" => display5  <= "00001100";
          when "0100" => display5  <= "10011000";
          when "0101" => display5  <= "01001000";
          when "0110" => display5  <= "01000000";       
          when "0111" => display5  <= "00011110";
          when "1000" => display5  <= "00000000";
          when others => display5  <= "00011000";
         end case;
     end if
  end process;	
		
		
end arq;
