forked from Hojo-Norem/PadSwitcher64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPadSwitcher64.c
1064 lines (839 loc) · 25.7 KB
/
PadSwitcher64.c
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 <avr/io.h>
#include <util/delay.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/power.h>
/*
This file is part of PadSwitcher64.
PadSwitcher64 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.
PadSwitcher64 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.
You should have received a copy of the GNU General Public License
along with PadSwitcher64. If not, see <https://www.gnu.org/licenses/>.
************************************
PADSwitcher64
************************************
V1.1 - Added turbo fire speed toggle
v1.11 - Fixed being unable to enter direct mode.
Hardware and software design my Hojo Norem.
*2018*
Super Nintendo control pad interface for the Commodore 64 (and probably the C128).
The adapter connects to both C64 joystick ports and allows the active port to be
changed on the fly with a push of a button.
Also, the interface allows up to four custom mappings to be programmed using a C64-
side application. The custom mappings allow any combination of joystick port two
and port one control lines to be mapped to any of the Super Nintendo controller
action buttons (A,B,X,Y,L,R). Any of the action buttons can also have an optional
rapid-fire function applied, of which a normal and 'inverted' state is avaliable.
'Inverted' rapid fire is simply that when active the output state of the effected
button will be opposite to of a normal rapid fire button when pushed.
For example, mapping a button to joystick left with rapid fire and another button
to joystick right with inverted rapid fire will simulate a rapid left/right
joystick waggle when both buttons are pushed.
This code was originally developed for the Atmel AVRMega8 mcu and was then later
ported over to the AVRMega88. This was mainly due to the availiablilty of the Mega8
and that the Mega88 has more power saving features.
This source code was written using AVR-GCC and AVR Libc.
TODO :
* Re-jig command mode for potential compatibility with non-C64 Atari joystick
compatible machines.
Pad Mappinng: (default)
SNES C64
-----------------------
DPAD UP UP
DPAD DOWN DOWN
DPAD LEFT LEFT
DPAD RIGHT RIGHT
Y FIRE
X TURBO FIRE
B UP
A 'Special' - UP + FIRE or DOWN + FIRE
R KEYBOARD MATRIX LINE(PORT 2 MODE ONLY), defaults to PB4 which
most software will interpret as the SPACEBAR.
Fixed Controls:
---------------
SELECT + UP TOGGLE DISABLE DPAD UP
START SWAP CONTROL PORTS
SELECT + A CHANGE SPECIAL (default mapping only)
SELECT + DOWN Change TURBO FIRE speed between FAST and SLOW. Power on default is FAST.
SELECT + L Swap 'Special' function between 'DOWN + FIRE' and 'UP + FIRE'.
SELECT + R SELECT KEYBOARD MATRIX LINE (CIA 1 PB0 to PB4, PB4 default at startup, PORT 2 MODE ONLY, default mapping only)
PB4 = SPACE R-SHIFT . M B C Z F1/F2
PB3 = 2 CLR/HOME - 0 8 6 4 F7/F8
PB2 = CTRL ; L J G D A CRSR ->/<-
PB1 = <- * P I Y R W RETURN
PB0 = 1 £ + 9 7 5 3 INST/DEL
SELECT + (A,B,X,Y) SELECT MAPPING
SELECT + START DEFAULT MAPPING
SELECT + START ENTER COMMAND MODE (only functional when prompted, only functional in default mapping mode.)
SELECT + START + L + R (In normal or mapped mode) Enter type 1 direct mode.
(In any other mode) Force normal mode.
MAPPING
-------
Four mappings, each with all 6 action buttons availiable. Each button has a 2 byte pattern corrisponding to the following:
Byte 1 - Bit 0 to 3 PB0 to PB3 (JOY 1 Directions & KB matrix)
Byte 1 - Bit 4 to 7 PA0 to PA3 (Joy 2 directions)
Byte 2 - Bit 0 PB4 (Joy 1 Fire & KB matrix)
Byte 2 - bit 1 PA4 (Joy 2 Fire)
Byte 2 - bit 2 Pad Button has turbofire action
Byte 2 - Bit 3 Pad Button has turbofire action (inverted)
Bits 4 - 7 of byte 2 are not accessable through the command interface.
DIRECT MODE: TYPE 1
-------------------
SNES Port BitDown C64 Joy Mapping
Up DC00 11110 up
Down DC00 11101 down
Left DC00 11011 left
Right DC00 10111 right
Y DC00 01111 fire
Start DC00 00000 all
B DC01 11110 up
X DC01 11101 down
L DC01 11011 left
R DC01 10111 right
A DC01 01111 fire
Select DC01 00000 all
*/
//Board selection -
#define DIP_PRODUCTION 0 // ATMEGA88 DIP production pcb
//#define SMD_PRODUCTION 0 // ATMEGA88 SMD production pcb
//Constants
#define INVERT_MAIN 0
#if defined DIP_PRODUCTION
#define SNES_DAT1_DDR DDRC
#define SNES_DAT2_DDR DDRC
#define SNES_CLK_DDR DDRC
#define SNES_LCH_DDR DDRC
#define SNES_DAT1_PORT PORTC
#define SNES_DAT1_PINS PINC
#define SNES_DAT2_PORT PORTC
#define SNES_DAT2_PINS PINC
#define SNES_CLK_PORT PORTC
#define SNES_LCH_PORT PORTC
#define SNES_CLOCK 1
#define SNES_LATCH 2
#define SNES_DATA_1 4
#define SNES_DATA_2 8
#endif
#if defined SMD_PRODUCTION
#define SNES_DAT1_DDR DDRD
#define SNES_DAT2_DDR DDRD
#define SNES_CLK_DDR DDRD
#define SNES_LCH_DDR DDRD
#define SNES_DAT1_PORT PORTD
#define SNES_DAT1_PINS PIND
#define SNES_DAT2_PORT PORTD
#define SNES_DAT2_PINS PIND
#define SNES_CLK_PORT PORTD
#define SNES_LCH_PORT PORTD
#define SNES_CLOCK 16
#define SNES_LATCH 8
#define SNES_DATA_1 4
#define SNES_DATA_2 2
#endif
// SNES Pad buttons
#define PAD_B 1
#define PAD_Y 2
#define PAD_SELECT 4
#define PAD_START 8
#define PAD_UP 16
#define PAD_DOWN 32
#define PAD_LEFT 64
#define PAD_RIGHT 128
#define PAD_A 256
#define PAD_X 512
#define PAD_L 1024
#define PAD_R 2048
#define PAD_CONFIG1 4096
#define PAD_CONFIG2 8192
#define PAD_L2 16384
#define PAD_R2 32768
// C64 Control port lines
#define CP_UP 1
#define CP_DOWN 2
#define CP_LEFT 4
#define CP_RIGHT 8
#define CP_FIRE 16
#define CP_DIRS 15 //All directions pressed
#define CP_ALL 31 //with fire
#define SNES_LATCH_LOW() do { SNES_LCH_PORT &= ~(SNES_LATCH); } while(0)
#define SNES_LATCH_HIGH() do { SNES_LCH_PORT |= SNES_LATCH; } while(0)
#define SNES_CLOCK_LOW() do { SNES_CLK_PORT &= ~(SNES_CLOCK); } while(0)
#define SNES_CLOCK_HIGH() do { SNES_CLK_PORT |= SNES_CLOCK; } while(0)
//#define RAPIDMAX 10 // Riapid fire rate control
//Extended registers
#define EXREGCOUNT 2
volatile uint8_t MAPPINGS[4][6][2];
volatile uint8_t EXREGS[EXREGCOUNT];
uint8_t ee_MAPPINGS[48] __attribute__((section(".eeprom")));
uint8_t ee_EXREGS[EXREGCOUNT] __attribute__((section(".eeprom")));;
uint8_t ee_EEPROM_INIT __attribute__((section(".eeprom")));
volatile uint8_t tick;
uint16_t PAD;
uint8_t PADPUSHED;
uint16_t RAPID;
uint8_t RAPIDSTATE;
uint8_t RAPIDMAX;
uint8_t NEWSCAN;
uint16_t COMTIMEOUT;
void ReadPads(void);
void SetupPorts(void);
uint8_t ReadCP2(void);
void SetCP2(uint8_t data);
void SetCP1(uint8_t data);
void SetLED(uint8_t data);
//void SetPOTs(Uint8_t data);
ISR(TIMER0_COMPA_vect)
{
tick++;
}
void main(void)
{
uint8_t MATRIX;
uint8_t SWAP;
uint8_t SPECIAL;
uint8_t A;
uint8_t B;
uint8_t D;
uint8_t error;
uint8_t IMODE;
uint8_t COMMAND;
uint8_t OPERAND;
uint8_t ACKWAIT;
uint16_t NEGTIMEOUT;
uint8_t MAPPING;
uint8_t BUTTON;
uint8_t EEPROM_INIT;
uint8_t ISOPERAND;
uint8_t EXCOM;
uint8_t DISABLE_UP;
uint8_t C64_PORT[2];
uint8_t MASTERHELD;
cli();
CLKPR=128; // Set 4Mhz main clock
CLKPR=1; //
//****************************
//Setting up system tick clock
//****************************
TCCR0A = (WGM01<<1); // Set timer0A CTC mode
TCCR0B = (CS02<<1)|(CS00<<1); // Set timer0A 1024 clock divider
OCR0A = 19; // set timer0A compare = 19 - should be roughly 205Hz
TIMSK0 = OCIE0A<<1; // Enable timer0A interrupt
SetupPorts();
COMMAND=0;
SNES_LATCH_LOW();
SNES_CLOCK_HIGH();
SWAP=0;
PADPUSHED=0;
MATRIX=16;
RAPID=0;
RAPIDSTATE=4;
SPECIAL = 2;
NEGTIMEOUT =0;
OPERAND = 0;
ISOPERAND=0;
NEWSCAN=0;
COMTIMEOUT=0;
RAPIDMAX=30;
DISABLE_UP=0;
MASTERHELD=0;
EEPROM_INIT=eeprom_read_byte(&ee_EEPROM_INIT);
if(EEPROM_INIT==64){
eeprom_read_block((void*)&MAPPINGS, (void*)&ee_MAPPINGS, sizeof(MAPPINGS));
eeprom_read_block((void*)&EXREGS, (void*)&ee_EXREGS, sizeof(EXREGS));
}else{
for(MAPPING=0;MAPPING<=3;MAPPING++){
for(BUTTON=0;BUTTON<=5;BUTTON++){
MAPPINGS[MAPPING][BUTTON][0]=0;
MAPPINGS[MAPPING][BUTTON][1]=0;
}
}
//EXREGS[PADREADRATE]=13;
//EXREGS[RAPIDMAX]=60; //Default rapid-fire rate
}
MAPPING=4;
BUTTON=0;
ACKWAIT=0;
power_adc_disable();
power_usart0_disable();
power_spi_disable();
power_timer1_disable();
power_timer2_disable();
power_twi_disable();
SetCP1(0);
SetCP2(0);
EXCOM=1;
IMODE=0;
error=10;
tick=0;
sei();
for(;;){
sleep_mode();
if(error!=0){
if(tick<100){
SetLED(1);
}else{
SetLED(0);
}
if(tick==200){
tick=0;
error--;
}
}
ReadPads();
if(MASTERHELD<2){
if((PAD&PAD_SELECT)==0){
if((PAD&PAD_L)==0){
if(((PAD&PAD_R)==0)&&((PAD&PAD_START)==0)){
if(IMODE==0){
IMODE=2;
MASTERHELD=2;
error=2;
}else{
IMODE=0;
MASTERHELD=2;
error=2;
}
}
}
}
}
if(MASTERHELD>=1){
if(PADPUSHED==0) MASTERHELD=0;
}else{
//A = ReadCP2();
if (((PAD&PAD_START)==0)&&((PAD&PAD_SELECT)==0)&&(IMODE==0)){
if((ReadCP2()==CP_FIRE)&&(MAPPING==4)){
/*
Comamnd MODE
-----------
Sequence -
Interface will only enter mode if SELECT + START are pressed with bits 0-3 of port 2 from c64 low.
Interface acks C64 by pulling all of port 1 low.
C64 bit 4 of port 2 low and lets bit 0 go high.
Interface acks this by inverting port 1.
C64 acks this by pulling bit 0 of port 2 low and releasing bit 2.
Interface aks this by inverting port 1 again.
C64 aks this by pulling bit 2 down again. Handshake is complete on C64 side.
Interface waits for port 2 bit two to go low. When it does it releases all of port 1 high.
Handshake complete.
Interface listens for commands on port 2.
C64 issues commands by placing it on port 2. pulls bit 4 HIGH to indicate a operand write. Operands are written before commands.
The operand register is 8 bits wide and is written and read in nybbles. Before each nybble written the register is shifted left four
bits. After each nybble read the register is shifted right four bits. The operand register does not wrap.
Interface acks commands by pulling port 1 fire (PB4) low and for read commands places register on PB3-PB0. Reads from interface to
C64 are inverted.
C64 acks reply by port 2 pulling port 2 low.
*/
if (IMODE==0){
SetCP1(CP_ALL);
SetLED(1);
tick=0;
NEGTIMEOUT=0;
B=0;
do{
A=ReadCP2();
if ((A==CP_UP)&&(B==0)){
B=1;
SetCP1(0);
}
//if ((B==1)&&((A!=CP_UP)&&(A!=CP_LEFT)))B=0;
if ((B==1)&&(A==CP_LEFT)){
B=2;
SetCP1(CP_ALL);
}
if ((B==2)&&(A==0))B=3;
if (tick==205){
NEGTIMEOUT++;
tick=0;
}
}while((B!=3)&&(NEGTIMEOUT<1));
if(NEGTIMEOUT<1){
OCR0A = 8;
IMODE=1;
SetCP1(CP_ALL);
COMTIMEOUT=0;
EXCOM=1;
}else{
SetCP1(0);
SetLED(0);
IMODE=0;
error=5;
}
}
//DDRC = 0b11111100;
//DDRD = 0;
}
}
/*
COMMANDS/REGISTERS (C64 > interface)
------------------------------------
Command page 1
00 - No command / ack read
01 - Select pad 1 (Non functional but acks)
02 - Select pad 2 (Non functional but acks)
03 - READ NYB 1: B, Y, SELECT, START
04 - READ NYB 2: UP, DOWN, LEFT, RIGHT
05 - READ NYB 3: A, X, L, R
06 - Write EEPROM
(Operand = 14)
07 - Select mapping
(Operand = 0 to 3)
08 - Select button
(Operand = 0 to 5)
09 - Read nybble 1
10 - Read nybble 2
11 - Read nybble 3
12 - Write nybble 1
(Operand)
13 - Write nybble 2
(Operand)
14 - write nybble 3
(Operand)
15 - Shift to command page 2
Command page 2
00 - No command / ack read
01 - Shift to command page 1
********** currently not implemented, if ever *************
02 - Select extended register
(Operand = register:
03 - Read Extended register
04 - Write Extended Register
(Operand*2 writes - Register written on 2nd write)
***********************************************************
05 - Enter Direct Mode, type 1
NOTE: Not ACK'd. Exits command mode. C64 software should act accordingly
06 - Enter Direct Mode, type 2
NOTE: Not currrently implemented, may never.
15 - Exit command mode
NOTE: Not ACK'd.
*/
if(IMODE==1) {
COMTIMEOUT++;
if(COMTIMEOUT>1025){ // ~5 second comunications timeout
SetCP1(0);
SetCP2(0);
EXCOM=1;
IMODE=0;
OCR0A = 19;
error=10;
}
COMMAND = ReadCP2()&CP_ALL;
SetLED(COMMAND&CP_FIRE);
if(COMMAND>=CP_FIRE) ISOPERAND=1; else ISOPERAND=0;
COMMAND = COMMAND&CP_DIRS;
if ((ISOPERAND==1)&&(ACKWAIT==0)){
OPERAND = OPERAND<<4;
OPERAND = OPERAND|COMMAND;
SetCP1(CP_FIRE);
SetLED(1);
ACKWAIT=1;
}
if ((ISOPERAND==0)&&(ACKWAIT==0)){
SetLED(0);
switch(EXCOM){
case 1:
if((COMMAND==7)&&((OPERAND&15)<=4)){
MAPPING=OPERAND&15;
SetCP1(CP_FIRE);
ACKWAIT=1;
}
if((COMMAND==6)&&((OPERAND&CP_DIRS)==14)){
SetCP1(CP_FIRE);
SetLED(1);
cli();
do{asm volatile ("nop");}while (!eeprom_is_ready());
eeprom_update_block((void*)&MAPPINGS, (void*)&ee_MAPPINGS, sizeof(MAPPINGS));
eeprom_update_block((void*)&EXREGS, (void*)&ee_EXREGS, sizeof(EXREGS));
eeprom_update_byte(&ee_EEPROM_INIT, 64);
sei();
ACKWAIT=1;
}
if(COMMAND==8){
BUTTON=(OPERAND&15);
SetCP1(CP_FIRE);
ACKWAIT=1;
}
if(COMMAND==9){
SetCP1((MAPPINGS[MAPPING][BUTTON][0]&15)|CP_FIRE);
ACKWAIT=1;
}
if(COMMAND==10){
SetCP1((MAPPINGS[MAPPING][BUTTON][0]>>4)|CP_FIRE);
ACKWAIT=1;
}
if(COMMAND==11){
SetCP1((MAPPINGS[MAPPING][BUTTON][1]&15)|CP_FIRE);
ACKWAIT=1;
}
if(COMMAND==12){
MAPPINGS[MAPPING][BUTTON][0]=(MAPPINGS[MAPPING][BUTTON][0]&240)|(OPERAND&15);
SetCP1(CP_FIRE);
ACKWAIT=1;
}
if(COMMAND==13){
MAPPINGS[MAPPING][BUTTON][0]=(MAPPINGS[MAPPING][BUTTON][0]&15)|(OPERAND<<4);
SetCP1(CP_FIRE);
ACKWAIT=1;
}
if(COMMAND==14){
MAPPINGS[MAPPING][BUTTON][1]=(MAPPINGS[MAPPING][BUTTON][1]&240)|(OPERAND&15);
SetCP1(CP_FIRE);
ACKWAIT=1;
}
if(COMMAND==15){
SetCP1(CP_FIRE);
EXCOM=2;
ACKWAIT=1;
}
if((COMMAND==1)||(COMMAND==2)){
//PADSEL=COMMAND-1;
SetCP1(CP_FIRE);
//ReadPads();
//AUTOREAD=0;
ACKWAIT=1;
}
if((COMMAND>=3)&&(COMMAND<=5)){
//if((AUTOREAD==1)&&(COMMAND==3)){
// ReadPads();
//}
B=0;
D=(COMMAND-3);
B=PAD>>(D*4);
B=B^15;
SetCP1(B|CP_FIRE);
ACKWAIT=1;
//AUTOREAD=1;
}
break;
case 2:
if(COMMAND==1){
SetCP1(CP_FIRE);
ACKWAIT=1;
}
if(COMMAND==5){
SetCP1(0);
SetCP2(0);
OCR0A = 19;
IMODE=2;
}
if(COMMAND==15){
SetCP1(0);
SetCP2(0);
IMODE=0;
OCR0A = 19;
EXCOM=1;
}
break;
}
}
if(ACKWAIT==1){
tick=0;
do{
B=ReadCP2();
}while((B!=0)&&(tick<255));
SetCP1(0);
if(tick==255) COMTIMEOUT=9999; else COMTIMEOUT=0;
ACKWAIT=0;
}
}
/*
DIRECT MODE: TYPE 1
-------------------
SNES Port BitDown C64 Joy Mapping
Up DC00 11110 up
Down DC00 11101 down
Left DC00 11011 left
Right DC00 10111 right
Y DC00 01111 fire
Start DC00 00000 all
B DC01 11110 up
X DC01 11101 down
L DC01 11011 left
R DC01 10111 right
A DC01 01111 fire
Select DC01 00000 all
*/
if (IMODE==2){
C64_PORT[0]=0;
if((PAD&PAD_UP)==0) C64_PORT[0]|=CP_UP;
if((PAD&PAD_DOWN)==0) C64_PORT[0]|=CP_DOWN;
if((PAD&PAD_LEFT)==0) C64_PORT[0]|=CP_LEFT;
if((PAD&PAD_RIGHT)==0) C64_PORT[0]|=CP_RIGHT;
if((PAD&PAD_Y)==0) C64_PORT[0]|=CP_FIRE;
if((PAD&PAD_START)==0) C64_PORT[0]|=CP_ALL;
C64_PORT[1]=0;
if((PAD&PAD_B)==0) C64_PORT[1]|=CP_UP;
if((PAD&PAD_X)==0) C64_PORT[1]|=CP_DOWN;
if((PAD&PAD_L)==0) C64_PORT[1]|=CP_LEFT;
if((PAD&PAD_R)==0) C64_PORT[1]|=CP_RIGHT;
if((PAD&PAD_A)==0) C64_PORT[1]|=CP_FIRE;
if((PAD&PAD_SELECT)==0) C64_PORT[1]|=CP_ALL;
SetCP2(C64_PORT[0]);
SetCP1(C64_PORT[1]);
}
if (IMODE==0){
C64_PORT[0]=0;
C64_PORT[1]=0;
if((PAD&PAD_SELECT)==0){
if ((PAD&PAD_DOWN)==0){
if(RAPIDMAX==30) RAPIDMAX=60; else RAPIDMAX=30;
MASTERHELD=1;
}
if ((PAD&PAD_UP)==0){
if (DISABLE_UP==0) DISABLE_UP=1; else DISABLE_UP=0;
MASTERHELD=1;
}
if ((PAD&PAD_A)==0){MAPPING=0;MASTERHELD=1;}
if ((PAD&PAD_B)==0){MAPPING=1;MASTERHELD=1;}
if ((PAD&PAD_X)==0){MAPPING=2;MASTERHELD=1;}
if ((PAD&PAD_Y)==0){MAPPING=3;MASTERHELD=1;}
if ((PAD&PAD_START)==0){MAPPING=4;MASTERHELD=1;}
}
if (((PAD&PAD_START)==0)&&((PAD&PAD_SELECT)!=0)){
SWAP=SWAP^1;
MASTERHELD=1;
}
B=1^SWAP;
if(MAPPING==4){
if ((PAD&PAD_SELECT)==0){
if((PAD&PAD_START)!=0){
if (SWAP==0){
if ((PAD&PAD_R)==0){
MATRIX=MATRIX<<1;
if (MATRIX>16) MATRIX=1;
MASTERHELD=1;
}
}
if ((PAD&PAD_L)==0){
SPECIAL=SPECIAL<<1;
if(SPECIAL>2) SPECIAL=1;
MASTERHELD=1;
}
}
SetLED(1);
}else{
if (((PAD&PAD_UP)==0)&&(DISABLE_UP==0)) C64_PORT[B]=C64_PORT[B]|CP_UP;
if ((PAD&PAD_B)==0) C64_PORT[B]=C64_PORT[B]|CP_UP;
if ((PAD&PAD_DOWN)==0) C64_PORT[B]=C64_PORT[B]|CP_DOWN;
if ((PAD&PAD_LEFT)==0) C64_PORT[B]=C64_PORT[B]|CP_LEFT;
if ((PAD&PAD_RIGHT)==0) C64_PORT[B]=C64_PORT[B]|CP_RIGHT;
if ((PAD&PAD_Y)==0) C64_PORT[B]=C64_PORT[B]|CP_FIRE;
if ((PAD&PAD_A)==0) C64_PORT[B]=C64_PORT[B]|SPECIAL|CP_FIRE;
if ((PAD&PAD_X)==0) C64_PORT[B]=C64_PORT[B]|((RAPIDSTATE&4)<<2);
if (((PAD&PAD_R)==0)&&(SWAP==0)) C64_PORT[0]=C64_PORT[0]|MATRIX;
}
}else{
if((PAD&PAD_SELECT)!=0){
if (((PAD&PAD_UP)==0)&&(DISABLE_UP==0)) C64_PORT[B]=C64_PORT[B]|CP_UP;
if ((PAD&PAD_DOWN)==0) C64_PORT[B]=C64_PORT[B]|CP_DOWN;
if ((PAD&PAD_LEFT)==0) C64_PORT[B]=C64_PORT[B]|CP_LEFT;
if ((PAD&PAD_RIGHT)==0) C64_PORT[B]=C64_PORT[B]|CP_RIGHT;
if ((PAD&PAD_A)==0) {
if((RAPIDSTATE&(MAPPINGS[MAPPING][0][1]&12))==0){
C64_PORT[0]=C64_PORT[0]|(MAPPINGS[MAPPING][0][0]&15)|((MAPPINGS[MAPPING][0][1]&1)<<4);
C64_PORT[1]=C64_PORT[1]|((MAPPINGS[MAPPING][0][0]&240)>>4)|((MAPPINGS[MAPPING][0][1]&2)<<3);
}
}
if ((PAD&PAD_B)==0) {
if((RAPIDSTATE&(MAPPINGS[MAPPING][1][1]&12))==0){
C64_PORT[0]=C64_PORT[0]|(MAPPINGS[MAPPING][1][0]&15)|((MAPPINGS[MAPPING][1][1]&1)<<4);
C64_PORT[1]=C64_PORT[1]|((MAPPINGS[MAPPING][1][0]&240)>>4)|((MAPPINGS[MAPPING][1][1]&2)<<3);
}
}
if ((PAD&PAD_X)==0) {
if((RAPIDSTATE&(MAPPINGS[MAPPING][2][1]&12))==0){
C64_PORT[0]=C64_PORT[0]|(MAPPINGS[MAPPING][2][0]&15)|((MAPPINGS[MAPPING][2][1]&1)<<4);
C64_PORT[1]=C64_PORT[1]|((MAPPINGS[MAPPING][2][0]&240)>>4)|((MAPPINGS[MAPPING][2][1]&2)<<3);
}
}
if ((PAD&PAD_Y)==0) {
if((RAPIDSTATE&(MAPPINGS[MAPPING][3][1]&12))==0){
C64_PORT[0]=C64_PORT[0]|(MAPPINGS[MAPPING][3][0]&15)|((MAPPINGS[MAPPING][3][1]&1)<<4);
C64_PORT[1]=C64_PORT[1]|((MAPPINGS[MAPPING][3][0]&240)>>4)|((MAPPINGS[MAPPING][3][1]&2)<<3);
}
}
if ((PAD&PAD_L)==0) {
if((RAPIDSTATE&(MAPPINGS[MAPPING][4][1]&12))==0){
C64_PORT[0]=C64_PORT[0]|(MAPPINGS[MAPPING][4][0]&15)|((MAPPINGS[MAPPING][4][1]&1)<<4);
C64_PORT[1]=C64_PORT[1]|((MAPPINGS[MAPPING][4][0]&240)>>4)|((MAPPINGS[MAPPING][4][1]&2)<<3);
}
}
if ((PAD&PAD_R)==0) {
if((RAPIDSTATE&(MAPPINGS[MAPPING][5][1]&12))==0){
C64_PORT[0]=C64_PORT[0]|(MAPPINGS[MAPPING][5][0]&15)|((MAPPINGS[MAPPING][5][1]&1)<<4);
C64_PORT[1]=C64_PORT[1]|((MAPPINGS[MAPPING][5][0]&240)>>4)|((MAPPINGS[MAPPING][5][1]&2)<<3);
}
}
}
// use spiffy new putput subroutines here
}
SetCP2(C64_PORT[1]);
SetCP1(C64_PORT[0]);
SetLED(SWAP);
}
}
}
}
void ReadPads(void){
uint8_t i;
RAPID++;
if (RAPID>RAPIDMAX){
RAPID=0;
RAPIDSTATE=RAPIDSTATE^12;
}
PAD=0;
PADPUSHED=0;
//PAD[16][1]=0;
SNES_LATCH_HIGH();
_delay_us(12);
SNES_LATCH_LOW();
_delay_us(6);
for (i=0; i<=15; i++)
{
SNES_CLOCK_LOW();
_delay_us(6);
if ((SNES_DAT1_PINS&SNES_DATA_1)!=0) PAD=PAD|32768; else PADPUSHED=1;
if (i<15)PAD=PAD>>1;
SNES_CLOCK_HIGH();
_delay_us(6);
}
}
/*
***** DIP and SMD prototype PCB PORT connections *****
C64 control port 1 - PORTD bits 0 to 3 and PORTC bit 1
C64 control port 2 - PORTD bits 4 to 7 and PORTC bit 0
LED - PORTC bit 2
*/
#if (defined DIP_PROTO || defined SMD_PROTO)
void SetupPorts(void){
SNES_DAT1_DDR = 0;
SNES_DAT2_DDR = 0;
SNES_CLK_DDR = 0;
SNES_LCH_DDR = 0;
SNES_DAT1_PORT = 255;
SNES_DAT2_PORT = 255;
DDRD = 0;
DDRC = 4;
PORTD = 0;
PORTC = 0b11111000;
//SNES_DAT1_DDR = SNES_DAT1_DDR | ~(SNES_DATA_1);
//SNES_DAT2_DDR = SNES_DAT2_DDR | ~(SNES_DATA_2);
SNES_CLK_DDR = SNES_CLK_DDR | SNES_CLOCK;
SNES_LCH_DDR = SNES_LCH_DDR | SNES_LATCH;
SetLED(0);
}
uint8_t ReadCP2(void){
return ((PIND&240) >> 4)|((PINC&1) << 4);
}
void SetCP2(uint8_t data){
DDRD = (DDRD & 15)|((data & 15) << 4);
DDRC = (DDRC & 254)|((data & 16) >> 4);
}
void SetCP1(uint8_t data){
DDRD = (DDRD & 240)|(data & 15);
DDRC = (DDRC & 253)|((data & 16) >> 3);
}
void SetLED(uint8_t data){
if (data!=0){
PORTC = (PORTC & 251)|4;
}else{
PORTC = PORTC&251;
}
}
//#define PORT_1_FIRE 2
//#define PORT_2_FIRE 1
//#define LED 4
//#define OUT_PORT PORTD
//#define OUT_PORT_2 PORTC
//#define LED_PORT PORTC
#endif
#if defined DIP_PRODUCTION
void SetupPorts(void){
SNES_DAT1_DDR = 0;
SNES_DAT2_DDR = 0;
SNES_CLK_DDR = 0;
SNES_LCH_DDR = 0;
SNES_DAT1_PORT = 255;
SNES_DAT2_PORT = 255;
DDRD = 0;
DDRC = 32;
DDRB = 0;
PORTD = 0;
PORTC = 223;
PORTB = 0b01111110;
//SNES_DAT1_DDR = SNES_DAT1_DDR | ~(SNES_DATA_1);
//SNES_DAT2_DDR = SNES_DAT2_DDR | ~(SNES_DATA_2);
SNES_CLK_DDR = SNES_CLK_DDR | SNES_CLOCK;
SNES_LCH_DDR = SNES_LCH_DDR | SNES_LATCH;
SetLED(0);
}
uint8_t ReadCP2(void){
return ((PIND&224) >> 5)|((PINB&1) << 3)|((PINB&128)>>3);
}
void SetCP2(uint8_t data){
DDRD = (DDRD & 31)|((data & 7) << 5);
DDRB = ((data & 8) >> 3)|((data & 16) << 3);
}
void SetCP1(uint8_t data){
DDRD = (DDRD & 224)|(data & 31);
//DDRB = (DDRB & 191)|((data & 16) << 2);
}
/*
void SetPOTs(uint8_t data){