-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtb_debouncer2.sv
119 lines (85 loc) · 5.23 KB
/
tb_debouncer2.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*===============================================================================================================================
Module : Test Bench - Debouncer
Description : Emulates a simple switch to test Debouncer.
Developer : Mitu Raj, chip@chipmunklogic.com at Chipmunk Logic ™, https://chipmunklogic.com
Notes : -
License : Open-source.
Date : Dec-01-2022
===============================================================================================================================*/
/*-------------------------------------------------------------------------------------------------------------------------------
T E S T B E N C H - D E B O U N C E R
-------------------------------------------------------------------------------------------------------------------------------*/
// Timescale for simulation
`timescale 1ns / 100ps
module tb_debouncer2 () ;
/*-------------------------------------------------------------------------------------------------------------------------------
Local Parameters to configure test
-------------------------------------------------------------------------------------------------------------------------------*/
localparam real CLK_PERIOD = 1000000 ; // Clock period in ns
localparam real BOUNCING = 10 ; // Bouncing time of switch in ms
/*-------------------------------------------------------------------------------------------------------------------------------
Derived Parameters
-------------------------------------------------------------------------------------------------------------------------------*/
localparam BOUNCING_CYCLES = int'((BOUNCING / CLK_PERIOD) * 1000000) ; // Bouncing time in clock cycles
localparam N_THRESH = $clog2 (BOUNCING_CYCLES) ; // N_THRESH configuration at DUT = >50% BOUNCING
/*-------------------------------------------------------------------------------------------------------------------------------
Internal Registers/Signals
-------------------------------------------------------------------------------------------------------------------------------*/
logic clk, rstn, sig_in, sig_debounced ; // DUT signals
/*-------------------------------------------------------------------------------------------------------------------------------
DUT: Debouncer instance
-------------------------------------------------------------------------------------------------------------------------------*/
debouncer2 #(
.N_THRESH ( N_THRESH ) ,
.IS_PULLUP ( 1 )
)
debouncer_inst (
.clk ( clk ) ,
.rstn ( rstn ) ,
.i_sig ( sig_in ) ,
.o_sig_debounced ( sig_debounced )
) ;
/*-------------------------------------------------------------------------------------------------------------------------------
Clocking block
-------------------------------------------------------------------------------------------------------------------------------*/
always #(CLK_PERIOD / 2) clk = ~ clk ;
/*-------------------------------------------------------------------------------------------------------------------------------
Initial block to setup clock and reset, and trigger stimulus
-------------------------------------------------------------------------------------------------------------------------------*/
initial begin
clk = 1'b0 ;
rstn <= 1'b0 ;
sig_in <= 1'b0 ;
// Reset de-assertion
repeat (10) @(posedge clk) ;
rstn <= 1'b1 ;
// Bouncing and clean switching to high state
repeat (BOUNCING_CYCLES) @(posedge clk) sig_in <= ~ sig_in ;
sig_in <= 1'b1 ;
repeat (BOUNCING_CYCLES * 3) @(posedge clk) ;
// Bouncing and clean switching to low state
repeat (BOUNCING_CYCLES) @(posedge clk) sig_in <= ~ sig_in ;
sig_in <= 1'b0 ;
repeat (BOUNCING_CYCLES * 3) @(posedge clk) ;
// Bouncing and not switching
repeat (BOUNCING_CYCLES) @(posedge clk) sig_in <= ~ sig_in ;
sig_in <= 1'b1 ;
repeat (BOUNCING_CYCLES / 2) @(posedge clk) ;
// Bouncing and clean switching to high state
repeat (BOUNCING_CYCLES) @(posedge clk) sig_in <= ~ sig_in ;
sig_in <= 1'b1 ;
repeat (BOUNCING_CYCLES * 3) @(posedge clk) ;
// Bouncing and not switching
repeat (BOUNCING_CYCLES) @(posedge clk) sig_in <= ~ sig_in ;
sig_in <= 1'b0 ;
repeat (BOUNCING_CYCLES / 2) @(posedge clk) ;
// Bouncing and clean switching to low state
repeat (BOUNCING_CYCLES) @(posedge clk) sig_in <= ~ sig_in ;
sig_in <= 1'b0 ;
repeat (BOUNCING_CYCLES * 3) @(posedge clk) ;
$finish ;
end
endmodule
/*-------------------------------------------------------------------------------------------------------------------------------
T E S T B E N C H - D E B O U N C E R
-------------------------------------------------------------------------------------------------------------------------------*/