-----------------------------------------------
-- Progetto	   : riconoscitore di numeri primi
-- Nome file   : TOP.vhdl
-- Descrizione : la rete logica riconosce se l'ingresso  un numero primo
-- Autore      : Guido Matrella
-- Revisione   : 1.0
-- Note        : si devono usare solo porte logiche e almeno un MUX8x1
-----------------------------------------------

-----------------------------------------------
-- definizione del COMPOMENTE MUX8x1
-----------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;

entity mux8x1 is 
  port(
       mux_in : in STD_LOGIC_VECTOR(7 downto 0);
	   sel : in STD_LOGIC_VECTOR(2 downto 0);
	   mux_out : out STD_LOGIC
	   );
end mux8x1 ;

architecture esempio of mux8x1  is 

  begin					 
	-- descrizione funzionale - con costrutto CASE serve il process
	process (sel, mux_in)
	begin
		case sel is
			when "000" => mux_out <= mux_in(0);
			when "001" => mux_out <= mux_in(1);
			when "010" => mux_out <= mux_in(2);
			when "011" => mux_out <= mux_in(3);
			when "100" => mux_out <= mux_in(4);
			when "101" => mux_out <= mux_in(5);
			when "110" => mux_out <= mux_in(6);
			when "111" => mux_out <= mux_in(7);
			when others => mux_out <= 'Z'; -- si noti l'utilizzo di OTHERS e di 'Z' (alta impendeza)
		end case;
	end process;
  end esempio;
  
-----------------------------------------------
-- definizione del TOP LEVEL
----------------------------------------------- 
library IEEE;
use IEEE.std_logic_1164.all;
-- in ingresso a TOP entra un numero binario B a 5 bit (B4 B3 B2 B1 B0)
-- l'uscita F vale 1 se e solo se B rapprensenta un NUMERO PRIMO
-- NUMERI PRIMI sono: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31
entity TOP is 
  port(
       B : in STD_LOGIC_VECTOR(4 downto 0);
	   F : out STD_LOGIC
	   );
end TOP;

architecture esercizio of TOP is
	component mux8x1 is port(mux_in  : in STD_LOGIC_VECTOR(7 downto 0); sel : in STD_LOGIC_VECTOR(2 downto 0); mux_out : out STD_LOGIC);
	end component;
	
	signal s0,s1,s2,s3,s4,s5,s6,s7 : STD_LOGIC;
	signal S : STD_LOGIC_VECTOR (7 downto 0); 
	signal ST : STD_LOGIC_VECTOR (2 downto 0);
	begin
	-- qui dovete descrivere in VHDL il vostro progetto
	s0 <= '0';
	s1 <= B(4) and not(B(3));
	s2 <= not(B(4)) and not(B(3));
	s3 <= B(4) nand B(3);
	s4 <= '0';
	s5 <= (not(B(4)) and not(B(3))) or (not(B(4)) and B(3)) or (B(4) and B(3)) ;
	s6 <= '0';
	s7 <= (not(B(4)) and not(B(3))) or (B(4) and not(B(3))) or (B(4) and B(3));
	-- si noti l'utilizzo di & operatore di CONCATENAZIONE per creare il vettore
	S <= (s7 & s6 & s5 & s4 & s3 & s2 & s1 & s0); 
	ST <= (B(2) & B(1) & B(0));
	-- esempio di istanza di un COMPONENTE
	-- componente1: COMPONENT_NAME port map (porta1 => segnale1, porta2 => segnale2, ...);
	
	mymux: mux8x1 port map(mux_in => S, sel => ST, mux_out=>  F);
	
	end esercizio;



 
-----------------------------------------------
-- definizione del TEST BENCH
----------------------------------------------- 
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;

entity TOP_tb is
end TOP_tb;

architecture test of TOP_tb is
	component TOP is port(B : in STD_LOGIC_VECTOR(4 downto 0);F : out STD_LOGIC);
	end component;

signal B_tb: STD_LOGIC_VECTOR(4 downto 0);
signal F_tb: STD_LOGIC;
signal ideal_out: STD_LOGIC;
signal ERRORE: STD_LOGIC := '0'; 
-- signal i: integer range 0 to 15;

begin
--Unit Undet Test: NOME_COMPONENTE port map (NOME_PORTA => NOME_SEGNALE, ...., ...);
UUT: TOP port map (B => B_tb, F => F_tb );
					
	process
	begin
		-- h vuol dire: espresso in esadecimanle 
	    B_tb <= std_logic_vector(to_unsigned(0,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(1,5));ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(2,5)); ideal_out <= '1'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(3,5)); ideal_out <= '1'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(4,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(5,5)); ideal_out <= '1'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(6,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(7,5)); ideal_out <= '1'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(8,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(9,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(10,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(11,5)); ideal_out <= '1'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(12,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(13,5)); ideal_out <= '1'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(14,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(15,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(16,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(17,5)); ideal_out <= '1'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(18,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(19,5)); ideal_out <= '1'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(20,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(21,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(22,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(23,5)); ideal_out <= '1'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(24,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(25,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(26,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(27,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(28,5)); ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(29,5)); ideal_out <= '1'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(30,5));ideal_out <= '0'; wait for 10 ns;
		B_tb <= std_logic_vector(to_unsigned(31,5)); ideal_out <= '1'; wait for 10 ns;

		wait;  
	end process;
			
	-- Confrontiamo il nostro progetto con un B_tbenchmark ideale
	ERRORE <= '0' when (ideal_out = F_tb) else '1' after 1 ns;  
	assert (ERRORE='0')
	report "attenzione! controlla la somma con GTKWAVE"
	severity WARNING;
end test;

  
 