Advertisement
DevilCarrot

Untitled

May 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 2.89 KB | None | 0 0
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4.  
  5. entity code_lock_simple is
  6.     port(
  7.         enter, clk, reset: in   std_logic;
  8.         code : in   std_logic_vector(3 downto 0);
  9.         LED : out   std_logic_vector(3 downto 0);
  10.         lock , err : out    std_logic
  11.     ); 
  12. end entity;
  13.  
  14. architecture fsm of code_lock_simple is
  15.     type state_type is (idle, ECode1, GCode2, ECode2, unlocked, going_idle, wrong_code, perm_locked);
  16.     signal state, next_state : state_type;
  17.     signal err_event, failed : std_logic;
  18.     signal retry_errors, next_retry_errors : unsigned(2 downto 0):= "000";
  19. begin
  20.     process (clk)
  21.     begin
  22.         if reset = '0' then
  23.             state <= idle;
  24.             retry_errors <= "000";
  25.         elsif rising_edge(clk) then
  26.             state <= next_state;
  27.             retry_errors <= next_retry_errors;
  28.         end if;
  29.     end process;
  30.    
  31.     process (state, enter)
  32.     begin
  33.             case state is
  34.                 when idle =>
  35.                     if enter = '0' then
  36.                         next_state <= ECode1;
  37.                     else
  38.                         next_state <= state;
  39.                     end if;
  40.                 when ECode1 =>
  41.                     if code = "1010" and enter = '1' then
  42.                         next_state <= GCode2;
  43.                     elsif code = "1010" and enter = '0' then
  44.                         next_state <= state;
  45.                     else
  46.                         next_state <= wrong_code;
  47.                     end if;
  48.                 when GCode2 =>
  49.                     if enter = '0' then
  50.                         next_state <= ECode2;
  51.                     else
  52.                         next_state <= state;
  53.                     end if;
  54.                 when ECode2 =>
  55.                     if code = "0101" and enter = '1' then
  56.                         next_state <= unlocked;
  57.                     elsif code = "0101" and enter = '0' then
  58.                         next_state <= state;
  59.                     else
  60.                         next_state <= wrong_code;
  61.                     end if;
  62.                 when unlocked =>
  63.                     if enter = '0' then
  64.                         next_state <= going_idle;
  65.                     else
  66.                         next_state <= state;
  67.                     end if;
  68.                 when going_idle =>
  69.                     if enter = '1' then
  70.                         next_state <= idle;
  71.                     else
  72.                         next_state <= state;
  73.                     end if;
  74.                 when wrong_code =>
  75.                     if enter = '0' then                
  76.                         next_state <= going_idle;
  77.                         next_retry_errors <= retry_errors + 1;
  78.                         if (retry_errors > 3) then
  79.                         next_state <= perm_locked;
  80.                         end if;
  81.                     else
  82.                         next_state <= state;
  83.                     end if;
  84.                 when perm_locked =>
  85.                         next_state <= perm_locked;
  86.             end case;
  87.     end process;
  88.    
  89.    
  90.     process (state, reset)
  91.     begin
  92.         if reset = '0' then
  93.             err <= '0';
  94.         else
  95.         case state is
  96.             when idle =>
  97.                 lock <= '1';
  98.                 err <= '0';
  99.                 LED(0) <= '0';
  100.             when ECode1 =>
  101.                 lock <= '1';
  102.                 err <= '0';
  103.                 LED(0) <= '0';
  104.             when GCode2 =>
  105.                 lock <= '1';
  106.                 err <= '0';
  107.                 LED(0) <= '0';
  108.             when ECode2 =>
  109.                 lock <= '1';
  110.                 err <= '0';
  111.                 LED(0) <= '0';
  112.             when unlocked =>
  113.                 lock <= '0';
  114.                 err <= '0';
  115.                 LED(0) <= '0';
  116.             when going_idle =>
  117.                 lock <= '1';
  118.                 err <= '0';
  119.                 LED(0) <= '0';
  120.             when wrong_code =>
  121.                 lock <= '1';
  122.                 err <= '1';
  123.                 LED(0) <= '0';
  124.             when perm_locked =>
  125.                 lock <= '1';
  126.                 err <= '0';
  127.                 LED(0) <= '1';
  128.         end case;
  129.         end if;
  130.     end process;
  131.    
  132. end fsm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement