-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathGBA.ino
2934 lines (2490 loc) · 79 KB
/
GBA.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
//******************************************
// GAME BOY ADVANCE MODULE
//******************************************
#ifdef ENABLE_GBX
/******************************************
Menu
*****************************************/
// GBA menu items
static const char GBAMenuItem4[] PROGMEM = "Force Savetype";
static const char* const menuOptionsGBA[] PROGMEM = { FSTRING_READ_ROM, FSTRING_READ_SAVE, FSTRING_WRITE_SAVE, GBAMenuItem4, FSTRING_RESET };
#if defined(ENABLE_FLASH)
// 369-in-1 menu items
static const char Menu369Item1[] PROGMEM = "Read 256MB";
static const char Menu369Item2[] PROGMEM = "Write 256MB";
static const char Menu369Item3[] PROGMEM = "Read Offset";
static const char Menu369Item4[] PROGMEM = "Write Offset";
static const char* const Options369GBA[] PROGMEM = { Menu369Item1, Menu369Item2, Menu369Item3, Menu369Item4, FSTRING_RESET };
#endif
// Rom menu
static const char GBARomItem1[] PROGMEM = "1 MB";
static const char GBARomItem2[] PROGMEM = "2 MB";
static const char GBARomItem3[] PROGMEM = "4 MB";
static const char GBARomItem4[] PROGMEM = "8 MB";
static const char GBARomItem5[] PROGMEM = "16 MB";
static const char GBARomItem6[] PROGMEM = "32 MB";
static const char* const romOptionsGBA[] PROGMEM = { GBARomItem1, GBARomItem2, GBARomItem3, GBARomItem4, GBARomItem5, GBARomItem6 };
// Save menu
static const char GBASaveItem1[] PROGMEM = "4K EEPROM";
static const char GBASaveItem2[] PROGMEM = "64K EEPROM";
static const char GBASaveItem3[] PROGMEM = "256K SRAM/FRAM";
static const char GBASaveItem4[] PROGMEM = "512K SRAM/FRAM";
static const char GBASaveItem5[] PROGMEM = "512K FLASH";
static const char GBASaveItem6[] PROGMEM = "1M FLASH";
static const char* const saveOptionsGBA[] PROGMEM = { GBASaveItem1, GBASaveItem2, GBASaveItem3, GBASaveItem4, GBASaveItem5, GBASaveItem6 };
void gbaMenu() {
// create menu with title and 5 options to choose from
unsigned char mainMenu;
// Copy menuOptions out of progmem
convertPgm(menuOptionsGBA, 5);
mainMenu = question_box(F("GBA Cart Reader"), menuOptions, 5, 0);
// wait for user choice to come back from the question box menu
switch (mainMenu) {
case 0:
// Read rom
if (cartSize == 0) {
const byte romOptionsGBASize[] = { 1, 2, 4, 8, 16, 32 };
// create submenu with title and 4 options to choose from
unsigned char GBARomMenu;
// Copy menuOptions out of progmem
convertPgm(romOptionsGBA, 6);
GBARomMenu = question_box(F("Select ROM size"), menuOptions, 6, 0);
// wait for user choice to come back from the question box menu
cartSize = romOptionsGBASize[GBARomMenu];
}
if (cartSize < 128) // Don't multiply cartSize on second dump
cartSize *= 0x100000;
display_Clear();
// Change working dir to root
sd.chdir("/");
readROM_GBA();
sd.chdir("/");
// Internal Checksum
compare_checksum_GBA();
// CRC32
compareCRC("gba.txt", 0, 1, 0);
#ifdef ENABLE_GLOBAL_LOG
save_log();
#endif
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
break;
case 1:
// Read save
display_Clear();
sd.chdir("/");
switch (getSaveType()) {
case 1:
// 4K EEPROM
readEeprom_GBA(4);
break;
case 2:
// 64K EEPROM
readEeprom_GBA(64);
break;
case 3:
// 256K SRAM/FRAM
readSRAM_GBA(1, 32768, 0);
break;
case 4:
// 512K FLASH
readFLASH_GBA(1, 65536, 0);
break;
case 5:
// 1M FLASH (divided into two banks)
switchBank_GBA(0x0);
setROM_GBA();
readFLASH_GBA(1, 65536, 0);
switchBank_GBA(0x1);
setROM_GBA();
readFLASH_GBA(0, 65536, 65536);
break;
case 6:
// 512K SRAM/FRAM
readSRAM_GBA(1, 65536, 0);
break;
}
setROM_GBA();
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();
break;
case 2:
// Write save
display_Clear();
sd.chdir("/");
switch (getSaveType()) {
case 1:
// 4K EEPROM
writeEeprom_GBA(4);
verifyEEP_GBA(4);
setROM_GBA();
break;
case 2:
// 64K EEPROM
writeEeprom_GBA(64);
verifyEEP_GBA(64);
break;
case 3:
// 256K SRAM/FRAM
writeSRAM_GBA(1, 32768, 0);
verifySRAM_GBA(32768, 0);
break;
case 4:
// 512K FLASH
idFlash_GBA();
resetFLASH_GBA();
if (flashid == 0x1F3D) {
printFlashTypeAndWait(F("Atmel AT29LV512"));
} else if (flashid == 0xBFD4) {
printFlashTypeAndWait(F("SST 39VF512"));
} else if (flashid == 0xC21C) {
printFlashTypeAndWait(F("Macronix MX29L512"));
} else if (flashid == 0x321B) {
printFlashTypeAndWait(F("Panasonic MN63F805MNP"));
} else {
printFlashTypeAndWait(F("Unknown"));
//print_FatalError(FSTRING_EMPTY);
}
if (flashid == 0x1F3D) { // Atmel
writeFLASH_GBA(1, 65536, 0, 1);
verifyFLASH_GBA(65536, 0);
} else {
eraseFLASH_GBA();
if (blankcheckFLASH_GBA(65536)) {
writeFLASH_GBA(1, 65536, 0, 0);
verifyFLASH_GBA(65536, 0);
}
}
break;
case 5:
// 1M FLASH
idFlash_GBA();
resetFLASH_GBA();
if (flashid == 0xC209) {
printFlashTypeAndWait(F("Macronix MX29L010"));
} else if (flashid == 0x6213) {
printFlashTypeAndWait(F("SANYO LE26FV10N1TS"));
} else {
printFlashTypeAndWait(F("Unknown"));
//print_FatalError(FSTRING_EMPTY);
}
eraseFLASH_GBA();
// 131072 bytes are divided into two 65536 byte banks
for (byte bank = 0; bank < 2; bank++) {
switchBank_GBA(bank);
setROM_GBA();
if (!blankcheckFLASH_GBA(65536))
break;
writeFLASH_GBA(!bank, 65536, bank ? 65536 : 0, 0);
if (verifyFLASH_GBA(65536, bank ? 65536 : 0))
break;
}
break;
case 6:
// 512K SRAM/FRAM
writeSRAM_GBA(1, 65536, 0);
verifySRAM_GBA(65536, 0);
break;
}
setROM_GBA();
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();
break;
case 3:
display_Clear();
saveType = 0;
saveType = getSaveType();
display_Clear();
break;
case 4:
resetArduino();
break;
}
}
#if defined(ENABLE_FLASH)
// Flash GBA Repro
void GBAReproMenu() {
setup_GBA_Repro();
flashRepro_GBA(0);
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();
resetArduino();
}
// Read/Write GBA 369-in-1 Repro
void repro369in1Menu() {
setup_GBA_Repro();
println_Msg(F("WARNING!!!"));
println_Msg(FS(FSTRING_EMPTY));
println_Msg(F("This will overwrite SRAM"));
println_Msg(FS(FSTRING_EMPTY));
println_Msg(F("(Ignore if no battery)"));
println_Msg(FS(FSTRING_EMPTY));
print_STR(press_button_STR, 1);
display_Update();
wait();
// create menu with title and 5 options to choose from
unsigned char menu369;
// Copy menuOptions out of progmem
convertPgm(Options369GBA, 5);
menu369 = question_box(F("369-in-1 Multicart"), menuOptions, 5, 0);
// wait for user choice to come back from the question box menu
switch (menu369) {
case 0:
display_Clear();
sd.chdir("/");
read369in1(0, 0);
break;
case 1:
display_Clear();
sd.chdir("/");
flashRepro_GBA(0);
break;
case 2:
display_Clear();
sd.chdir("/");
read369in1(selectBlockNumber(1), selectBlockNumber(0));
break;
case 3:
display_Clear();
sd.chdir("/");
flashRepro_GBA(1);
break;
case 4:
resetArduino();
break;
}
println_Msg(FS(FSTRING_EMPTY));
print_STR(press_button_STR, 1);
display_Update();
wait();
resetArduino();
}
#endif
/******************************************
Setup
*****************************************/
void setup_GBA() {
// Request 3.3V
setVoltage(VOLTS_SET_3V3);
setROM_GBA();
// Get cart info
getCartInfo_GBA();
display_Clear();
// Print start page
print_Msg(FS(FSTRING_NAME));
println_Msg(romName);
print_Msg(FS(FSTRING_SERIAL));
println_Msg(cartID);
print_Msg(FS(FSTRING_REVISION));
println_Msg(romVersion);
print_Msg(FS(FSTRING_ROM_SIZE));
if (cartSize == 0)
println_Msg(F("Unknown"));
else {
print_Msg(cartSize);
println_Msg(F(" MB"));
}
print_Msg(F("Save Type: "));
switch (saveType) {
case 0:
println_Msg(F("None/Unknown"));
break;
case 1:
println_Msg(F("4K EEPROM"));
break;
case 2:
println_Msg(F("64K EEPROM"));
break;
case 3:
println_Msg(F("256K SRAM"));
break;
case 4:
println_Msg(F("512K FLASH"));
break;
case 5:
println_Msg(F("1M FLASH"));
break;
}
print_Msg(F("Header Checksum: "));
println_Msg(checksumStr);
// Wait for user input
println_Msg("");
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
}
#if defined(ENABLE_FLASH)
void setup_GBA_Repro() {
// Request 3.3V
setVoltage(VOLTS_SET_3V3);
setROM_GBA();
display_Clear();
}
#endif
/******************************************
Low level functions
*****************************************/
static byte getSaveType() {
if (saveType == 0) {
const byte saveOptionsGBAType[] = { 1, 2, 3, 6, 4, 5 };
// create submenu with title and 6 options to choose from
unsigned char GBASaveMenu;
// Copy menuOptions out of progmem
convertPgm(saveOptionsGBA, 6);
GBASaveMenu = question_box(F("Select save type"), menuOptions, 6, 0);
// wait for user choice to come back from the question box menu
saveType = saveOptionsGBAType[GBASaveMenu];
}
return saveType;
}
static void printFlashTypeAndWait(const __FlashStringHelper* caption) {
print_Msg(F("FLASH ID: "));
println_Msg(flashid_str);
println_Msg(FS(FSTRING_EMPTY));
println_Msg(F("FLASH Type: "));
println_Msg(caption);
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();
display_Clear();
display_Update();
}
void setROM_GBA() {
// CS_SRAM(PH0)
DDRH |= (1 << 0);
PORTH |= (1 << 0);
// CS_ROM(PH3)
DDRH |= (1 << 3);
PORTH |= (1 << 3);
// WR(PH5)
DDRH |= (1 << 5);
PORTH |= (1 << 5);
// RD(PH6)
DDRH |= (1 << 6);
PORTH |= (1 << 6);
// AD0-AD7
DDRF = 0xFF;
// AD8-AD15
DDRK = 0xFF;
// AD16-AD23
DDRC = 0xFF;
// Wait
delay(500);
}
word readWord_GBA(unsigned long myAddress) {
// Set address/data ports to output
DDRF = 0xFF;
DDRK = 0xFF;
DDRC = 0xFF;
// Divide address by two to get word addressing
myAddress = myAddress >> 1;
// Output address to address pins,
PORTF = myAddress;
PORTK = myAddress >> 8;
PORTC = myAddress >> 16;
// Pull CS(PH3) to LOW
PORTH &= ~(1 << 3);
// Set address/data ports to input
PORTF = 0x0;
PORTK = 0x0;
DDRF = 0x0;
DDRK = 0x0;
// Pull RD(PH6) to LOW
PORTH &= ~(1 << 6);
// Delay here or read error with repro
__asm__("nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t");
word myWord = (PINK << 8) | PINF;
// Switch RD(PH6) to HIGH
PORTH |= (1 << 6);
// Switch CS_ROM(PH3) to HIGH
PORTH |= (1 << 3);
return myWord;
}
void writeWord_GBA(unsigned long myAddress, word myWord) {
// Set address/data ports to output
DDRF = 0xFF;
DDRK = 0xFF;
DDRC = 0xFF;
// Divide address by two to get word addressing
myAddress = myAddress >> 1;
// Output address to address pins,
PORTF = myAddress;
PORTK = myAddress >> 8;
PORTC = myAddress >> 16;
// Pull CS(PH3) to LOW
PORTH &= ~(1 << 3);
__asm__("nop\n\t"
"nop\n\t");
// Output data
PORTF = myWord & 0xFF;
PORTK = myWord >> 8;
// Pull WR(PH5) to LOW
PORTH &= ~(1 << 5);
__asm__("nop\n\t"
"nop\n\t");
// Switch WR(PH5) to HIGH
PORTH |= (1 << 5);
// Switch CS_ROM(PH3) to HIGH
PORTH |= (1 << 3);
}
// This function swaps bit at positions p1 and p2 in an integer n
word swapBits(word n, word p1, word p2) {
// Move p1'th to rightmost side
word bit1 = (n >> p1) & 1;
// Move p2'th to rightmost side
word bit2 = (n >> p2) & 1;
// XOR the two bits */
word x = (bit1 ^ bit2);
// Put the xor bit back to their original positions
x = (x << p1) | (x << p2);
// XOR 'x' with the original number so that the two sets are swapped
word result = n ^ x;
return result;
}
// Some repros have D0 and D1 switched
word readWord_GAB(unsigned long myAddress) {
word tempWord = swapBits(readWord_GBA(myAddress), 0, 1);
return tempWord;
}
void writeWord_GAB(unsigned long myAddress, word myWord) {
writeWord_GBA(myAddress, swapBits(myWord, 0, 1));
}
byte readByte_GBA(uint16_t myAddress) {
// Set address ports to output
DDRF = 0xFF;
DDRK = 0xFF;
// Set data port to input
DDRC = 0x0;
// Output address to address pins,
PORTF = myAddress;
PORTK = myAddress >> 8;
// Pull OE_SRAM(PH6) to LOW
PORTH &= ~(1 << 6);
// Pull CE_SRAM(PH0) to LOW
PORTH &= ~(1 << 0);
// Hold address for at least 25ns and wait 150ns before access
__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");
// Read byte
byte tempByte = PINC;
// Pull CE_SRAM(PH0) HIGH
PORTH |= (1 << 0);
// Pull OE_SRAM(PH6) HIGH
PORTH |= (1 << 6);
return tempByte;
}
void writeByte_GBA(uint16_t myAddress, byte myData) {
// Set address ports to output
DDRF = 0xFF;
DDRK = 0xFF;
// Set data port to output
DDRC = 0xFF;
// Output address to address pins
PORTF = myAddress;
PORTK = myAddress >> 8;
// Output data to data pins
PORTC = myData;
// 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");
// Pull WE_SRAM(PH5) to LOW
PORTH &= ~(1 << 5);
// Pull CE_SRAM(PH0) to LOW
PORTH &= ~(1 << 0);
// 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");
// Pull CE_SRAM(PH0) HIGH
PORTH |= (1 << 0);
// Pull WE_SRAM(PH5) 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");
}
/******************************************
GBA ROM Functions
*****************************************/
// Compute the checksum of rom header
// "header" must contain at least the rom's first 188 bytes
byte checksumHeader_GBA(const byte* header) {
byte result = 0x00;
for (byte n = 0xA0; n < 0xBD; n++) {
result -= header[n];
}
return result - 0x19;
}
// Read info out of rom header
void getCartInfo_GBA() {
char saveTypeStr[14];
// Read Header into array
for (int currWord = 0; currWord < 192; currWord += 2) {
word tempWord = readWord_GBA(currWord);
sdBuffer[currWord] = tempWord & 0xFF;
sdBuffer[currWord + 1] = (tempWord >> 8) & 0xFF;
}
// Compare Nintendo logo against known checksum, 156 bytes starting at 0x04
word logoChecksum = 0;
for (int currByte = 0x4; currByte < 0xA0; currByte++) {
logoChecksum += sdBuffer[currByte];
}
if (logoChecksum != 0x4B1B) {
display_Clear();
print_Error(F("CARTRIDGE ERROR"));
strcpy(romName, "ERROR");
println_Msg(FS(FSTRING_EMPTY));
println_Msg(FS(FSTRING_EMPTY));
println_Msg(FS(FSTRING_EMPTY));
println_Msg(F("Press Button to"));
println_Msg(F("ignore or powercycle"));
println_Msg(F("to try again"));
display_Update();
wait();
} else {
char tempStr[5];
tempStr[4] = 0;
// cart not in list
cartSize = 0;
saveType = 0;
// Get cart ID
cartID[0] = char(sdBuffer[0xAC]);
cartID[1] = char(sdBuffer[0xAD]);
cartID[2] = char(sdBuffer[0xAE]);
cartID[3] = char(sdBuffer[0xAF]);
display_Clear();
println_Msg(F("Searching database..."));
display_Update();
//go to root
sd.chdir();
if (myFile.open("gba.txt", O_READ)) {
char gamename[100];
#ifdef ENABLE_GLOBAL_LOG
// Disable log to prevent unnecessary logging
dont_log = true;
#endif
// Loop through file
while (myFile.available()) {
// Skip first line with name
skip_line(&myFile);
// Skip over the CRC checksum
myFile.seekCur(9);
// Read 4 bytes into String, do it one at a time so byte order doesn't get mixed up
for (byte i = 0; i < 4; i++) {
tempStr[i] = char(myFile.read());
}
// Check if string is a match
if (strcmp(tempStr, cartID) == 0) {
// Rewind to start of entry
rewind_line(myFile);
// Display database
while (myFile.available()) {
display_Clear();
// Read game name
get_line(gamename, &myFile, 96);
// Skip over the CRC checksum
myFile.seekCur(9);
// Read 4 bytes into String, do it one at a time so byte order doesn't get mixed up
for (byte i = 0; i < 4; i++) {
tempStr[i] = char(myFile.read());
}
// Skip the , in the file
myFile.seekCur(1);
// Read the next ascii character and subtract 48 to convert to decimal
cartSize = ((myFile.read() - 48) * 10) + (myFile.read() - 48);
// Skip the , in the file
myFile.seekCur(1);
// Read save type into string
get_line(saveTypeStr, &myFile, 14);
// skip third empty line
skip_line(&myFile);
// Print current database entry
println_Msg(gamename);
print_Msg(FS(FSTRING_SERIAL));
println_Msg(tempStr);
print_Msg(FS(FSTRING_ROM_SIZE));
print_Msg(cartSize);
println_Msg(F(" MB"));
print_Msg(F("Save Lib: "));
println_Msg(saveTypeStr);
printInstructions();
uint8_t b = 0;
while (1) {
// Check button input
b = checkButton();
// Next
if (b == 1) {
// Break out of loop to read next entry
break;
}
// Previous
else if (b == 2) {
rewind_line(myFile, 6);
break;
}
// Selection made
else if (b == 3) {
// Close file and break to exit both loops
myFile.close();
break;
}
}
}
}
// If no match advance and try again
else {
// skip rest of line
skip_line(&myFile);
// skip third empty line
skip_line(&myFile);
}
}
// Close the file:
myFile.close();
#ifdef ENABLE_GLOBAL_LOG
// Enable log again
dont_log = false;
#endif
} else {
print_FatalError(F("GBA.txt missing"));
}
// Get name
buildRomName(romName, &sdBuffer[0xA0], 12);
// Get ROM version
romVersion = sdBuffer[0xBC];
// Calculate Checksum
byte calcChecksum = checksumHeader_GBA(sdBuffer);
// Convert checksum from header into string
// (used in compare_checksum_GBA... it should just exchange an integer
// instead)
sprintf(checksumStr, "%02X", sdBuffer[0xBD]);
// Compare checksum
if (sdBuffer[0xBD] != calcChecksum) {
char calcChecksumStr[3];
display_Clear();
print_Msg(F("Result: "));
// Turn into string
sprintf(calcChecksumStr, "%02X", calcChecksum);
println_Msg(calcChecksumStr);
print_Error(F("Checksum Error"));
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();
}
/* Convert saveTypeStr to saveType
Save types in ROM
EEPROM_Vnnn EEPROM 512 bytes or 8 Kbytes (4Kbit or 64Kbit)
SRAM_Vnnn SRAM 32 Kbytes (256Kbit)
SRAM_F_Vnnn FRAM 32 Kbytes (256Kbit)
FLASH_Vnnn FLASH 64 Kbytes (512Kbit) (ID used in older files)
FLASH512_Vnnn FLASH 64 Kbytes (512Kbit) (ID used in newer files)
FLASH1M_Vnnn FLASH 128 Kbytes (1Mbit)
Save types in Cart Reader Code
0 = Unknown or no save
1 = 4K EEPROM
2 = 64K EEPROM
3 = 256K SRAM
4 = 512K FLASH
5 = 1M FLASH
6 = 512K SRAM
*/
if (saveTypeStr[0] == 'N') {
saveType = 0;
} else if (saveTypeStr[0] == 'E') {
// Test if 4kbit or 64kbit EEPROM
// Disable interrupts for more uniform clock pulses
noInterrupts();
// Fill sd Buffer
readBlock_EEP(0, 64);
interrupts();
delay(1000);
// Enable ROM again
setROM_GBA();
saveType = 1;
// Reading 4kbit EEPROM as 64kbit just gives the same 8 bytes repeated
for (int currByte = 0; currByte < 512 - 8; currByte++) {
if (sdBuffer[currByte] != sdBuffer[currByte + 8]) {
saveType = 2;
break;
}
}
} else if (saveTypeStr[0] == 'S') {
saveType = 3;
} else if ((saveTypeStr[0] == 'F') && (saveTypeStr[5] == '1')) {
saveType = 5;
} else if (saveTypeStr[0] == 'F') {
saveType = 4;
}
}
}
// Dump ROM
void readROM_GBA() {
// Get name, add extension and convert to char array for sd lib
createFolderAndOpenFile("GBA", "ROM", romName, "gba");
//Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = (uint32_t)(cartSize);
draw_progressbar(0, totalProgressBar);
// Read rom
for (unsigned long myAddress = 0; myAddress < cartSize; myAddress += 512) {
// Blink led
if (myAddress % 16384 == 0)
blinkLED();
for (int currWord = 0; currWord < 512; currWord += 2) {
word tempWord = readWord_GBA(myAddress + currWord);
sdBuffer[currWord] = tempWord & 0xFF;
sdBuffer[currWord + 1] = (tempWord >> 8) & 0xFF;
}
// Write to SD
myFile.write(sdBuffer, 512);
processedProgressBar += 512;
draw_progressbar(processedProgressBar, totalProgressBar);
}
// Fix unmapped ROM area of cartridges with 32 MB ROM + EEPROM save type
if ((cartSize == 0x2000000) && ((saveType == 1) || (saveType == 2))) {
byte padding_byte[256];
char tempStr[32];
myFile.seek(0x1FFFEFF);
myFile.read(padding_byte, 1);
sprintf(tempStr, "Fixing ROM padding (0x%02X)", padding_byte[0]);
println_Msg(tempStr);
memset(padding_byte + 1, padding_byte[0], 255);
myFile.write(padding_byte, 256);
}
// Close the file:
myFile.close();
}
// Calculate the checksum of the dumped rom
boolean compare_checksum_GBA() {
print_Msg(FS(FSTRING_CHECKSUM));
display_Update();
strcpy(fileName, romName);
strcat(fileName, ".gba");
// last used rom folder
EEPROM_readAnything(0, foldern);
sprintf(folder, "GBA/ROM/%s/%d", romName, foldern - 1);
sd.chdir(folder);
// If file exists
if (myFile.open(fileName, O_READ)) {
// Read rom header
myFile.read(sdBuffer, 512);
myFile.close();
// Calculate Checksum and turn into string
char calcChecksumStr[3];
sprintf(calcChecksumStr, "%02X", checksumHeader_GBA(sdBuffer));
print_Msg(calcChecksumStr);
if (strcmp(calcChecksumStr, checksumStr) == 0) {
println_Msg(F(" -> OK"));
display_Update();
return 1;
} else {
print_Msg(F(" != "));
println_Msg(checksumStr);
print_Error(F("Invalid Checksum"));
return 0;
}
}
// Else show error
else {
print_Error(F("Failed to open rom"));
return 0;
}
}
/******************************************
GBA SRAM SAVE Functions
*****************************************/
void readSRAM_GBA(boolean browseFile, uint32_t sramSize, uint32_t pos) {
if (browseFile) {
// Get name, add extension and convert to char array for sd lib
createFolder("GBA", "SAVE", romName, "srm");
printAndIncrementFolder();
}
//open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(sd_error_STR);
}