-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWOS.cpp
More file actions
1708 lines (1383 loc) · 49.2 KB
/
Copy pathWOS.cpp
File metadata and controls
1708 lines (1383 loc) · 49.2 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
#include <Arduino.h>
#include <EEPROM.h>
#define WOS_CPP_FILE
#include "WOS_Config.h"
#include "WOS.h"
// Machine State
#define WOS_NUM_LAMP_BANKS 8
volatile byte LampStates[WOS_NUM_LAMP_BANKS], LampDim1[WOS_NUM_LAMP_BANKS], LampDim2[WOS_NUM_LAMP_BANKS];
volatile byte LampFlashPeriod[WOS_MAX_LAMPS];
byte DimDivisor1 = 2;
byte DimDivisor2 = 3;
#ifndef WOS_NUM_DIGITS
#define WOS_NUM_DIGITS 6
#endif
#ifndef INTERRUPT_OCR1A_COUNTER
#define INTERRUPT_OCR1A_COUNTER 16574
#endif
volatile byte DisplayDigits[4][WOS_NUM_DIGITS];
#ifdef WOSS_11_MPU
volatile byte DisplayText[2][WOS_NUM_DIGITS];
#endif
volatile byte DisplayCreditDigits[2];
volatile byte DisplayCreditDigitEnable;
volatile byte DisplayBIPDigits[2];
volatile byte DisplayBIPDigitEnable;
volatile byte DisplayDigitEnable[4];
volatile byte BoardLEDs = 0;
#define SWITCH_COLUMNS 8
#define SWITCH_STACK_SIZE 32
#define SWITCH_STACK_EMPTY 0xFF
volatile byte SwitchStackFirst;
volatile byte SwitchStackLast;
volatile byte SwitchStack[SWITCH_STACK_SIZE];
volatile byte SwitchesMinus2[SWITCH_COLUMNS];
volatile byte SwitchesMinus1[SWITCH_COLUMNS];
volatile byte SwitchesNow[SWITCH_COLUMNS];
volatile boolean UpDownSwitch = false;
#define SOUND_STACK_SIZE 64
#define SOUND_STACK_EMPTY 0x0000
volatile byte SoundStackFirst;
volatile byte SoundStackLast;
volatile unsigned short SoundStack[SOUND_STACK_SIZE];
#define TIMED_SOUND_STACK_SIZE 20
struct TimedSoundEntry {
byte inUse;
unsigned long pushTime;
unsigned short soundNumber;
byte numPushes;
};
TimedSoundEntry TimedSoundStack[TIMED_SOUND_STACK_SIZE] = {0, 0, 0, 0};
#define SOLENOID_STACK_SIZE 250
#define SOLENOID_STACK_EMPTY 0xFF
unsigned short ContinuousSolenoidBits = 0;
volatile byte SolenoidStackFirst;
volatile byte SolenoidStackLast;
volatile byte SolenoidStack[SOLENOID_STACK_SIZE];
boolean SolenoidStackEnabled = true;
volatile byte CurrentSolenoidByte = 0xFF;
#define TIMED_SOLENOID_STACK_SIZE 32
struct TimedSolenoidEntry {
byte inUse;
unsigned long pushTime;
byte solenoidNumber;
byte numPushes;
byte disableOverride;
};
TimedSolenoidEntry TimedSolenoidStack[TIMED_SOLENOID_STACK_SIZE] = {0, 0, 0, 0, 0};
#ifdef WOSS_11_MPU
const uint16_t SeventSegmentNumbers[10] = {
0x3F, /* 0 */
0x06, /* 1 */
0x5B, /* 2 */
0x4F, /* 3 */
0x66, /* 4 */
0x6D, /* 5 */
0x7D, /* 6 */
0x07, /* 7 */
0x7F, /* 8 */
0x6F /* 9 */
};
// WOS alphanumeric 14-segment display (ASCII)
const uint16_t FourteenSegmentASCII[96] = {
0x0000,/* converted 0x0000 to 0x0000*/
0x0006,/* ! converted 0x4006 to 0x0006*/
0x0102,/* " converted 0x0202 to 0x0102*/
0x154E,/* # converted 0x12CE to 0x154E*/
0x156D,/* $ converted 0x12ED to 0x156D*/
0x3FE4,/* % converted 0x3FE4 to 0x3FE4*/
0x09D9,/* & converted 0x2359 to 0x09D9*/
0x0100,/* ' converted 0x0200 to 0x0100*/
0x0A00,/* ( converted 0x2400 to 0x0A00*/
0x2080,/* ) converted 0x0900 to 0x2080*/
0x3FC0,/* * converted 0x3FC0 to 0x3FC0*/
0x1540,/* + converted 0x12C0 to 0x1540*/
0x2000,/* , converted 0x0800 to 0x2000*/
0x0440,/* - converted 0x00C0 to 0x0440*/
0x0000,/* . converted 0x4000 to 0x0000*/
0x2200,/* / converted 0x0C00 to 0x2200*/
0x223F,/* 0 converted 0x0C3F to 0x223F*/
0x0206,/* 1 converted 0x0406 to 0x0206*/
0x045B,/* 2 converted 0x00DB to 0x045B*/
0x040F,/* 3 converted 0x008F to 0x040F*/
0x0466,/* 4 converted 0x00E6 to 0x0466*/
0x0869,/* 5 converted 0x2069 to 0x0869*/
0x047D,/* 6 converted 0x00FD to 0x047D*/
0x0007,/* 7 converted 0x0007 to 0x0007*/
0x047F,/* 8 converted 0x00FF to 0x047F*/
0x046F,/* 9 converted 0x00EF to 0x046F*/
0x1100,/* : converted 0x1200 to 0x1100*/
0x2100,/* ; converted 0x0A00 to 0x2100*/
0x0A40,/* < converted 0x2440 to 0x0A40*/
0x0448,/* = converted 0x00C8 to 0x0448*/
0x2480,/* > converted 0x0980 to 0x2480*/
0x1403,/* ? converted 0x5083 to 0x1403*/
0x053B,/* @ converted 0x02BB to 0x053B*/
0x0477,/* A converted 0x00F7 to 0x0477*/
0x150F,/* B converted 0x128F to 0x150F*/
0x0039,/* C converted 0x0039 to 0x0039*/
0x110F,/* D converted 0x120F to 0x110F*/
0x0079,/* E converted 0x0079 to 0x0079*/
0x0071,/* F converted 0x0071 to 0x0071*/
0x043D,/* G converted 0x00BD to 0x043D*/
0x0476,/* H converted 0x00F6 to 0x0476*/
0x1109,/* I converted 0x1209 to 0x1109*/
0x001E,/* J converted 0x001E to 0x001E*/
0x0A70,/* K converted 0x2470 to 0x0A70*/
0x0038,/* L converted 0x0038 to 0x0038*/
0x02B6,/* M converted 0x0536 to 0x02B6*/
0x08B6,/* N converted 0x2136 to 0x08B6*/
0x003F,/* O converted 0x003F to 0x003F*/
0x0473,/* P converted 0x00F3 to 0x0473*/
0x083F,/* Q converted 0x203F to 0x083F*/
0x0C73,/* R converted 0x20F3 to 0x0C73*/
0x046D,/* S converted 0x00ED to 0x046D*/
0x1101,/* T converted 0x1201 to 0x1101*/
0x003E,/* U converted 0x003E to 0x003E*/
0x2230,/* V converted 0x0C30 to 0x2230*/
0x2836,/* W converted 0x2836 to 0x2836*/
0x2A80,/* X converted 0x2D00 to 0x2A80*/
0x046E,/* Y converted 0x00EE to 0x046E*/
0x2209,/* Z converted 0x0C09 to 0x2209*/
0x0039,/* [ converted 0x0039 to 0x0039*/
0x0880,/* \ converted 0x2100 to 0x0880*/
0x000F,/* ] converted 0x000F to 0x000F*/
0x2800,/* ^ converted 0x2800 to 0x2800*/
0x0008,/* _ converted 0x0008 to 0x0008*/
0x0080,/* ` converted 0x0100 to 0x0080*/
0x1058,/* a converted 0x1058 to 0x1058*/
0x0878,/* b converted 0x2078 to 0x0878*/
0x0458,/* c converted 0x00D8 to 0x0458*/
0x240E,/* d converted 0x088E to 0x240E*/
0x2058,/* e converted 0x0858 to 0x2058*/
0x1640,/* f converted 0x14C0 to 0x1640*/
0x060E,/* g converted 0x048E to 0x060E*/
0x1070,/* h converted 0x1070 to 0x1070*/
0x1000,/* i converted 0x1000 to 0x1000*/
0x2110,/* j converted 0x0A10 to 0x2110*/
0x1B00,/* k converted 0x3600 to 0x1B00*/
0x0030,/* l converted 0x0030 to 0x0030*/
0x1454,/* m converted 0x10D4 to 0x1454*/
0x1050,/* n converted 0x1050 to 0x1050*/
0x045C,/* o converted 0x00DC to 0x045C*/
0x00F0,/* p converted 0x0170 to 0x00F0*/
0x0606,/* q converted 0x0486 to 0x0606*/
0x0050,/* r converted 0x0050 to 0x0050*/
0x0C08,/* s converted 0x2088 to 0x0C08*/
0x0078,/* t converted 0x0078 to 0x0078*/
0x001C,/* u converted 0x001C to 0x001C*/
0x2010,/* v converted 0x0810 to 0x2010*/
0x2814,/* w converted 0x2814 to 0x2814*/
0x2A80,/* x converted 0x2D00 to 0x2A80*/
0x050E,/* y converted 0x028E to 0x050E*/
0x2048,/* z converted 0x0848 to 0x2048*/
0x20C9,/* { converted 0x0949 to 0x20C9*/
0x1100,/* | converted 0x1200 to 0x1100*/
0x0E09,/* } converted 0x2489 to 0x0E09*/
0x2640,/* ~ converted 0x0CC0 to 0x2640*/
0x0000 /* converted 0x0000 to 0x0000*/
};
#endif
/*
* PIA I - 0x2800
* Displays
*
* PA0-PA3 are decoded into 16 display output strobes
* PA4-PA7 are inputs for dip switches
* CA1 is diagnostic switch input
* CA2 is R/E for DIP switches & LEDs
*
* PB0-PB3 are BCD2 outputs
* PB4-BP7 are BCD1 outputs
* CB1 is diagnostic switch input
* CB2 is ST6 (special trigger 6) for solenoid board
*
* PIA II - 0x3000
* Switches
*
* PA0-PA7 are switch inputs
* CA1 is nothing
* CA2 is ST4
* PB0-PB7 are switch outputs
* CB1 is nothing
* CB2 is ST3
*
* PIA III - 0x2400
* Lamps
*
* PA0-PA7 are lamp rows (grounds)
* CA1 is nothing
* CA2 is ST2
* PB0-PB7 are lamp strobes (power)
* CB1 is nothing
* CB2 is ST1
*
* PIA IV - 0x2200
* Solenoids
*
* PA0-PA7 are solenoid drives 1-8 (1=on)
* CA1 is nothing
* CA2 is ST5
* PB0-PB7 are solenoid drives 8-16 (1=on)
* CB1 is nothing
* CB2 turns on mometary solenoids and flippers
*
*/
#define PIA_DISPLAY_PORT_A 0x2800
#define PIA_DISPLAY_CONTROL_A 0x2801
#define PIA_DISPLAY_PORT_B 0x2802
#define PIA_DISPLAY_CONTROL_B 0x2803
#define PIA_SWITCH_PORT_A 0x3000
#define PIA_SWITCH_CONTROL_A 0x3001
#define PIA_SWITCH_PORT_B 0x3002
#define PIA_SWITCH_CONTROL_B 0x3003
#define PIA_LAMPS_PORT_A 0x2400
#define PIA_LAMPS_CONTROL_A 0x2401
#define PIA_LAMPS_PORT_B 0x2402
#define PIA_LAMPS_CONTROL_B 0x2403
#define PIA_SOLENOID_PORT_A 0x2200
#define PIA_SOLENOID_CONTROL_A 0x2201
#define PIA_SOLENOID_PORT_B 0x2202
#define PIA_SOLENOID_CONTROL_B 0x2203
#ifdef WOS_7_MPU
#define PIA_SOUND_COMMA_PORT_A 0x2100
#define PIA_SOUND_COMMA_CONTROL_A 0x2101
#define PIA_SOUND_COMMA_PORT_B 0x2102
#define PIA_SOUND_COMMA_CONTROL_B 0x2103
#endif
#ifdef WOSS_11_MPU
#define PIA_SOUND_11_PORT_A 0x2100
#define PIA_SOUND_11_CONTROL_A 0x2101
#define PIA_SOLENOID_11_PORT_B 0x2102
#define PIA_SOLENOID_11_CONTROL_B 0x2103
#define PIA_ALPHA_DISPLAY_PORT_A 0x2C00
#define PIA_ALPHA_DISPLAY_CONTROL_A 0x2C01
#define PIA_ALPHA_DISPLAY_PORT_B 0x2C02
#define PIA_ALPHA_DISPLAY_CONTROL_B 0x2C03
#define PIA_NUM_DISPLAY_PORT_A 0x3400
#define PIA_NUM_DISPLAY_CONTROL_A 0x3401
#define PIA_WIDGET_PORT_B 0x3402
#define PIA_WIDGET_CONTROL_B 0x3403
#endif
#define WOS_PINS_OUTPUT true
#define WOS_PINS_INPUT false
#if (RPU_VERSION==1)
#define RPU_VMA_PIN 4
#define RPU_RW_PIN 5
#define RPU_PHI2_PIN 3
#define RPU_SWITCH_PIN 13
#define RPU_BUFFER_DISABLE 2
#define RPU_HALT_PIN 14
#define RPU_RESET_PIN 14
/*
* Helper Functions
*/
void WOS_SetAddressPinsDirection(boolean pinsOutput) {
for (int count=0; count<16; count++) {
pinMode(16+count, pinsOutput?OUTPUT:INPUT);
}
}
void WOS_SetDataPinsDirection(boolean pinsOutput) {
for (int count=0; count<7; count++) {
pinMode(6+count, pinsOutput?OUTPUT:INPUT);
}
pinMode(15, pinsOutput?OUTPUT:INPUT);
}
// REV 1 HARDWARE
void WOS_DataWrite(int address, byte data) {
// Set data pins to output
DDRH = DDRH | 0x78;
DDRB = DDRB | 0x70;
DDRJ = DDRJ | 0x01;
// Set R/W to LOW
PORTE = (PORTE & 0xF7);
// Put data on pins
// Lower Nibble goes on PortH3 through H6
PORTH = (PORTH&0x87) | ((data&0x0F)<<3);
// Bits 4-6 go on PortB4 through B6
PORTB = (PORTB&0x8F) | ((data&0x70));
// Bit 7 goes on PortJ0
PORTJ = (PORTJ&0xFE) | (data>>7);
// Set up address lines
PORTH = (PORTH & 0xFC) | ((address & 0x0001)<<1) | ((address & 0x0002)>>1); // A0-A1
PORTD = (PORTD & 0xF0) | ((address & 0x0004)<<1) | ((address & 0x0008)>>1) | ((address & 0x0010)>>3) | ((address & 0x0020)>>5); // A2-A5
PORTA = ((address & 0x3FC0)>>6); // A6-A13
PORTC = (PORTC & 0x3F) | ((address & 0x4000)>>7) | ((address & 0x8000)>>9); // A14-A15
// Set clock low
PORTE &= ~0x20;
//delayMicroseconds(3);
// Pulse VMA over one clock cycle
// Set VMA ON
PORTG = PORTG | 0x20;
// Set clock high
PORTE |= 0x20;
//delayMicroseconds(3);
// Set clock low
PORTE &= ~0x20;
//delayMicroseconds(3);
// Set clock high
PORTE |= 0x20;
// Set VMA OFF
PORTG = PORTG & 0xDF;
// Unset address lines
PORTH = (PORTH & 0xFC);
PORTD = (PORTD & 0xF0);
PORTA = 0;
PORTC = (PORTC & 0x3F);
// Set R/W back to HIGH
PORTE = (PORTE | 0x08);
// Set data pins to input
DDRH = DDRH & 0x87;
DDRB = DDRB & 0x8F;
DDRJ = DDRJ & 0xFE;
}
byte WOS_DataRead(int address) {
// Set data pins to input
DDRH = DDRH & 0x87;
DDRB = DDRB & 0x8F;
DDRJ = DDRJ & 0xFE;
// Set R/W to HIGH
DDRE = DDRE | 0x08;
PORTE = (PORTE | 0x08);
// Set up address lines
PORTH = (PORTH & 0xFC) | ((address & 0x0001)<<1) | ((address & 0x0002)>>1); // A0-A1
PORTD = (PORTD & 0xF0) | ((address & 0x0004)<<1) | ((address & 0x0008)>>1) | ((address & 0x0010)>>3) | ((address & 0x0020)>>5); // A2-A5
PORTA = ((address & 0x3FC0)>>6); // A6-A13
PORTC = (PORTC & 0x3F) | ((address & 0x4000)>>7) | ((address & 0x8000)>>9); // A14-A15
// Set clock low
PORTE &= ~0x20;
//delayMicroseconds(3);
// Pulse VMA over one clock cycle
// Set VMA ON
PORTG = PORTG | 0x20;
// Set clock high
PORTE |= 0x20;
//delayMicroseconds(3);
// Set clock low
PORTE &= ~0x20;
//delayMicroseconds(3);
// Set clock high
PORTE |= 0x20;
byte inputData;
inputData = (PINH & 0x78)>>3;
inputData |= (PINB & 0x70);
inputData |= PINJ << 7;
// Set VMA OFF
PORTG = PORTG & 0xDF;
// Set R/W to LOW
PORTE = (PORTE & 0xF7);
// Unset address lines
PORTH = (PORTH & 0xFC);
PORTD = (PORTD & 0xF0);
PORTA = 0;
PORTC = (PORTC & 0x3F);
return inputData;
}
void WaitClockCycle(int numCycles=1) {
for (int count=0; count<numCycles; count++) {
// Wait while clock is low
while(!(PINE & 0x20));
// Wait for a falling edge of the clock
while((PINE & 0x20));
}
}
#elif (RPU_VERSION==2)
#define RPU_VMA_PIN 40
#define RPU_RW_PIN 3
#define RPU_PHI2_PIN 39
#define RPU_SWITCH_PIN 38
#define RPU_BUFFER_DISABLE 5
#define RPU_HALT_PIN 41
#define RPU_RESET_PIN 42
#define RPU_DIAGNOSTIC_PIN 44
void WOS_SetAddressPinsDirection(boolean pinsOutput) {
for (int count=0; count<16; count++) {
pinMode(A0+count, pinsOutput?OUTPUT:INPUT);
}
}
void WOS_SetDataPinsDirection(boolean pinsOutput) {
for (int count=0; count<8; count++) {
pinMode(22, pinsOutput?OUTPUT:INPUT);
}
}
// REVISION 2 HARDWARE
void WOS_DataWrite(int address, byte data) {
// Set data pins to output
DDRA = 0xFF;
// Set R/W to LOW
PORTE = (PORTE & 0xDF);
// Put data on pins
PORTA = data;
// Set up address lines
PORTF = (byte)(address & 0x00FF);
PORTK = (byte)(address/256);
// Set clock low (PG2) (if 6802/8)
PORTG &= ~0x04;
//delayMicroseconds(3);
// Pulse VMA over one clock cycle
// Set VMA ON
PORTG = PORTG | 0x02;
// Set clock high
PORTG |= 0x04;
//delayMicroseconds(3);
// Set clock low
PORTG &= ~0x04;
//delayMicroseconds(3);
// Set clock high
PORTG |= 0x04;
// Set VMA OFF
PORTG = PORTG & 0xFD;
// Unset address lines
PORTF = 0x00;
PORTK = 0x00;
// Set R/W back to HIGH
PORTE = (PORTE | 0x20);
// Set data pins to input
DDRA = 0x00;
}
byte WOS_DataRead(int address) {
// Set data pins to input
DDRA = 0x00;
// Set R/W to HIGH
DDRE = DDRE | 0x20;
PORTE = (PORTE | 0x20);
// Set up address lines
PORTF = (byte)(address & 0x00FF);
PORTK = (byte)(address/256);
// Set clock low
PORTG &= ~0x04;
//delayMicroseconds(3);
// Pulse VMA over one clock cycle
// Set VMA ON
PORTG = PORTG | 0x02;
// Set clock high
PORTG |= 0x04;
//delayMicroseconds(3);
// Set clock low
PORTG &= ~0x04;
//delayMicroseconds(3);
// Set clock high
PORTG |= 0x04;
byte inputData;
inputData = PINA;
// Set VMA OFF
PORTG = PORTG & 0xFD;
// Set R/W to LOW
PORTE = (PORTE & 0xDF);
// Unset address lines
PORTF = 0x00;
PORTK = 0x00;
return inputData;
}
#else
#error "RPU Hardware Definition Not Recognized"
#endif
void WOS_InitializePIAs() {
WOS_DataWrite(PIA_DISPLAY_CONTROL_A, 0x31);
WOS_DataWrite(PIA_DISPLAY_PORT_A, 0xFF);
WOS_DataWrite(PIA_DISPLAY_CONTROL_A, 0x3D);
WOS_DataWrite(PIA_DISPLAY_PORT_A, 0xC0);
WOS_DataWrite(PIA_DISPLAY_CONTROL_B, 0x31);
WOS_DataWrite(PIA_DISPLAY_PORT_B, 0xFF);
WOS_DataWrite(PIA_DISPLAY_CONTROL_B, 0x3D);
WOS_DataWrite(PIA_DISPLAY_PORT_B, 0x00);
WOS_DataWrite(PIA_SWITCH_CONTROL_A, 0x38);
WOS_DataWrite(PIA_SWITCH_PORT_A, 0x00);
WOS_DataWrite(PIA_SWITCH_CONTROL_A, 0x3C);
WOS_DataWrite(PIA_SWITCH_CONTROL_B, 0x38);
WOS_DataWrite(PIA_SWITCH_PORT_B, 0xFF);
WOS_DataWrite(PIA_SWITCH_CONTROL_B, 0x3C);
WOS_DataWrite(PIA_SWITCH_PORT_B, 0x00);
WOS_DataWrite(PIA_LAMPS_CONTROL_A, 0x38);
WOS_DataWrite(PIA_LAMPS_PORT_A, 0xFF);
WOS_DataWrite(PIA_LAMPS_CONTROL_A, 0x3C);
WOS_DataWrite(PIA_LAMPS_PORT_A, 0xFF);
WOS_DataWrite(PIA_LAMPS_CONTROL_B, 0x38);
WOS_DataWrite(PIA_LAMPS_PORT_B, 0xFF);
WOS_DataWrite(PIA_LAMPS_CONTROL_B, 0x3C);
WOS_DataWrite(PIA_LAMPS_PORT_B, 0x00);
#ifndef WOS_11_MPU
WOS_DataWrite(PIA_SOLENOID_CONTROL_A, 0x38);
WOS_DataWrite(PIA_SOLENOID_PORT_A, 0xFF);
WOS_DataWrite(PIA_SOLENOID_CONTROL_A, 0x3C);
#endif
WOS_DataWrite(PIA_SOLENOID_PORT_A, 0x00);
#ifndef WOS_11_MPU
WOS_DataWrite(PIA_SOLENOID_CONTROL_B, 0x30);
WOS_DataWrite(PIA_SOLENOID_PORT_B, 0xFF);
WOS_DataWrite(PIA_SOLENOID_CONTROL_B, 0x34);
WOS_DataWrite(PIA_SOLENOID_PORT_B, 0x00);
#endif
#ifdef WOS_11_MPU
WOS_DataWrite(PIA_SOLENOID_11_CONTROL_B, 0x38);
WOS_DataWrite(PIA_SOLENOID_11_PORT_B, 0xFF);
WOS_DataWrite(PIA_SOLENOID_11_CONTROL_B, 0x3C);
WOS_DataWrite(PIA_SOLENOID_11_PORT_B, 0x00);
WOS_DataWrite(PIA_ALPHA_DISPLAY_CONTROL_A, 0x38);
WOS_DataWrite(PIA_ALPHA_DISPLAY_PORT_A, 0xFF);
WOS_DataWrite(PIA_ALPHA_DISPLAY_CONTROL_A, 0x3C);
WOS_DataWrite(PIA_ALPHA_DISPLAY_PORT_A, 0x00);
WOS_DataWrite(PIA_ALPHA_DISPLAY_CONTROL_B, 0x38);
WOS_DataWrite(PIA_ALPHA_DISPLAY_PORT_B, 0xFF);
WOS_DataWrite(PIA_ALPHA_DISPLAY_CONTROL_B, 0x3C);
WOS_DataWrite(PIA_ALPHA_DISPLAY_PORT_B, 0x00);
WOS_DataWrite(PIA_NUM_DISPLAY_CONTROL_A, 0x38);
WOS_DataWrite(PIA_NUM_DISPLAY_PORT_A, 0xFF);
WOS_DataWrite(PIA_NUM_DISPLAY_CONTROL_A, 0x3C);
WOS_DataWrite(PIA_NUM_DISPLAY_PORT_A, 0x00);
WOS_DataWrite(PIA_SOUND_11_CONTROL_A, 0x38);
WOS_DataWrite(PIA_SOUND_11_PORT_A, 0xFF);
WOS_DataWrite(PIA_SOUND_11_CONTROL_A, 0x3C);
WOS_DataWrite(PIA_SOUND_11_PORT_A, 0x00);
WOS_DataWrite(PIA_WIDGET_CONTROL_B, 0x38);
WOS_DataWrite(PIA_WIDGET_PORT_B, 0xFF);
WOS_DataWrite(PIA_WIDGET_CONTROL_B, 0x3C);
WOS_DataWrite(PIA_WIDGET_PORT_B, 0x00);
#endif
#ifdef WOS_7_MPU
WOS_DataWrite(PIA_SOUND_COMMA_CONTROL_A, 0x38);
WOS_DataWrite(PIA_SOUND_COMMA_PORT_A, 0xFF);
WOS_DataWrite(PIA_SOUND_COMMA_CONTROL_A, 0x3C);
WOS_DataWrite(PIA_SOUND_COMMA_PORT_A, 0x00);
WOS_DataWrite(PIA_SOUND_COMMA_CONTROL_B, 0x38);
WOS_DataWrite(PIA_SOUND_COMMA_PORT_B, 0xFF);
WOS_DataWrite(PIA_SOUND_COMMA_CONTROL_B, 0x3C);
WOS_DataWrite(PIA_SOUND_COMMA_PORT_B, 0x00);
#endif
}
void WOS_SetBoardLEDs(boolean LED1, boolean LED2, byte BCDValue) {
BoardLEDs = 0;
if (BCDValue==0xFF) {
if (LED1) BoardLEDs |= 0x20;
if (LED2) BoardLEDs |= 0x10;
} else {
BoardLEDs = BCDValue * 16;
}
}
void WOS_SetupInterrupt() {
pinMode(13,OUTPUT);
digitalWrite(13, 0);
cli();
//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for selected increment
// OCR1A = 16574;
OCR1A = INTERRUPT_OCR1A_COUNTER;
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR1B |= (0 << CS12) | (0 << CS11) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();
}
void WOS_ClearVariables() {
for (byte count=0; count<WOS_NUM_LAMP_BANKS; count++) {
LampStates[WOS_NUM_LAMP_BANKS] = 0xFF;
LampDim1[WOS_NUM_LAMP_BANKS] = 0x00;
LampDim2[WOS_NUM_LAMP_BANKS] = 0x00;
}
for (byte count=0; count<WOS_MAX_LAMPS; count++) {
LampFlashPeriod[count] = 0;
}
for (byte count=0; count<4; count++) {
for (byte digit=0; digit<WOS_NUM_DIGITS; digit++) {
DisplayDigits[count][digit] = 0;
}
if (WOS_NUM_DIGITS==6) {
DisplayDigitEnable[count] = 0x3F;
} else if (WOS_NUM_DIGITS==7) {
DisplayDigitEnable[count] = 0x7F;
}
}
for (byte count=0; count<2; count++) {
DisplayCreditDigits[count] = 0;
DisplayBIPDigits[count] = 0;
}
DisplayCreditDigitEnable = 0x03;
DisplayBIPDigitEnable = 0x03;
for (byte count=0; count<SWITCH_COLUMNS; count++) {
SwitchesMinus2[count] = 0xFF;
SwitchesMinus1[count] = 0xFF;
SwitchesNow[count] = 0xFF;
}
// Reset solenoid stack
SolenoidStackFirst = 0;
SolenoidStackLast = 0;
// Reset switch stack
SwitchStackFirst = 0;
SwitchStackLast = 0;
// Reset sound stack
SoundStackFirst = 0;
SoundStackLast = 0;
for (byte count=0; count<TIMED_SOLENOID_STACK_SIZE; count++) {
TimedSolenoidStack[count].inUse = 0;
TimedSolenoidStack[count].pushTime = 0;
TimedSolenoidStack[count].solenoidNumber = 0;
TimedSolenoidStack[count].numPushes = 0;
TimedSolenoidStack[count].disableOverride = 0;
}
for (byte count=0; count<TIMED_SOUND_STACK_SIZE; count++) {
TimedSoundStack[count].inUse = 0;
TimedSoundStack[count].pushTime = 0;
TimedSoundStack[count].soundNumber = 0;
TimedSoundStack[count].numPushes = 0;
}
}
/*
* Lamp handling functions
*/
void WOS_SetDimDivisor(byte level, byte divisor) {
if (level==1) DimDivisor1 = divisor;
if (level==2) DimDivisor2 = divisor;
}
void WOS_ApplyFlashToLamps(unsigned long curTime) {
for (int count=0; count<WOS_MAX_LAMPS; count++) {
if ( LampFlashPeriod[count]!=0 ) {
unsigned long adjustedLampFlash = (unsigned long)LampFlashPeriod[count] * (unsigned long)50;
if ((curTime/adjustedLampFlash)%2) {
LampStates[count/8] &= ~(0x01<<(count%8));
} else {
LampStates[count/8] |= (0x01<<(count%8));
}
} // end if this light should flash
} // end loop on lights
}
void WOS_FlashAllLamps(unsigned long curTime) {
for (int count=0; count<WOS_MAX_LAMPS; count++) {
WOS_SetLampState(count, 1, 0, 500);
}
WOS_ApplyFlashToLamps(curTime);
}
void WOS_TurnOffAllLamps() {
for (int count=0; count<WOS_MAX_LAMPS; count++) {
WOS_SetLampState(count, 0, 0, 0);
}
}
void WOS_SetLampState(int lampNum, byte s_lampState, byte s_lampDim, int s_lampFlashPeriod) {
if (lampNum>=WOS_MAX_LAMPS || lampNum<0) return;
if (s_lampState) {
int adjustedLampFlash = s_lampFlashPeriod/50;
if (s_lampFlashPeriod!=0 && adjustedLampFlash==0) adjustedLampFlash = 1;
if (adjustedLampFlash>250) adjustedLampFlash = 250;
// Only turn on the lamp if there's no flash, because if there's a flash
// then the lamp will be turned on by the ApplyFlashToLamps function
if (s_lampFlashPeriod==0) LampStates[lampNum/8] &= ~(0x01<<(lampNum%8));
LampFlashPeriod[lampNum] = adjustedLampFlash;
} else {
LampStates[lampNum/8] |= (0x01<<(lampNum%8));
LampFlashPeriod[lampNum] = 0;
}
if (s_lampDim & 0x01) {
LampDim1[lampNum/8] |= (0x01<<(lampNum%8));
} else {
LampDim1[lampNum/8] &= ~(0x01<<(lampNum%8));
}
if (s_lampDim & 0x02) {
LampDim2[lampNum/8] |= (0x01<<(lampNum%8));
} else {
LampDim2[lampNum/8] &= ~(0x01<<(lampNum%8));
}
}
/*
* Sound handling functions
*/
unsigned short SoundLowerLimit = 0x0100;
unsigned short SoundUpperLimit = 0x1F00;
void WOS_SetSoundValueLimits(unsigned short lowerLimit, unsigned short upperLimit) {
SoundLowerLimit = lowerLimit;
SoundUpperLimit = upperLimit;
}
int SpaceLeftOnSoundStack() {
if (SoundStackFirst>=SOUND_STACK_SIZE || SoundStackLast>=SOUND_STACK_SIZE) return 0;
if (SoundStackLast>=SoundStackFirst) return ((SOUND_STACK_SIZE-1) - (SoundStackLast-SoundStackFirst));
return (SoundStackFirst - SoundStackLast) - 1;
}
#ifdef WOS_7_MPU
void WOS_PlayType2Sound(byte soundNumber) {
#ifndef WOS_TYPE_2_SOUND
return;
#endif
WOS_DataWrite(PIA_SOUND_COMMA_PORT_A, (~soundNumber) & 0x7F);
}
#endif
void WOS_PushToSoundStack(unsigned short soundNumber, byte numPushes) {
// If the solenoid stack last index is out of range, then it's an error - return
if (SpaceLeftOnSoundStack()==0) return;
if (soundNumber<SoundLowerLimit || soundNumber>SoundUpperLimit) return;
//Serial.write("SoundPush\n");
for (int count=0; count<numPushes; count++) {
SoundStack[SoundStackLast] = soundNumber;
SoundStackLast += 1;
if (SoundStackLast==SOUND_STACK_SIZE) {
// If the end index is off the end, then wrap
SoundStackLast = 0;
}
// If the stack is now full, return
if (SpaceLeftOnSoundStack()==0) return;
}
}
unsigned short PullFirstFromSoundStack() {
// If first and last are equal, there's nothing on the stack
if (SoundStackFirst==SoundStackLast) {
return SOUND_STACK_EMPTY;
}
unsigned short retVal = SoundStack[SoundStackFirst];
SoundStackFirst += 1;
if (SoundStackFirst>=SOUND_STACK_SIZE) SoundStackFirst = 0;
return retVal;
}
boolean WOS_PushToTimedSoundStack(unsigned short soundNumber, byte numPushes, unsigned long whenToPlay) {
for (int count=0; count<TIMED_SOUND_STACK_SIZE; count++) {
if (!TimedSoundStack[count].inUse) {
TimedSoundStack[count].inUse = true;
TimedSoundStack[count].pushTime = whenToPlay;
TimedSoundStack[count].soundNumber = soundNumber;
TimedSoundStack[count].numPushes = numPushes;
return true;
}
}
return false;
}
void WOS_UpdateTimedSoundStack(unsigned long curTime) {
for (int count=0; count<TIMED_SOUND_STACK_SIZE; count++) {
if (TimedSoundStack[count].inUse && TimedSoundStack[count].pushTime<curTime) {
//Serial.write("Sound\n");
WOS_PushToSoundStack(TimedSoundStack[count].soundNumber, TimedSoundStack[count].numPushes);
TimedSoundStack[count].inUse = false;
}
}
}
#ifdef WOS_11_MPU
void WOS_PlayWOS11Sound(byte soundNum) {
WOS_DataWrite(PIA_SOUND_11_PORT_A, soundNum);
// Strobe CA2
WOS_DataWrite(PIA_SOUND_11_CONTROL_A, 0x34);
WOS_DataWrite(PIA_SOUND_11_CONTROL_A, 0x3C);
}
void WOS_PlayWOS11Music(byte songNum) {
WOS_DataWrite(PIA_WIDGET_PORT_B, songNum);
// Strobe CA2
WOS_DataWrite(PIA_WIDGET_CONTROL_B, 0x34);
WOS_DataWrite(PIA_WIDGET_CONTROL_B, 0x3C);
}
#endif
/*
* Solenoid handling functions
*/
void WOS_DisableSolenoidStack() {
SolenoidStackEnabled = false;
WOS_DataWrite(PIA_SOLENOID_CONTROL_B, 0x34);
}
void WOS_EnableSolenoidStack() {
SolenoidStackEnabled = true;
WOS_DataWrite(PIA_SOLENOID_CONTROL_B, 0x3C);
}
void WOS_SetDisableFlippers(boolean disableFlippers) {
if (disableFlippers) WOS_DataWrite(PIA_SOLENOID_CONTROL_B, 0x34);
else WOS_DataWrite(PIA_SOLENOID_CONTROL_B, 0x3C);
}
void WOS_SetContinuousSolenoid(boolean solOn, byte solNum) {
unsigned short oldCont = ContinuousSolenoidBits;
if (solOn) ContinuousSolenoidBits |= (1<<solNum);
else ContinuousSolenoidBits &= ~(1<<solNum);
if (oldCont!=ContinuousSolenoidBits) {
byte origPortA = WOS_DataRead(PIA_SOLENOID_PORT_A);
byte origPortB = WOS_DataRead(PIA_SOLENOID_PORT_B);
if (origPortA!=(ContinuousSolenoidBits&0xFF)) WOS_DataWrite(PIA_SOLENOID_PORT_A, (ContinuousSolenoidBits&0xFF));
if (origPortB!=(ContinuousSolenoidBits/256)) WOS_DataWrite(PIA_SOLENOID_PORT_B, (ContinuousSolenoidBits/256));
}
}
void WOS_SetCoinLockout(boolean lockoutOn, byte solNum) {
WOS_SetContinuousSolenoid(lockoutOn, solNum);
}
int SpaceLeftOnSolenoidStack() {
if (SolenoidStackFirst>=SOLENOID_STACK_SIZE || SolenoidStackLast>=SOLENOID_STACK_SIZE) return 0;
if (SolenoidStackLast>=SolenoidStackFirst) return ((SOLENOID_STACK_SIZE-1) - (SolenoidStackLast-SolenoidStackFirst));
return (SolenoidStackFirst - SolenoidStackLast) - 1;
}
void WOS_PushToSolenoidStack(byte solenoidNumber, byte numPushes, boolean disableOverride) {
if (solenoidNumber>21) return;
//Serial.write("SolPush\n");
// if the solenoid stack is disabled and this isn't an override push, then return
if (!disableOverride && !SolenoidStackEnabled) return;
// If the solenoid stack last index is out of range, then it's an error - return
if (SpaceLeftOnSolenoidStack()==0) return;
for (int count=0; count<numPushes; count++) {
SolenoidStack[SolenoidStackLast] = solenoidNumber;
SolenoidStackLast += 1;
if (SolenoidStackLast==SOLENOID_STACK_SIZE) {
// If the end index is off the end, then wrap