-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrain.vhd
More file actions
1858 lines (1716 loc) · 50.3 KB
/
Copy pathTrain.vhd
File metadata and controls
1858 lines (1716 loc) · 50.3 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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
--
-- FPGA Tain VHDL Module - Train Control State Machine for Altera DE2 board
--
-- VGA output displays train and switch state
-- Students supply train_control state machine (tcontrol.vhd) to control train
-- Key1 Pushbutton is run/stop
-- Key2 Pushbutton is reset
-- Sw1..2 is trainA speed
-- Sw3..4 is trainB speed
-- SensorX are track sensors for train, train near=1 (inputs for state machine)
-- SwitchX are track switches, Sw=0 connects to outside track
-- TrackX select power source A=0 or B=1 for track segment
-- DirX selects direction: 00-stop 01-counterclockwise 10-clockwise
-- Note: Screen blinks when trains crash or go through switch in wrong direction
--,j
-- -----------Sw3--------------
-- | T1 \T4 T1 |
-- | -------|------- |
-- | | T3 | T3 | |
-- | | | S5 | |
-- S1 | S2| | S3 | S4
-- | \ T2 / |
-- ------Sw1---------Sw2-------
--
-- Track Layout
--
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.all;
USE IEEE.numeric_std.all;
USE IEEE.STD_LOGIC_UNSIGNED.all;
LIBRARY lpm;
USE lpm.lpm_components.ALL;
ENTITY train IS
Generic(ADDR_WIDTH: integer := 12; DATA_WIDTH: integer := 1);
PORT( SIGNAL PBSWITCH_7, SW8, Clock_50Mhz : in std_logic;
SIGNAL VGA_Red,VGA_Green,VGA_Blue : out std_logic;
SIGNAL VGA_HSync,VGA_VSync : out std_logic;
SIGNAL Video_blank_out,Video_clock_out : out std_logic;
SIGNAL DIPSwitch_1, DIPSwitch_2, DIPSwitch_3, DIPSwitch_4: in std_logic;
SIGNAL LCD_RS, LCD_E : OUT STD_LOGIC;
SIGNAL LCD_RW, LCD_ON : OUT STD_LOGIC;
SIGNAL DATA_BUS : INOUT STD_LOGIC_VECTOR(7 DOWNTO 0));
END train;
architecture behavior of train is
TYPE character_string IS ARRAY ( 0 TO 31 ) OF STD_LOGIC_VECTOR( 7 DOWNTO 0 );
TYPE STATE_TYPE IS (HOLD, FUNC_SET, DISPLAY_ON, MODE_SET, Print_String,
LINE2, RETURN_HOME, DROP_LCD_E, RESET1, RESET2,
RESET3, DISPLAY_OFF, DISPLAY_CLEAR);
-- Users state machine to control trains
--COMPONENT Tcontrol
--PORT(reset, clock, sensor1, sensor2, sensor3, sensor4, sensor5: in std_logic;
-- switch1, switch2, switch3: out std_logic;
-- track1, track2, track3, track4: out std_logic;
--DirA, DirB : out std_logic_vector(1 DOWNTO 0));
--END COMPONENT;
--component Train_control
--port(reset, clock, sensor1, sensor2, sensor3, sensor4: in std_logic;
--sw1, sw2: out std_logic;
--dirA, dirB: out std_logic_vector(1 downto 0));
--end component;
component Tcontrol
port
(
-- Input ports
reset, clock, sensor1, sensor2,
sensor3, sensor4 : in std_logic;
-- Output ports
entry_A, entry_B, switch3 : out std_logic;
dirA, dirB : out std_logic_vector(1 downto 0)
);
end component;
-- PLL to generate Video clock
COMPONENT video_PLL
PORT
(
inclk0 : IN STD_LOGIC := '0';
c0 : OUT STD_LOGIC
);
end component;
-- LCD Display Signals
SIGNAL state, next_command: STATE_TYPE;
SIGNAL LCD_display_string : character_string;
-- Enter new ASCII hex data above for LCD Display
SIGNAL DATA_BUS_VALUE, Next_Char: STD_LOGIC_VECTOR(7 DOWNTO 0);
SIGNAL CLK_COUNT_400HZ: STD_LOGIC_VECTOR(19 DOWNTO 0);
SIGNAL CHAR_COUNT: STD_LOGIC_VECTOR(4 DOWNTO 0);
SIGNAL CLK_400HZ,LCD_RW_INT : STD_LOGIC;
SIGNAL Line1_chars, Line2_chars: STD_LOGIC_VECTOR(127 DOWNTO 0);
-- Video Train Simulation Signals
SIGNAL speed_A, speed_B: std_logic_vector(3 DOWNTO 0);
SIGNAL switch, switch_sync, cycle_count: std_logic_vector(7 DOWNTO 0);
SIGNAL speed_signal_A, speed_signal_B, train_run, train_stop, reset: std_logic;
-- Train Signals for virtual train display
SIGNAL trainArow, trainAcol: std_logic_vector(6 DOWNTO 0);
SIGNAL next_trainArow, next_trainAcol: std_logic_vector(6 DOWNTO 0);
SIGNAL next_trainBrow, next_trainBcol: std_logic_vector(6 DOWNTO 0);
SIGNAL old_trainArow, old_trainAcol: std_logic_vector(6 DOWNTO 0);
SIGNAL old2_trainArow, old2_trainAcol: std_logic_vector(6 DOWNTO 0);
SIGNAL trainBrow, trainBcol: std_logic_vector(6 DOWNTO 0);
SIGNAL old_trainBrow, old_trainBcol: std_logic_vector(6 DOWNTO 0);
SIGNAL old2_trainBrow, old2_trainBcol: std_logic_vector(6 DOWNTO 0);
SIGNAL col_adjustA, row_adjustA: std_logic_vector(6 DOWNTO 0);
SIGNAL col_adjustB, row_adjustB: std_logic_vector(6 DOWNTO 0);
SIGNAL C10A,C30A,C40A,C50A,C70A,R10A,R30A,R70A,R4xA,R5xA,T1,T2,T3,T4: boolean;
SIGNAL C10B,C30B,C40B,C50B,C70B,R10B,R30B,R70B,R4xB,R5xB: boolean;
SIGNAL crash, crashA, crashB, second: std_logic;
SIGNAL sensor, old_sensor: std_logic_vector(4 DOWNTO 0);
-- Train Signals for state machine control
SIGNAL sensor1, sensor2, sensor3, sensor4 : std_logic;
SIGNAL switch1, switch2, switch3 : std_logic;
SIGNAL DirA, DirB: std_logic_vector(1 DOWNTO 0);
SIGNAL track1, track2, track3, track4 : std_logic;
-- Make these pins outputs to monitor status on Logic Analyzer
SIGNAL o_sensor1, o_sensor2, o_sensor3, o_sensor4 : std_logic;
SIGNAL o_switch1, o_switch2, o_switch3 : std_logic;
SIGNAL o_dirA1, o_dirA0, o_dirB1, o_dirB0: std_logic;
SIGNAL o_track1, o_track2, o_track3, o_track4 : std_logic;
SIGNAL o_clock, o_reset : std_logic;
-- Video Display Signals
SIGNAL rom_address: std_logic_vector(11 DOWNTO 0);
SIGNAL H_count,V_count: std_logic_vector(9 DOWNTO 0);
SIGNAL F_count: std_logic_vector(4 DOWNTO 0);
SIGNAL Color_count: std_logic_vector(3 DOWNTO 0);
SIGNAL data: std_logic_vector(0 DOWNTO 0);
SIGNAL clock, Red_Data, Green_Data, Blue_Data, we, slow_clock: std_logic;
SIGNAL Red_Mux_Data, Green_Mux_Data, Blue_Mux_Data: std_logic;
SIGNAL Red_Display_Data, Green_Display_Data, Blue_Display_Data, display_item: std_logic;
-- Signals for Video Memory for Pixel Data
SIGNAL address: std_logic_vector(13 DOWNTO 0);
SIGNAL col_address, row_address: std_logic_vector(6 DOWNTO 0);
SIGNAL pixel_col_count, pixel_row_count: std_logic_vector(5 DOWNTO 0);
SIGNAL rom_output: std_logic_vector(0 DOWNTO 0);
-- Signals for Push buttons
SIGNAL PBSWITCH_7_sync, PBSWITCH_7_Single_Pulse: std_logic;
SIGNAL PBSWITCH_7_debounced, PBSWITCH_7_debounced_Sync: std_logic;
SIGNAL OLD_PBSWITCH_7_debounced_Sync: std_logic;
SIGNAL PBSWITCH_7_debounced_delay, Debounce_clock: std_logic;
SIGNAL SHIFT_PBSWITCH_7 : std_logic_vector(3 DOWNTO 0);
constant H_max : std_logic_vector(9 DOWNTO 0) := STD_LOGIC_VECTOR(to_unsigned(799,10));
-- 799 is max horiz count
constant V_max : std_logic_vector(9 DOWNTO 0) := STD_LOGIC_VECTOR(to_unsigned(524,10));
-- 524 is max vert count
SIGNAL video_on, video_on_H, video_on_V: std_logic;
BEGIN
------------------------------------------------------------------------------
-- PLL below is used to generate the 25.175Mhz pixel clock frequency
-- Uses DE2's 50Mhz USB clock for PLL's input clock
video_PLL_inst : video_PLL PORT MAP (
inclk0 => Clock_50Mhz,
c0 => clock
);-- 64 by 64 by 1 video rom for pixel background data
--
back_rom: lpm_rom
GENERIC MAP (lpm_widthad => ADDR_WIDTH,
lpm_outdata => "UNREGISTERED",
lpm_address_control => "REGISTERED",
lpm_file => "train.mif",
lpm_width => DATA_WIDTH)
-- Reads in mif file for initial data - train track background
PORT MAP (inclock => clock, address => rom_address(11 DOWNTO 0), q => rom_output);
CONTROL: Tcontrol
-- Use Student supplied module for Train control
port map( reset => reset, clock => slow_clock,
sensor1 => sensor1, sensor2 => sensor2,
sensor3 => sensor3, sensor4 => sensor4,
-- sensor5 => sensor5,
entry_A => switch1, entry_B => switch2,
switch3 => switch3,
-- track1 => track1, track2 => track2,
-- track3 => track3, track4 => track4,
DirA => DirA, DirB => DirB);
-- Copy state machine I/Os to Cyclone chip output pins for use by logic analyzer
o_sensor1 <= sensor1;
o_sensor2 <= sensor2;
o_sensor3 <= sensor3;
o_sensor4 <= sensor4;
--o_sensor5 <= sensor5;
o_switch1 <= switch1;
o_switch2 <= switch2;
o_switch3 <= switch3;
o_track1 <= track1;
o_track2 <= track2;
o_track3 <= track3;
o_track4 <= track4;
o_dirA1 <= DirA(1);
o_dirA0 <= DirA(0);
o_dirB1 <= DirB(1);
o_dirB0 <= DirB(0);
o_clock <= slow_clock;
o_reset <= reset;
-- Colors for pixel data on video SIGNAL
-- address video_ram for pixel background color data
-- IF display_item='1' use display data instead of background
Red_Mux_Data <= rom_output(0) When display_item='0' else red_display_data;
Green_Mux_Data <= rom_output(0) When display_item='0' else green_display_data;
Blue_Mux_Data <= '1' When display_item='0' else blue_display_data;
Red_Data <= (Red_Mux_Data xor (Crash and Second));
Green_Data <= (Green_Mux_Data xor (Crash and Second));
Blue_Data <= Blue_Mux_Data;
rom_address(11 DOWNTO 6) <= row_address(6 DOWNTO 1);
rom_address(5 DOWNTO 0) <= col_address(6 DOWNTO 1);
slow_clock <= Debounce_Clock;
VGA_Red <= Red_Data and video_on;
VGA_Green <= Green_Data and video_on;
VGA_Blue <= Blue_Data and video_on;
-- video_on turns off pixel data when not in the view area
video_on <= video_on_H and video_on_V;
-- Combine Dip Switch Inputs into Switch vector
Switch <= DIPSwitch_1 & DIPSwitch_2 & "00" &
DIPSwitch_3 & DIPSwitch_4 & "00";
-- Controls Speed of TrainA with input from Flex Switches 7..4
SPEED_TRAINA: PROCESS
BEGIN
WAIT UNTIL (slow_clock'Event) and (slow_clock='1');
if Speed_A = Switch_Sync(7 DOWNTO 4) THEN
Speed_A <="0000";
Speed_Signal_A <='1';
ELSE
Speed_A <= Speed_A + 1;
Speed_Signal_A <='0';
END IF;
END PROCESS SPEED_TRAINA;
SPEED_TRAINB: PROCESS
BEGIN
WAIT UNTIL (slow_clock'Event) and (slow_clock='1');
if Speed_B = Switch_Sync(3 DOWNTO 0) THEN
Speed_B <="0000";
Speed_Signal_B <='1';
ELSE
Speed_B <= Speed_B + 1;
Speed_Signal_B <= '0';
END IF;
END PROCESS SPEED_TRAINB;
-- Makes PBSWITCH_7 the Train Run/Step Button
PAUSE_TRAIN: PROCESS
BEGIN
WAIT UNTIL (slow_clock'Event) and (slow_clock='1');
IF PBSWITCH_7_DEBOUNCED_SYNC /= OLD_PBSWITCH_7_DEBOUNCED_SYNC THEN
OLD_PBSWITCH_7_DEBOUNCED_SYNC <= PBSWITCH_7_DEBOUNCED_SYNC;
IF PBSWITCH_7_DEBOUNCED_SYNC = '1' THEN
Train_Run <= Not Train_Run;
Old_sensor <= Sensor;
End IF;
End IF;
End PROCESS PAUSE_TRAIN;
Sensor <= '0' & Sensor4 & Sensor3 & Sensor2 & Sensor1;
-- Stops Train only at Sensor Change for Single Step
STEP_TRAIN: PROCESS
BEGIN
WAIT UNTIL (slow_clock'event) and (slow_clock = '1');
IF Train_run = '1' THEN Train_stop <= '1'; ELSE
IF (Sensor /= Old_Sensor) THEN
Train_stop <= '0';
END IF;
END IF;
End PROCESS STEP_TRAIN;
-- Move Train
-- Generate boolean signals for each track segment
-- to determine train's current location
C10A <= (trainAcol(6 DOWNTO 1) = "001000");
C30A <= (trainAcol(6 DOWNTO 1) = "011000");
C40A <= (trainAcol(6 DOWNTO 1)= "100000");
C50A <= (trainAcol(6 DOWNTO 1) = "101000");
C70A <= (trainAcol(6 DOWNTO 1) = "111000");
R10A <= (trainArow(6 DOWNTO 1) = "001000");
R30A <= (trainArow(6 DOWNTO 1) = "011000");
R4xA <= (trainArow(6 DOWNTO 4) = "100");
R5xA <= (trainArow(6 DOWNTO 4) = "101");
R70A <= (trainArow(6 DOWNTO 1) = "111000");
C10B <= (trainBcol(6 DOWNTO 1) = "001000");
C30B <= (trainBcol(6 DOWNTO 1) = "011000");
C40B <= (trainBcol(6 DOWNTO 1)= "100000");
C50B <= (trainBcol(6 DOWNTO 1) = "101000");
C70B <= (trainBcol(6 DOWNTO 1) = "111000");
R10B <= (trainBrow(6 DOWNTO 1) = "001000");
R30B <= (trainBrow(6 DOWNTO 1) = "011000");
R4xB <= (trainBrow(6 DOWNTO 4) = "100");
R5xB <= (trainBrow(6 DOWNTO 4) = "101");
R70B <= (trainBrow(6 DOWNTO 1) = "111000");
T1 <= (track1 = '1');
T2 <= (track2 = '1');
T3 <= (track3 = '1');
T4 <= (track4 = '1');
Next_TrainAcol <= TrainAcol + col_adjustA;
Next_TrainArow <= TrainArow + row_adjustA;
Next_TrainBcol <= TrainBcol + col_adjustB;
Next_TrainBrow <= TrainBrow + row_adjustB;
Crash <= CrashA or CrashB;
MOVE_TRAINA: PROCESS (reset, trainArow, slow_clock)
BEGIN
IF reset='1' or trainArow = "0000000" THEN
trainArow <= "0010000";
trainAcol <= "0100000";
old_trainArow <= "0010000";
old_trainAcol <= "0100010";
col_adjustA <= "0000000";
row_adjustA <= "0000000";
crashA <='0';
ELSIF (slow_clock'Event) and (slow_clock = '1') THEN
IF (trainArow=trainBrow) and (trainAcol=trainBcol) THEN
crashA <='1';
END IF;
IF (Speed_Signal_A = '1') and (crash='0') and (Train_stop = '1') THEN
Old2_TrainAcol <= Old_TrainAcol;
Old2_TrainArow <= Old_TrainArow;
Old_TrainAcol <= TrainAcol;
Old_TrainArow <= TrainArow;
TrainAcol <= Next_TrainAcol;
TrainArow <= Next_TrainArow;
-----------------------------------------------------------
-- Track 1
IF R10A and not(C10A) and not(C70A) and not(C40A) THEN
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustA <= "1111111";
row_adjustA <= "0000000";
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustA <= "0000001";
row_adjustA <= "0000000";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
IF R10A and C10A THEN
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "0000001";
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustA <= "0000001";
row_adjustA <= "0000000";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
-- Switch 3 Location
IF R10A and C40A THEN
IF Switch3 = '0' THEN
-- Switch connected to outside track
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustA <= "1111111";
row_adjustA <= "0000000";
-- crash if going wrong way into switch
IF old_trainArow > "0010000" THEN crashA <= '1'; END IF;
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustA <= "0000001";
row_adjustA <= "0000000";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
-- Switch connected to inside track
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustA <= "1111111";
row_adjustA <= "0000000";
-- crash if going wrong way into switch
IF old_trainAcol > "1000001" THEN crashA <= '1'; END IF;
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "0000001";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
END IF;
ELSE
IF R10A and C70A THEN
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustA <= "1111111";
row_adjustA <= "0000000";
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "0000001";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
IF C10A and not(R10A) and not(R70A) THEN
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "0000001";
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "1111111";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
IF C10A and R70A THEN
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustA <= "0000001";
row_adjustA <= "0000000";
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "1111111";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
IF C70A and not(R70A) and not(R10A) THEN
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "1111111";
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "0000001";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
-- Track 2 When col>=30 and <=50 else Track 1
IF R70A and not(C10A) and not(C70A) and not(C30A) and not(C50A) THEN
IF trainAcol >= "0110000" and trainAcol <= "1010001" THEN
-- Track 2 region
-- is train moving counterclockwise?
IF (not(T2) and DirA(0)='1') or (T2 and DirB(0)='1') THEN
col_adjustA <= "0000001";
row_adjustA <= "0000000";
ELSE
-- train is moving clockwise
IF (not(T2) and DirA(1)='1') or (T2 and DirB(1)='1') THEN
col_adjustA <= "1111111";
row_adjustA <= "0000000";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
-- Track 1 region
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustA <= "0000001";
row_adjustA <= "0000000";
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustA <= "1111111";
row_adjustA <= "0000000";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
END IF;
ELSE
IF R70A and C70A THEN
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "1111111";
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustA <= "1111111";
row_adjustA <= "0000000";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
-- Switch 1 Location
IF R70A and C30A THEN
IF Switch1 = '0' THEN
-- Switch connected to outside track
-- is train moving counterclockwise?
IF (not(T2) and DirA(0)='1') or (T2 and DirB(0)='1') THEN
col_adjustA <= "0000001";
row_adjustA <= "0000000";
-- crash if going wrong way into switch
IF old_trainArow < "1110000" THEN crashA <= '1'; END IF;
ELSE
-- train is moving clockwise
IF (not(T2) and DirA(1)='1') or (T2 and DirB(1)='1') THEN
col_adjustA <= "1111111";
row_adjustA <= "0000000";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
-- Switch connected to inside track
-- is train moving counterclockwise?
IF (not(T2) and DirA(0)='1') or (T2 and DirB(0)='1') THEN
col_adjustA <= "0000001";
row_adjustA <= "0000000";
-- crash if going wrong way into switch
IF old_trainAcol < "0110000" THEN crashA <= '1'; END IF;
ELSE
-- is train moving counterclockwise?
IF (not(T2) and DirA(1)='1') or (T2 and DirB(1)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "1111111";
-- crash if going wrong way into switch
IF old2_trainAcol(6 DOWNTO 1) < "011000" THEN crashA <= '1'; END IF;
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
END IF;
ELSE
-- Switch 2 Location
IF R70A and C50A THEN
IF Switch2 = '0' THEN
-- Switch connected to outside track
-- is train moving counterclockwise?
IF (not(T2) and DirA(0)='1') or (T2 and DirB(0)='1') THEN
col_adjustA <= "0000001";
row_adjustA <= "0000000";
ELSE
-- train is moving clockwise
IF (not(T2) and DirA(1)='1') or (T2 and DirB(1)='1') THEN
col_adjustA <= "1111111";
row_adjustA <= "0000000";
-- crash if going wrong way into switch
IF old_trainArow < "1110000" THEN crashA <= '1'; END IF;
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
-- Switch connected to inside track
-- is train moving counterclockwise?
IF (not(T2) and DirA(0)='1') or (T2 and DirB(0)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "1111111";
ELSE
-- train is moving clockwise
IF (not(T2) and DirA(1)='1') or (T2 and DirB(1)='1') THEN
col_adjustA <= "1111111";
row_adjustA <= "0000000";
-- crash if going wrong way into switch
IF old_trainAcol > "1010001" THEN crashA <= '1'; END IF;
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
END IF;
ELSE
------------------------------------------------------------
-- Track 3
IF C30A and not(R70A) and not(R30A) THEN
-- is train moving counterclockwise?
IF (not(T3) and DirA(0)='1') or (T3 and DirB(0)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "0000001";
ELSE
-- train is moving clockwise
IF (not(T3) and DirA(1)='1') or (T3 and DirB(1)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "1111111";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
IF C30A and R30A THEN
-- is train moving counterclockwise?
IF (not(T3) and DirA(0)='1') or (T3 and DirB(0)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "0000001";
ELSE
-- train is moving clockwise
IF (not(T3) and DirA(1)='1') or (T3 and DirB(1)='1') THEN
col_adjustA <= "0000001";
row_adjustA <= "0000000";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
IF R30A and not(C50A) and not(C30A) and not(C40A) THEN
-- is train moving counterclockwise?
IF (not(T3) and DirA(0)='1') or (T3 and DirB(0)='1') THEN
col_adjustA <= "1111111";
row_adjustA <= "0000000";
ELSE
-- train is moving clockwise
IF (not(T3) and DirA(1)='1') or (T3 and DirB(1)='1') THEN
col_adjustA <= "0000001";
row_adjustA <= "0000000";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
IF R30A and C40A THEN
-- Tracks cross which way was train moving?
IF Old_TrainArow /= "0110000" or TrainArow(0)='1' THEN
-- N/S Track
-- is train moving counterclockwise?
IF (not(T4) and DirA(0)='1') or (T4 and DirB(0)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "1111111";
ELSE
-- train is moving clockwise
IF (not(T4) and DirA(1)='1') or (T4 and DirB(1)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "0000001";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
-- E/W Track
-- is train moving counterclockwise?
IF (not(T3) and DirA(0)='1') or (T3 and DirB(0)='1') THEN
col_adjustA <= "1111111";
row_adjustA <= "0000000";
ELSE
-- train is moving clockwise
IF (not(T3) and DirA(1)='1') or (T3 and DirB(1)='1') THEN
col_adjustA <= "0000001";
row_adjustA <= "0000000";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
END IF;
ELSE
IF C50A and not(R70A) and not(R30A) THEN
-- is train moving counterclockwise?
IF (not(T3) and DirA(0)='1') or (T3 and DirB(0)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "1111111";
ELSE
-- train is moving clockwise
IF (not(T3) and DirA(1)='1') or (T3 and DirB(1)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "0000001";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
IF C50A and R30A THEN
-- is train moving counterclockwise?
IF (not(T3) and DirA(0)='1') or (T3 and DirB(0)='1') THEN
col_adjustA <= "1111111";
row_adjustA <= "0000000";
ELSE
-- train is moving clockwise
IF (not(T3) and DirA(1)='1') or (T3 and DirB(1)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "0000001";
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
--------------------------------------------------------------------------
-- Track 4
IF C40A and not(R10A) THEN
-- is train moving counterclockwise?
IF (not(T4) and DirA(0)='1') or (T4 and DirB(0)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "1111111";
ELSE
-- train is moving clockwise
IF (not(T4) and DirA(1)='1') or (T4 and DirB(1)='1') THEN
col_adjustA <= "0000000";
row_adjustA <= "0000001";
IF trainArow > "1010000" THEN CrashA <= '1'; END IF;
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
ELSE
col_adjustA <= "0000000";
row_adjustA <= "0000000";
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
End PROCESS MOVE_TRAINA;
-----------------------------------------------------------
-- Move Train B
MOVE_TRAINB: PROCESS (reset, trainBrow, slow_clock)
BEGIN
IF reset='1' or trainBrow = "0000000" THEN
trainbrow <= "1000000";
trainbcol <= "0110000";
old_trainbrow <= "1000010";
old_trainbcol <= "0110000";
col_adjustB <= "0000000";
row_adjustB <= "0000000";
crashB <= '0';
ELSIF (slow_clock'Event) and (slow_clock = '1') THEN
IF (trainArow=trainBrow) and (trainAcol=trainBcol) THEN
crashB <='1';
END IF;
IF (Speed_Signal_B = '1') and (crash='0') and (Train_stop ='1') THEN
Old2_TrainBcol <= Old_TrainBcol;
Old2_TrainBrow <= Old_TrainBrow;
Old_TrainBcol <= TrainBcol;
Old_TrainBrow <= TrainBrow;
TrainBcol <= Next_TrainBcol;
TrainBrow <= Next_TrainBrow;
-----------------------------------------------------------
-- Track 1
IF R10B and not(C10B) and not(C70B) and not(C40B) THEN
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustB <= "1111111";
row_adjustB <= "0000000";
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustB <= "0000001";
row_adjustB <= "0000000";
ELSE
col_adjustB <= "0000000";
row_adjustB <= "0000000";
END IF;
END IF;
ELSE
IF R10B and C10B THEN
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustB <= "0000000";
row_adjustB <= "0000001";
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustB <= "0000001";
row_adjustB <= "0000000";
ELSE
col_adjustB <= "0000000";
row_adjustB <= "0000000";
END IF;
END IF;
ELSE
-- Switch 3 Location
IF R10B and C40B THEN
IF Switch3 = '0' THEN
-- Switch connected to outside track
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustB <= "1111111";
row_adjustB <= "0000000";
-- crash if going wrong way into switch
IF old_trainBrow > "0010000" THEN crashB <= '1'; END IF;
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustB <= "0000001";
row_adjustB <= "0000000";
ELSE
col_adjustB <= "0000000";
row_adjustB <= "0000000";
END IF;
END IF;
ELSE
-- Switch connected to inside track
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustB <= "1111111";
row_adjustB <= "0000000";
-- crash if going wrong way into switch
IF old_trainBcol > "1000000" THEN crashB <= '1'; END IF;
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustB <= "0000000";
row_adjustB <= "0000001";
ELSE
col_adjustB <= "0000000";
row_adjustB <= "0000000";
END IF;
END IF;
END IF;
ELSE
IF R10B and C70B THEN
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustB <= "1111111";
row_adjustB <= "0000000";
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustB <= "0000000";
row_adjustB <= "0000001";
ELSE
col_adjustB <= "0000000";
row_adjustB <= "0000000";
END IF;
END IF;
ELSE
IF C10B and not(R10B) and not(R70B) THEN
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustB <= "0000000";
row_adjustB <= "0000001";
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustB <= "0000000";
row_adjustB <= "1111111";
ELSE
col_adjustB <= "0000000";
row_adjustB <= "0000000";
END IF;
END IF;
ELSE
IF C10B and R70B THEN
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustB <= "0000001";
row_adjustB <= "0000000";
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustB <= "0000000";
row_adjustB <= "1111111";
ELSE
col_adjustB <= "0000000";
row_adjustB <= "0000000";
END IF;
END IF;
ELSE
IF C70B and not(R70B) and not(R10B) THEN
-- is train moving counterclockwise?
IF (not(T1) and DirA(0)='1') or (T1 and DirB(0)='1') THEN
col_adjustB <= "0000000";
row_adjustB <= "1111111";
ELSE
-- train is moving clockwise
IF (not(T1) and DirA(1)='1') or (T1 and DirB(1)='1') THEN
col_adjustB <= "0000000";
row_adjustB <= "0000001";
ELSE
col_adjustB <= "0000000";
row_adjustB <= "0000000";
END IF;
END IF;
ELSE
-- Track 2 When col>=30 and <=50 else Track 1
IF R70B and not(C10B) and not(C70B) and not(C30B) and not(C50B) THEN
IF trainBcol >= "0110000" and trainBcol <= "1010001" THEN
-- Track 2 region
-- is train moving counterclockwise?
IF (not(T2) and DirA(0)='1') or (T2 and DirB(0)='1') THEN
col_adjustB <= "0000001";
row_adjustB <= "0000000";
ELSE
-- train is moving clockwise
IF (not(T2) and DirA(1)='1') or (T2 and DirB(1)='1') THEN
col_adjustB <= "1111111";
row_adjustB <= "0000000";
ELSE
col_adjustB <= "0000000";