Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library ieee;
- use ieee.std_logic_1164.all;
- use ieee.numeric_std.all;
- entity code_lock_simple is
- port(
- enter, clk, reset: in std_logic;
- code : in std_logic_vector(3 downto 0);
- LED : out std_logic_vector(3 downto 0);
- lock , err : out std_logic
- );
- end entity;
- architecture fsm of code_lock_simple is
- type state_type is (idle, ECode1, GCode2, ECode2, unlocked, going_idle, wrong_code, perm_locked);
- signal state, next_state : state_type;
- signal err_event, failed : std_logic;
- signal retry_errors, next_retry_errors : unsigned(2 downto 0):= "000";
- begin
- process (clk)
- begin
- if reset = '0' then
- state <= idle;
- retry_errors <= "000";
- elsif rising_edge(clk) then
- state <= next_state;
- retry_errors <= next_retry_errors;
- end if;
- end process;
- process (state, enter)
- begin
- case state is
- when idle =>
- if enter = '0' then
- next_state <= ECode1;
- else
- next_state <= state;
- end if;
- when ECode1 =>
- if code = "1010" and enter = '1' then
- next_state <= GCode2;
- elsif code = "1010" and enter = '0' then
- next_state <= state;
- else
- next_state <= wrong_code;
- end if;
- when GCode2 =>
- if enter = '0' then
- next_state <= ECode2;
- else
- next_state <= state;
- end if;
- when ECode2 =>
- if code = "0101" and enter = '1' then
- next_state <= unlocked;
- elsif code = "0101" and enter = '0' then
- next_state <= state;
- else
- next_state <= wrong_code;
- end if;
- when unlocked =>
- if enter = '0' then
- next_state <= going_idle;
- else
- next_state <= state;
- end if;
- when going_idle =>
- if enter = '1' then
- next_state <= idle;
- else
- next_state <= state;
- end if;
- when wrong_code =>
- if enter = '0' then
- next_state <= going_idle;
- next_retry_errors <= retry_errors + 1;
- if (retry_errors > 3) then
- next_state <= perm_locked;
- end if;
- else
- next_state <= state;
- end if;
- when perm_locked =>
- next_state <= perm_locked;
- end case;
- end process;
- process (state, reset)
- begin
- if reset = '0' then
- err <= '0';
- else
- case state is
- when idle =>
- lock <= '1';
- err <= '0';
- LED(0) <= '0';
- when ECode1 =>
- lock <= '1';
- err <= '0';
- LED(0) <= '0';
- when GCode2 =>
- lock <= '1';
- err <= '0';
- LED(0) <= '0';
- when ECode2 =>
- lock <= '1';
- err <= '0';
- LED(0) <= '0';
- when unlocked =>
- lock <= '0';
- err <= '0';
- LED(0) <= '0';
- when going_idle =>
- lock <= '1';
- err <= '0';
- LED(0) <= '0';
- when wrong_code =>
- lock <= '1';
- err <= '1';
- LED(0) <= '0';
- when perm_locked =>
- lock <= '1';
- err <= '0';
- LED(0) <= '1';
- end case;
- end if;
- end process;
- end fsm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement