forked from pConst/basic_verilog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debounce.v
66 lines (51 loc) · 1.38 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
//--------------------------------------------------------------------------------
// debounce.v
// Konstantin Pavlov, [email protected]
//--------------------------------------------------------------------------------
// INFO --------------------------------------------------------------------------------
// Debounce for two inpus signal samples
// Signal may and maynot be periodic
// Switches up and down with 3 ticks delay
/* --- INSTANTIATION TEMPLATE BEGIN ---
debounce DB1 (
.clk(),
.nrst( 1'b1 ),
.en( 1'b1 ),
.in(),
.out()
);
defparam DB1.WIDTH = 1;
--- INSTANTIATION TEMPLATE END ---*/
module debounce(clk,nrst,en,in,out);
input wire clk;
input wire nrst;
input wire en;
input wire [(WIDTH-1):0] in;
output wire [(WIDTH-1):0] out; // also "present state"
parameter WIDTH = 1;
reg [(WIDTH-1):0] d1 = 0;
reg [(WIDTH-1):0] d2 = 0;
always @ (posedge clk) begin
if (~nrst) begin
d1[(WIDTH-1):0] <= 0;
d2[(WIDTH-1):0] <= 0;
end
else begin
if (en) begin
d1[(WIDTH-1):0] <= d2[(WIDTH-1):0];
d2[(WIDTH-1):0] <= in[(WIDTH-1):0];
end; // if
end // else
end
wire [(WIDTH-1):0] switch_hi = (d2[(WIDTH-1):0] & d1[(WIDTH-1):0]);
wire [(WIDTH-1):0] n_switch_lo = (d2[(WIDTH-1):0] | d1[(WIDTH-1):0]);
SetReset SR (
.clk(clk),
.nrst(nrst),
.s(switch_hi[(WIDTH-1):0]),
.r(~n_switch_lo[(WIDTH-1):0]),
.q(out[(WIDTH-1):0]),
.nq()
);
defparam SR.WIDTH = WIDTH;
endmodule