summaryrefslogtreecommitdiff
path: root/debounce.v
blob: 704a58825a9e894f6c033a79bf549bf2e5a7e933 (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
module debounce (
  input rst_i,
  input clk_i,
  input signal_i,
  output reg signal_o
);

parameter STABLE_PERIOD = 50;
parameter INIT_SIG_STATE = 1'b1;

reg [31:0] clk_counter;
reg prev_state;

always @(posedge clk_i or negedge rst_i) begin
  if (!rst_i) begin
    clk_counter <= 0;
    prev_state <= INIT_SIG_STATE;
    signal_o <= INIT_SIG_STATE;
  end else begin
    clk_counter <= clk_counter + 1;

    if (signal_i != prev_state) begin
      clk_counter <= 0;
      prev_state <= signal_i;
    end

    if (clk_counter == STABLE_PERIOD) begin
      signal_o <= signal_i;
    end
  end
end

// Learning?
// Apparently, (and obviously, when you think about it),
// it's not possible to drive a signal by two blocks
// always @(signal_i) begin
//   if (signal_i != prev_state) begin
//     clk_counter <= 0;
//     prev_state <= signal_i;
//   end
// end

endmodule