summaryrefslogtreecommitdiffstats
path: root/debounce.v
blob: 206ea5e40bb5dbc89faacc2997501479433cd520 (plain)
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
// -----------------------------------------------------------------------
//
//   Copyright 2004-2010 H. Peter Anvin - All Rights Reserved
//
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
//   Bostom MA 02111-1307, USA; either version 2 of the License, or
//   (at your option) any later version; incorporated herein by reference.
//
// -----------------------------------------------------------------------

//
// Parameterized debouncer module
//
module debounce_bit
  #(parameter count = 4)
  (
   input              clk,
   input              in,
   output             out,
   output             strobe	// Set to 1 when out just changed
   );

   reg 		      d   = 1'bx;
   reg [1:0] 	      q   = 2'bxx;
   reg [count-1:0]    ctr = {1'b1, {(count-1){1'b0}}};

   assign    out = q[0];
   assign    strobe = q[0] ^ q[1];

   always @(posedge clk)
     begin
	q[1] <= q[0];
	d    <= in;

	if ( d )
	  begin
	     if ( &ctr )
	       q[0] <= 1'b1;
	     else
	       ctr <= ctr + 1'b1;
	  end
	else
	  begin
	     if ( ~|ctr )
	       q[0] <= 1'b0;
	     else
	       ctr <= ctr - 1'b1;
	  end // else: !if( d )
     end // always @ (posedge clk)
endmodule // debounce_bit

module debounce
  #(parameter width = 1,
    parameter count = 4)	// 2^count bits needed in a row
  (
   input              clk,
   input  [width-1:0] in,
   output [width-1:0] out,
   output [width-1:0] strobe	// Set to 1 when out just changed
   );

   debounce_bit #(.count(count)) b[width-1:0]
     (
      .clk	( clk ),
      .in	( in ),
      .out	( out ),
      .strobe	( strobe )
      );

endmodule // debounce