forked from RetroPinUpgrade/Trident2020
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RPU.cpp
3787 lines (3027 loc) · 112 KB
/
RPU.cpp
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 RPU_CPP_FILE
#include "RPU_config.h"
#include "RPU.h"
#define DEBUG_MESSAGES 0
#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 !defined(RPU_MPU_BUILD_FOR_6800) || (RPU_MPU_BUILD_FOR_6800==1)
boolean UsesM6800Processor = true;
#if (RPU_MPU_ARCHITECTURE>11) && (RPU_OS_HARDWARE_REV<102)
#error "Architecture > 11 doesn't make sense with RPU_MPU_BUILD_FOR_6800=1. Set RPU_MPU_BUILD_FOR_6800 to 0 in RPU_Config.h or choose a different RPU_MPU_ARCHITECTURE"
#endif
#else
boolean UsesM6800Processor = false;
#endif
#if (RPU_MPU_ARCHITECTURE<10)
#ifdef RPU_USE_EXTENDED_SWITCHES_ON_PB4
#define NUM_SWITCH_BYTES 6
#define NUM_SWITCH_BYTES_ON_U10_PORT_A 5
#define MAX_NUM_SWITCHES 48
#define DEFAULT_SOLENOID_STATE 0x8F
#define ST5_CONTINUOUS_SOLENOID_BIT 0x10
#elif defined(RPU_USE_EXTENDED_SWITCHES_ON_PB7)
#define NUM_SWITCH_BYTES 6
#define NUM_SWITCH_BYTES_ON_U10_PORT_A 5
#define MAX_NUM_SWITCHES 48
#define DEFAULT_SOLENOID_STATE 0x1F
#define ST5_CONTINUOUS_SOLENOID_BIT 0x80
#else
#define RPU_NUM_SOLENOIDS 15
#define NUM_SWITCH_BYTES 5
#define NUM_SWITCH_BYTES_ON_U10_PORT_A 5
#define MAX_NUM_SWITCHES 40
#define DEFAULT_SOLENOID_STATE 0x9F
#endif
#if !defined(RPU_OS_SWITCH_DELAY_IN_MICROSECONDS) || !defined(RPU_OS_TIMING_LOOP_PADDING_IN_MICROSECONDS)
#error "Must define RPU_OS_SWITCH_DELAY_IN_MICROSECONDS and RPU_OS_TIMING_LOOP_PADDING_IN_MICROSECONDS in RPU_Config.h"
#endif
#elif (RPU_MPU_ARCHITECTURE >= 10)
#define RPU_NUM_SOLENOIDS 22
#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;
#if (RPU_MPU_ARCHITECTURE>=13)
volatile byte DisplayCommas;
#endif
#if (RPU_MPU_ARCHITECTURE == 15)
volatile byte DisplayText[2][RPU_OS_NUM_DIGITS];
#endif
#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
#if (RPU_OS_HARDWARE_REV>2)
#define SOLENOID_STACK_SIZE 150
#else
#define SOLENOID_STACK_SIZE 60
#endif
#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==1)
#if (RPU_MPU_ARCHITECTURE!=1)
#error "RPU_OS_HARDWARE_REV 1 only works on machines with RPU_MPU_ARCHITECTURE of 1"
#endif
#define ADDRESS_U10_A 0x14
#define ADDRESS_U10_A_CONTROL 0x15
#define ADDRESS_U10_B 0x16
#define ADDRESS_U10_B_CONTROL 0x17
#define ADDRESS_U11_A 0x18
#define ADDRESS_U11_A_CONTROL 0x19
#define ADDRESS_U11_B 0x1A
#define ADDRESS_U11_B_CONTROL 0x1B
#define ADDRESS_SB100 0x10
#elif (RPU_OS_HARDWARE_REV==2)
#if (RPU_MPU_ARCHITECTURE!=1)
#error "RPU_OS_HARDWARE_REV 2 only works on machines with RPU_MPU_ARCHITECTURE of 1"
#endif
#define ADDRESS_U10_A 0x00
#define ADDRESS_U10_A_CONTROL 0x01
#define ADDRESS_U10_B 0x02
#define ADDRESS_U10_B_CONTROL 0x03
#define ADDRESS_U11_A 0x08
#define ADDRESS_U11_A_CONTROL 0x09
#define ADDRESS_U11_B 0x0A
#define ADDRESS_U11_B_CONTROL 0x0B
#define ADDRESS_SB100 0x10
#define ADDRESS_SB100_CHIMES 0x18
#define ADDRESS_SB300_SQUARE_WAVES 0x10
#define ADDRESS_SB300_ANALOG 0x18
#elif (RPU_OS_HARDWARE_REV==3) || (RPU_OS_HARDWARE_REV==4)
#if (RPU_MPU_ARCHITECTURE!=1)
#error "RPU_OS_HARDWARE_REV 3 and 4 only work on machines with RPU_MPU_ARCHITECTURE of 1"
#endif
#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
#elif (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
/******************************************************
* Hardware Interface Functions
*
* These functions have conditional compilation for different RPU_OS_HARDWARE_REVs
*
* RPU_OS_HARDWARE_REV 1 - Nano board that plugs into J5 (only works on -17, -35, 100, and 200 MPUs)
* RPU_OS_HARDWARE_REV 2 - Nano board that plugs into J5 (only works on -17, -35, 100, and 200 MPUs)
* adds support for SB300 sound cards
* RPU_OS_HARDWARE_REV 3 - MEGA2560 PRO board that plugs into J5 (only works on -17, -35, 100, and 200 MPUs)
* adds support for full address space
* RPU_OS_HARDWARE_REV 4 - MEGA2560 PRO board that plugs into J5 (only works on -17, -35, 100, and 200 MPUs)
* adds support for OLED display, WIFI, and multiple serial ports
* RPU_OS_HARDWARE_REV 4 - MEGA2560 PRO board that plugs into J5 (only works on -17, -35, 100, and 200 MPUs)
* adds support for OLED display, WIFI, and multiple serial ports
* RPU_OS_HARDWARE_REV 100 - MEGA2560 PRO board that plugs into processor socket (prototype)
* RPU_OS_HARDWARE_REV 101 - MEGA2560 PRO board that plugs into processor socket
* adds support for multiple serial ports (limited release)
* RPU_OS_HARDWARE_REV 102 - MEGA2560 PRO board that plugs into processor socket (prototype)
* adds support for OLED display, WIFI, autodetection of processor type
*
*/
#if (RPU_OS_HARDWARE_REV==1) or (RPU_OS_HARDWARE_REV==2)
#if defined(__AVR_ATmega2560__)
#error "ATMega requires RPU_OS_HARDWARE_REV of 3, check RPU_Config.h and adjust settings"
#endif
void RPU_DataWrite(int address, byte data) {
// Set data pins to output
// Make pins 5-7 output (and pin 3 for R/W)
DDRD = DDRD | 0xE8;
// Make pins 8-12 output
DDRB = DDRB | 0x1F;
// Set R/W to LOW
PORTD = (PORTD & 0xF7);
// Put data on pins
// Put lower three bits on 5-7
PORTD = (PORTD&0x1F) | ((data&0x07)<<5);
// Put upper five bits on 8-12
PORTB = (PORTB&0xE0) | (data>>3);
// Set up address lines
PORTC = (PORTC & 0xE0) | address;
// Wait for a falling edge of the clock
while((PIND & 0x10));
// Pulse VMA over one clock cycle
// Set VMA ON
PORTC = PORTC | 0x20;
// Wait while clock is low
while(!(PIND & 0x10));
// Wait while clock is high
while((PIND & 0x10));
// Wait while clock is low
while(!(PIND & 0x10));
// Set VMA OFF
PORTC = PORTC & 0xDF;
// Unset address lines
PORTC = PORTC & 0xE0;
// Set R/W back to HIGH
PORTD = (PORTD | 0x08);
// Set data pins to input
// Make pins 5-7 input
DDRD = DDRD & 0x1F;
// Make pins 8-12 input
DDRB = DDRB & 0xE0;
}
byte RPU_DataRead(int address) {
// Set data pins to input
// Make pins 5-7 input
DDRD = DDRD & 0x1F;
// Make pins 8-12 input
DDRB = DDRB & 0xE0;
// Set R/W to HIGH
DDRD = DDRD | 0x08;
PORTD = (PORTD | 0x08);
// Set up address lines
PORTC = (PORTC & 0xE0) | address;
// Wait for a falling edge of the clock
while((PIND & 0x10));
// Pulse VMA over one clock cycle
// Set VMA ON
PORTC = PORTC | 0x20;
// Wait a full clock cycle to make sure data lines are ready
// (important for faster clocks)
// Wait while clock is low
while(!(PIND & 0x10));
// Wait for a falling edge of the clock
while((PIND & 0x10));
// Wait while clock is low
while(!(PIND & 0x10));
byte inputData = (PIND>>5) | (PINB<<3);
// Set VMA OFF
PORTC = PORTC & 0xDF;
// Wait for a falling edge of the clock
// Doesn't seem to help while((PIND & 0x10));
// Set R/W to LOW
PORTD = (PORTD & 0xF7);
// Clear address lines
PORTC = (PORTC & 0xE0);
return inputData;
}
void WaitClockCycle(int numCycles=1) {
for (int count=0; count<numCycles; count++) {
// Wait while clock is low
while(!(PIND & 0x10));
// Wait for a falling edge of the clock
while((PIND & 0x10));
}
}
#elif (RPU_OS_HARDWARE_REV==3)
// Rev 3 connections
// Pin D2 = IRQ
// Pin D3 = CLOCK
// Pin D4 = VMA
// Pin D5 = R/W
// Pin D6-12 = D0-D6
// Pin D13 = SWITCH
// Pin D14 = HALT
// Pin D15 = D7
// Pin D16-30 = A0-A14
#if defined(__AVR_ATmega328P__)
#error "RPU_OS_HARDWARE_REV 3 requires ATMega2560, check RPU_Config.h and adjust settings"
#endif
void RPU_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
// Wait for a falling edge of the clock
while((PINE & 0x20));
// Pulse VMA over one clock cycle
// Set VMA ON
PORTG = PORTG | 0x20;
// Wait while clock is low
while(!(PINE & 0x20));
// Wait while clock is high
while((PINE & 0x20));
// Wait while clock is low
while(!(PINE & 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 RPU_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
// Wait for a falling edge of the clock
while((PINE & 0x20));
// Pulse VMA over one clock cycle
// Set VMA ON
PORTG = PORTG | 0x20;
// Wait a full clock cycle to make sure data lines are ready
// (important for faster clocks)
// Wait while clock is low
while(!(PINE & 0x20));
// Wait for a falling edge of the clock
while((PINE & 0x20));
// Wait while clock is low
while(!(PINE & 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_OS_HARDWARE_REV==4)
// Rev 3 connections
// Pin D2 = IRQ
// Pin D3 = CLOCK
// Pin D4 = VMA
// Pin D5 = R/W
// Pin D6-12 = D0-D6
// Pin D13 = SWITCH
// Pin D14 = HALT
// Pin D15 = D7
// Pin D16-30 = A0-A14
#if defined(__AVR_ATmega328P__)
#error "RPU_OS_HARDWARE_REV 4 requires ATMega2560, check RPU_Config.h and adjust settings"
#endif
#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
#define RPU_PINS_OUTPUT true
#define RPU_PINS_INPUT false
void RPU_SetAddressPinsDirection(boolean pinsOutput) {
for (int count=0; count<16; count++) {
pinMode(A0+count, pinsOutput?OUTPUT:INPUT);
}
}
void RPU_SetDataPinsDirection(boolean pinsOutput) {
for (int count=0; count<8; count++) {
pinMode(22+count, pinsOutput?OUTPUT:INPUT);
}
}
// REVISION 4 HARDWARE
void RPU_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);
if (UsesM6800Processor) {
// Wait for a falling edge of the clock
while((PING & 0x04));
} else {
// Set clock low (PG2) (if 6802/8)
PORTG &= ~0x04;
}
// Pulse VMA over one clock cycle
// Set VMA ON
PORTG = PORTG | 0x02;
if (UsesM6800Processor) {
// Wait while clock is low
while(!(PING & 0x04));
// Wait while clock is high
while((PING & 0x04));
// Wait while clock is low
while(!(PING & 0x04));
} else {
// Set clock high
PORTG |= 0x04;
// Set clock low
PORTG &= ~0x04;
// 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 RPU_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);
if (UsesM6800Processor) {
// Wait for a falling edge of the clock
while((PING & 0x04));
} else {
// Set clock low
PORTG &= ~0x04;
}
// Pulse VMA over one clock cycle
// Set VMA ON
PORTG = PORTG | 0x02;
if (UsesM6800Processor) {
// Wait a full clock cycle to make sure data lines are ready
// (important for faster clocks)
// Wait while clock is low
while(!(PING & 0x04));
// Wait for a falling edge of the clock
while((PING & 0x04));
// Wait while clock is low
while(!(PING & 0x04));
} else {
// Set clock high
PORTG |= 0x04;
// Set clock low
PORTG &= ~0x04;
// 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;
}
#elif (RPU_OS_HARDWARE_REV==100)
#if defined(__AVR_ATmega328P__)
#error "RPU_OS_HARDWARE_REV 100 requires ATMega2560, check RPU_Config.h and adjust settings"
#endif
#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
#define RPU_PINS_OUTPUT true
#define RPU_PINS_INPUT false
void RPU_SetAddressPinsDirection(boolean pinsOutput) {
for (int count=0; count<16; count++) {
pinMode(16+count, pinsOutput?OUTPUT:INPUT);
}
}
void RPU_SetDataPinsDirection(boolean pinsOutput) {
for (int count=0; count<7; count++) {
pinMode(6+count, pinsOutput?OUTPUT:INPUT);
}
pinMode(15, pinsOutput?OUTPUT:INPUT);
}
// REV 100 HARDWARE
void RPU_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;
// Pulse VMA over one clock cycle
// Set VMA ON
PORTG = PORTG | 0x20;
// Set clock high
PORTE |= 0x20;
// Set clock low
PORTE &= ~0x20;
// 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 RPU_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;
// Pulse VMA over one clock cycle
// Set VMA ON
PORTG = PORTG | 0x20;
// Set clock high
PORTE |= 0x20;
// Set clock low
PORTE &= ~0x20;
// 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_OS_HARDWARE_REV==101) || (RPU_OS_HARDWARE_REV==102)
#if defined(__AVR_ATmega328P__)
#error "RPU_OS_HARDWARE_REV >100 requires ATMega2560, check RPU_Config.h and adjust settings"
#endif
#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_BA_PIN 43
#define RPU_DIAGNOSTIC_PIN 44
#define RPU_DISABLE_PHI_FROM_MPU 7
#define RPU_DISABLE_PHI_FROM_CPU 6
#define RPU_BOARD_SEL_0 30
#define RPU_BOARD_SEL_1 31
#define RPU_BOARD_SEL_2 32
#define RPU_BOARD_SEL_3 33
#define RPU_PINS_OUTPUT true
#define RPU_PINS_INPUT false
void RPU_SetAddressPinsDirection(boolean pinsOutput) {
for (int count=0; count<16; count++) {
pinMode(A0+count, pinsOutput?OUTPUT:INPUT);
}
}
void RPU_SetDataPinsDirection(boolean pinsOutput) {
for (int count=0; count<8; count++) {
pinMode(22+count, pinsOutput?OUTPUT:INPUT);
}
}
// REVISION 101/102 HARDWARE
void RPU_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);
if (UsesM6800Processor) {
// Wait for a falling edge of the clock
while((PING & 0x04));
} else {
// Set clock low (PG2) (if 6802/8)
PORTG &= ~0x04;
}
// Pulse VMA over one clock cycle
// Set VMA ON
PORTG = PORTG | 0x02;
if (UsesM6800Processor) {
// Wait while clock is low
while(!(PING & 0x04));
// Wait while clock is high
while((PING & 0x04));
// Wait while clock is low
while(!(PING & 0x04));
} else {
// Set clock high
PORTG |= 0x04;
// Set clock low
PORTG &= ~0x04;
// 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 RPU_DataRead(int address) {