-----------------------------------------------
-- Progetto    : Flip Flop
-- Nome file   : FF.vhdl
-- Descrizione : descrizione di un FFD e di un FFT
-- Autore      : Guido Matrella
-- Revisione   : 1.0
-- Note        : esempio di progettino tipo esame
-----------------------------------------------
-- PROBLEMA: vogliamo realizzare un circuito che riconosca 
-- una particolare sequenza che riceve in ingresso
-- dopo una fase di reset iniziale, in cui l'uscita è posta a 0,
-- se in ingresso si presenta la sequanza 111 allora l'uscita va a 1
-- l'uscita torna a 0 se si presenta ancora la sequanza 111
-- oppure quando il segnare di reset è posto a 1
-----------------------------------------------


library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;

    entity FFD is -- descrizione del FFD
    port    (
            CLK, D, reset : in std_logic;
            Q : out std_logic
            );
    end FFD;
    
    architecture behaviour of FFD is
    begin
        process(CLK, reset)
        begin
            if reset='1' then Q<='0';  
            elsif (clk'event and clk='1') then Q<=D;                                            
            else null;
            end if;
        end process;
    end behaviour;


library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;

    entity FFT is -- descrizione del FFT 
    port    (
            CLK, T, reset : in std_logic;
            Q : out std_logic
            );
    end FFT;
    
    architecture behaviour of FFT is
    signal x: std_logic;
    begin
        process(CLK, reset)
        begin
            if reset='1' then x<='0';  
            elsif (clk'event and clk='1') then 
                                            if T='1' then x<=not x;
                                            else null;
                                            end if;
            else null;
            end if;
        end process;
        Q<=x;
        
    end behaviour;



library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;

entity ESERCIZIO is 
  port(
        clk, reset, datain: in STD_LOGIC;
        dataout : out STD_LOGIC
       );
end ESERCIZIO;

-- vogliamo realizzare un circuito che riceva in ingresso una sequenza
-- dopo una fase di reset iniziale in cui l'uscita è posta a 0
-- se in ingresso si presenta la sequanza 111 allora l'uscita va a 1
-- l'uscita torna a 0 se si presenta ancora la sequanza 111

architecture behaviour of ESERCIZIO is 
-- per risolvere l'esercizio si usino FFD e FFT
component FFD is port(CLK, D, reset : in std_logic; Q : out std_logic); end component;
component FFT is port(CLK, T, reset : in std_logic; Q : out std_logic); end component;

signal a,b,c,d : STD_LOGIC; -- interconnessioni interne

  begin  
  -- ESEMPIO INSTANZIAZIONE:
  -- label: NOME_COMPONENTE port map (nome_porta => nome_segnale, ...);
  --ff1: FFT port map(CLK=>clk, reset=>reset, T=>datain, Q=>d);
  ff2: FFD port map(CLK=>clk, reset=>reset, D=>datain, Q=>a);
  ff3: FFD port map(CLK=>clk, reset=>reset, D=>a, Q=>b);
  ff4: FFD port map(CLK=>clk, reset=>reset, D=>b, Q=>c);
  
  ff1: FFT port map(CLK=>clk, reset=>reset, T=>d, Q=>dataout);
  d <= a and b and c;
  
  end behaviour;
  
-------------------------------------------- 
-----------------TEST BENCH-----------------
--------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;

entity ESERCIZIO_tb is
end ESERCIZIO_tb;

architecture test of ESERCIZIO_tb is 
component ESERCIZIO is
port(
        clk, reset, datain: in STD_LOGIC;
        dataout : out STD_LOGIC
       );
end component;

constant clk_period : time := 10 ns; 
signal clk, reset, datain: STD_LOGIC;
signal dataout : STD_LOGIC;

signal ideal_out: STD_LOGIC := '0';
signal ERRORE: STD_LOGIC := '0'; 
signal i: integer range 0 to 31;

begin

UUT: ESERCIZIO port map (--porta dell'istanza => segnale collegato
                                    clk => clk,
                                    reset => reset,
                                    datain => datain,
                                    dataout => dataout
                                 );
                    
    clk_process: process --processo di generazione del CLK
        begin
            clk <= '0';
            wait for clk_period/2;
            clk <= '1';
            wait for clk_period/2;
            I<=I+1;
        
            if I=15 then wait; -- durata della simulazione: 15 colpi di CLK
            else null;
            end if;
        
        end process;
        
    stymolus_process: process -- processo per la generazione degli stimoli ed uscita ideale
        begin
       reset<='1'; datain<='0'; wait for clk_period/2; ideal_out<='0'; wait for clk_period/2;
       reset<='1'; datain<='0'; wait for clk_period/2; ideal_out<='0'; wait for clk_period/2;
       reset<='0'; datain<='0'; wait for clk_period/2; ideal_out<='0'; wait for clk_period/2;
       reset<='0'; datain<='1'; wait for clk_period/2; ideal_out<='0'; wait for clk_period/2;
       reset<='0'; datain<='1'; wait for clk_period/2; ideal_out<='0'; wait for clk_period/2;
       reset<='0'; datain<='0'; wait for clk_period/2; ideal_out<='0'; wait for clk_period/2;
       reset<='0'; datain<='1'; wait for clk_period/2; ideal_out<='0'; wait for clk_period/2;
       reset<='0'; datain<='1'; wait for clk_period/2; ideal_out<='0'; wait for clk_period/2;
       reset<='0'; datain<='1'; wait for clk_period/2; ideal_out<='0'; wait for clk_period/2;
       reset<='0'; datain<='0'; wait for clk_period/2; ideal_out<='1'; wait for clk_period/2;
       reset<='0'; datain<='0'; wait for clk_period/2; ideal_out<='1'; wait for clk_period/2;
       reset<='0'; datain<='1'; wait for clk_period/2; ideal_out<='1'; wait for clk_period/2;
       reset<='0'; datain<='1'; wait for clk_period/2; ideal_out<='1'; wait for clk_period/2;
       reset<='0'; datain<='1'; wait for clk_period/2; ideal_out<='1'; wait for clk_period/2;
       reset<='0'; datain<='0'; wait for clk_period/2; ideal_out<='0'; wait for clk_period/2;
       
       wait;
       
       end process;     
       
    
    ERRORE <= '0' when (ideal_out = dataout) else '1' after 1 ns;  
    assert (ERRORE='0')
    report "attenzione! controlla con GTKWAVE"
    severity WARNING;
end test;

  
 