library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity calc_notas is
    Port ( 	CLK : in  STD_LOGIC;                                    	-- Tiene que funcionar a 48000
				notasent : in STD_LOGIC_VECTOR (11 downto 0); 				-- Notas arpa funcionan solo 12 de C a C - F/G
				nota1 : out STD_LOGIC_VECTOR (4 downto 0);
				nota2 : out STD_LOGIC_VECTOR (4 downto 0)
				); 
end calc_notas;

architecture Behavioral of calc_notas is

signal i11 : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');
signal i10 : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');
signal i9 : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');
signal i8 : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');
signal i7 : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');
signal i6 : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');
signal i5 : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');
signal i4 : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');
signal i3 : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');
signal i2 : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');
signal i1 : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');
signal i0 : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');
signal cant_notas : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');
signal nota1int : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');
signal nota2int : STD_LOGIC_VECTOR (4 downto 0):= (others=>'0');

begin

--cant_notas <= "0000" & notasent(11) + "0000" & notasent(10) + "0000" & notasent(9) +
--				  "0000" & notasent(8) + "0000" & notasent(7) + "0000" & notasent(6) +
--				  "0000" & notasent(5) + "0000" & notasent(4) + "0000" & notasent(3)+
--				  "0000" & notasent(2) + "0000" & notasent(1) + "0000" & notasent(0);

Process (CLK)
begin
	if notasent(11)='1' then i11<="00001"; else i11<="00000";end if;
	if notasent(10)='1' then i10<="00001"; else i10<="00000";end if;
	if notasent(9)='1' then i9<="00001"; else i9<="00000";end if;
	if notasent(8)='1' then i8<="00001"; else i8<="00000";end if;
	if notasent(7)='1' then i7<="00001"; else i7<="00000";end if;
	if notasent(6)='1' then i6<="00001"; else i6<="00000";end if;
	if notasent(5)='1' then i5<="00001"; else i5<="00000";end if;
	if notasent(4)='1' then i4<="00001"; else i4<="00000";end if;
	if notasent(3)='1' then i3<="00001"; else i3<="00000";end if;
	if notasent(2)='1' then i2<="00001"; else i2<="00000";end if;
	if notasent(1)='1' then i1<="00001"; else i1<="00000";end if;
	if notasent(0)='1' then i0<="00001"; else i0<="00000";end if;
end process;

cant_notas <= i11+i10+i9+i8+i7+i6+i5+i4+i3+i2+i1+i0;
				  
