-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButtonSync.sv
96 lines (72 loc) · 1.93 KB
/
ButtonSync.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
module ButtonSync( Clk, Bis, Bo );
input Bis; // unregistered input button press
input Clk; // system clock
output logic Bo; // our output
logic Bi_; // can introduce more to register
// State assignments
localparam S_A = 2'h0,
S_B = 2'h1,
S_C = 2'h2,
S_D = 2'h3;
logic [1:0] State = S_A, StateNext;
logic Bi = 0; // registered input
// CombLogic
always_comb begin
Bo = 0; // default
case ( State )
S_A: begin
if ( Bi )
StateNext = S_B; // button push detected
else
StateNext = S_A;
end
S_B: begin
Bo = 1; // turn output ON
if ( Bi )
StateNext = S_C;
else
StateNext = S_A;
end
S_C: begin
if ( Bi )
StateNext = S_C; // stay in this state
else
StateNext = S_A; // otherwise, back to A
end
S_D: begin // the only other possible value of State
Bo = 0;
StateNext = S_A;
end
endcase
end // always
// StateReg
always_ff @( posedge Clk ) begin
Bi_ <= Bis;
Bi <= Bi_;
State <= StateNext; // otherwise go to the state we set
end // always
endmodule
//********************************************//
// Testbench //
//********************************************//
module ButtonSyncReg_testbench;
logic Clock,
ButtonIn,
ButtonOut;
ButtonSyncReg DUT( Clock, ButtonIn, ButtonOut );
// develop a clock (50 MHz)
always begin
Clock <= 0;
#10;
Clock <= 1;
#10;
end
initial // Test stimulus
begin
ButtonIn = 0;
#100 ButtonIn = 1;
#110 ButtonIn = 0;
#100 $stop;
end
// view waveforms
endmodule