-
Notifications
You must be signed in to change notification settings - Fork 0
/
State_Machine.vhd
461 lines (360 loc) · 9.48 KB
/
State_Machine.vhd
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
--Group 9, Hermela Gebretsion, Bowen Zheng
Entity State_Machine IS Port
(
clk_input, reset, enable : IN std_logic; -- clock, reset and clock enable
NS_hold_state, EW_hold_state : IN std_logic; -- holding states for NS and EW
blink : IN std_logic; -- blinking lights signal
NS_cross, EW_cross : OUT std_logic;
NS_light, EW_light : OUT std_logic_vector(6 downto 0); -- green, red and amber lights for NS and EW
clr_NS, clr_EW : OUT std_logic; -- clears signals for NS and EW
states : OUT std_logic_vector(3 downto 0) -- 4 bit output of states
-- EW_grn, EW_ambr, EW_red : OUT std_logic;
--
-- NS_grn, NS_ambr, NS_red : OUT std_logic
);
END ENTITY;
Architecture SM of State_Machine is
TYPE STATE_NAMES IS (S0, S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, S12, S13, S14, S15); -- list all the STATE_NAMES values
SIGNAL current_state, next_state : STATE_NAMES; -- signals of type STATE_NAMES
BEGIN
--------------------------------------------------------------------------------
--State Machine:
--------------------------------------------------------------------------------
-- REGISTER_LOGIC PROCESS
Register_Section: PROCESS (clk_input) -- this process updates with a clock
BEGIN
IF(rising_edge(clk_input)) THEN -- updates on rising edge of the clock
IF (reset = '1') THEN
current_state <= S0; -- resets current state back to S0
ELSIF (reset = '0' AND enable ='1') THEN -- when enable is on update current state to next state but only if reset is not active
current_state <= next_State;
END IF;
END IF;
END PROCESS;
-- TRANSITION LOGIC PROCESS
-- Logic that determines the next state after the clock changes
Transition_Section: PROCESS (current_state)
BEGIN
CASE current_state IS
WHEN S0 =>
IF(EW_hold_state='1' AND NS_hold_state = '0') THEN -- If only the EW holding register is active jump from S0 to S6
next_state <= S6;
ELSE
next_state <= S1;
END IF;
WHEN S1 =>
IF(EW_hold_state='1' AND NS_hold_state = '0') THEN -- If only the EW holding register is active jump from S1 to S6
next_state <= S6;
ELSE
next_state <= S2;
END IF;
WHEN S2 =>
next_state <= S3;
WHEN S3 =>
next_state <=S4;
WHEN S4 =>
next_state <= S5;
WHEN S5 =>
next_state <= S6;
WHEN S6 =>
next_state <= S7;
WHEN S7 =>
next_state <= S8;
WHEN S8 =>
IF (NS_hold_State = '1' AND EW_hold_State ='0') THEN -- If only the NS holding register is active jump from S8 to S14
next_state <= S14;
ELSE
next_state <= S9;
END IF;
WHEN S9 =>
IF (NS_hold_State = '1' AND EW_hold_State ='0') THEN -- If only the NS holding register is active jump from S9 to S14
next_state <= S14;
ELSE
next_state <= S10;
END IF;
WHEN S10 =>
next_state <= S11;
WHEN S11 =>
next_state <= S12;
WHEN S12 =>
next_state <= S13;
WHEN S13 =>
next_state <= S14;
WHEN S14 =>
next_state <= S15;
WHEN S15 => -- Return to the first state after reaching the last state
next_state <= S0;
END CASE;
END PROCESS;
-- DECODER SECTION PROCESS (MOORE FORM SHOWN)
-- red : 0000001
-- amber : 1000000
-- green : 0001000
-- all outputs generated here
Decoder_Section: PROCESS (current_state, blink)
BEGIN
CASE current_state IS -- cases for each state
WHEN S0 =>
NS_light <= ("0001000" AND ("000" & blink & "000")); -- blinking green
EW_light <= "0000001";
clr_NS <= '0';
clr_EW <= '0';
NS_cross <= '0';
EW_cross <= '0';
states <= "0000"; -- current state represesented in binary
-- EW_grn <= '0';
-- EW_ambr <= '0';
-- EW_red <= '1';
-- IF (blink= '1') THEN
-- NS_grn <= '1';
-- ELSE
-- Ns_grn <= '0';
-- END IF;
--
-- NS_ambr <= '0';
-- NS_red <= '0';
WHEN S1 =>
NS_light <= ("0001000" AND ("000" & blink & "000")) ; -- blinking green
EW_light <= "0000001";
clr_NS <= '0';
clr_EW <= '0';
NS_cross <= '0';
EW_cross <= '0';
states <= "0001";
-- EW_grn <= '0';
-- EW_ambr <= '0';
-- EW_red <= '1';
-- IF (blink='1') THEN
-- NS_grn <= '1';
-- ELSE
-- Ns_grn <= '0';
-- END IF;
--
-- NS_ambr <= '0';
-- NS_red <= '0';
WHEN S2 =>
NS_light <= "0001000";
EW_light <= "0000001";
clr_NS <= '0';
clr_EW <= '0';
NS_cross <= '1'; -- NS crossing signal ON at NS solid green light
EW_cross <= '0';
states <= "0010";
-- EW_grn <= '0';
-- EW_ambr <= '0';
-- EW_red <= '1';
--
-- NS_grn <='1';
-- NS_ambr <='0';
-- NS_red <='0';
WHEN S3 =>
NS_light <= "0001000";
EW_light <= "0000001";
clr_NS <= '0';
clr_EW <= '0';
NS_cross <= '1';
EW_cross <= '0';
states <= "0011";
-- EW_grn <= '0';
-- EW_ambr <= '0';
-- EW_red <= '1';
--
-- NS_grn <='1';
-- NS_ambr <='0';
-- NS_red <='0';
WHEN S4 =>
NS_light <= "0001000";
EW_light <= "0000001";
clr_NS <= '0';
clr_EW <= '0';
NS_cross <= '1';
EW_cross <= '0';
states <= "0100";
-- EW_grn <= '0';
-- EW_ambr <= '0';
-- EW_red <= '1';
--
-- NS_grn <='1';
-- NS_ambr <='0';
-- NS_red <='0';
WHEN S5 =>
NS_light <= "0001000";
EW_light <= "0000001";
clr_NS <= '0';
clr_EW <= '0';
NS_cross <= '1';
EW_cross <= '0';
states <= "0101";
-- EW_grn <= '0';
-- EW_ambr <= '0';
-- EW_red <= '1';
--
-- NS_grn <='1';
-- NS_ambr <='0';
-- NS_red <='0';
WHEN S6 =>
NS_light <= "1000000";
EW_light <= "0000001";
clr_NS <= '1'; -- clear NS holding register after reaching S6
clr_EW <= '0';
NS_cross <= '0';
EW_cross <= '0';
states <= "0110";
-- EW_grn <= '0';
-- EW_ambr <= '0';
-- EW_red <= '1';
--
-- NS_grn <='0';
-- NS_ambr <='1';
-- NS_red <='0';
WHEN S7 =>
NS_light <= "1000000";
EW_light <= "0000001";
clr_NS <= '0';
clr_EW <= '0';
NS_cross <= '0';
EW_cross <= '0';
states <= "0111";
-- EW_grn <= '0';
-- EW_ambr <= '0';
-- EW_red <= '1';
--
-- NS_grn <='0';
-- NS_ambr <='1';
-- NS_red <='0';
WHEN S8 =>
NS_light <= "0000001";
EW_light <= ("0001000" AND ("000" & blink & "000")); -- blinking green
clr_NS <= '0';
clr_EW <= '0';
NS_cross <= '0';
EW_cross <= '0';
states <= "1000";
-- IF (blink='1') THEN
-- EW_grn <= '1';
-- ELSE
-- EW_grn <= '0';
-- END IF;
--
-- EW_ambr <= '0';
-- EW_red <= '0';
--
-- Ns_grn <= '0';
-- NS_ambr <= '0';
-- NS_red <= '1';
WHEN S9 =>
NS_light <= "0000001";
EW_light <= ("0001000" AND ("000" & blink & "000")); -- blinking green
clr_NS <= '0';
clr_EW <= '0';
NS_cross <= '0';
EW_cross <= '0';
states <= "1001";
-- IF (blink='1') THEN
-- EW_grn <= '1';
-- ELSE
-- EW_grn <= '0';
-- END IF;
--
-- EW_ambr <= '0';
-- EW_red <= '0';
--
-- Ns_grn <= '0';
-- NS_ambr <= '0';
-- NS_red <= '1';
WHEN S10 =>
NS_light <= "0000001";
EW_light <= "0001000";
clr_NS <= '0';
clr_EW <= '0';
NS_cross <= '0';
EW_cross <= '1'; -- EW crossing signal ON at EW solid green light
states <="1010";
-- EW_grn <= '1';
-- EW_ambr <= '0';
-- EW_red <= '0';
--
-- NS_grn <='0';
-- NS_ambr <='0';
-- NS_red <='1';
WHEN S11 =>
NS_light <= "0000001";
EW_light <= "0001000";
clr_NS <= '0';
clr_EW <= '0';
NS_cross <= '0';
EW_cross <= '1';
states <="1011";
-- EW_grn <= '1';
-- EW_ambr <= '0';
-- EW_red <= '0';
--
-- NS_grn <='0';
-- NS_ambr <='0';
-- NS_red <='1';
WHEN S12 =>
NS_light <= "0000001";
EW_light <= "0001000";
clr_NS <= '0';
clr_EW <= '0';
NS_cross <= '0';
EW_cross <= '1';
states <="1100";
-- EW_grn <= '1';
-- EW_ambr <= '0';
-- EW_red <= '0';
--
-- NS_grn <='0';
-- NS_ambr <='0';
-- NS_red <='1';
WHEN S13 =>
NS_light <= "0000001";
EW_light <= "0001000";
clr_NS <= '0';
clr_EW <= '0';
NS_cross <= '0';
EW_cross <= '1';
states <="1101";
-- EW_grn <= '1';
-- EW_ambr <= '0';
-- EW_red <= '0';
--
-- NS_grn <='0';
-- NS_ambr <='0';
-- NS_red <='1';
WHEN S14 =>
NS_light <= "0000001";
EW_light <= "1000000";
clr_NS <= '0';
clr_EW <= '1'; -- clear EW holding register after reaching S14
NS_cross <= '0';
EW_cross <= '0';
states <="1110";
-- EW_grn <= '0';
-- EW_ambr <= '1';
-- EW_red <= '0';
--
-- NS_grn <='0';
-- NS_ambr <='0';
-- NS_red <='1';
WHEN S15 =>
NS_light <= "0000001";
EW_light <= "1000000";
clr_NS <= '0';
clr_EW <= '0';
NS_cross <= '0';
EW_cross <= '0';
states <="1111";
-- EW_grn <= '0';
-- EW_ambr <= '1';
-- EW_red <= '0';
--
-- NS_grn <='0';
-- NS_ambr <='0';
-- NS_red <='1';
END CASE;
END PROCESS;
END ARCHITECTURE SM;