-
Notifications
You must be signed in to change notification settings - Fork 228
/
SNES.ino
2482 lines (2241 loc) · 72.3 KB
/
SNES.ino
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
//******************************************
// SUPER NINTENDO MODULE
//******************************************
#ifdef ENABLE_SNES
/******************************************
Defines
*****************************************/
// SNES Hi and LoRom, SA is HI with different Sram dumping
#define EX 4
#define SA 3
#define HI 1
#define LO 0
/******************************************
Variables
*****************************************/
// Define SNES Cart Reader Variables
int romSpeed = 0; // 0 = SlowROM, 3 = FastROM
int romChips = 0; // 0 = ROM only, 1 = ROM & RAM, 2 = ROM & Save RAM, 3 = ROM & DSP1, 4 = ROM & RAM & DSP1, 5 = ROM & Save RAM & DSP1, 19 = ROM & SFX
// 227 = ROM & RAM & GameBoy data, 243 = CX4, 246 = ROM & DSP2
byte romSizeExp = 0; // ROM-Size Exponent
boolean NP = false;
byte cx4Type = 0;
byte cx4Map = 0;
/******************************************
Menu
*****************************************/
// SNES/Nintendo Power SF Memory start menu
static const char snsMenuItem1[] PROGMEM = "SNES/SFC cartridge";
static const char snsMenuItem2[] PROGMEM = "SF Memory Cassette";
static const char snsMenuItem3[] PROGMEM = "Satellaview BS-X";
static const char snsMenuItem4[] PROGMEM = "Sufami Turbo";
static const char snsMenuItem5[] PROGMEM = "Game Processor RAM";
static const char snsMenuItem6[] PROGMEM = "Flash repro";
#ifdef OPTION_CLOCKGEN_CALIBRATION
static const char snsMenuItem7[] PROGMEM = "Calibrate Clock";
static const char* const menuOptionsSNS[] PROGMEM = { snsMenuItem1, snsMenuItem2, snsMenuItem3, snsMenuItem4, snsMenuItem5, snsMenuItem6, snsMenuItem7, FSTRING_RESET };
#else
static const char* const menuOptionsSNS[] PROGMEM = { snsMenuItem1, snsMenuItem2, snsMenuItem3, snsMenuItem4, snsMenuItem5, snsMenuItem6, FSTRING_RESET };
#endif
// SNES menu items
static const char SnesMenuItem4[] PROGMEM = "Test SRAM";
static const char SnesMenuItem6[] PROGMEM = "Force cart type";
static const char* const menuOptionsSNES[] PROGMEM = { FSTRING_READ_ROM, FSTRING_READ_SAVE, FSTRING_WRITE_SAVE, SnesMenuItem4, FSTRING_REFRESH_CART, SnesMenuItem6, FSTRING_RESET };
// Manual config menu items
static const char confMenuItem1[] PROGMEM = "Use header info";
static const char confMenuItem2[] PROGMEM = "4MB LoROM 256K SRAM";
static const char confMenuItem3[] PROGMEM = "4MB HiROM 64K SRAM";
static const char confMenuItem4[] PROGMEM = "6MB ExROM 256K SRAM";
static const char* const menuOptionsConfManual[] PROGMEM = { confMenuItem1, confMenuItem2, confMenuItem3, confMenuItem4, FSTRING_RESET };
#if (defined(ENABLE_FLASH) && defined(ENABLE_FLASH8))
// Repro menu items
static const char reproMenuItem1[] PROGMEM = "CFI LoROM";
static const char reproMenuItem2[] PROGMEM = "CFI HiROM";
static const char reproMenuItem3[] PROGMEM = "LoROM (P0)";
static const char reproMenuItem4[] PROGMEM = "HiROM (P0)";
static const char reproMenuItem5[] PROGMEM = "ExLoROM (P1)";
static const char reproMenuItem6[] PROGMEM = "ExHiROM (P1)";
static const char* const menuOptionsRepro[] PROGMEM = { reproMenuItem1, reproMenuItem2, reproMenuItem3, reproMenuItem4, reproMenuItem5, reproMenuItem6, FSTRING_RESET };
// CFI ROM config
static const char reproCFIItem1[] PROGMEM = "1x 2MB";
static const char reproCFIItem2[] PROGMEM = "2x 2MB";
static const char reproCFIItem3[] PROGMEM = "1x 4MB";
static const char reproCFIItem4[] PROGMEM = "2x 4MB";
static const char reproCFIItem5[] PROGMEM = "4x 2MB";
static const char reproCFIItem6[] PROGMEM = "1x 8MB";
static const char* const menuOptionsReproCFI[] PROGMEM = { reproCFIItem1, reproCFIItem2, reproCFIItem3, reproCFIItem4, reproCFIItem5, reproCFIItem6, FSTRING_RESET };
/* ROM file order
static const char reproEXItem1[] PROGMEM = "ExROM (CDAB)";
static const char reproEXItem2[] PROGMEM = "standard (ABCD)";
static const char* const menuOptionsReproEX[] PROGMEM = { reproEXItem1, reproEXItem2, FSTRING_RESET };
*/
/*boolean reproEXMenu() {
boolean fileOrder = 0;
// create menu with title and 3 options to choose from
unsigned char snsReproEX;
// Copy menuOptions out of progmem
convertPgm(menuOptionsReproEX, 3);
snsReproEX = question_box(F("ROM file order"), menuOptions, 3, 0);
// wait for user choice to come back from the question box menu
switch (snsReproEX) {
case 0:
fileOrder = 1;
break;
case 1:
fileOrder = 0;
break;
case 2:
resetArduino();
break;
}
return fileOrder;
}*/
// Setup number of flashroms
void reproCFIMenu() {
boolean reversed = 1;
// create menu with title and 7 options to choose from
unsigned char snsReproCFI;
// Copy menuOptions out of progmem
convertPgm(menuOptionsReproCFI, 7);
snsReproCFI = question_box(F("Select Flash Config"), menuOptions, 7, 0);
// wait for user choice to come back from the question box menu
switch (snsReproCFI) {
case 0:
setupCFI();
flashSize = 2097152;
writeCFI_Flash(1, 1, 0);
verifyFlash(1, 1, 0);
break;
case 1:
setupCFI();
flashSize = 4194304;
// Write first rom chip
writeCFI_Flash(1, 2, 0);
verifyFlash(1, 2, 0);
delay(300);
// Switch to second ROM chip, see flash.ino low level functions line 811
// LoROM
if (mapping == 1)
mapping = 122;
// HiROM
else if (mapping == 2)
mapping = 222;
// Write second rom chip
display_Clear();
writeCFI_Flash(2, 2, 0);
verifyFlash(2, 2, 0);
break;
case 2:
setupCFI();
flashSize = 4194304;
writeCFI_Flash(1, 1, 0);
verifyFlash(1, 1, 0);
break;
case 3:
//reversed = reproEXMenu();
setupCFI();
flashSize = 8388608;
// Write first rom chip
writeCFI_Flash(1, 2, reversed);
verifyFlash(1, 2, reversed);
delay(300);
// Switch to second ROM chip, see flash.ino low level functions line 811
// LoROM
if (mapping == 1)
mapping = 124;
// HiROM
else if (mapping == 2)
mapping = 224;
// Write second rom chip
display_Clear();
writeCFI_Flash(2, 2, reversed);
verifyFlash(2, 2, reversed);
break;
case 4:
//reversed = reproEXMenu();
setupCFI();
flashSize = 8388608;
// Write first rom chip
writeCFI_Flash(1, 4, reversed);
verifyFlash(1, 4, reversed);
delay(300);
// Switch to second ROM chip, see flash.ino low level functions line 811
// LoROM
if (mapping == 1)
mapping = 122;
// HiROM
else if (mapping == 2)
mapping = 222;
// Write second rom chip
display_Clear();
writeCFI_Flash(2, 4, reversed);
verifyFlash(2, 4, reversed);
// Switch to third ROM chip
// LoROM
if (mapping == 122)
mapping = 124;
// HiROM
else if (mapping == 222)
mapping = 224;
// Write second rom chip
display_Clear();
writeCFI_Flash(3, 4, reversed);
verifyFlash(3, 4, reversed);
// Switch to fourth ROM chip
// LoROM
if (mapping == 124)
mapping = 142;
// HiROM
else if (mapping == 224)
mapping = 242;
// Write second rom chip
display_Clear();
writeCFI_Flash(4, 4, reversed);
verifyFlash(4, 4, reversed);
break;
case 5:
//reversed = reproEXMenu();
setupCFI();
flashSize = 8388608;
writeCFI_Flash(1, 1, reversed);
verifyFlash(1, 1, reversed);
break;
case 6:
resetArduino();
break;
}
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 0);
display_Update();
wait();
resetArduino();
}
// SNES repro menu
void reproMenu() {
// create menu with title and 7 options to choose from
unsigned char snsRepro;
// Copy menuOptions out of progmem
convertPgm(menuOptionsRepro, 7);
snsRepro = question_box(F("Select Repro Type"), menuOptions, 7, 0);
// wait for user choice to come back from the question box menu
switch (snsRepro) {
case 0:
// CFI LoROM
mapping = 1;
reproCFIMenu();
break;
case 1:
// CFI HiROM
mapping = 2;
reproCFIMenu();
break;
case 2:
// LoRom
display_Clear();
display_Update();
mapping = 1;
setup_Flash8();
id_Flash8();
wait();
mode = CORE_FLASH8;
break;
case 3:
// HiRom
display_Clear();
display_Update();
mapping = 2;
setup_Flash8();
id_Flash8();
wait();
mode = CORE_FLASH8;
break;
case 4:
// ExLoRom
display_Clear();
display_Update();
mapping = 124;
setup_Flash8();
id_Flash8();
wait();
mode = CORE_FLASH8;
break;
case 5:
// ExHiRom
display_Clear();
display_Update();
mapping = 224;
setup_Flash8();
id_Flash8();
wait();
mode = CORE_FLASH8;
break;
case 6:
resetArduino();
break;
}
}
#endif
// SNES start menu
void snsMenu() {
// create menu with title and 7 options to choose from
unsigned char snsCart;
// Copy menuOptions out of progmem
#ifdef OPTION_CLOCKGEN_CALIBRATION
convertPgm(menuOptionsSNS, 8);
snsCart = question_box(FS(FSTRING_SELECT_CART_TYPE), menuOptions, 8, 0);
#else
convertPgm(menuOptionsSNS, 7);
snsCart = question_box(FS(FSTRING_SELECT_CART_TYPE), menuOptions, 7, 0);
#endif
// wait for user choice to come back from the question box menu
switch (snsCart) {
case 0:
display_Clear();
display_Update();
setup_Snes();
mode = CORE_SNES;
break;
#ifdef ENABLE_SFM
case 1:
display_Clear();
display_Update();
setup_SFM();
mode = CORE_SFM;
break;
#endif
#ifdef ENABLE_SV
case 2:
display_Clear();
display_Update();
setup_SV();
mode = CORE_SV;
break;
#endif
#ifdef ENABLE_ST
case 3:
display_Clear();
display_Update();
setup_ST();
mode = CORE_ST;
break;
#endif
#ifdef ENABLE_GPC
case 4:
display_Clear();
display_Update();
setup_GPC();
mode = CORE_GPC;
break;
#endif
#if (defined(ENABLE_FLASH) && defined(ENABLE_FLASH8))
case 5:
setup_FlashVoltage();
reproMenu();
break;
#endif
case 6:
#ifdef OPTION_CLOCKGEN_CALIBRATION
clkcal();
break;
case 7:
#endif
resetArduino();
break;
default:
print_MissingModule(); // does not return
}
}
// SNES Menu
void snesMenu() {
// create menu with title and 7 options to choose from
unsigned char mainMenu;
// Copy menuOptions out of progmem
convertPgm(menuOptionsSNES, 7);
mainMenu = question_box(F("SNES Cart Reader"), menuOptions, 7, 0);
// wait for user choice to come back from the question box menu
switch (mainMenu) {
case 0:
{
if (numBanks > 0) {
display_Clear();
// Change working dir to root
sd.chdir("/");
// start reading from cart
readROM_SNES();
// Internal Checksum
compare_checksum();
// CRC32
compareCRC("snes.txt", 0, 1, 0);
#ifdef ENABLE_GLOBAL_LOG
save_log();
#endif
display_Update();
} else {
display_Clear();
print_Error(F("Does not have ROM"));
}
}
break;
case 1:
if (sramSize > 0) {
display_Clear();
// Change working dir to root
sd.chdir("/");
readSRAM();
} else {
display_Clear();
print_Error(F("Does not have SRAM"));
}
break;
case 2:
if (sramSize > 0) {
display_Clear();
// Change working dir to root
sd.chdir("/");
writeSRAM(1);
unsigned long wrErrors;
wrErrors = verifySRAM();
if (wrErrors == 0) {
println_Msg(F("Verified OK"));
display_Update();
} else {
print_STR(error_STR, 0);
print_Msg(wrErrors);
print_STR(_bytes_STR, 1);
print_Error(did_not_verify_STR);
}
} else {
display_Clear();
print_Error(F("Does not have SRAM"));
}
break;
case 3:
if (sramSize > 0) {
display_Clear();
println_Msg(F("Warning:"));
println_Msg(F("This can erase"));
println_Msg(F("your save games"));
println_Msg(FS(FSTRING_EMPTY));
println_Msg(FS(FSTRING_EMPTY));
println_Msg(F("Press any button to"));
println_Msg(F("start sram testing"));
display_Update();
wait();
display_Clear();
// Change working dir to root
sd.chdir("/");
readSRAM();
eraseSRAM(0x0F);
eraseSRAM(0xF0);
writeSRAM(0);
unsigned long wrErrors = verifySRAM();
if (wrErrors == 0) {
println_Msg(F("Restored OK"));
display_Update();
} else {
print_STR(error_STR, 0);
print_Msg(wrErrors);
print_STR(_bytes_STR, 1);
print_Error(did_not_verify_STR);
}
} else {
display_Clear();
print_Error(F("Does not have SRAM"));
}
break;
case 4:
// For arcademaster1 (Markfrizb) multi-game carts
// Set reset pin to output (PH0)
DDRH |= (1 << 0);
// Switch RST(PH0) to LOW
PORTH &= ~(1 << 0);
// Note: It is probably not intended to reset CIC or clocks here
// But if that's false, uncomment this:
// stopSnesClocks_resetCic_resetCart();
display_Clear();
print_Msg(F("Resetting..."));
display_Update();
delay(3000); // wait 3 secs to switch to next game
resetArduino();
break;
case 5:
confMenuManual();
display_Clear();
display_Update();
break;
case 6:
stopSnesClocks_resetCic_resetCart();
resetArduino();
break;
}
//println_Msg(FS(FSTRING_EMPTY));
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
}
// Menu for manual configuration
void confMenuManual() {
// create menu with title and 5 options to choose from
unsigned char subMenu;
// Copy menuOptions out of progmem
convertPgm(menuOptionsConfManual, 5);
subMenu = question_box(F("Choose mapping"), menuOptions, 5, 0);
// wait for user choice to come back from the question box menu
switch (subMenu) {
case 0:
break;
case 1:
romType = LO;
numBanks = 128;
sramSize = 256;
strcpy(romName, "LoROM");
break;
case 2:
romType = HI;
numBanks = 64;
sramSize = 64;
strcpy(romName, "HiROM");
break;
case 3:
romType = EX;
numBanks = 96;
sramSize = 256;
strcpy(romName, "ExROM");
break;
case 4:
// Reset
stopSnesClocks_resetCic_resetCart();
resetArduino();
break;
}
}
void stopSnesClocks_resetCic_resetCart() {
DDRG |= (1 << 1); // Set cicrstPin(PG1) to Output
PORTG |= (1 << 1); // pull high = reset CIC
DDRH |= (1 << 0); // Set RST(PH0) pin to Output
PORTH &= ~(1 << 0); // Switch RST(PH0) to LOW
if (i2c_found) {
clockgen.output_enable(SI5351_CLK1, 0); // CPU clock
clockgen.output_enable(SI5351_CLK2, 0); // CIC clock
clockgen.output_enable(SI5351_CLK0, 0); // master clock
}
}
/******************************************
Setup
*****************************************/
void setup_Snes() {
// Request 5V
setVoltage(VOLTS_SET_5V);
// Set cicrstPin(PG1) to Output
DDRG |= (1 << 1);
// Output a high signal until we're ready to start
PORTG |= (1 << 1);
// Set cichstPin(PG0) to Input
DDRG &= ~(1 << 0);
// Set Address Pins to Output
//A0-A7
DDRF = 0xFF;
//A8-A15
DDRK = 0xFF;
//BA0-BA7
DDRL = 0xFF;
//PA0-PA7
DDRA = 0xFF;
// Set Control Pins to Output RST(PH0) CS(PH3) WR(PH5) RD(PH6)
DDRH |= (1 << 0) | (1 << 3) | (1 << 5) | (1 << 6);
// Switch RST(PH0) and WR(PH5) to HIGH
PORTH |= (1 << 0) | (1 << 5);
// Switch CS(PH3) and RD(PH6) to LOW
PORTH &= ~((1 << 3) | (1 << 6));
// Set Refresh(PE5) to Output
DDRE |= (1 << 5);
// Switch Refresh(PE5) to LOW (needed for SA-1)
PORTE &= ~(1 << 5);
// Set CPU Clock(PH1) to Output
DDRH |= (1 << 1);
//PORTH &= ~(1 << 1);
// Set IRQ(PH4) to Input
DDRH &= ~(1 << 4);
// Activate Internal Pullup Resistors
//PORTH |= (1 << 4);
// Set expand(PG5) to Input
DDRG &= ~(1 << 5);
// Activate Internal Pullup Resistors
//PORTG |= (1 << 5);
// Set Data Pins (D0-D7) to Input
DDRC = 0x00;
// Enable Internal Pullups
//PORTC = 0xFF;
// Unused pins
// Set wram(PE4) to Output
DDRE |= (1 << 4);
//PORTE &= ~(1 << 4);
// Set pawr(PJ1) to Output
DDRJ |= (1 << 1);
//PORTJ &= ~(1 << 1);
// Set pard(PJ0) to Output
DDRJ |= (1 << 0);
//PORTJ &= ~(1 << 0);
// Adafruit Clock Generator
initializeClockOffset();
if (i2c_found) {
// Set clocks to 4Mhz/1Mhz for better SA-1 unlocking
clockgen.set_freq(100000000ULL, SI5351_CLK1); // CPU
clockgen.set_freq(100000000ULL, SI5351_CLK2); // CIC
clockgen.set_freq(400000000ULL, SI5351_CLK0); // EXT
// Start outputting master clock, CIC clock
clockgen.output_enable(SI5351_CLK1, 0); // no CPU clock yet; seems to affect SA-1 success a lot
clockgen.output_enable(SI5351_CLK2, 1); // CIC clock (should go before master clock)
clockgen.output_enable(SI5351_CLK0, 1); // master clock
// Wait for clock generator
clockgen.update_status();
delay(500);
}
#ifdef ENABLE_CLOCKGEN
else {
display_Clear();
print_FatalError(F("Clock Generator not found"));
}
#endif
// Start CIC by outputting a low signal to cicrstPin(PG1)
PORTG &= ~(1 << 1);
// Wait for CIC reset
delay(500);
// Print all the info
getCartInfo_SNES();
if (i2c_found) {
//Set clocks to standard or else SA-1 sram writing will fail
clockgen.set_freq(2147727200ULL, SI5351_CLK0);
clockgen.set_freq(357954500ULL, SI5351_CLK1);
clockgen.set_freq(307200000ULL, SI5351_CLK2);
}
}
/******************************************
I/O Functions
*****************************************/
// Switch control pins to write
void controlOut_SNES() {
// Switch RD(PH6) and WR(PH5) to HIGH
PORTH |= (1 << 6) | (1 << 5);
// Switch CS(PH3) to LOW
PORTH &= ~(1 << 3);
}
// Switch control pins to read
void controlIn_SNES() {
// Switch WR(PH5) to HIGH
PORTH |= (1 << 5);
// Switch CS(PH3) and RD(PH6) to LOW
PORTH &= ~((1 << 3) | (1 << 6));
}
/******************************************
Low level functions
*****************************************/
// Write one byte of data to a location specified by bank and address, 00:0000
void writeBank_SNES(byte myBank, word myAddress, byte myData) {
PORTL = myBank;
PORTF = myAddress & 0xFF;
PORTK = (myAddress >> 8) & 0xFF;
PORTC = myData;
// Arduino running at 16Mhz -> one nop = 62.5ns
// Wait till output is stable
__asm__("nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t");
// Switch WR(PH5) to LOW
PORTH &= ~(1 << 5);
// Leave WR low for at least 60ns
__asm__("nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t");
// Switch WR(PH5) to HIGH
PORTH |= (1 << 5);
// Leave WR high for at least 50ns
__asm__("nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t");
}
// Read one byte of data from a location specified by bank and address, 00:0000
byte readBank_SNES(byte myBank, word myAddress) {
PORTL = myBank;
PORTF = myAddress & 0xFF;
PORTK = (myAddress >> 8) & 0xFF;
// Wait for the Byte to appear on the data bus
// Arduino running at 16Mhz -> one nop = 62.5ns
// slowRom is good for 200ns, fastRom is <= 120ns; S-CPU best case read speed: 3.57MHz / 280ns
// let's be conservative and use 6 x 62.5 = 375ns
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
// Read
byte tempByte = PINC;
return tempByte;
}
void readLoRomBanks(unsigned int start, unsigned int total, FsFile* file) {
byte buffer[1024] = { 0 };
uint16_t c = 0;
uint16_t currByte = 32768;
//Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = (uint32_t)(total - start) * 1024;
draw_progressbar(0, totalProgressBar);
for (word currBank = start; currBank < total; currBank++) {
PORTL = currBank;
// Blink led
blinkLED();
currByte = 32768;
while (1) {
c = 0;
while (c < 1024) {
PORTF = (currByte & 0xFF);
PORTK = ((currByte >> 8) & 0xFF);
// Wait for the Byte to appear on the data bus
// Arduino running at 16Mhz -> one nop = 62.5ns
// slowRom is good for 200ns, fastRom is <= 120ns; S-CPU best case read speed: 3.57MHz / 280ns
// let's be conservative and use 6 x 62.5 = 375ns
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
buffer[c] = PINC;
c++;
currByte++;
}
file->write(buffer, 1024);
// exit while(1) loop once the uint16_t currByte overflows from 0xffff to 0 (current bank is done)
if (currByte == 0) break;
}
// update progress bar
processedProgressBar += 1024;
draw_progressbar(processedProgressBar, totalProgressBar);
}
}
void readHiRomBanks(unsigned int start, unsigned int total, FsFile* file) {
byte buffer[1024] = { 0 };
uint16_t c = 0;
uint16_t currByte = 0;
//Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = (uint32_t)(total - start) * 1024;
draw_progressbar(0, totalProgressBar);
for (word currBank = start; currBank < total; currBank++) {
PORTL = currBank;
// Blink led
blinkLED();
currByte = 0;
while (1) {
c = 0;
while (c < 1024) {
PORTF = (currByte & 0xFF);
PORTK = ((currByte >> 8) & 0xFF);
// Wait for the Byte to appear on the data bus
// Arduino running at 16Mhz -> one nop = 62.5ns
// slowRom is good for 200ns, fastRom is <= 120ns; S-CPU best case read speed: 3.57MHz / 280ns
// let's be conservative and use 6 x 62.5 = 375ns
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
buffer[c] = PINC;
c++;
currByte++;
}
file->write(buffer, 1024);
// exit while(1) loop once the uint16_t currByte overflows from 0xffff to 0 (current bank is done)
if (currByte == 0) break;
}
// update progress bar
processedProgressBar += 1024;
draw_progressbar(processedProgressBar, totalProgressBar);
}
}
/******************************************
SNES ROM Functions
******************************************/
void getCartInfo_SNES() {
boolean manualConfig = 0;
//Prime SA1 cartridge
PORTL = 192;
for (uint16_t currByte = 0; currByte < 1024; currByte++) {
PORTF = currByte & 0xFF;
PORTK = currByte >> 8;
// Wait for the Byte to appear on the data bus
// Arduino running at 16Mhz -> one nop = 62.5ns
// slowRom is good for 200ns, fastRom is <= 120ns; S-CPU best case read speed: 3.57MHz / 280ns
// let's be conservative and use 6 x 62.5 = 375ns
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
}
// Print start page
if (checkcart_SNES() == 0) {
// Checksum either corrupt or 0000
manualConfig = 1;
errorLvl = 1;
rgbLed(red_color);
display_Clear();
println_Msg(F("ERROR"));
println_Msg(F("Rom header corrupt"));
println_Msg(F("or missing"));
println_Msg(FS(FSTRING_EMPTY));
println_Msg(FS(FSTRING_EMPTY));
println_Msg(F("Press button for"));
println_Msg(F("manual configuration"));
println_Msg(F("or powercycle if SA1"));
display_Update();
wait();
// Wait() clears errors but in this case we still have an error
errorLvl = 1;
}
display_Clear();
print_Msg(FS(FSTRING_NAME));
println_Msg(romName);
print_Msg(FS(FSTRING_REVISION));
println_Msg(romVersion);
print_Msg(F("Type: "));
if (romType == HI)
print_Msg(F("HiROM"));
else if (romType == LO)
print_Msg(F("LoROM"));
else if (romType == EX)
print_Msg(F("ExHiRom"));
else
print_Msg(romType);
print_Msg(FS(FSTRING_SPACE));
if (romSpeed == 0)
println_Msg(F("SlowROM"));
else if (romSpeed == 2)
println_Msg(F("SlowROM"));
else if (romSpeed == 3)
println_Msg(F("FastROM"));
else
println_Msg(romSpeed);
print_Msg(F("ICs: ROM "));
if (romChips == 0)
println_Msg(F("ONLY"));
else if (romChips == 1)
println_Msg(F("RAM"));
else if (romChips == 2)
println_Msg(F("SAVE"));
else if (romChips == 3)
println_Msg(F("DSP1"));
else if (romChips == 4)
println_Msg(F("DSP1 RAM"));
else if (romChips == 5)
println_Msg(F("DSP1 SAVE"));
else if ((romChips == 19) || (romChips == 20) || (romChips == 21) || (romChips == 26))
println_Msg(F("SuperFX"));
else if (romChips == 52) {
println_Msg(F("SA1 RAM"));
romType = SA;
} else if (romChips == 53) {
println_Msg(F("SA1 RAM BATT"));
romType = SA;
} else if (romChips == 67) {
println_Msg(F("SDD1"));
} else if (romChips == 69) {
println_Msg(F("SDD1 BATT"));