hbinderup94

code_lock_simple

May 26th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 1.95 KB | None | 0 0
  1. ------ code_lock_simple ------
  2. library ieee;
  3. use ieee.std_logic_1164.all;
  4.  
  5. entity code_lock_simple is
  6. port(
  7.     enter   : in std_logic;
  8.     reset   : in std_logic;
  9.     clk     : in std_logic;
  10.     code    : in std_logic_vector(3 downto 0);
  11.     lock    : out std_logic);
  12. end code_lock_simple;
  13.  
  14. architecture state_machine of code_lock_simple is
  15.     type    state is(idle, evaluating_code_1, getting_code_2, evaluating_code_2, unlocked, going_idle);
  16.     signal  present_state, next_state : state;
  17.     signal  code1, code2 : std_logic_vector(3 downto 0);
  18. begin
  19.  
  20.     code1 <= "1111";                -- code1 og code2 defineres
  21.     code2 <= "0000";
  22.  
  23.     state_reg: process(clk, reset)  -- state register defineres
  24.     begin
  25.         if reset = '0' then
  26.             present_state <= idle;
  27.         elsif rising_edge(clk) then
  28.             present_state <= next_state;
  29.         end if;
  30.     end process;
  31.    
  32.     nxt_state: process(present_state, enter, code)
  33.     begin
  34.         next_state <= present_state;    -- next_state defineres ud fra ibd
  35.         case present_state is
  36.             when idle =>
  37.                 if enter = '0' then
  38.                     next_state <= evaluating_code_1;
  39.                 end if;
  40.             when evaluating_code_1 =>
  41.                 if (enter = '1' and code = code1) then
  42.                     next_state <= getting_code_2;
  43.                 elsif (enter = '1' and code = NOT code1) then
  44.                     next_state <= idle;
  45.                 end if;
  46.             when getting_code_2 =>
  47.                 if enter = '0' then
  48.                     next_state <= evaluating_code_2;
  49.                 end if;
  50.             when evaluating_code_2 =>
  51.                 if (enter = '1' and code = code2) then
  52.                     next_state <= unlocked;
  53.                 elsif (enter = '1' and code = NOT code2) then
  54.                     next_state <= idle;
  55.                 end if;
  56.             when unlocked =>
  57.                 if enter = '0' then
  58.                     next_state <= going_idle;
  59.                 end if;
  60.             when going_idle =>
  61.                 if enter = '1' then
  62.                     next_state <= idle;
  63.                 end if;
  64.         end case;
  65.     end process;
  66.    
  67.     outputs: process(present_state) -- outputs defineres ud fra ibd
  68.     begin
  69.         case present_state is
  70.             when unlocked =>
  71.                 lock <= '0';
  72.             when others =>
  73.                 lock <= '1';
  74.         end case;
  75.     end process;
  76.  
  77. end state_machine;
Advertisement
Add Comment
Please, Sign In to add comment