-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateMachine.sv
More file actions
387 lines (372 loc) · 10.7 KB
/
StateMachine.sv
File metadata and controls
387 lines (372 loc) · 10.7 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
module StateMachine (
input rst, input clk,
input mode_input, // button
input cancel_input, // button
//input up_input, // button - não é nenecessário para a maquina de estados
//input down_input, //button - não é nenecessário para a maquina de estados
input ok_input, // button
input rst_3, // counter 3s reset pressed
input alarm_match, // compare alarm register with clock
input counter_end, // counter stopwatch is zero
output buzer_output, // play buzzer
output timer_output, // led - determina o sign tbm - determina o display também
output stopwatch_output, // led - determina o display também
output alarm_on_output, // era o led - 1 liga o alarme
output alarm_off_output, // 1 desliga o alarme
output [1:0]count_sel, // mux sel - determina o copy - nao seleciona o display do timer
output set_left, // enable set left
output set_right, // enable set right
output blink_dots, // enable blink display - quando os dois lados estiverem sem alteração, os dois piscam
output copy, // enable direct in
output counting, // counting_enable
output mst_rst, // master reset
output cnt_rst // counter stopwatch reset
,output [4:0]temp_state
);
//Estados da máquina
enum int unsigned { clock_state=0, rst_state_hour=1, rst_state_min=2, pre_set_clock=3, pos_set_clock=4, // função relogio
stopwatch_state=5, counter_state_up=6, rst_counter_state_up=7, // função cronometro
timer_state_min=8, timer_state_sec=9, counter_state_down=10, rst_counter_state_down=11, // função temporizador
alarm_state_hour=12, alarm_state_min=13, pre_set_alarm=14, pos_set_alarm=15, // funcção alarme
alarm_state_active=16, timer_alarm_active=17 // buzzer
} fstate, fnegstate, reg_fstate; // fstate - estado da máquina sincronizado na borda de subida,
//fnegstate - estado da maquina sincronizado na borda de descida,
//reg_fstate - próximo estado
localparam clock_display = 2'b000,
set_clock_display = 2'b001,
alarm_display = 2'b010,
set_alarm_display = 2'b011;
//reg alarm_active; // informa se o alarme esta ativo ou inativo // podemos substituir por um registrador no avaliador do alarme
always_ff @(posedge clk) // fstate sincronizado na borda de subida - influenciado pelas entradas
begin
if (clk) begin
fstate <= reg_fstate;
end
end
always_ff @(negedge clk) // fnegstate sincronizado na borda de descida - influencia as saidas
begin
if (!clk) begin
fnegstate <= fstate;
end
/* poderá sumir apos alteração no avaliador do alarme
case (fstate)
pre_set_clock:alarm_active<=1'b0;
alarm_state_hour:alarm_active<=1'b0;
pos_set_alarm:alarm_active<=1'b1;
endcase
*/
end
/*poderá sumir apos alteração no avaliador do alarme
assign alarm_on_output = alarm_active;
*/
assign temp_state = fstate; // exibe temporariamente o estado da maquina na saida
always_comb begin // O modo combinacional com as entradas e saidas garante a
// criação de registradores apenas para os estados, diminuindo e organizando o circuito
if (rst) begin // Reset básico, retoma o estado de relógio
reg_fstate <= clock_state;
buzer_output<= 1'b0;
timer_output<= 1'b0;
stopwatch_output<= 1'b0;
alarm_on_output<=1'b0;
alarm_off_output<=1'b0;
count_sel<= clock_display;
set_left<= 1'b0;
set_right<= 1'b0;
blink_dots<= 1'b0;
copy<= 1'b0;
counting<= 1'b0;
mst_rst<= 1'b0;
cnt_rst<= 1'b1;
end
else if (rst_3) begin // reset_master, reinicializa todos os elementos do relógio
reg_fstate <= pre_set_clock;
buzer_output<= 1'b0;
timer_output<= 1'b0;
alarm_on_output<=1'b0;
alarm_off_output<=1'b1; // desliga o alarme
stopwatch_output<= 1'b0;
count_sel<= clock_display;
set_left<= 1'b0;
set_right<= 1'b0;
blink_dots<= 1'b0;
copy<= 1'b0;
counting<= 1'b0;
mst_rst<= 1'b1; // reinicializa todos os contadores
cnt_rst<= 1'b1;
//alarm_active<=1'b0; // mst_rst desliga o alame tambem
end
else if (alarm_match) begin // && alarm_active // Se o avaliador informar alarme igual
reg_fstate <= alarm_state_active; // vai para o estado que ativa o buzzer
buzer_output<= 1'b0;
timer_output<= 1'b0;
alarm_on_output<=1'b0;
alarm_off_output<=1'b0;
stopwatch_output<= 1'b0;
count_sel<= clock_display;
set_left<= 1'b0;
set_right<= 1'b0;
blink_dots<= 1'b0;
copy<= 1'b0;
counting<= 1'b0;
mst_rst<= 1'b0;
cnt_rst<= 1'b0;
end
else begin
// Especifica um valor para todas as saidas para evitar criação de latches
buzer_output<= 1'b0;
timer_output<= 1'b0;
stopwatch_output<= 1'b0;
alarm_on_output<=1'b0;
alarm_off_output<=1'b0;
count_sel<= clock_display;
set_left<= 1'b0;
set_right<= 1'b0;
blink_dots<= 1'b0;
copy<= 1'b0;
counting<= 1'b0;
mst_rst<= 1'b0;
cnt_rst<= 1'b0;
// sincronizado na borda de subida,determina o próximo estado
case (fstate)
clock_state: begin
if (mode_input)
reg_fstate <= stopwatch_state;
else
reg_fstate <= clock_state;
end
rst_state_hour: begin
if (cancel_input)
reg_fstate <= clock_state;
else if (ok_input)
reg_fstate <= rst_state_min;
else
reg_fstate <= rst_state_hour;
end
rst_state_min: begin
if (cancel_input)
reg_fstate <= pre_set_clock;
else if (ok_input)
reg_fstate <= pos_set_clock;
else
reg_fstate <= rst_state_min;
end
stopwatch_state: begin
if (cancel_input)
reg_fstate <= clock_state;
else if (ok_input)
reg_fstate <= counter_state_up;
else if (mode_input)
reg_fstate <= timer_state_min;
else
reg_fstate <= stopwatch_state;
end
counter_state_up: begin
if (cancel_input)
reg_fstate <= rst_counter_state_up;
else if (ok_input)
reg_fstate <= stopwatch_state;
else if (counter_end)
reg_fstate <= stopwatch_state;
else
reg_fstate <= counter_state_up;
end
timer_state_min: begin
if (cancel_input)
reg_fstate <= clock_state;
else if (ok_input)
reg_fstate <= timer_state_sec;
else if (mode_input)
reg_fstate <= pre_set_alarm;
else
reg_fstate <= timer_state_min;
end
timer_state_sec: begin
if (cancel_input)
reg_fstate <= timer_state_min;
else if (ok_input)
reg_fstate <= counter_state_down;
else if (mode_input)
reg_fstate <= pre_set_alarm;
else
reg_fstate <= timer_state_sec;
end
counter_state_down: begin
if (cancel_input)
reg_fstate <= rst_counter_state_down;
else if (ok_input)
reg_fstate <= timer_state_min;
else if (counter_end)
reg_fstate <= timer_alarm_active;
else
reg_fstate <= counter_state_down;
end
timer_alarm_active: begin
if (ok_input| cancel_input)
reg_fstate <= timer_state_min;
else
reg_fstate <= timer_alarm_active;
end
alarm_state_hour: begin
if (ok_input)
reg_fstate <= alarm_state_min;
else if (cancel_input | mode_input)
reg_fstate <= clock_state;
else
reg_fstate <= alarm_state_hour;
end
alarm_state_min: begin
if (cancel_input)
reg_fstate <= pre_set_alarm;
else if (mode_input)
reg_fstate <= clock_state;
else if (ok_input)
reg_fstate <= pos_set_alarm;
else
reg_fstate <= alarm_state_min;
end
alarm_state_active: begin
if (ok_input | cancel_input)
reg_fstate <= clock_state;
else if (mode_input)
reg_fstate <= pre_set_alarm;
else
reg_fstate <= alarm_state_active;
end
pre_set_alarm: begin
reg_fstate <= alarm_state_hour;
end
pre_set_clock: begin
reg_fstate <= rst_state_hour;
end
pos_set_alarm: begin
reg_fstate <= clock_state;
end
pos_set_clock: begin
reg_fstate <= clock_state;
end
rst_counter_state_up: begin
reg_fstate <= stopwatch_state;
end
rst_counter_state_down: begin
reg_fstate <= timer_state_min;
end
default: begin
end
endcase
// Sincrinizado na borda de descida, determina o valor das saídas
case (fnegstate)
clock_state: begin
end
rst_state_hour: begin
count_sel<= set_clock_display;
set_left<= 1'b1;
set_right<= 1'b0;
blink_dots<= 1'b1;
end
rst_state_min: begin
count_sel<= set_clock_display;
set_left<= 1'b0;
set_right<= 1'b1;
blink_dots<= 1'b1;
end
stopwatch_state: begin
stopwatch_output<= 1'b1;
blink_dots<= 1'b1;
counting<= 1'b0;
cnt_rst<=1'b0;
end
counter_state_up: begin
stopwatch_output<= 1'b1;
blink_dots<= 1'b0;
counting<= 1'b1;
end
timer_state_min: begin
buzer_output<= 1'b0;
timer_output<= 1'b1;
set_left<= 1'b1;
set_right<= 1'b0;
blink_dots<= 1'b1;
counting<= 1'b0;
end
timer_state_sec: begin
buzer_output<= 1'b0;
timer_output<= 1'b1;
set_left<= 1'b0;
set_right<= 1'b1;
blink_dots<= 1'b1;
counting<= 1'b0;
end
counter_state_down: begin
buzer_output<= 1'b0;
timer_output<= 1'b1;
set_left<= 1'b0;
set_right<= 1'b0;
blink_dots<= 1'b0;
counting<= 1'b1;
end
timer_alarm_active: begin
buzer_output<= 1'b1;
timer_output<= 1'b1;
set_left<= 1'b0;
set_right<= 1'b0;
blink_dots<= 1'b1;
counting<= 1'b0;
cnt_rst<=1'b1;
end
alarm_state_hour: begin
count_sel<= set_alarm_display;
set_left<= 1'b1;
set_right<= 1'b0;
blink_dots<= 1'b1;
end
alarm_state_min: begin
count_sel<= set_alarm_display;
set_left<= 1'b0;
set_right<= 1'b1;
blink_dots<= 1'b1;
end
alarm_state_active: begin
buzer_output<= 1'b1;
blink_dots<= 1'b1;
end
pre_set_alarm: begin
copy<= 1'b1;
count_sel<= alarm_display;
alarm_off_output<=1'b1; // desliga o alarme // alarm_active <= 1'b0;
end
pre_set_clock: begin
copy<= 1'b1;
count_sel<= clock_display;
end
pos_set_alarm: begin
alarm_on_output<=1'b1;// liga o alarme // alarm_active<=1'b1;
copy<= 1'b1;
count_sel<= set_alarm_display;
end
pos_set_clock: begin
copy<= 1'b1;
count_sel<= set_clock_display;
end
rst_counter_state_up: begin
cnt_rst <= 1'b1;
end
rst_counter_state_down: begin
cnt_rst <= 1'b1;
end
default: begin
buzer_output<= 1'bx;
timer_output<= 1'bx;
stopwatch_output<= 1'bx;
set_left<= 1'bx;
set_right<= 1'bx;
blink_dots<= 1'bx;
copy<= 1'bx;
counting<= 1'bx;
mst_rst<= 1'bx;
cnt_rst<= 1'bx;
$display ("Reach undefined state");
end
endcase
end
end
endmodule