-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiezo_drv.sv
186 lines (158 loc) · 3.13 KB
/
piezo_drv.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
module piezo_drv(clk, rst_n, batt_low, fanfare, piezo, piezo_n);
parameter FAST_SIM = 1;
input logic clk, rst_n, batt_low, fanfare;
output logic piezo, piezo_n;
logic [23:0] dur_cnt;
logic [14:0] freq_cnt;
logic rst_dur, rst_freq;
typedef enum logic[3:0] {IDLE, G6, G7,C7,E7, E7_LOWBAT, G6_LOWBAT, C7_LOWBAT, G7_2, E7_2} state_t;
always_ff @(posedge clk) begin
if(rst_dur)
dur_cnt <= '0;
else
dur_cnt <= (FAST_SIM)? dur_cnt + 16: dur_cnt + 1;
end
always_ff @(posedge clk) begin
if(rst_freq)
freq_cnt <= '0;
else
freq_cnt <= (FAST_SIM)? freq_cnt + 16: freq_cnt + 1;
end
state_t state, nxt_state;
always_comb begin
piezo = 0;
nxt_state = state;
rst_freq = 0;
rst_dur = 0;
case (state)
IDLE: begin
if(batt_low) begin
nxt_state = G6_LOWBAT;
rst_freq = 1;
rst_dur = 1;
end
else if (fanfare) begin
nxt_state = G6;
rst_freq = 1;
rst_dur = 1;
end
end
G6_LOWBAT: begin
if (dur_cnt >= 8388608) begin
rst_dur = 1;
rst_freq = 1;
nxt_state = C7_LOWBAT;
end
else if(freq_cnt < 16000)
piezo = 1;
else if(freq_cnt > 31888) begin
rst_freq = 1;
end
end
C7_LOWBAT: begin
if (dur_cnt >= 8388608) begin
rst_dur = 1;
rst_freq =1;
nxt_state = E7_LOWBAT;
end
else if(freq_cnt < 12000)
piezo = 1;
else if(freq_cnt > 23890) begin
rst_freq = 1;
end
end
E7_LOWBAT: begin
if (dur_cnt >= 8388608) begin
rst_dur = 1;
rst_freq =1;
nxt_state = IDLE;
end
else if(freq_cnt < 9500)
piezo = 1;
else if(freq_cnt > 18960) begin
rst_freq = 1;
end
end
G6: begin
if(dur_cnt > 8388608) begin
rst_dur = 1;
rst_freq = 1;
nxt_state = C7;
end
else if(freq_cnt < 16000)
piezo = 1;
else if(freq_cnt > 31888) begin
rst_freq = 1;
end
end
C7: begin
if (dur_cnt >= 8388608) begin
rst_dur = 1;
rst_freq =1;
nxt_state = E7;
end
else if(freq_cnt < 12000)
piezo = 1;
else if(freq_cnt > 23890) begin
rst_freq = 1;
end
end
E7: begin
if (dur_cnt >= 8388608) begin
rst_dur = 1;
rst_freq =1;
nxt_state = G7;
end
else if(freq_cnt < 9500)
piezo = 1;
else if(freq_cnt > 18960) begin
rst_freq = 1;
end
end
G7: begin
if(dur_cnt > 12582912) begin
rst_dur = 1;
rst_freq = 1;
nxt_state = E7_2;
end
else if(freq_cnt < 7971)
piezo = 1;
else if(freq_cnt > 15943) begin
rst_freq = 1;
end
end
E7_2: begin
if (dur_cnt >= 8388608) begin
rst_dur = 1;
rst_freq =1;
nxt_state = G7_2;
end
else if(freq_cnt < 9500)
piezo = 1;
else if(freq_cnt > 18960) begin
rst_freq = 1;
end
end
G7_2:begin
if(dur_cnt > 12582912) begin
rst_dur = 1;
rst_freq = 1;
nxt_state = IDLE;
end
else if(freq_cnt < 7971)
piezo = 1;
else if(freq_cnt > 15943) begin
rst_freq = 1;
end
end
default: nxt_state = IDLE;
endcase
end
always_ff @(posedge clk, negedge rst_n) begin
if(!rst_n)
state <= IDLE;
else
state <= nxt_state;
end
assign piezo_n = ~piezo;
endmodule