Advertisement
hbinderup94

code_lock

May 26th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 3.54 KB | None | 0 0
  1. ------ code_lock ------
  2. library ieee;
  3. use ieee.std_logic_1164.all;
  4. use ieee.numeric_std.all;
  5.  
  6. entity code_lock is
  7. port(
  8.     enter   : in std_logic;
  9.     reset   : in std_logic;
  10.     clk     : in std_logic;
  11.     code    : in std_logic_vector(3 downto 0);
  12.     lock    : out std_logic;
  13.     err     : out std_logic;
  14.     err_hex : out std_logic_vector(3 downto 0));
  15. end code_lock;
  16.  
  17. architecture state_machine of code_lock is
  18.     type        state is(idle, evaluating_code_1, getting_code_2, -- forskellige states defineres
  19.                             evaluating_code_2, unlocked, going_idle, wrong_code,
  20.                             permanently_locked);
  21.     type        wrong is(err_00, err_11, err_22, err_33);
  22.     signal  present_wrong, next_wrong : wrong;
  23.     signal  present_state, next_state : state;
  24.     constant    code1   : std_logic_vector(3 downto 0) := "1111";   -- code1 og code2 hardcodes i programmet
  25.     constant    code2   : std_logic_vector(3 downto 0) := "0000";
  26.     signal  err_cnt     : integer range 0 to 3 := 0;
  27.     signal  err_event   : std_logic;
  28. begin
  29.  
  30.     state_reg: process(clk, reset)  -- state register defineres
  31.     begin
  32.         if reset = '0' then
  33.             present_state <= idle;
  34.             present_wrong <= err_00;
  35.         elsif rising_edge(clk) then
  36.             present_state <= next_state;
  37.             present_wrong <= next_wrong;
  38.         end if;
  39.     end process;
  40.    
  41.     nxt_state: process(present_state, enter, code, err_cnt)
  42.     begin
  43.         err_event <= '0';
  44.         next_state <= present_state;    -- next_state defineres ud fra ibd
  45.         case present_state is
  46.             when idle =>
  47.                 if enter = '0' then
  48.                     next_state  <= evaluating_code_1;
  49.                 end if;
  50.             when evaluating_code_1 =>
  51.                 if (enter = '1' and code = code1) then
  52.                     next_state  <= getting_code_2;
  53.                 elsif (enter = '1' and code = NOT code1) then
  54.                     next_state  <= wrong_code;
  55.                     err_event   <= '1';
  56.                 end if;
  57.             when getting_code_2 =>
  58.                 if enter = '0' then
  59.                     next_state  <= evaluating_code_2;
  60.                 end if;
  61.             when evaluating_code_2 =>
  62.                 if (enter = '1' and code = code2) then
  63.                     next_state  <= unlocked;
  64.                 elsif (enter = '1' and code = NOT code2) then
  65.                     next_state  <= wrong_code;
  66.                     err_event   <= '1';
  67.                 end if;
  68.             when unlocked =>
  69.                 if enter = '0' then
  70.                     next_state  <= going_idle;
  71.                 end if;
  72.             when wrong_code =>
  73.                 if (enter = '0' and err_cnt = 3) then
  74.                     next_state  <= permanently_locked;
  75.                 elsif (enter ='0' and err_cnt /= 3) then
  76.                     next_state  <= going_idle;
  77.                 end if;
  78.             when going_idle =>
  79.                 if enter = '1' then
  80.                     next_state  <= idle;
  81.                 end if;
  82.             when permanently_locked =>
  83.                 NULL;
  84.         end case;
  85.     end process;
  86.    
  87.     outputs: process(present_state) -- outputs defineres ud fra ibd
  88.     begin
  89.         case present_state is
  90.             when unlocked =>
  91.                 lock <= '0';
  92.             when others =>
  93.                 lock <= '1';
  94.         end case;
  95.     end process;
  96.    
  97.    
  98.     nxt_wrong: process(present_wrong, present_state, err_event)
  99.     begin
  100.         next_wrong <= present_wrong;    -- next_state defineres ud fra ibd
  101.         if err_event = '1' then
  102.             case present_wrong is
  103.                     when err_00 =>
  104.                         next_wrong <= err_11;
  105.                     when err_11 =>
  106.                         next_wrong <= err_22;
  107.                     when err_22 =>
  108.                         next_wrong <= err_33;
  109.                     when err_33 =>
  110.                         NULL;
  111.             end case;
  112.         end if;
  113.     end process;
  114.                
  115.     W_outut: process(present_wrong) -- outputs defineres ud fra ibd
  116.     begin
  117.         case present_wrong is
  118.             when err_33 =>
  119.                     err_cnt     <= 3;
  120.                     err         <= '1';
  121.             when err_00 =>
  122.                     err_cnt     <= 0;
  123.                     err         <= '0';
  124.             when err_11 =>
  125.                     err_cnt     <= 1;
  126.                     err         <= '0';
  127.             when err_22 =>
  128.                     err_cnt     <= 2;
  129.                     err         <= '0';
  130.         end case;
  131.     end process;
  132.    
  133.     err_hex <= std_logic_vector(to_unsigned(err_cnt,4)); -- display af antal fejl
  134.    
  135. end state_machine;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement