-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebounce_2.vhd
102 lines (96 loc) · 2.67 KB
/
debounce_2.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date: 10:12:41 12/30/2022
-- Design Name:
-- Module Name: debounce_2 - Behavioral
-- Project Name:
-- Target Devices:
-- Tool versions:
-- Description:
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
----------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
entity debounce_2 is
port(
clk, reset: in std_logic;
sw: in std_logic;
db_level, db_tick: out std_logic
);
end debounce_2;
architecture fsmd_arch of debounce_2 is
constant N: integer:=21; -- filter of 2^N * 20ns = 40ms
type state_type is (zero, wait0, one, wait1);
signal state_reg, state_next: state_type;
signal q_reg, q_next: unsigned(N-1 downto 0);
begin
-- FSMD state & data registers
process(clk,reset)
begin
if reset='1' then
state_reg <= zero;
q_reg <= (others=>'0');
elsif (clk'event and clk='1') then
state_reg <= state_next;
q_reg <= q_next;
end if;
end process;
-- next-state logic & data path functional units/routing
process(state_reg,q_reg,sw,q_next)
begin
state_next <= state_reg;
q_next <= q_reg;
db_tick <= '0';
case state_reg is
when zero =>
db_level <= '0';
if (sw='1') then
state_next <= wait1;
q_next <= (others=>'1');
end if;
when wait1=>
db_level <= '0';
if (sw='1') then
q_next <= q_reg - 1;
if (q_next=0) then
state_next <= one;
db_tick <= '1';
end if;
else -- sw='0'
state_next <= zero;
end if;
when one =>
db_level <= '1';
if (sw='0') then
state_next <= wait0;
q_next <= (others=>'1');
end if;
when wait0=>
db_level <= '1';
if (sw='0') then
q_next <= q_reg - 1;
if (q_next=0) then
state_next <= zero;
end if;
else -- sw='1'
state_next <= one;
end if;
end case;
end process;
end fsmd_arch;