library ieee;
use ieee.std_logic_1164.all;

entity dff_generic is
  generic (TPLH_PQ, TPHL_PQ,
           TPLH_CQ, TPHL_CQ,
           TPLH_CKQ, TPHL_CKQ : time := 5 ns);
  port (D, CLK, CLEAR, PRESET : in std_logic;
        Q, QN : out std_logic);
end dff_generic;

architecture behav of dff_generic is
begin
  process (CLEAR, PRESET, CLK)
    variable QLAST, QTEMP : std_logic;
  begin
    QLAST := QTEMP;
    if (CLEAR'event) then
      if (CLEAR = '1') then
        QTEMP := '0';
      else
        QTEMP := '1';
      end if;
    elsif (PRESET'event) then
      if (PRESET = '1') then
        QTEMP := '1';
      else
        QTEMP := '0';
      end if;
    elsif (CLK'event and CLK = '0') then
      if (D = '1') then
        QTEMP := '1';
      elsif (D = '0') then
        QTEMP := '0';
      end if;
    end if;
    if (CLEAR'event) then 
      if (QTEMP = '1' and QLAST = '0') then
        Q <= QTEMP after TPLH_CQ;
        QN <= not QTEMP after TPHL_CQ;
      elsif (QTEMP = '0' and QLAST = '1') then
        Q <= QTEMP after TPHL_CQ;
        QN <= not QTEMP after TPLH_CQ;
      end if;
    elsif (PRESET'event) then
      if (QTEMP = '1' and QLAST = '0') then
        Q <= QTEMP after TPLH_PQ;
        QN <= not QTEMP after TPHL_PQ;
      elsif (QTEMP = '0' and QLAST = '1') then
        Q <= QTEMP after TPHL_PQ;
        QN <= not QTEMP after TPLH_PQ;
      end if;
    elsif (CLK'event and CLK = '0') then
      if (QTEMP = '1' and QLAST = '0') then
        Q <= QTEMP after TPLH_CKQ;
        QN <= not QTEMP after TPHL_CKQ;
      elsif (QTEMP = '0' and QLAST = '1') then
        Q <= QTEMP after TPHL_CKQ;
        QN <= not QTEMP after TPLH_CKQ;
      end if;
    end if;
  end process;
end behav;

entity test_dff;
end test_dff;
      
      

