debouncing sys_res_n
[hwmod.git] / src / debouncing / debounce_fsm_beh.vhd
diff --git a/src/debouncing/debounce_fsm_beh.vhd b/src/debouncing/debounce_fsm_beh.vhd
new file mode 100644 (file)
index 0000000..f30d43b
--- /dev/null
@@ -0,0 +1,71 @@
+library ieee;\r
+use ieee.std_logic_1164.all;\r
+use ieee.numeric_std.all;\r
+\r
+architecture beh of debounce_fsm is\r
+  type DEBOUNCE_FSM_STATE_TYPE is\r
+    (IDLE0, TIMEOUT0, IDLE1, TIMEOUT1);\r
+  signal debounce_fsm_state, debounce_fsm_state_next : DEBOUNCE_FSM_STATE_TYPE;\r
+begin\r
+  next_state : process(debounce_fsm_state, i, cnt)\r
+  begin\r
+    debounce_fsm_state_next <= debounce_fsm_state;\r
+    case debounce_fsm_state is\r
+      when IDLE0 =>\r
+        if i = '1' then\r
+          debounce_fsm_state_next <= TIMEOUT0;\r
+        end if;\r
+      when TIMEOUT0 =>\r
+        if i = '0' then\r
+          debounce_fsm_state_next <= IDLE0;\r
+        elsif to_integer(unsigned(cnt)) = CNT_MAX then\r
+          debounce_fsm_state_next <= IDLE1;\r
+        end if;\r
+      when IDLE1 =>\r
+        if i = '0' then\r
+          debounce_fsm_state_next <= TIMEOUT1;\r
+        end if;\r
+      when TIMEOUT1 =>\r
+        if i = '1' then\r
+          debounce_fsm_state_next <= IDLE1;\r
+        elsif to_integer(unsigned(cnt)) = CNT_MAX then\r
+          debounce_fsm_state_next <= IDLE0;\r
+        end if;\r
+    end case;\r
+  end process next_state;\r
+\r
+  output : process(debounce_fsm_state)\r
+  begin\r
+    o <= RESET_VALUE;\r
+    clear_cnt <= '1';\r
+\r
+    case debounce_fsm_state is\r
+      when IDLE0 =>\r
+        o <= '0';\r
+      when TIMEOUT0 =>\r
+        o <= '0';\r
+        clear_cnt <= '0';\r
+      when IDLE1 =>\r
+        o <= '1';\r
+      when TIMEOUT1 =>\r
+        o <= '1';\r
+        clear_cnt <= '0';\r
+    end case;\r
+  end process output;\r
+\r
+  assert RESET_VALUE = '0' or RESET_VALUE = '1' report\r
+    "RESET_VALUE may only be 0 or 1!" severity failure;\r
+\r
+  sync : process(sys_clk, sys_res_n)\r
+  begin\r
+    if sys_res_n = '0' then\r
+      if RESET_VALUE = '0' then\r
+        debounce_fsm_state <= IDLE0;\r
+      else\r
+        debounce_fsm_state <= IDLE1;\r
+      end if;\r
+    elsif rising_edge(sys_clk) then\r
+      debounce_fsm_state <= debounce_fsm_state_next;\r
+    end if;\r
+  end process sync;\r
+end architecture beh;\r