-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSA-RPU.cpp
More file actions
1578 lines (1283 loc) · 48.1 KB
/
SA-RPU.cpp
File metadata and controls
1578 lines (1283 loc) · 48.1 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
/**************************************************************************
* This file is part of the RPU for Arduino Project.
I, Dick Hamill, the author of this program disclaim all copyright
in order to make this program freely available in perpetuity to
anyone who would like to use it. Dick Hamill, 3/31/2023
RPU is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RPU is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
See <https://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include <EEPROM.h>
//#define DEBUG_MESSAGES 1
#define RPU_CPP_FILE
#include "RPU_config.h"
#include "SA-RPU.h"
#ifndef RPU_OS_HARDWARE_REV
#define RPU_OS_HARDWARE_REV 1
#endif
/******************************************************
* The board type, MPU architecture, and supported
* features are all controlled through the
* RPU_Config.h file.
*/
#include "RPU_Config.h"
/******************************************************
* Defines and library variables
*/
#if (RPU_MPU_ARCHITECTURE >= 10)
#define NUM_SWITCH_BYTES 8
#define MAX_NUM_SWITCHES 64
#ifndef INTERRUPT_OCR1A_COUNTER
#define INTERRUPT_OCR1A_COUNTER 16574
#endif
volatile byte BoardLEDs = 0;
volatile boolean UpDownSwitch = false;
unsigned short ContinuousSolenoidBits = 0;
volatile byte DisplayCreditDigits[2];
volatile byte DisplayCreditDigitEnable;
volatile byte DisplayBIPDigits[2];
volatile byte DisplayBIPDigitEnable;
#endif // End of condition based on RPU_MPU_ARCHITECTURE
// Global variables
volatile byte DisplayDigits[5][RPU_OS_NUM_DIGITS];
volatile byte DisplayDigitEnable[5];
volatile boolean DisplayOffCycle = false;
volatile byte CurrentDisplayDigit=0;
volatile byte LampStates[RPU_NUM_LAMP_BANKS], LampDim1[RPU_NUM_LAMP_BANKS], LampDim2[RPU_NUM_LAMP_BANKS];
volatile byte LampFlashPeriod[RPU_MAX_LAMPS];
byte DimDivisor1 = 2;
byte DimDivisor2 = 3;
volatile byte SwitchesMinus2[NUM_SWITCH_BYTES];
volatile byte SwitchesMinus1[NUM_SWITCH_BYTES];
volatile byte SwitchesNow[NUM_SWITCH_BYTES];
#ifdef RPU_OS_USE_DIP_SWITCHES
byte DipSwitches[4];
#endif
#define SOLENOID_STACK_SIZE 60
#define SOLENOID_STACK_EMPTY 0xFF
volatile byte SolenoidStackFirst;
volatile byte SolenoidStackLast;
volatile byte SolenoidStack[SOLENOID_STACK_SIZE];
boolean SolenoidStackEnabled = true;
volatile byte CurrentSolenoidByte = 0xFF;
volatile byte RevertSolenoidBit = 0x00;
volatile byte NumCyclesBeforeRevertingSolenoidByte = 0;
#define TIMED_SOLENOID_STACK_SIZE 30
struct TimedSolenoidEntry {
byte inUse;
unsigned long pushTime;
byte solenoidNumber;
byte numPushes;
byte disableOverride;
};
TimedSolenoidEntry TimedSolenoidStack[TIMED_SOLENOID_STACK_SIZE] = {0, 0, 0, 0, 0};
#define SWITCH_STACK_SIZE 60
#define SWITCH_STACK_EMPTY 0xFF
volatile byte SwitchStackFirst;
volatile byte SwitchStackLast;
volatile byte SwitchStack[SWITCH_STACK_SIZE];
// The WTYPE1 and WTYPE2 sound cards can only play one sound at a time,
// so these structures allow the app to send in as many calls as they
// want, but with a priority and requested amount of time to let
// it play. This could be ported over to other architectures
// like S&T or -51, etc., but right now I've only implemented it for
// MPU Architecture > 9
#if (RPU_MPU_ARCHITECTURE >= 10)
#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};
#endif
#if (RPU_OS_HARDWARE_REV>=100)
#if (RPU_MPU_ARCHITECTURE<10)
#define ADDRESS_U10_A 0x88
#define ADDRESS_U10_A_CONTROL 0x89
#define ADDRESS_U10_B 0x8A
#define ADDRESS_U10_B_CONTROL 0x8B
#define ADDRESS_U11_A 0x90
#define ADDRESS_U11_A_CONTROL 0x91
#define ADDRESS_U11_B 0x92
#define ADDRESS_U11_B_CONTROL 0x93
#define ADDRESS_SB100 0xA0
#define ADDRESS_SB100_CHIMES 0xC0
#define ADDRESS_SB300_SQUARE_WAVES 0xA0
#define ADDRESS_SB300_ANALOG 0xC0
#else
#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
#if (RPU_MPU_ARCHITECTURE==13)
#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
#if (RPU_MPU_ARCHITECTURE==15)
#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
#endif
#endif
#if (RPU_OS_HARDWARE_REV==1000)
void RPU_DataWrite(int portAddress, byte val) {
// STM32 port mapping here to pins or internal registers
}
byte RPU_DataRead(int portAddress) {
// STM32 port mapping here to pins or internal registers
return 0xFF;
}
#else
#error "RPU Hardware Definition Not Recognized"
#endif
void RPU_InitializePIAs() {
RPU_DataWrite(PIA_DISPLAY_CONTROL_A, 0x31);
RPU_DataWrite(PIA_DISPLAY_PORT_A, 0xFF);
RPU_DataWrite(PIA_DISPLAY_CONTROL_A, 0x3D);
RPU_DataWrite(PIA_DISPLAY_PORT_A, 0xC0);
RPU_DataWrite(PIA_DISPLAY_CONTROL_B, 0x31);
RPU_DataWrite(PIA_DISPLAY_PORT_B, 0xFF);
RPU_DataWrite(PIA_DISPLAY_CONTROL_B, 0x3D);
RPU_DataWrite(PIA_DISPLAY_PORT_B, 0x00);
RPU_DataWrite(PIA_SWITCH_CONTROL_A, 0x38);
RPU_DataWrite(PIA_SWITCH_PORT_A, 0x00);
RPU_DataWrite(PIA_SWITCH_CONTROL_A, 0x3C);
RPU_DataWrite(PIA_SWITCH_CONTROL_B, 0x38);
RPU_DataWrite(PIA_SWITCH_PORT_B, 0xFF);
RPU_DataWrite(PIA_SWITCH_CONTROL_B, 0x3C);
RPU_DataWrite(PIA_SWITCH_PORT_B, 0x00);
RPU_DataWrite(PIA_LAMPS_CONTROL_A, 0x38);
RPU_DataWrite(PIA_LAMPS_PORT_A, 0xFF);
RPU_DataWrite(PIA_LAMPS_CONTROL_A, 0x3C);
RPU_DataWrite(PIA_LAMPS_PORT_A, 0xFF);
RPU_DataWrite(PIA_LAMPS_CONTROL_B, 0x38);
RPU_DataWrite(PIA_LAMPS_PORT_B, 0xFF);
RPU_DataWrite(PIA_LAMPS_CONTROL_B, 0x3C);
RPU_DataWrite(PIA_LAMPS_PORT_B, 0x00);
#if (RPU_MPU_ARCHITECTURE<15)
RPU_DataWrite(PIA_SOLENOID_CONTROL_A, 0x38);
RPU_DataWrite(PIA_SOLENOID_PORT_A, 0xFF);
RPU_DataWrite(PIA_SOLENOID_CONTROL_A, 0x3C);
#endif
RPU_DataWrite(PIA_SOLENOID_PORT_A, 0x00);
#if (RPU_MPU_ARCHITECTURE<15)
RPU_DataWrite(PIA_SOLENOID_CONTROL_B, 0x30);
RPU_DataWrite(PIA_SOLENOID_PORT_B, 0xFF);
RPU_DataWrite(PIA_SOLENOID_CONTROL_B, 0x34);
RPU_DataWrite(PIA_SOLENOID_PORT_B, 0x00);
#endif
#if (RPU_MPU_ARCHITECTURE==15)
RPU_DataWrite(PIA_SOLENOID_11_CONTROL_B, 0x38);
RPU_DataWrite(PIA_SOLENOID_11_PORT_B, 0xFF);
RPU_DataWrite(PIA_SOLENOID_11_CONTROL_B, 0x3C);
RPU_DataWrite(PIA_SOLENOID_11_PORT_B, 0x00);
RPU_DataWrite(PIA_ALPHA_DISPLAY_CONTROL_A, 0x38);
RPU_DataWrite(PIA_ALPHA_DISPLAY_PORT_A, 0xFF);
RPU_DataWrite(PIA_ALPHA_DISPLAY_CONTROL_A, 0x3C);
RPU_DataWrite(PIA_ALPHA_DISPLAY_PORT_A, 0x00);
RPU_DataWrite(PIA_ALPHA_DISPLAY_CONTROL_B, 0x38);
RPU_DataWrite(PIA_ALPHA_DISPLAY_PORT_B, 0xFF);
RPU_DataWrite(PIA_ALPHA_DISPLAY_CONTROL_B, 0x3C);
RPU_DataWrite(PIA_ALPHA_DISPLAY_PORT_B, 0x00);
RPU_DataWrite(PIA_NUM_DISPLAY_CONTROL_A, 0x38);
RPU_DataWrite(PIA_NUM_DISPLAY_PORT_A, 0xFF);
RPU_DataWrite(PIA_NUM_DISPLAY_CONTROL_A, 0x3C);
RPU_DataWrite(PIA_NUM_DISPLAY_PORT_A, 0x00);
RPU_DataWrite(PIA_SOUND_11_CONTROL_A, 0x38);
RPU_DataWrite(PIA_SOUND_11_PORT_A, 0xFF);
RPU_DataWrite(PIA_SOUND_11_CONTROL_A, 0x3C);
RPU_DataWrite(PIA_SOUND_11_PORT_A, 0x00);
RPU_DataWrite(PIA_WIDGET_CONTROL_B, 0x38);
RPU_DataWrite(PIA_WIDGET_PORT_B, 0xFF);
RPU_DataWrite(PIA_WIDGET_CONTROL_B, 0x3C);
RPU_DataWrite(PIA_WIDGET_PORT_B, 0x00);
#endif
#if (RPU_MPU_ARCHITECTURE==13)
RPU_DataWrite(PIA_SOUND_COMMA_CONTROL_A, 0x38);
RPU_DataWrite(PIA_SOUND_COMMA_PORT_A, 0xFF);
RPU_DataWrite(PIA_SOUND_COMMA_CONTROL_A, 0x3C);
RPU_DataWrite(PIA_SOUND_COMMA_PORT_A, 0x00);
RPU_DataWrite(PIA_SOUND_COMMA_CONTROL_B, 0x38);
RPU_DataWrite(PIA_SOUND_COMMA_PORT_B, 0xFF);
RPU_DataWrite(PIA_SOUND_COMMA_CONTROL_B, 0x3C);
RPU_DataWrite(PIA_SOUND_COMMA_PORT_B, 0x00);
#endif
}
unsigned long RPU_TestPIAs() {
unsigned long piaErrors = 0;
byte piaResult = RPU_DataRead(PIA_DISPLAY_CONTROL_A);
if (piaResult!=0x3D) piaErrors |= RPU_RET_PIA_1_ERROR;
piaResult = RPU_DataRead(PIA_DISPLAY_CONTROL_B);
if (piaResult!=0x3D) piaErrors |= RPU_RET_PIA_1_ERROR;
piaResult = RPU_DataRead(PIA_SWITCH_CONTROL_A);
if (piaResult!=0x3C) piaErrors |= RPU_RET_PIA_2_ERROR;
piaResult = RPU_DataRead(PIA_SWITCH_CONTROL_B);
if (piaResult!=0x3C) piaErrors |= RPU_RET_PIA_2_ERROR;
piaResult = RPU_DataRead(PIA_LAMPS_CONTROL_A);
if (piaResult!=0x3C) piaErrors |= RPU_RET_PIA_3_ERROR;
piaResult = RPU_DataRead(PIA_LAMPS_CONTROL_B);
if (piaResult!=0x3C) piaErrors |= RPU_RET_PIA_3_ERROR;
piaResult = RPU_DataRead(PIA_SOLENOID_CONTROL_A);
if (piaResult!=0x3C) piaErrors |= RPU_RET_PIA_4_ERROR;
piaResult = RPU_DataRead(PIA_SOLENOID_CONTROL_B);
if (piaResult!=0x3C) piaErrors |= RPU_RET_PIA_4_ERROR;
#if (RPU_MPU_ARCHITECTURE==13)
piaResult = RPU_DataRead(PIA_SOUND_COMMA_CONTROL_A);
if (piaResult!=0x3C) piaErrors |= RPU_RET_PIA_5_ERROR;
piaResult = RPU_DataRead(PIA_SOUND_COMMA_CONTROL_B);
if (piaResult!=0x3C) piaErrors |= RPU_RET_PIA_5_ERROR;
#endif
return piaErrors;
}
void RPU_SetBoardLEDs(boolean LED1, boolean LED2, byte BCDValue) {
BoardLEDs = 0;
if (BCDValue==0xFF) {
if (LED1) BoardLEDs |= 0x20;
if (LED2) BoardLEDs |= 0x10;
} else {
BoardLEDs = BCDValue * 16;
}
}
/******************************************************
* Switch Handling Functions
*/
int SpaceLeftOnSwitchStack() {
if (SwitchStackFirst>=SWITCH_STACK_SIZE || SwitchStackLast>=SWITCH_STACK_SIZE) return 0;
if (SwitchStackLast>=SwitchStackFirst) return ((SWITCH_STACK_SIZE-1) - (SwitchStackLast-SwitchStackFirst));
return (SwitchStackFirst - SwitchStackLast) - 1;
}
void PushToSwitchStack(byte switchNumber) {
//if ((switchNumber>=MAX_NUM_SWITCHES && switchNumber!=SW_SELF_TEST_SWITCH)) return;
if (switchNumber==SWITCH_STACK_EMPTY) return;
// If the switch stack last index is out of range, then it's an error - return
if (SpaceLeftOnSwitchStack()==0) return;
// Self test is a special case - there's no good way to debounce it
// so if it's already first on the stack, ignore it
if (switchNumber==SW_SELF_TEST_SWITCH) {
if (SwitchStackLast!=SwitchStackFirst && SwitchStack[SwitchStackFirst]==SW_SELF_TEST_SWITCH) return;
}
SwitchStack[SwitchStackLast] = switchNumber;
SwitchStackLast += 1;
if (SwitchStackLast==SWITCH_STACK_SIZE) {
// If the end index is off the end, then wrap
SwitchStackLast = 0;
}
}
void RPU_PushToSwitchStack(byte switchNumber) {
PushToSwitchStack(switchNumber);
}
byte RPU_PullFirstFromSwitchStack() {
// If first and last are equal, there's nothing on the stack
if (SwitchStackFirst==SwitchStackLast) return SWITCH_STACK_EMPTY;
byte retVal = SwitchStack[SwitchStackFirst];
SwitchStackFirst += 1;
if (SwitchStackFirst>=SWITCH_STACK_SIZE) SwitchStackFirst = 0;
return retVal;
}
boolean RPU_ReadSingleSwitchState(byte switchNum) {
if (switchNum>=MAX_NUM_SWITCHES) return false;
int switchByte = switchNum/8;
int switchBit = switchNum%8;
if ( ((SwitchesNow[switchByte])>>switchBit) & 0x01 ) return true;
else return false;
}
byte RPU_GetDipSwitches(byte index) {
#ifdef RPU_OS_USE_DIP_SWITCHES
if (index>3) return 0x00;
return DipSwitches[index];
#else
return 0x00 & index;
#endif
}
void RPU_SetupGameSwitches(int s_numSwitches, int s_numPrioritySwitches, PlayfieldAndCabinetSwitch *s_gameSwitchArray) {
NumGameSwitches = s_numSwitches;
NumGamePrioritySwitches = s_numPrioritySwitches;
GameSwitches = s_gameSwitchArray;
}
void RPU_ClearUpDownSwitchState() {
UpDownSwitch = false;
}
boolean RPU_GetUpDownSwitchState() {
return UpDownSwitch;
}
byte RPU_GetSwitchCount() {
#if (RPU_MPU_ARCHITECTURE>=10)
return MAX_NUM_SWITCHES + 3; // added one for self-test switch, one for up/down, and one for clear high score
#else
return MAX_NUM_SWITCHES + 1; // added one for self-test switch
#endif
}
/******************************************************
* Solenoid Handling Functions
*/
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 RPU_PushToSolenoidStack(byte solenoidNumber, byte numPushes, boolean disableOverride) {
if (solenoidNumber>14) return;
// 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
SolenoidStackLast = 0;
}
// If the stack is now full, return
if (SpaceLeftOnSolenoidStack()==0) return;
}
}
void PushToFrontOfSolenoidStack(byte solenoidNumber, byte numPushes) {
// If the stack is full, return
if (SpaceLeftOnSolenoidStack()==0 || !SolenoidStackEnabled) return;
for (int count=0; count<numPushes; count++) {
if (SolenoidStackFirst==0) SolenoidStackFirst = SOLENOID_STACK_SIZE-1;
else SolenoidStackFirst -= 1;
SolenoidStack[SolenoidStackFirst] = solenoidNumber;
if (SpaceLeftOnSolenoidStack()==0) return;
}
}
byte PullFirstFromSolenoidStack() {
// If first and last are equal, there's nothing on the stack
if (SolenoidStackFirst==SolenoidStackLast) return SOLENOID_STACK_EMPTY;
byte retVal = SolenoidStack[SolenoidStackFirst];
SolenoidStackFirst += 1;
if (SolenoidStackFirst>=SOLENOID_STACK_SIZE) SolenoidStackFirst = 0;
return retVal;
}
boolean RPU_PushToTimedSolenoidStack(byte solenoidNumber, byte numPushes, unsigned long whenToFire, boolean disableOverride) {
for (int count=0; count<TIMED_SOLENOID_STACK_SIZE; count++) {
if (!TimedSolenoidStack[count].inUse) {
TimedSolenoidStack[count].inUse = true;
TimedSolenoidStack[count].pushTime = whenToFire;
TimedSolenoidStack[count].disableOverride = disableOverride;
TimedSolenoidStack[count].solenoidNumber = solenoidNumber;
TimedSolenoidStack[count].numPushes = numPushes;
return true;
}
}
return false;
}
void RPU_UpdateTimedSolenoidStack(unsigned long curTime) {
for (int count=0; count<TIMED_SOLENOID_STACK_SIZE; count++) {
if (TimedSolenoidStack[count].inUse && TimedSolenoidStack[count].pushTime<curTime) {
RPU_PushToSolenoidStack(TimedSolenoidStack[count].solenoidNumber, TimedSolenoidStack[count].numPushes, TimedSolenoidStack[count].disableOverride);
TimedSolenoidStack[count].inUse = false;
}
}
}
void RPU_SetDisableFlippers(boolean disableFlippers, byte solbit) {
(void)solbit;
if (disableFlippers) RPU_DataWrite(PIA_SOLENOID_CONTROL_B, 0x34);
else RPU_DataWrite(PIA_SOLENOID_CONTROL_B, 0x3C);
}
void RPU_SetContinuousSolenoid(boolean solOn, byte solNum) {
unsigned short oldCont = ContinuousSolenoidBits;
if (solOn) ContinuousSolenoidBits |= (1<<solNum);
else ContinuousSolenoidBits &= ~(1<<solNum);
if (oldCont!=ContinuousSolenoidBits) {
byte origPortA = RPU_DataRead(PIA_SOLENOID_PORT_A);
byte origPortB = RPU_DataRead(PIA_SOLENOID_PORT_B);
if (origPortA!=(ContinuousSolenoidBits&0xFF)) RPU_DataWrite(PIA_SOLENOID_PORT_A, (ContinuousSolenoidBits&0xFF));
if (origPortB!=(ContinuousSolenoidBits/256)) RPU_DataWrite(PIA_SOLENOID_PORT_B, (ContinuousSolenoidBits/256));
}
}
byte RPU_ReadContinuousSolenoids() {
return ContinuousSolenoidBits;
}
void RPU_SetCoinLockout(boolean lockoutOn, byte solNum) {
RPU_SetContinuousSolenoid(lockoutOn, solNum);
}
void RPU_DisableSolenoidStack() {
SolenoidStackEnabled = false;
RPU_DataWrite(PIA_SOLENOID_CONTROL_B, 0x34);
}
void RPU_EnableSolenoidStack() {
SolenoidStackEnabled = true;
RPU_DataWrite(PIA_SOLENOID_CONTROL_B, 0x3C);
}
byte RPU_GetSolenoidCount() {
// 16 pulsed and 6 triggered solenoids
return 16 + 6;
}
boolean RPU_GetSolenoidStatus(byte solNumber) {
boolean solOn = false;
if (solNumber>21) return false;
if (solNumber>15) {
// TODO:
// Caller is asking for the status of a triggered
// solenoid. We can't tell if it's triggered on,
// but we can tell if it's turned on by software
} else {
unsigned short currentSolenoids = 0;
// For System 4, 6, and 7 architectures,
// all solenoids are on PIA_SOLENOID_PORT_A and PORT_B
#if (RPU_MPU_ARCHITECTURE<15)
currentSolenoids = RPU_DataRead(PIA_SOLENOID_PORT_A);
currentSolenoids += ((unsigned short)RPU_DataRead(PIA_SOLENOID_PORT_B))*256;
#else
currentSolenoids = Sys11LastLowerSol;
currentSolenoids += ((unsigned short)RPU_DataRead(PIA_SOLENOID_11_PORT_B))*256;
#endif
if (currentSolenoids & (1<<solNumber)) solOn = true;
}
return solOn;
}
byte RPU_ConvertMSToSolenoidTime(byte solMilliseconds) {
// ISR runs at 960, and solenoids are decremented every other time
// so it's just about 2ms per tick
return solMilliseconds / 2;
}
/******************************************************
* Display Handling Functions
*/
#if (RPU_MPU_ARCHITECTURE<15)
byte RPU_SetDisplay(int displayNumber, unsigned long value, boolean blankByMagnitude, byte minDigits) {
if (displayNumber<0 || displayNumber>4) return 0;
byte blank = 0x00;
for (int count=0; count<RPU_OS_NUM_DIGITS; count++) {
blank = blank * 2;
if (value!=0 || count<minDigits) blank |= 1;
DisplayDigits[displayNumber][(RPU_OS_NUM_DIGITS-1)-count] = value%10;
value /= 10;
}
if (blankByMagnitude) DisplayDigitEnable[displayNumber] = blank;
return blank;
}
byte RPU_SetDisplayBCDArray(int displayNumber, byte *bcdArray, boolean blankByMagnitude) {
if (displayNumber<0 || displayNumber>5) return 0;
boolean bcdDataFinished = false;
byte blank = 0x00;
byte curBlankBit = 0x01;
byte curBCD;
if (displayNumber<4) {
for (byte count=0; count<RPU_OS_NUM_DIGITS; count++) {
curBCD = bcdArray[count];
if (!bcdDataFinished && curBCD!=0) {
if (curBCD>='0' && curBCD<='9') {
DisplayDigits[displayNumber][count] = curBCD - '0';
blank |= curBlankBit;
} else {
DisplayDigits[displayNumber][count] = 0x0F;
}
} else {
bcdDataFinished = true;
}
curBlankBit *= 2;
}
if (blankByMagnitude) DisplayDigitEnable[displayNumber] = blank;
} else {
if (displayNumber==4) {
// Credit display
#if (RPU_MPU_ARCHITECTURE<15)
curBCD = bcdArray[0];
if (curBCD>='0' && curBCD<='9') {
DisplayCreditDigits[0] = curBCD - '0';
blank |= 1;
}
curBCD = bcdArray[1];
if (curBCD>='0' && curBCD<='9') {
DisplayCreditDigits[1] = curBCD - '0';
blank |= 2;
}
if (blankByMagnitude) DisplayCreditDigitEnable = blank;
#endif
} else if (displayNumber==5) {
// BIP display
#if (RPU_MPU_ARCHITECTURE<15)
curBCD = bcdArray[0];
if (curBCD>='0' && curBCD<='9') {
DisplayBIPDigits[0] = curBCD - '0';
blank |= 1;
}
curBCD = bcdArray[1];
if (curBCD>='0' && curBCD<='9') {
DisplayBIPDigits[1] = curBCD - '0';
blank |= 2;
}
if (blankByMagnitude) DisplayBIPDigitEnable = blank;
#endif
}
}
return blank;
}
#endif
#if (RPU_MPU_ARCHITECTURE<15)
void RPU_SetDisplayCredits(int value, boolean displayOn, boolean showBothDigits) {
byte blank = 0x02;
value = value % 100;
if (value>=10) {
DisplayCreditDigits[0] = value/10;
blank |= 1;
} else {
DisplayCreditDigits[0] = 0;
if (showBothDigits) blank |= 1;
}
DisplayCreditDigits[1] = value%10;
if (displayOn) DisplayCreditDigitEnable = blank;
else DisplayCreditDigitEnable = 0;
}
void RPU_SetDisplayBallInPlay(int value, boolean displayOn, boolean showBothDigits) {
byte blank = 0x02;
value = value % 100;
if (value>=10) {
DisplayBIPDigits[0] = value/10;
blank |= 1;
} else {
DisplayBIPDigits[0] = 0;
if (showBothDigits) blank |= 1;
}
DisplayBIPDigits[1] = value%10;
if (displayOn) DisplayBIPDigitEnable = blank;
else DisplayBIPDigitEnable = 0;
}
#endif
void RPU_CycleAllDisplays(unsigned long curTime, byte digitNum) {
int displayDigit = (curTime/250)%10;
unsigned long value;
#if (RPU_OS_NUM_DIGITS==7)
value = displayDigit*1111111;
#else
value = displayDigit*111111;
#endif
byte displayNumToShow = 0;
byte displayBlank = RPU_OS_ALL_DIGITS_MASK;
if (digitNum!=0) {
displayNumToShow = (digitNum-1)/6;
#if (RPU_OS_NUM_DIGITS==7)
displayBlank = (0x40)>>((digitNum-1)%7);
#else
displayBlank = (0x20)>>((digitNum-1)%6);
#endif
}
for (int count=0; count<5; count++) {
if (digitNum) {
RPU_SetDisplay(count, value);
if (count==displayNumToShow) RPU_SetDisplayBlank(count, displayBlank);
else RPU_SetDisplayBlank(count, 0);
} else {
RPU_SetDisplay(count, value, true);
}
}
}
void RPU_SetDisplayMatch(int value, boolean displayOn, boolean showBothDigits) {
RPU_SetDisplayBallInPlay(value, displayOn, showBothDigits);
}
// This is confusing -
// Digit mask is like this
// bit= b7 b6 b5 b4 b3 b2 b1 b0
// digit= x x 6 5 4 3 2 1
// (with digit 6 being the least-significant, 1's digit
//
// so, looking at it from left to right on the display
// digit= 1 2 3 4 5 6
// bit= b0 b1 b2 b3 b4 b5
void RPU_SetDisplayBlank(int displayNumber, byte bitMask) {
if (displayNumber<0 || displayNumber>4) return;
DisplayDigitEnable[displayNumber] = bitMask;
}
byte RPU_GetDisplayBlank(int displayNumber) {
if (displayNumber<0 || displayNumber>4) return 0;
return DisplayDigitEnable[displayNumber];
}
void RPU_SetDisplayFlash(int displayNumber, unsigned long value, unsigned long curTime, int period, byte minDigits) {
// A period of zero toggles display every other time
if (period) {
if ((curTime/period)%2) {
RPU_SetDisplay(displayNumber, value, true, minDigits);
} else {
RPU_SetDisplayBlank(displayNumber, 0);
}
}
}
void RPU_SetDisplayFlashCredits(unsigned long curTime, int period) {
if (period) {
if ((curTime/period)%2) {
DisplayDigitEnable[4] |= 0x06;
} else {
DisplayDigitEnable[4] &= 0x39;
}
}
}
#if (RPU_MPU_ARCHITECTURE==15)
byte RPU_SetDisplayText(int displayNumber, char *text, boolean blankByLength) {
if (displayNumber>1 || displayNumber<0) return 0;
byte stringLength = 0xff;
boolean writeSpace = false;
byte blank = 0;
byte placeMask = 0x01;
for (stringLength=0; stringLength<RPU_OS_NUM_DIGITS; stringLength++) {
if (text[stringLength]==0) writeSpace = true;
if (!writeSpace) DisplayText[displayNumber][stringLength] = (byte)text[stringLength]-0x20;
else DisplayText[displayNumber][stringLength] = 0;
if (DisplayText[displayNumber][stringLength]) blank |= placeMask;
placeMask *= 2;
}
if (blankByLength) DisplayDigitEnable[displayNumber] = blank;
return stringLength;
}
// Architectures with alpha store numbers as 7-seg
byte RPU_SetDisplay(int displayNumber, unsigned long value, boolean blankByMagnitude, byte minDigits) {
if (displayNumber<0 || displayNumber>3) return 0;
byte blank = 0x00;
for (int count=0; count<RPU_OS_NUM_DIGITS; count++) {
blank = blank * 2;
if (value!=0 || count<minDigits) {
blank |= 1;
if (displayNumber/2) DisplayDigits[displayNumber][(RPU_OS_NUM_DIGITS-1)-count] = SevenSegmentNumbers[value%10];
else DisplayText[displayNumber][(RPU_OS_NUM_DIGITS-1)-count] = (value%10)+16;
} else {
if (displayNumber/2) DisplayDigits[displayNumber][(RPU_OS_NUM_DIGITS-1)-count] = 0;
else DisplayText[displayNumber][(RPU_OS_NUM_DIGITS-1)-count] = 0;
}
value /= 10;
}
if (blankByMagnitude) DisplayDigitEnable[displayNumber] = blank;
return blank;
}
void RPU_SetDisplayCredits(int value, boolean displayOn, boolean showBothDigits) {
byte blank = 0x02;
value = value % 100;
if (value>=10) {
DisplayCreditDigits[0] = SevenSegmentNumbers[value/10];
blank |= 1;
} else {
DisplayCreditDigits[0] = SevenSegmentNumbers[0];
if (showBothDigits) blank |= 1;
}
DisplayCreditDigits[1] = SevenSegmentNumbers[value%10];
if (displayOn) DisplayCreditDigitEnable = blank;
else DisplayCreditDigitEnable = 0;
}
void RPU_SetDisplayBallInPlay(int value, boolean displayOn, boolean showBothDigits) {
byte blank = 0x02;
value = value % 100;
if (value>=10) {
DisplayBIPDigits[0] = SevenSegmentNumbers[value/10];
blank |= 1;
} else {
DisplayBIPDigits[0] = SevenSegmentNumbers[0];
if (showBothDigits) blank |= 1;
}
DisplayBIPDigits[1] = SevenSegmentNumbers[value%10];
if (displayOn) DisplayBIPDigitEnable = blank;
else DisplayBIPDigitEnable = 0;
}
#endif
/******************************************************
* Lamp Handling Functions
*/
void RPU_SetDimDivisor(byte level, byte divisor) {
if (level==1) DimDivisor1 = divisor;
if (level==2) DimDivisor2 = divisor;
}
void RPU_SetLampState(int lampNum, byte s_lampState, byte s_lampDim, int s_lampFlashPeriod) {
if (lampNum>=RPU_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));
}
}
byte RPU_ReadLampState(int lampNum) {
if (lampNum>=RPU_MAX_LAMPS || lampNum<0) return 0x00;
byte lampStateByte = LampStates[lampNum/8];
return (lampStateByte & (0x01<<(lampNum%8))) ? 0 : 1;
}
byte RPU_ReadLampDim(int lampNum) {
if (lampNum>=RPU_MAX_LAMPS || lampNum<0) return 0x00;
byte lampDim = 0;
byte lampDimByte = LampDim1[lampNum/8];
if (lampDimByte & (0x01<<(lampNum%8))) lampDim |= 1;
lampDimByte = LampDim2[lampNum/8];
if (lampDimByte & (0x01<<(lampNum%8))) lampDim |= 2;
return lampDim;
}
int RPU_ReadLampFlash(int lampNum) {
if (lampNum>=RPU_MAX_LAMPS || lampNum<0) return 0;
return LampFlashPeriod[lampNum]*50;
}
void RPU_ApplyFlashToLamps(unsigned long curTime) {
int curLampByte = 0;
byte curLampBit = 0;
int curLampNum = 0;
for (curLampByte=0; curLampByte<RPU_NUM_LAMP_BANKS; curLampByte++) {
curLampBit = 0x01;
for (byte curBit=0; curBit<8; curBit++) {
if ( LampFlashPeriod[curLampNum]!=0 ) {
unsigned long adjustedLampFlash = (unsigned long)LampFlashPeriod[curLampNum] * (unsigned long)50;
if ((curTime/adjustedLampFlash)%2) {
LampStates[curLampByte] &= ~(curLampBit);
} else {
LampStates[curLampByte] |= (curLampBit);
}
}
curLampBit *= 2;
curLampNum += 1;
}
}
}
void RPU_FlashAllLamps(unsigned long curTime) {
for (int count=0; count<RPU_MAX_LAMPS; count++) {
RPU_SetLampState(count, 1, 0, 500);
}
RPU_ApplyFlashToLamps(curTime);
}
void RPU_TurnOffAllLamps() {
for (int count=0; count<RPU_MAX_LAMPS; count++) {
RPU_SetLampState(count, 0, 0, 0);
}
}
int RPU_GetLampCount() {
return RPU_MAX_LAMPS;
}
/******************************************************
* Helper Functions
*/
void RPU_ClearVariables() {
// Reset solenoid stack
SolenoidStackFirst = 0;
SolenoidStackLast = 0;
// Reset switch stack
SwitchStackFirst = 0;
SwitchStackLast = 0;