-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebounce.sv
92 lines (78 loc) · 2.07 KB
/
debounce.sv
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Engineer: Mike Evans
//
// Create Date: 01/15/2017 09:34:59 PM
// Design Name: Arty LED Test Control Module
// Module Name: led_ctrl
// Project Name: Arty LED Test
// Target Devices: Digilient Arty Dev Board
// Tool Versions: Vivado 2016.4
// Description: This module controls the LED's colors and brightness levels.
//
// Dependencies: led_top.sv, debounce.sv, Arty_sw_btn_led.xdc (constraints file, 1/1)
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module debounce (
sclk,
resetn,
signal_in,
signal_out
);
parameter WIDTH = 1;
input sclk;
input resetn;
input [WIDTH-1:0] signal_in;
output logic [WIDTH-1:0] signal_out;
logic [WIDTH-1:0] signal_1d;
logic [WIDTH-1:0] signal_2d;
always_ff @(posedge sclk) begin
if(!resetn) begin
signal_1d <= 'd0;
signal_2d <= 'd0;
end
else begin
signal_1d <= signal_in;
signal_2d <= signal_1d;
end
end
//1ms = 1000ns
//Sample the button every 20 ms = 20000ns = 2000 clocks
logic [10:0] counter;
always_ff @(posedge sclk) begin
if(!resetn) counter <= 'd0;
else counter <= counter + 1'b1;
end
wire sample = counter == 2000;
logic state[WIDTH];
logic next_state[WIDTH];
always_comb begin
for(int i=0; i<WIDTH; i++) begin
case(state[i])
1'b0: begin
if(signal_2d[i]) next_state[i] = 1'b1;
else next_state[i] = 1'b0;
end
1'b1: begin
if(!signal_2d[i]) next_state[i] = 1'b0;
else next_state[i] = 1'b1;
end
endcase
end
end
always_ff @(posedge sclk) begin
for(int i=0; i<WIDTH; i++) begin
if(!resetn) state[i] <= 1'b0;
else if(sample) state[i] <= next_state[i];
end
end
always_comb begin
for(int i=0; i<WIDTH; i++) begin
signal_out[i] = state[i] == 1'b1;
end
end
endmodule