debouncing sys_res_n
[hwmod.git] / src / debouncing / debounce_fsm_beh.vhd
1 library ieee;\r
2 use ieee.std_logic_1164.all;\r
3 use ieee.numeric_std.all;\r
4 \r
5 architecture beh of debounce_fsm is\r
6   type DEBOUNCE_FSM_STATE_TYPE is\r
7     (IDLE0, TIMEOUT0, IDLE1, TIMEOUT1);\r
8   signal debounce_fsm_state, debounce_fsm_state_next : DEBOUNCE_FSM_STATE_TYPE;\r
9 begin\r
10   next_state : process(debounce_fsm_state, i, cnt)\r
11   begin\r
12     debounce_fsm_state_next <= debounce_fsm_state;\r
13     case debounce_fsm_state is\r
14       when IDLE0 =>\r
15         if i = '1' then\r
16           debounce_fsm_state_next <= TIMEOUT0;\r
17         end if;\r
18       when TIMEOUT0 =>\r
19         if i = '0' then\r
20           debounce_fsm_state_next <= IDLE0;\r
21         elsif to_integer(unsigned(cnt)) = CNT_MAX then\r
22           debounce_fsm_state_next <= IDLE1;\r
23         end if;\r
24       when IDLE1 =>\r
25         if i = '0' then\r
26           debounce_fsm_state_next <= TIMEOUT1;\r
27         end if;\r
28       when TIMEOUT1 =>\r
29         if i = '1' then\r
30           debounce_fsm_state_next <= IDLE1;\r
31         elsif to_integer(unsigned(cnt)) = CNT_MAX then\r
32           debounce_fsm_state_next <= IDLE0;\r
33         end if;\r
34     end case;\r
35   end process next_state;\r
36 \r
37   output : process(debounce_fsm_state)\r
38   begin\r
39     o <= RESET_VALUE;\r
40     clear_cnt <= '1';\r
41 \r
42     case debounce_fsm_state is\r
43       when IDLE0 =>\r
44         o <= '0';\r
45       when TIMEOUT0 =>\r
46         o <= '0';\r
47         clear_cnt <= '0';\r
48       when IDLE1 =>\r
49         o <= '1';\r
50       when TIMEOUT1 =>\r
51         o <= '1';\r
52         clear_cnt <= '0';\r
53     end case;\r
54   end process output;\r
55 \r
56   assert RESET_VALUE = '0' or RESET_VALUE = '1' report\r
57     "RESET_VALUE may only be 0 or 1!" severity failure;\r
58 \r
59   sync : process(sys_clk, sys_res_n)\r
60   begin\r
61     if sys_res_n = '0' then\r
62       if RESET_VALUE = '0' then\r
63         debounce_fsm_state <= IDLE0;\r
64       else\r
65         debounce_fsm_state <= IDLE1;\r
66       end if;\r
67     elsif rising_edge(sys_clk) then\r
68       debounce_fsm_state <= debounce_fsm_state_next;\r
69     end if;\r
70   end process sync;\r
71 end architecture beh;\r