Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------ code_lock ------
- library ieee;
- use ieee.std_logic_1164.all;
- use ieee.numeric_std.all;
- entity code_lock is
- port(
- enter : in std_logic;
- reset : in std_logic;
- clk : in std_logic;
- code : in std_logic_vector(3 downto 0);
- lock : out std_logic;
- err : out std_logic;
- err_hex : out std_logic_vector(3 downto 0));
- end code_lock;
- architecture state_machine of code_lock is
- type state is(idle, evaluating_code_1, getting_code_2, -- forskellige states defineres
- evaluating_code_2, unlocked, going_idle, wrong_code,
- permanently_locked);
- type wrong is(err_00, err_11, err_22, err_33);
- signal present_wrong, next_wrong : wrong;
- signal present_state, next_state : state;
- constant code1 : std_logic_vector(3 downto 0) := "1111"; -- code1 og code2 hardcodes i programmet
- constant code2 : std_logic_vector(3 downto 0) := "0000";
- signal err_cnt : integer range 0 to 3 := 0;
- signal err_event : std_logic;
- begin
- state_reg: process(clk, reset) -- state register defineres
- begin
- if reset = '0' then
- present_state <= idle;
- present_wrong <= err_00;
- elsif rising_edge(clk) then
- present_state <= next_state;
- present_wrong <= next_wrong;
- end if;
- end process;
- nxt_state: process(present_state, enter, code, err_cnt)
- begin
- err_event <= '0';
- next_state <= present_state; -- next_state defineres ud fra ibd
- case present_state is
- when idle =>
- if enter = '0' then
- next_state <= evaluating_code_1;
- end if;
- when evaluating_code_1 =>
- if (enter = '1' and code = code1) then
- next_state <= getting_code_2;
- elsif (enter = '1' and code = NOT code1) then
- next_state <= wrong_code;
- err_event <= '1';
- end if;
- when getting_code_2 =>
- if enter = '0' then
- next_state <= evaluating_code_2;
- end if;
- when evaluating_code_2 =>
- if (enter = '1' and code = code2) then
- next_state <= unlocked;
- elsif (enter = '1' and code = NOT code2) then
- next_state <= wrong_code;
- err_event <= '1';
- end if;
- when unlocked =>
- if enter = '0' then
- next_state <= going_idle;
- end if;
- when wrong_code =>
- if (enter = '0' and err_cnt = 3) then
- next_state <= permanently_locked;
- elsif (enter ='0' and err_cnt /= 3) then
- next_state <= going_idle;
- end if;
- when going_idle =>
- if enter = '1' then
- next_state <= idle;
- end if;
- when permanently_locked =>
- NULL;
- end case;
- end process;
- outputs: process(present_state) -- outputs defineres ud fra ibd
- begin
- case present_state is
- when unlocked =>
- lock <= '0';
- when others =>
- lock <= '1';
- end case;
- end process;
- nxt_wrong: process(present_wrong, present_state, err_event)
- begin
- next_wrong <= present_wrong; -- next_state defineres ud fra ibd
- if err_event = '1' then
- case present_wrong is
- when err_00 =>
- next_wrong <= err_11;
- when err_11 =>
- next_wrong <= err_22;
- when err_22 =>
- next_wrong <= err_33;
- when err_33 =>
- NULL;
- end case;
- end if;
- end process;
- W_outut: process(present_wrong) -- outputs defineres ud fra ibd
- begin
- case present_wrong is
- when err_33 =>
- err_cnt <= 3;
- err <= '1';
- when err_00 =>
- err_cnt <= 0;
- err <= '0';
- when err_11 =>
- err_cnt <= 1;
- err <= '0';
- when err_22 =>
- err_cnt <= 2;
- err <= '0';
- end case;
- end process;
- err_hex <= std_logic_vector(to_unsigned(err_cnt,4)); -- display af antal fejl
- end state_machine;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement