-- G. Gora
-- MSWSiS 2016


library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity ThermoObject is
	port(
		clk : in std_logic;
		reset_n : in std_logic := '1';
		
		a, b, c : out std_logic;
		HEX2, HEX1, HEX0, HEX5 : out unsigned(7 downto 0)
		
	);
end entity;


architecture ThermoObject of ThermoObject is

	constant CNT_MAX: unsigned(31 downto 0) := x"017D783F"; -- 24 999 999
	constant COUNT_MAX : unsigned(7 downto 0) := x"C7"; -- 199

	signal cnt: unsigned(31 downto 0) := (others => '0');
	signal count, count0, count1, count2, count3, count4, count5: unsigned(7 downto 0) := (others => '0');
	signal temp, temp_01, temp_0, temp_0_tmp, temp_10 : unsigned(7 downto 0) := (others => '0');
	signal trig_count, sA, sB, sC : std_logic := '0';
	signal hex_0, hex_1, hex_2 : unsigned(6 downto 0) := (others => '0');
	
	


begin


	process(clk, reset_n)
	begin
		if(reset_n = '0')then
			cnt <= (others => '0');
		elsif rising_edge(clk) then
			if(cnt < CNT_MAX)then
				cnt <= cnt + 1;
			else
				cnt <= (others => '0');
			end if;
		end if;
	end process;
	
	trig_count <= '1' when(cnt = CNT_MAX) else '0';
	
	
	process(clk, reset_n)
	begin
		if(reset_n = '0')then
			count <= (others => '0');
		elsif rising_edge(clk) then
			if(trig_count = '1')then
				if(count < COUNT_MAX)then
					count <= count + 1;
				else
					count <= (others => '0');
				end if;
			end if;
		end if;
	end process;
	
	
	count0 <= count + x"BE";
	count1 <= x"E6" - (count - x"28");
	count2 <= count + x"96";
	count3 <= x"FA" - (count - x"64");
	count4 <= count + x"46";
	count5 <= x"E6" - (count - x"A0");
	
	temp <= count0 when(count < x"28")
		else count1 when(count < x"3C")
		else count2 when(count < x"64")
		else count3 when(count < x"8C")
		else count4 when(count < x"A0")
		else count5;
		

	temp_10 <= x"02" when(temp >= x"C8") else x"01";
	temp_0_tmp <= (temp-x"C8") when(temp >= x"C8") else (temp-x"64");
	temp_0 <= temp_0_tmp/x"0A";
	temp_01 <= temp mod x"0A";
	
	
	hex_2 <= "0111111" when(temp_10 = "0000") else
		"0000110" when(temp_10 = "0001") else
		"1011011" when(temp_10 = "0010") else
		"1001111" when(temp_10 = "0011") else
		"1100110" when(temp_10 = "0100") else
		"1101101" when(temp_10 = "0101") else
		"1111101" when(temp_10 = "0110") else
		"0000111" when(temp_10 = "0111") else
		"1111111" when(temp_10 = "1000") else
		"1101111" when(temp_10 = "1001") else
		"1100100" when(temp_10 = "1010") else
		"1010010" when(temp_10 = "1011") else
		"1111111";

	hex_1 <= "0111111" when(temp_0 = "0000") else
		"0000110" when(temp_0 = "0001") else
		"1011011" when(temp_0 = "0010") else
		"1001111" when(temp_0 = "0011") else
		"1100110" when(temp_0 = "0100") else
		"1101101" when(temp_0 = "0101") else
		"1111101" when(temp_0 = "0110") else
		"0000111" when(temp_0 = "0111") else
		"1111111" when(temp_0 = "1000") else
		"1101111" when(temp_0 = "1001") else
		"1100100" when(temp_0 = "1010") else
		"1010010" when(temp_0 = "1011") else
		"1111111";
		
	hex_0 <= "0111111" when(temp_01 = "0000") else
		"0000110" when(temp_01 = "0001") else
		"1011011" when(temp_01 = "0010") else
		"1001111" when(temp_01 = "0011") else
		"1100110" when(temp_01 = "0100") else
		"1101101" when(temp_01 = "0101") else
		"1111101" when(temp_01 = "0110") else
		"0000111" when(temp_01 = "0111") else
		"1111111" when(temp_01 = "1000") else
		"1101111" when(temp_01 = "1001") else
		"1100100" when(temp_01 = "1010") else
		"1010010" when(temp_01 = "1011") else
		"1111111";
		
		
		
	sA <= '1' when(temp > x"F0") else '0';
	sB <= '1' when(temp > x"DC") else '0';
	sC <= '1' when(temp > x"C8") else '0';
		
	
	
	process(clk, reset_n)
	begin
		if(reset_n = '0')then
			a <= '0';
			b <= '0';
			c <= '0';
		elsif rising_edge(clk) then
			a <= sA;
			b <= sB;
			c <= sC;
		end if;
	end process;
		
		
	HEX2(7) <= '1';
	HEX2(6 downto 0) <= not hex_2;
	
	HEX1(7) <= '0';
	HEX1(6 downto 0) <= not hex_1;
	
	HEX0(7) <= '1';
	HEX0(6 downto 0) <= not hex_0;
	
	HEX5(7) <= '1';
	HEX5(6) <= not sB;
	HEX5(5) <= '1';
	HEX5(4) <= '1';
	HEX5(3) <= not sC;
	HEX5(2) <= '1';
	HEX5(1) <= '1';
	HEX5(0) <= not sA;


end architecture;
