forked from bogini/Pong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debounce.v
94 lines (87 loc) · 2.41 KB
/
debounce.v
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
// Listing 6.2
module debounce
(
input wire clk, reset,
input wire sw,
output reg db_level, db_tick
);
// symbolic state declaration
localparam [1:0]
zero = 2'b00,
wait0 = 2'b01,
one = 2'b10,
wait1 = 2'b11;
// number of counter bits (2^N * 20ns = 40ms)
localparam N=21;
// signal declaration
reg [N-1:0] q_reg, q_next;
reg [1:0] state_reg, state_next;
// body
// fsmd state & data registers
always @(posedge clk, posedge reset)
if (reset)
begin
state_reg <= zero;
q_reg <= 0;
end
else
begin
state_reg <= state_next;
q_reg <= q_next;
end
// next-state logic & data path functional units/routing
always @*
begin
state_next = state_reg; // default state: the same
q_next = q_reg; // default q: unchnaged
db_tick = 1'b0; // default output: 0
case (state_reg)
zero:
begin
db_level = 1'b0;
if (sw)
begin
state_next = wait1;
q_next = {N{1'b1}}; // load 1..1
end
end
wait1:
begin
db_level = 1'b0;
if (sw)
begin
q_next = q_reg - 1;
if (q_next==0)
begin
state_next = one;
db_tick = 1'b1;
end
end
else // sw==0
state_next = zero;
end
one:
begin
db_level = 1'b1;
if (~sw)
begin
state_next = wait0;
q_next = {N{1'b1}}; // load 1..1
end
end
wait0:
begin
db_level = 1'b1;
if (~sw)
begin
q_next = q_reg - 1;
if (q_next==0)
state_next = zero;
end
else // sw==1
state_next = one;
end
default: state_next = zero;
endcase
end
endmodule