forked from sanni/cartreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGBA.ino
2655 lines (2240 loc) · 69.7 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
//******************************************
#include "options.h"
#ifdef enable_GBX
/******************************************
Variables
*****************************************/
char calcChecksumStr[5];
boolean readType;
/******************************************
Menu
*****************************************/
// GBA menu items
static const char GBAMenuItem1[] PROGMEM = "Read Rom";
static const char GBAMenuItem2[] PROGMEM = "Read Save";
static const char GBAMenuItem3[] PROGMEM = "Write Save";
static const char GBAMenuItem4[] PROGMEM = "Force Savetype";
static const char GBAMenuItem5[] PROGMEM = "Flash Repro";
static const char GBAMenuItem6[] PROGMEM = "Reset";
static const char* const menuOptionsGBA[] PROGMEM = {GBAMenuItem1, GBAMenuItem2, GBAMenuItem3, GBAMenuItem4, GBAMenuItem5, GBAMenuItem6};
// Rom menu
static const char GBARomItem1[] PROGMEM = "1MB";
static const char GBARomItem2[] PROGMEM = "2MB";
static const char GBARomItem3[] PROGMEM = "4MB";
static const char GBARomItem4[] PROGMEM = "8MB";
static const char GBARomItem5[] PROGMEM = "16MB";
static const char GBARomItem6[] PROGMEM = "32MB";
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 FLASHROM";
static const char GBASaveItem6[] PROGMEM = "1M FLASHROM";
static const char* const saveOptionsGBA[] PROGMEM = {GBASaveItem1, GBASaveItem2, GBASaveItem3, GBASaveItem4, GBASaveItem5, GBASaveItem6};
void gbaMenu() {
// create menu with title and 4 options to choose from
unsigned char mainMenu;
// Copy menuOptions out of progmem
convertPgm(menuOptionsGBA, 6);
mainMenu = question_box(F("GBA Cart Reader"), menuOptions, 6, 0);
// wait for user choice to come back from the question box menu
switch (mainMenu)
{
case 0:
// Read rom
switch (cartSize)
{
case 0:
// 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
switch (GBARomMenu)
{
case 0:
// 1MB
cartSize = 0x100000;
break;
case 1:
// 2MB
cartSize = 0x200000;
break;
case 2:
// 4MB
cartSize = 0x400000;
break;
case 3:
// 8MB
cartSize = 0x800000;
break;
case 4:
// 16MB
cartSize = 0x1000000;
break;
case 5:
// 32MB
cartSize = 0x2000000;
break;
}
break;
case 1:
// 1MB
cartSize = 0x100000;
break;
case 4:
// 4MB
cartSize = 0x400000;
break;
case 8:
// 8MB
cartSize = 0x800000;
break;
case 16:
// 16MB
cartSize = 0x1000000;
break;
case 32:
// 32MB
cartSize = 0x2000000;
break;
}
display_Clear();
// Change working dir to root
sd.chdir("/");
readROM_GBA();
sd.chdir("/");
compare_checksum_GBA();
println_Msg(F(""));
println_Msg(F("Press Button..."));
display_Update();
wait();
break;
case 1:
// Read save
if (saveType == 0) {
// 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
switch (GBASaveMenu)
{
case 0:
// 4K EEPROM
saveType = 1;
break;
case 1:
// 64K EEPROM
saveType = 2;
break;
case 2:
// 256K SRAM/FRAM
saveType = 3;
break;
case 3:
// 512K SRAM/FRAM
saveType = 6;
break;
case 4:
// 512K FLASH
saveType = 4;
break;
case 5:
// 1024K FLASH
saveType = 5;
break;
}
}
switch (saveType)
{
case 1:
display_Clear();
sd.chdir("/");
// 4K EEPROM
readEeprom_GBA(4);
setROM_GBA();
break;
case 2:
display_Clear();
sd.chdir("/");
// 64K EEPROM
readEeprom_GBA(64);
setROM_GBA();
break;
case 3:
display_Clear();
sd.chdir("/");
// 256K SRAM/FRAM
readSRAM_GBA(1, 32768, 0);
setROM_GBA();
break;
case 4:
display_Clear();
sd.chdir("/");
// 512K FLASH
readFLASH_GBA(1, 65536, 0);
setROM_GBA();
break;
case 5:
display_Clear();
sd.chdir("/");
// 1024K 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);
setROM_GBA();
break;
case 6:
display_Clear();
sd.chdir("/");
// 512K SRAM/FRAM
readSRAM_GBA(1, 65536, 0);
setROM_GBA();
break;
}
println_Msg(F(""));
println_Msg(F("Press Button..."));
display_Update();
wait();
break;
case 2:
// Write save
if (saveType == 0) {
// create submenu with title and 6 options to choose from
unsigned char GBASavesMenu;
// Copy menuOptions out of progmem
convertPgm(saveOptionsGBA, 6);
GBASavesMenu = question_box(F("Select save type"), menuOptions, 6, 0);
// wait for user choice to come back from the question box menu
switch (GBASavesMenu)
{
case 0:
// 4K EEPROM
saveType = 1;
break;
case 1:
// 64K EEPROM
saveType = 2;
break;
case 2:
// 256K SRAM/FRAM
saveType = 3;
break;
case 3:
// 512K SRAM/FRAM
saveType = 6;
break;
case 4:
// 512K FLASH
saveType = 4;
break;
case 5:
// 1024K FLASH
saveType = 5;
break;
}
}
switch (saveType)
{
case 1:
display_Clear();
sd.chdir("/");
// 4K EEPROM
writeEeprom_GBA(4);
writeErrors = verifyEEP_GBA(4);
if (writeErrors == 0) {
println_Msg(F("Verified OK"));
display_Update();
}
else {
print_Msg(F("Error: "));
print_Msg(writeErrors);
println_Msg(F(" bytes "));
print_Error(F("did not verify."), false);
}
setROM_GBA();
break;
case 2:
display_Clear();
sd.chdir("/");
// 64K EEPROM
writeEeprom_GBA(64);
writeErrors = verifyEEP_GBA(64);
if (writeErrors == 0) {
println_Msg(F("Verified OK"));
display_Update();
}
else {
print_Msg(F("Error: "));
print_Msg(writeErrors);
println_Msg(F(" bytes "));
print_Error(F("did not verify."), false);
}
setROM_GBA();
break;
case 3:
display_Clear();
// Change working dir to root
sd.chdir("/");
// 256K SRAM/FRAM
writeSRAM_GBA(1, 32768, 0);
writeErrors = verifySRAM_GBA(32768, 0);
if (writeErrors == 0) {
println_Msg(F("Verified OK"));
display_Update();
}
else {
print_Msg(F("Error: "));
print_Msg(writeErrors);
println_Msg(F(" bytes "));
print_Error(F("did not verify."), false);
}
setROM_GBA();
break;
case 4:
display_Clear();
sd.chdir("/");
// 512K FLASH
idFlash_GBA();
resetFLASH_GBA();
if (strcmp(flashid, "BFD4") != 0) {
println_Msg(F("Flashrom Type not supported"));
print_Msg(F("ID: "));
println_Msg(flashid);
print_Error(F(""), true);
}
eraseFLASH_GBA();
if (blankcheckFLASH_GBA(65536)) {
writeFLASH_GBA(1, 65536, 0);
verifyFLASH_GBA(65536, 0);
}
else {
print_Error(F("Erase failed"), false);
}
setROM_GBA();
break;
case 5:
display_Clear();
sd.chdir("/");
// 1M FLASH
idFlash_GBA();
resetFLASH_GBA();
if (strcmp(flashid, "C209") != 0) {
println_Msg(F("Flashrom Type not supported"));
print_Msg(F("ID: "));
println_Msg(flashid);
print_Error(F(""), true);
}
eraseFLASH_GBA();
// 131072 bytes are divided into two 65536 byte banks
switchBank_GBA(0x0);
setROM_GBA();
if (blankcheckFLASH_GBA(65536)) {
writeFLASH_GBA(1, 65536, 0);
verifyFLASH_GBA(65536, 0);
}
else {
print_Error(F("Erase failed"), false);
}
switchBank_GBA(0x1);
setROM_GBA();
if (blankcheckFLASH_GBA(65536)) {
writeFLASH_GBA(0, 65536, 65536);
verifyFLASH_GBA(65536, 65536);
}
else {
print_Error(F("Erase failed"), false);
}
setROM_GBA();
break;
case 6:
display_Clear();
// Change working dir to root
sd.chdir("/");
// 512K SRAM/FRAM
writeSRAM_GBA(1, 65536, 0);
writeErrors = verifySRAM_GBA(65536, 0);
if (writeErrors == 0) {
println_Msg(F("Verified OK"));
display_Update();
}
else {
print_Msg(F("Error: "));
print_Msg(writeErrors);
println_Msg(F(" bytes "));
print_Error(F("did not verify."), false);
}
setROM_GBA();
break;
}
println_Msg(F(""));
println_Msg(F("Press Button..."));
display_Update();
wait();
break;
case 3:
display_Clear();
// create submenu with title and 7 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
switch (GBASaveMenu)
{
case 0:
// 4K EEPROM
saveType = 1;
break;
case 1:
// 64K EEPROM
saveType = 2;
break;
case 2:
// 256K SRAM/FRAM
saveType = 3;
break;
case 3:
// 512K SRAM/FRAM
saveType = 6;
break;
case 4:
// 512K FLASH
saveType = 4;
break;
case 5:
// 1024K FLASH
saveType = 5;
break;
}
display_Clear();
break;
case 4:
display_Clear();
flashRepro_GBA();
println_Msg(F(""));
println_Msg(F("Press Button..."));
display_Update();
wait();
resetArduino();
break;
case 5:
resetArduino();
break;
}
}
/******************************************
Setup
*****************************************/
void setup_GBA() {
setROM_GBA();
// Print start page
getCartInfo_GBA();
display_Clear();
print_Msg(F("Name: "));
println_Msg(romName);
print_Msg(F("Cart ID: "));
println_Msg(cartID);
print_Msg(F("Rom Size: "));
if (cartSize == 0)
println_Msg(F("Unknown"));
else {
print_Msg(cartSize);
println_Msg(F("MB"));
}
print_Msg(F("Save: "));
switch (saveType)
{
case 0:
println_Msg(F("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("1024K Flash"));
break;
}
print_Msg(F("Checksum: "));
println_Msg(checksumStr);
print_Msg(F("Version: 1."));
println_Msg(romVersion);
// Wait for user input
println_Msg(F("Press Button..."));
display_Update();
wait();
}
/******************************************
Low level functions
*****************************************/
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(unsigned long 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(unsigned long 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
*****************************************/
// Read info out of rom header
void getCartInfo_GBA() {
// 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) {
print_Error(F("CARTRIDGE ERROR"), false);
strcpy(romName, "ERROR");
println_Msg(F(""));
println_Msg(F(""));
println_Msg(F(""));
println_Msg(F("Press Button to"));
println_Msg(F("ignore or powercycle"));
println_Msg(F("to try again"));
display_Update();
wait();
}
else {
char tempStr2[2];
char tempStr[5];
// 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]);
if (myFile.open("gba.txt", O_READ)) {
// Loop through file
while (myFile.available()) {
// Read 4 bytes into String, do it one at a time so byte order doesn't get mixed up
sprintf(tempStr, "%c", myFile.read());
for (byte i = 0; i < 3; i++) {
sprintf(tempStr2, "%c", myFile.read());
strcat(tempStr, tempStr2);
}
// Check if string is a match
if (strcmp(tempStr, cartID) == 0) {
// Skip the , in the file
myFile.seekSet(myFile.curPosition() + 1);
// Read the next ascii character and subtract 48 to convert to decimal
cartSize = myFile.read() - 48;
// Remove leading 0 for single digit cart sizes
if (cartSize != 0) {
cartSize = cartSize * 10 + myFile.read() - 48;
}
else {
cartSize = myFile.read() - 48;
}
// Skip the , in the file
myFile.seekSet(myFile.curPosition() + 1);
// Read the next ascii character and subtract 48 to convert to decimal
saveType = myFile.read() - 48;
}
// If no match, empty string, advance by 7 and try again
else {
myFile.seekSet(myFile.curPosition() + 7);
}
}
// Close the file:
myFile.close();
}
else {
print_Error(F("GBA.txt missing"), true);
}
// Get name
byte myByte = 0;
byte myLength = 0;
for (int addr = 0xA0; addr <= 0xAB; addr++) {
myByte = sdBuffer[addr];
if (((char(myByte) >= 48 && char(myByte) <= 57) || (char(myByte) >= 65 && char(myByte) <= 122)) && myLength < 15) {
romName[myLength] = char(myByte);
myLength++;
}
}
// Get ROM version
romVersion = sdBuffer[0xBC];
// Get Checksum as string
sprintf(checksumStr, "%02X", sdBuffer[0xBD]);
// Calculate Checksum
int calcChecksum = 0x00;
for (int n = 0xA0; n < 0xBD; n++) {
calcChecksum -= sdBuffer[n];
}
calcChecksum = (calcChecksum - 0x19) & 0xFF;
// Turn into string
sprintf(calcChecksumStr, "%02X", calcChecksum);
// Compare checksum
if (strcmp(calcChecksumStr, checksumStr) != 0) {
print_Msg(F("Result: "));
println_Msg(calcChecksumStr);
print_Error(F("Checksum Error"), false);
println_Msg(F(""));
println_Msg(F("Press Button..."));
display_Update();
wait();
}
}
}
// Dump ROM
void readROM_GBA() {
// Get name, add extension and convert to char array for sd lib
strcpy(fileName, romName);
strcat(fileName, ".gba");
// create a new folder for the rom file
EEPROM_readAnything(0, foldern);
sprintf(folder, "GBA/ROM/%s/%d", romName, foldern);
sd.mkdir(folder, true);
sd.chdir(folder);
//clear the screen
display_Clear();
print_Msg(F("Saving to "));
print_Msg(folder);
println_Msg(F("/..."));
display_Update();
// write new folder number back to eeprom
foldern = foldern + 1;
EEPROM_writeAnything(0, foldern);
//open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_Error(F("Can't create file on SD"), true);
}
// Read rom
for (int myAddress = 0; myAddress < cartSize; myAddress += 512) {
// Blink led
if (myAddress % 16384 == 0)
PORTB ^= (1 << 4);
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);
}
// Close the file:
myFile.close();
}
// Calculate the checksum of the dumped rom
boolean compare_checksum_GBA () {
println_Msg(F("Calculating 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
int calcChecksum = 0x00;
for (int n = 0xA0; n < 0xBD; n++) {
calcChecksum -= sdBuffer[n];
}
calcChecksum = (calcChecksum - 0x19) & 0xFF;
// Turn into string
sprintf(calcChecksumStr, "%02X", calcChecksum);
if (strcmp(calcChecksumStr, checksumStr) == 0) {
println_Msg(F("Checksum matches"));
display_Update();
return 1;
}
else {
print_Msg(F("Result: "));
println_Msg(calcChecksumStr);
print_Error(F("Checksum Error"), false);
return 0;
}
}
// Else show error
else {
print_Error(F("Failed to open rom"), false);
return 0;
}
}
/******************************************
GBA SRAM SAVE Functions
*****************************************/
void readSRAM_GBA(boolean browseFile, unsigned long sramSize, uint32_t pos) {
if (browseFile) {
// Get name, add extension and convert to char array for sd lib
strcpy(fileName, romName);
strcat(fileName, ".srm");
// create a new folder for the save file
EEPROM_readAnything(0, foldern);
sprintf(folder, "GBA/SAVE/%s/%d", romName, foldern);
sd.mkdir(folder, true);
sd.chdir(folder);
// Save location
print_Msg(F("Saving to "));
print_Msg(folder);
println_Msg(F("/..."));
display_Update();
// write new folder number back to eeprom
foldern = foldern + 1;
EEPROM_writeAnything(0, foldern);
}
//open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_Error(F("SD Error"), true);
}
// Seek to a new position in the file
if (pos != 0)
myFile.seekCur(pos);
for (unsigned long currAddress = 0; currAddress < sramSize; currAddress += 512) {
for (int c = 0; c < 512; c++) {
// Read byte