-----------------------------------------------
-- Progetto    : FFD ricavato dalle porte logiche
-- Nome file   : TOP.vhdl
-- Descrizione : il circuito di un FFD ricavato passo-passo
-- Autore      : Guido Matrella
-- Revisione   : 1.0
-- Note        : //
-----------------------------------------------

-----------------------------------------------
----- realizzazione del LATCH di tipo SR  -----
-----------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;

entity SR_LATCH is 
  port(
       S : in STD_LOGIC;
       R : in STD_LOGIC;
       Q : inout STD_LOGIC; -- è necesario INOUT perché c'è il feedback
       QN: inout STD_LOGIC  
       );
end SR_LATCH;


architecture esempio of SR_LATCH  is 
begin                    
    Q <= R nor QN;
    QN <= S nor Q;
end esempio;
  
  
-----------------------------------------------
----- realizzazione del LATCH SR_GATED    -----
-----------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;

entity SR_GATED is 
  port(
       S : in STD_LOGIC;
       R : in STD_LOGIC;
       E : in STD_LOGIC;
       Q : inout STD_LOGIC; -- è necesario INOUT perché c'è il feedback
       QN: inout STD_LOGIC  
       );
end SR_GATED;


architecture esempio of SR_GATED  is 
signal R_g, S_g: STD_LOGIC;

begin                    
    R_g <= R and E;
    S_g <= S and E;
    Q <= R_g and QN;
    QN <= S_g and Q;
end esempio;

-----------------------------------------------
----- realizzazione del LATCH di tipo D   -----
-----------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;

entity D_LATCH is 
  port(
       D : in STD_LOGIC;
       E : in STD_LOGIC;
       Q : inout STD_LOGIC; -- è necesario INOUT perché c'è il feedback
       QN: inout STD_LOGIC  
       );
end D_LATCH;


architecture esempio of D_LATCH  is 
signal R_g, S_g: STD_LOGIC;
-- sfruttando DE MORGAN di possono usare solo porte NAND
begin                    
    S_g <= D nand E;
    R_g <= E nand not D;
    Q <= S_g nand QN;
    QN <= R_g nand Q;
end esempio;

-----------------------------------------------
----- realizzazione del LATCH di tipo MS  -----
-----------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;

entity MS_D_LATCH is 
  port(
       D : in STD_LOGIC;
       E : in STD_LOGIC;
       Q : inout STD_LOGIC; -- è necesario INOUT perché c'è il feedback
       QN: inout STD_LOGIC  
       );
end MS_D_LATCH;


architecture esempio of MS_D_LATCH  is 
component D_LATCH is 
  port(
       D : in STD_LOGIC;
       E : in STD_LOGIC;
       Q : inout STD_LOGIC; -- è necesario INOUT perché c'è il feedback
       QN: inout STD_LOGIC  
       );
end component;
signal Qm, SG: STD_LOGIC;
begin             
    SG <= not E;       
    dl_01: D_LATCH port map (D=>D, E=>SG, Q=>Qm);
    dl_02: D_LATCH port map (D=>Qm, E=>E, Q=>Q, QN=>QN);
end esempio;

-----------------------------------------------
----- realizzazione del Flip Flop di tipo D----
-----------------------------------------------

library IEEE;
use IEEE.std_logic_1164.all;

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

-----------------------------------------------
----- realizzazione del Flip Flop di tipo D con RESET
----- ATTENZIONE: per noi il FFD è questo!
----- Imparate il costrutto a MEMORIA
-----------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;

    entity FFD is -- descrizione funzionale FFD con RESET
    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;
 