Process (CLK)
begin
	if (CLK'event and CLK='1') then
		if	(cant_notas=0) then 
			nota1int <= "00000";
			nota2int <= "00000";
		elsif (cant_notas=1) then
			if (notasent(11)='1') then 
				nota1int <= "01110";
				nota2int <= "00000";
			elsif (notasent(10)='1') then 
				nota1int <= "01101";
				nota2int <= "00000";
			elsif (notasent(9)='1') then 
				nota1int <= "01100";
				nota2int <= "00000";
			elsif (notasent(8)='1') then 
				nota1int <= "01011";
				nota2int <= "00000";
			elsif (notasent(7)='1') then 
				nota1int <= "01010";
				nota2int <= "00000";
			elsif (notasent(6)='1') then 
				nota1int <= "01001";
				nota2int <= "00000";
			elsif (notasent(5)='1') then 
				nota1int <= "00111";
				nota2int <= "00000";
			elsif (notasent(4)='1') then 
				nota1int <= "00110";
				nota2int <= "00000";
			elsif (notasent(3)='1') then 
				nota1int <= "00101";
				nota2int <= "00000";
			elsif (notasent(2)='1') then 
				nota1int <= "00100";
				nota2int <= "00000";
			elsif (notasent(1)='1') then 
				nota1int <= "00011";
				nota2int <= "00000";
			elsif (notasent(0)='1') then 
				nota1int <= "00010";
				nota2int <= "00000";
			end if;
		elsif (cant_notas > 1) then
			if (notasent(11)='1') then
				if notasent(0)='1' then
					nota1int <= "01110";
					nota2int <= "00010";
				elsif notasent(1)='1' then
						nota1int <= "01110";
						nota2int <= "00011";
				elsif notasent(2)='1' then
						nota1int <= "01110";
						nota2int <= "00100";
				elsif notasent(3)='1' then
						nota1int <= "01110";
						nota2int <= "00101";
				elsif notasent(4)='1' then
						nota1int <= "01110";
						nota2int <= "00110";
				elsif notasent(5)='1' then
						nota1int <= "01110";
						nota2int <= "00111";
				elsif notasent(6)='1' then
						nota1int <= "01110";
						nota2int <= "01001";
				elsif notasent(7)='1' then
						nota1int <= "01110";
						nota2int <= "01010";
				elsif notasent(8)='1' then
						nota1int <= "01110";
						nota2int <= "01011";
				elsif notasent(9)='1' then
						nota1int <= "01110";
						nota2int <= "01100";
				elsif notasent(10)='1' then
						nota1int <= "01110";
						nota2int <= "01101";
				end if;
			elsif (notasent(10)='1') then
				if notasent(0)='1' then
					nota1int <= "01101";
					nota2int <= "00010";
				elsif notasent(1)='1' then
						nota1int <= "01101";
						nota2int <= "00011";
				elsif notasent(2)='1' then
						nota1int <= "01101";
						nota2int <= "00100";
				elsif notasent(3)='1' then
						nota1int <= "01101";
						nota2int <= "00101";
				elsif notasent(4)='1' then
						nota1int <= "01101";
						nota2int <= "00110";
				elsif notasent(5)='1' then
						nota1int <= "01101";
						nota2int <= "00111";
				elsif notasent(6)='1' then
						nota1int <= "01101";
						nota2int <= "01001";
				elsif notasent(7)='1' then
						nota1int <= "01101";
						nota2int <= "01010";
				elsif notasent(8)='1' then
						nota1int <= "01101";
						nota2int <= "01011";
				elsif notasent(9)='1' then
						nota1int <= "01101";
						nota2int <= "01100";
				end if;
			elsif (notasent(9)='1') then
				if notasent(0)='1' then
					nota1int <= "01100";
					nota2int <= "00010";
				elsif notasent(1)='1' then
						nota1int <= "01100";
						nota2int <= "00011";
				elsif notasent(2)='1' then
						nota1int <= "01100";
						nota2int <= "00100";
				elsif notasent(3)='1' then
						nota1int <= "01100";
						nota2int <= "00101";
				elsif notasent(4)='1' then
						nota1int <= "01100";
						nota2int <= "00110";
				elsif notasent(5)='1' then
						nota1int <= "01100";
						nota2int <= "00111";
				elsif notasent(6)='1' then
						nota1int <= "01100";
						nota2int <= "01001";
				elsif notasent(7)='1' then
						nota1int <= "01100";
						nota2int <= "01010";
				elsif notasent(8)='1' then
						nota1int <= "01100";
						nota2int <= "01011";
				end if;
			elsif (notasent(8)='1') then
				if notasent(0)='1' then
					nota1int <= "01011";
					nota2int <= "00010";
				elsif notasent(1)='1' then
						nota1int <= "01011";
						nota2int <= "00011";
				elsif notasent(2)='1' then
						nota1int <= "01011";
						nota2int <= "00100";
				elsif notasent(3)='1' then
						nota1int <= "01011";
						nota2int <= "00101";
				elsif notasent(4)='1' then
						nota1int <= "01011";
						nota2int <= "00110";
				elsif notasent(5)='1' then
						nota1int <= "01011";
						nota2int <= "00111";
				elsif notasent(6)='1' then
						nota1int <= "01011";
						nota2int <= "01001";
				elsif notasent(7)='1' then
						nota1int <= "01011";
						nota2int <= "01010";
				end if;
			elsif (notasent(7)='1') then
				if notasent(0)='1' then
					nota1int <= "01010";
					nota2int <= "00010";
				elsif notasent(1)='1' then
						nota1int <= "01010";
						nota2int <= "00011";
				elsif notasent(2)='1' then
						nota1int <= "01010";
						nota2int <= "00100";
				elsif notasent(3)='1' then
						nota1int <= "01010";
						nota2int <= "00101";
				elsif notasent(4)='1' then
						nota1int <= "01010";
						nota2int <= "00110";
				elsif notasent(5)='1' then
						nota1int <= "01010";
						nota2int <= "00111";
				elsif notasent(6)='1' then
						nota1int <= "01010";
						nota2int <= "01001";
				end if;
			elsif (notasent(6)='1') then
				if notasent(0)='1' then
					nota1int <= "01001";
					nota2int <= "00010";
				elsif notasent(1)='1' then
						nota1int <= "01001";
						nota2int <= "00011";
				elsif notasent(2)='1' then
						nota1int <= "01001";
						nota2int <= "00100";
				elsif notasent(3)='1' then
						nota1int <= "01001";
						nota2int <= "00101";
				elsif notasent(4)='1' then
						nota1int <= "01001";
						nota2int <= "00110";
				elsif notasent(5)='1' then
						nota1int <= "01001";
						nota2int <= "00111";
				end if;
			elsif (notasent(5)='1') then
				if notasent(0)='1' then
					nota1int <= "00111";
					nota2int <= "00010";
				elsif notasent(1)='1' then
						nota1int <= "00111";
						nota2int <= "00011";
				elsif notasent(2)='1' then
						nota1int <= "00111";
						nota2int <= "00100";
				elsif notasent(3)='1' then
						nota1int <= "00111";
						nota2int <= "00101";
				elsif notasent(4)='1' then
						nota1int <= "00111";
						nota2int <= "00110";
				end if;
			elsif (notasent(4)='1') then
				if notasent(0)='1' then
					nota1int <= "00110";
					nota2int <= "00010";
				elsif notasent(1)='1' then
						nota1int <= "00110";
						nota2int <= "00011";
				elsif notasent(2)='1' then
						nota1int <= "00110";
						nota2int <= "00100";
				elsif notasent(3)='1' then
						nota1int <= "00110";
						nota2int <= "00101";
				end if;
			elsif (notasent(3)='1') then
				if notasent(0)='1' then
					nota1int <= "00101";
					nota2int <= "00010";
				elsif notasent(1)='1' then
						nota1int <= "00101";
						nota2int <= "00011";
				elsif notasent(2)='1' then
						nota1int <= "00101";
						nota2int <= "00100";
				end if;
			elsif (notasent(2)='1') then
				if notasent(0)='1' then
					nota1int <= "00100";
					nota2int <= "00010";
				elsif notasent(1)='1' then
						nota1int <= "00100";
						nota2int <= "00011";
				end if;
			elsif (notasent(1)='1') then
					nota1int <= "00100";
					nota2int <= "00010";
			end if;
		end if;
	end if;
end process;

nota1 <= nota1int;
nota2 <= nota2int;

end Behavioral;
