1
0
mirror of https://github.com/pConst/basic_verilog.git synced 2025-01-14 06:42:54 +08:00
basic_verilog/DeBounce.v

65 lines
1.4 KiB
Coq
Raw Normal View History

2015-12-14 21:13:15 +03:00
//--------------------------------------------------------------------------------
// DeBounce.v
// Konstantin Pavlov, pavlovconst@gmail.com
//--------------------------------------------------------------------------------
// INFO --------------------------------------------------------------------------------
// Debounce for two inpus signal samples. Signal may and maynot be periodic
// Switches up and down with 3 ticks delay
2015-12-14 21:13:15 +03:00
/* --- INSTANTIATION TEMPLATE BEGIN ---
DeBounce DB1 (
.clk(),
.nrst( 1'b1 ),
.en( 1'b1 ),
.in(),
.out()
);
defparam DB1.WIDTH = 1;
--- INSTANTIATION TEMPLATE END ---*/
2016-01-19 19:23:27 +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]);
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