Advertisement
B-Matt

VHDL - JK Flip Flop

Mar 26th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 2.15 KB | None | 0 0
  1. --- Author: Matej Arlović, 2018.
  2. library IEEE;
  3. use IEEE.STD_LOGIC_1164.ALL;
  4.  
  5. entity jk_flipflop is
  6.     port(
  7.             J, K, set, cp: in STD_LOGIC;
  8.             Q, Qn: out STD_LOGIC
  9.         );
  10. end jk_flipflop;
  11.  
  12. architecture Behavioral of jk_flipflop is
  13. signal temp: STD_LOGIC;
  14. begin
  15.     process(cp)
  16.     begin
  17.         if(set = '1') then
  18.             temp <= '1';
  19.         elsif(cp'EVENT and cp = '0') then
  20.             if(J = '0' and K = '0') then
  21.                 temp <= temp;
  22.             elsif(J = '0' and K = '1') then
  23.                 temp <= '0';
  24.             elsif(J = '1' and K = '1') then
  25.                 temp <= not(temp);
  26.             else
  27.                 temp <= '1';
  28.             end if;
  29.         end if;
  30.     end process;
  31.     Q <= temp;
  32.     Qn <= not(temp);
  33. end Behavioral;
  34.  
  35. --- Testbench:
  36. LIBRARY ieee;
  37. USE ieee.std_logic_1164.ALL;
  38.  
  39. ENTITY jk_flipflop_w IS
  40. END jk_flipflop_w;
  41.  
  42. ARCHITECTURE behavior OF jk_flipflop_w IS
  43.     -- Component Declaration for the Unit Under Test (UUT)
  44.     COMPONENT jk_flipflop
  45.     PORT(
  46.          J : IN  std_logic;
  47.          K : IN  std_logic;
  48.          set : IN  std_logic;
  49.          cp : IN  std_logic;
  50.          Q : OUT  std_logic;
  51.          Qn : OUT  std_logic
  52.         );
  53.     END COMPONENT;
  54.  
  55.    --Inputs
  56.    signal J : std_logic := '0';
  57.    signal K : std_logic := '0';
  58.    signal set : std_logic := '0';
  59.    signal cp : std_logic := '0';
  60.  
  61.     --Outputs
  62.    signal Q : std_logic;
  63.    signal Qn : std_logic;
  64.    constant cp_period : time := 200 ns;
  65.  
  66. BEGIN
  67.  
  68.     -- Instantiate the Unit Under Test (UUT)
  69.    uut: jk_flipflop PORT MAP (
  70.           J => J,
  71.           K => K,
  72.           set => set,
  73.           cp => cp,
  74.           Q => Q,
  75.           Qn => Qn
  76.         );
  77.  
  78.    -- Clock process definitions
  79.    cp_process :process
  80.    begin
  81.         cp <= '0';
  82.         wait for cp_period/2;
  83.         cp <= '1';
  84.         wait for cp_period/2;
  85.    end process;
  86.    
  87.    -- Stimulus process
  88.    stim_proc: process
  89.    begin
  90.         set <= '1';
  91.         J <= '0';
  92.         K <= '0';
  93.       wait for 100 ns;
  94.         set <= '0';
  95.         J <= '0';
  96.         K <= '0';
  97.         wait for 100 ns;
  98.         J <= '1';
  99.         K <= '0';
  100.         wait for 100 ns;
  101.         J <= '0';
  102.         K <= '1';
  103.         wait for 100 ns;
  104.         J <= '1';
  105.         K <= '1';
  106.         wait for 100 ns;
  107.         J <= '0';
  108.         K <= '0';
  109.         wait for 100 ns;
  110.         J <= '0';
  111.         K <= '0';
  112.       wait for 100 ns;
  113.    end process;
  114. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement