2015-12-14 21:13:15 +03:00
|
|
|
//--------------------------------------------------------------------------------
|
2019-02-23 00:20:06 +03:00
|
|
|
// debounce.v
|
2015-12-14 21:13:15 +03:00
|
|
|
// Konstantin Pavlov, pavlovconst@gmail.com
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// INFO --------------------------------------------------------------------------------
|
2019-02-23 00:20:06 +03:00
|
|
|
// Debounce for two inpus signal samples
|
|
|
|
// Signal may and maynot be periodic
|
2016-12-12 11:41:56 +03:00
|
|
|
// Switches up and down with 3 ticks delay
|
2015-12-14 21:13:15 +03:00
|
|
|
|
2016-01-01 22:39:14 +03:00
|
|
|
|
2016-12-12 11:41:56 +03:00
|
|
|
/* --- INSTANTIATION TEMPLATE BEGIN ---
|
|
|
|
|
2019-02-23 00:20:06 +03:00
|
|
|
debounce DB1 (
|
2016-01-01 22:39:14 +03:00
|
|
|
.clk(),
|
2016-12-12 11:41:56 +03:00
|
|
|
.nrst( 1'b1 ),
|
|
|
|
.en( 1'b1 ),
|
2016-01-01 22:39:14 +03:00
|
|
|
.in(),
|
|
|
|
.out()
|
|
|
|
);
|
2016-12-12 11:41:56 +03:00
|
|
|
defparam DB1.WIDTH = 1;
|
|
|
|
|
|
|
|
--- INSTANTIATION TEMPLATE END ---*/
|
2016-01-01 22:39:14 +03:00
|
|
|
|
|
|
|
|
2019-02-23 00:20:06 +03:00
|
|
|
module debounce(clk,nrst,en,in,out);
|
2015-12-14 21:13:15 +03:00
|
|
|
|
|
|
|
input wire clk;
|
|
|
|
input wire nrst;
|
2016-01-19 19:23:27 +03:00
|
|
|
input wire en;
|
|
|
|
|
2015-12-14 21:13:15 +03:00
|
|
|
input wire [(WIDTH-1):0] in;
|
|
|
|
output wire [(WIDTH-1):0] out; // also "present state"
|
|
|
|
|
|
|
|
parameter WIDTH = 1;
|
|
|
|
|
2016-01-19 19:23:27 +03:00
|
|
|
reg [(WIDTH-1):0] d1 = 0;
|
|
|
|
reg [(WIDTH-1):0] d2 = 0;
|
2015-12-14 21:13:15 +03:00
|
|
|
|
|
|
|
always @ (posedge clk) begin
|
|
|
|
if (~nrst) begin
|
2016-01-19 19:23:27 +03:00
|
|
|
d1[(WIDTH-1):0] <= 0;
|
|
|
|
d2[(WIDTH-1):0] <= 0;
|
2015-12-14 21:13:15 +03:00
|
|
|
end
|
|
|
|
else begin
|
2016-01-19 19:23:27 +03:00
|
|
|
if (en) begin
|
|
|
|
d1[(WIDTH-1):0] <= d2[(WIDTH-1):0];
|
|
|
|
d2[(WIDTH-1):0] <= in[(WIDTH-1):0];
|
|
|
|
end; // if
|
|
|
|
end // else
|
2015-12-14 21:13:15 +03:00
|
|
|
end
|
|
|
|
|
2016-01-19 19:23:27 +03:00
|
|
|
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]);
|
2019-02-23 00:20:06 +03:00
|
|
|
|
2016-01-19 19:23:27 +03:00
|
|
|
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()
|
|
|
|
);
|
2015-12-14 21:13:15 +03:00
|
|
|
defparam SR.WIDTH = WIDTH;
|
|
|
|
|
|
|
|
endmodule
|