forked from sanni/cartreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GB.ino
1815 lines (1577 loc) · 45.9 KB
/
GB.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 MODULE
//******************************************
#include "options.h"
#ifdef enable_GBX
/******************************************
Variables
*****************************************/
// Game Boy
int sramBanks;
int romBanks;
word lastByte = 0;
/******************************************
Menu
*****************************************/
// GBx start menu
static const char gbxMenuItem1[] PROGMEM = "Game Boy (Color)";
static const char gbxMenuItem2[] PROGMEM = "Game Boy Advance";
static const char gbxMenuItem3[] PROGMEM = "Flash GBC Cart";
static const char gbxMenuItem4[] PROGMEM = "NPower GB Memory";
static const char gbxMenuItem5[] PROGMEM = "Reset";
static const char* const menuOptionsGBx[] PROGMEM = {gbxMenuItem1, gbxMenuItem2, gbxMenuItem3, gbxMenuItem4, gbxMenuItem5};
// GB menu items
static const char GBMenuItem1[] PROGMEM = "Read Rom";
static const char GBMenuItem2[] PROGMEM = "Read Save";
static const char GBMenuItem3[] PROGMEM = "Write Save";
static const char GBMenuItem4[] PROGMEM = "Reset";
static const char* const menuOptionsGB[] PROGMEM = {GBMenuItem1, GBMenuItem2, GBMenuItem3, GBMenuItem4};
// GB Flash items
static const char GBFlashItem1[] PROGMEM = "29F Cart (MBC3)";
static const char GBFlashItem2[] PROGMEM = "29F Cart (MBC5)";
static const char GBFlashItem3[] PROGMEM = "29F Cart (CAM)";
static const char GBFlashItem4[] PROGMEM = "CFI Cart";
static const char GBFlashItem5[] PROGMEM = "CFI Cart and Save";
static const char GBFlashItem6[] PROGMEM = "GB Smart";
static const char GBFlashItem7[] PROGMEM = "Reset";
static const char* const menuOptionsGBFlash[] PROGMEM = {GBFlashItem1, GBFlashItem2, GBFlashItem3, GBFlashItem4, GBFlashItem5, GBFlashItem6, GBFlashItem7};
// Start menu for both GB and GBA
void gbxMenu() {
// create menu with title and 4 options to choose from
unsigned char gbType;
// Copy menuOptions out of progmem
convertPgm(menuOptionsGBx, 5);
gbType = question_box(F("Select Game Boy"), menuOptions, 5, 0);
// wait for user choice to come back from the question box menu
switch (gbType)
{
case 0:
display_Clear();
display_Update();
setup_GB();
mode = mode_GB;
break;
case 1:
display_Clear();
display_Update();
setup_GBA();
mode = mode_GBA;
break;
case 2:
// create submenu with title and 7 options to choose from
unsigned char gbFlash;
// Copy menuOptions out of progmem
convertPgm(menuOptionsGBFlash, 7);
gbFlash = question_box(F("Select type"), menuOptions, 7, 0);
// wait for user choice to come back from the question box menu
switch (gbFlash)
{
case 0:
//Flash MBC3
display_Clear();
display_Update();
setup_GB();
mode = mode_GB;
// Change working dir to root
sd.chdir("/");
//MBC3
writeFlash29F_GB(3, 1);
// Reset
println_Msg(F("Press Button..."));
display_Update();
wait();
resetArduino();
break;
case 1:
//Flash MBC5
display_Clear();
display_Update();
setup_GB();
mode = mode_GB;
// Change working dir to root
sd.chdir("/");
//MBC5
writeFlash29F_GB(5, 1);
// Reset
println_Msg(F("Press Button..."));
display_Update();
wait();
resetArduino();
break;
case 2:
//Flash GB Camera
display_Clear();
display_Update();
setup_GB();
mode = mode_GB;
//Flash first bank with erase
// Change working dir to root
sd.chdir("/");
//MBC3
writeFlash29F_GB(3, 1);
println_Msg(F("Press Button..."));
display_Update();
wait();
display_Clear();
println_Msg(F("Please change the"));
println_Msg(F("switch on the cart"));
println_Msg(F("to B2 (Bank 2)"));
println_Msg(F("if you want to flash"));
println_Msg(F("a second game"));
println_Msg(F(""));
println_Msg(F("Press Button..."));
display_Update();
wait();
// Flash second bank without erase
// Change working dir to root
sd.chdir("/");
//MBC3
writeFlash29F_GB(3, 0);
// Reset
println_Msg(F(""));
println_Msg(F("Press Button..."));
display_Update();
wait();
resetArduino();
break;
case 3:
// Flash CFI
display_Clear();
display_Update();
setup_GB();
mode = mode_GB;
// Change working dir to root
sd.chdir("/");
// Launch filebrowser
filePath[0] = '\0';
sd.chdir("/");
fileBrowser(F("Select file"));
display_Clear();
identifyCFI_GB();
if (!writeCFI_GB()) {
display_Clear();
println_Msg(F("Flashing failed, time out!"));
println_Msg(F("Press Button..."));
display_Update();
wait();
}
// Reset
wait();
resetArduino();
break;
case 4:
// Flash CFI and Save
display_Clear();
display_Update();
setup_GB();
mode = mode_GB;
// Change working dir to root
sd.chdir("/");
// Launch filebrowser
filePath[0] = '\0';
sd.chdir("/");
fileBrowser(F("Select file"));
display_Clear();
identifyCFI_GB();
if (!writeCFI_GB()) {
display_Clear();
println_Msg(F("Flashing failed, time out!"));
println_Msg(F("Press Button..."));
display_Update();
wait();
resetArduino();
}
getCartInfo_GB();
// Does cartridge have SRAM
if (lastByte > 0) {
// Remove file name ending
int pos = -1;
while (fileName[++pos] != '\0') {
if (fileName[pos] == '.') {
fileName[pos] = '\0';
break;
}
}
sprintf(filePath, "/GB/SAVE/%s/", fileName);
bool saveFound = false;
if (sd.exists(filePath)) {
EEPROM_readAnything(0, foldern);
for (int i = foldern; i >= 0; i--) {
sprintf(filePath, "/GB/SAVE/%s/%d/%s.SAV", fileName, i, fileName);
if (sd.exists(filePath)) {
print_Msg(F("Save number "));
print_Msg(i);
println_Msg(F(" found."));
saveFound = true;
sprintf(filePath, "/GB/SAVE/%s/%d", fileName, i);
sprintf(fileName, "%s.SAV", fileName);
writeSRAM_GB();
unsigned long wrErrors;
wrErrors = verifySRAM_GB();
if (wrErrors == 0) {
println_Msg(F("Verified OK"));
display_Update();
}
else {
print_Msg(F("Error: "));
print_Msg(wrErrors);
println_Msg(F(" bytes "));
print_Error(F("did not verify."), false);
}
break;
}
}
}
if (!saveFound) {
println_Msg(F("Error: No save found."));
}
}
else {
print_Error(F("Cart has no Sram"), false);
}
// Reset
wait();
resetArduino();
break;
case 5:
// Flash GB Smart
display_Clear();
display_Update();
setup_GBSmart();
mode = mode_GB_GBSmart;
break;
case 6:
resetArduino();
break;
}
break;
case 3:
// Flash GB Memory
display_Clear();
display_Update();
setup_GBM();
mode = mode_GBM;
break;
case 4:
resetArduino();
break;
}
}
void gbMenu() {
// create menu with title and 3 options to choose from
unsigned char mainMenu;
// Copy menuOptions out of progmem
convertPgm(menuOptionsGB, 4);
mainMenu = question_box(F("GB Cart Reader"), menuOptions, 4, 0);
// wait for user choice to come back from the question box menu
switch (mainMenu)
{
case 0:
display_Clear();
// Change working dir to root
sd.chdir("/");
readROM_GB();
compare_checksums_GB();
#ifdef global_log
save_log();
#endif
break;
case 1:
display_Clear();
// Does cartridge have SRAM
if (lastByte > 0) {
// Change working dir to root
sd.chdir("/");
readSRAM_GB();
}
else {
print_Error(F("Cart has no Sram"), false);
}
println_Msg(F(""));
break;
case 2:
display_Clear();
// Does cartridge have SRAM
if (lastByte > 0) {
// Change working dir to root
sd.chdir("/");
filePath[0] = '\0';
fileBrowser(F("Select sav file"));
writeSRAM_GB();
unsigned long wrErrors;
wrErrors = verifySRAM_GB();
if (wrErrors == 0) {
println_Msg(F("Verified OK"));
display_Update();
}
else {
print_Msg(F("Error: "));
print_Msg(wrErrors);
println_Msg(F(" bytes "));
print_Error(F("did not verify."), false);
}
}
else {
print_Error(F("Cart has no Sram"), false);
}
println_Msg(F(""));
break;
case 3:
resetArduino();
break;
}
println_Msg(F("Press Button..."));
display_Update();
wait();
}
/******************************************
Setup
*****************************************/
void setup_GB() {
// Set Address Pins to Output
//A0-A7
DDRF = 0xFF;
//A8-A15
DDRK = 0xFF;
// Set Control Pins to Output RST(PH0) CLK(PH1) CS(PH3) WR(PH5) RD(PH6)
DDRH |= (1 << 0) | (1 << 1) | (1 << 3) | (1 << 5) | (1 << 6);
// Output a high signal on all pins, pins are active low therefore everything is disabled now
PORTH |= (1 << 3) | (1 << 5) | (1 << 6);
// Output a low signal on CLK(PH1) to disable writing GB Camera RAM
// Output a low signal on RST(PH0) to initialize MMC correctly
PORTH &= ~((1 << 0) | (1 << 1));
// Set Data Pins (D0-D7) to Input
DDRC = 0x00;
// Enable Internal Pullups
PORTC = 0xFF;
delay(400);
// RST(PH0) to H
PORTH |= (1 << 0);
// Print start page
getCartInfo_GB();
showCartInfo_GB();
// MMM01 initialize
if (romType >= 11 && romType <= 13) {
writeByte_GB(0x3fff, 0x00);
writeByte_GB(0x5fff, 0x40);
writeByte_GB(0x7fff, 0x01);
writeByte_GB(0x1fff, 0x3a);
writeByte_GB(0x1fff, 0x7a);
}
}
void showCartInfo_GB() {
display_Clear();
if (strcmp(checksumStr, "00") != 0) {
println_Msg(F("GB Cart Info"));
print_Msg(F("Name: "));
println_Msg(romName);
print_Msg(F("Mapper: "));
if ((romType == 0) || (romType == 8) || (romType == 9))
print_Msg(F("none"));
else if ((romType == 1) || (romType == 2) || (romType == 3))
print_Msg(F("MBC1"));
else if ((romType == 5) || (romType == 6))
print_Msg(F("MBC2"));
else if ((romType == 11) || (romType == 12) || (romType == 13))
print_Msg(F("MMM01"));
else if ((romType == 15) || (romType == 16) || (romType == 17) || (romType == 18) || (romType == 19))
print_Msg(F("MBC3"));
else if ((romType == 21) || (romType == 22) || (romType == 23))
print_Msg(F("MBC4"));
else if ((romType == 25) || (romType == 26) || (romType == 27) || (romType == 28) || (romType == 29) || (romType == 309))
print_Msg(F("MBC5"));
if (romType == 252)
print_Msg(F("Camera"));
println_Msg(F(" "));
print_Msg(F("Rom Size: "));
switch (romSize) {
case 0:
print_Msg(F("32KB"));
break;
case 1:
print_Msg(F("64KB"));
break;
case 2:
print_Msg(F("128KB"));
break;
case 3:
print_Msg(F("256KB"));
break;
case 4:
print_Msg(F("512KB"));
break;
case 5:
print_Msg(F("1MB"));
break;
case 6:
print_Msg(F("2MB"));
break;
case 7:
print_Msg(F("4MB"));
break;
}
println_Msg(F(""));
print_Msg(F("Banks: "));
println_Msg(romBanks);
print_Msg(F("Sram Size: "));
switch (sramSize) {
case 0:
if (romType == 6) {
print_Msg(F("512B"));
}
else {
print_Msg(F("none"));
}
break;
case 1:
print_Msg(F("2KB"));
break;
case 2:
print_Msg(F("8KB"));
break;
case 3:
print_Msg(F("32KB"));
break;
case 4:
print_Msg(F("128KB"));
break;
default: print_Msg(F("none"));
}
println_Msg(F(""));
print_Msg(F("Checksum: "));
println_Msg(checksumStr);
display_Update();
// Wait for user input
println_Msg(F("Press Button..."));
display_Update();
wait();
}
else {
print_Error(F("GAMEPAK ERROR"), true);
}
}
/******************************************
Low level functions
*****************************************/
byte readByte_GB(word myAddress) {
// Set address
PORTF = myAddress & 0xFF;
PORTK = (myAddress >> 8) & 0xFF;
// Switch data pins to input
DDRC = 0x00;
// Enable pullups
PORTC = 0xFF;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Switch RD(PH6) to LOW
PORTH &= ~(1 << 6);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Read
byte tempByte = PINC;
// Switch and RD(PH6) to HIGH
PORTH |= (1 << 6);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
return tempByte;
}
void writeByte_GB(int myAddress, byte myData) {
// Set address
PORTF = myAddress & 0xFF;
PORTK = (myAddress >> 8) & 0xFF;
// Set data
PORTC = myData;
// Switch data pins to output
DDRC = 0xFF;
// Wait till output is stable
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Pull WR(PH5) low
PORTH &= ~(1 << 5);
// Leave WR low for at least 60ns
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Pull WR(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");
// Switch data pins to input
DDRC = 0x00;
// Enable pullups
PORTC = 0xFF;
}
// Triggers CS and CLK pin
byte readByteSRAM_GB(word myAddress) {
PORTF = myAddress & 0xFF;
PORTK = (myAddress >> 8) & 0xFF;
// Switch data pins to input
DDRC = 0x00;
// Enable pullups
PORTC = 0xFF;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Pull CS(PH3) CLK(PH1)(for FRAM MOD) LOW
PORTH &= ~((1 << 3) | (1 << 1));
// Pull RD(PH6) LOW
PORTH &= ~(1 << 6);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Read
byte tempByte = PINC;
// Pull RD(PH6) HIGH
PORTH |= (1 << 6);
if (romType == 252) {
// Pull CS(PH3) HIGH
PORTH |= (1 << 3);
}
else {
// Pull CS(PH3) CLK(PH1)(for FRAM MOD) HIGH
PORTH |= (1 << 3) | (1 << 1);
}
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
return tempByte;
}
// Triggers CS and CLK pin
void writeByteSRAM_GB(int myAddress, byte myData) {
// Set address
PORTF = myAddress & 0xFF;
PORTK = (myAddress >> 8) & 0xFF;
// Set data
PORTC = myData;
// Switch data pins to output
DDRC = 0xFF;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
if (romType == 252) {
// Pull CS(PH3) LOW
PORTH &= ~(1 << 3);
// Pull CLK(PH1)(for GB CAM) HIGH
PORTH |= (1 << 1);
// Pull WR(PH5) low
PORTH &= ~(1 << 5);
}
else {
// Pull CS(PH3) CLK(PH1)(for FRAM MOD) LOW
PORTH &= ~((1 << 3) | (1 << 1));
// Pull WR(PH5) low
PORTH &= ~(1 << 5);
}
// Leave WR low for at least 60ns
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
if (romType == 252) {
// Pull WR(PH5) HIGH
PORTH |= (1 << 5);
// Pull CS(PH3) HIGH
PORTH |= (1 << 3);
// Pull CLK(PH1) LOW (for GB CAM)
PORTH &= ~(1 << 1);
}
else {
// Pull WR(PH5) HIGH
PORTH |= (1 << 5);
// Pull CS(PH3) CLK(PH1)(for FRAM MOD) HIGH
PORTH |= (1 << 3) | (1 << 1);
}
// Leave WR high for at least 50ns
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Switch data pins to input
DDRC = 0x00;
// Enable pullups
PORTC = 0xFF;
}
/******************************************
Game Boy functions
*****************************************/
// Read Cartridge Header
void getCartInfo_GB() {
// Read Header into array
for (int currByte = 0x100; currByte < 0x150; currByte++) {
sdBuffer[currByte] = readByte_GB(currByte);
}
/* Compare Nintendo logo against known checksum, 156 bytes starting at 0x04
word logoChecksum = 0;
for (int currByte = 0x104; currByte < 0x134; currByte++) {
logoChecksum += sdBuffer[currByte];
}
if (logoChecksum != 0x1546) {
print_Error(F("STARTUP LOGO ERROR"), false);
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();
}
*/
// Calculate header checksum
byte headerChecksum = 0;
for (int currByte = 0x134; currByte < 0x14D; currByte++) {
headerChecksum = headerChecksum - sdBuffer[currByte] - 1;
}
if (headerChecksum != sdBuffer[0x14D]) {
// Read Header into array a second time
for (int currByte = 0x100; currByte < 0x150; currByte++) {
sdBuffer[currByte] = readByte_GB(currByte);
}
// Calculate header checksum a second time
headerChecksum = 0;
for (int currByte = 0x134; currByte < 0x14D; currByte++) {
headerChecksum = headerChecksum - sdBuffer[currByte] - 1;
}
}
if (headerChecksum != sdBuffer[0x14D]) {
print_Error(F("HEADER CHECKSUM ERROR"), false);
println_Msg(F(""));
println_Msg(F(""));
println_Msg(F(""));
println_Msg(F("Press Button to"));
println_Msg(F("ignore or clean"));
println_Msg(F("cart and try again"));
display_Update();
wait();
}
romType = sdBuffer[0x147];
romSize = sdBuffer[0x148];
sramSize = sdBuffer[0x149];
// ROM banks
switch (romSize) {
case 0x00:
romBanks = 2;
break;
case 0x01:
romBanks = 4;
break;
case 0x02:
romBanks = 8;
break;
case 0x03:
romBanks = 16;
break;
case 0x04:
romBanks = 32;
break;
case 0x05:
romBanks = 64;
break;
case 0x06:
romBanks = 128;
break;
case 0x07:
romBanks = 256;
break;
default:
romBanks = 2;
}
// SRAM banks
sramBanks = 0;
if (romType == 6) {
sramBanks = 1;
}
// SRAM size
switch (sramSize) {
case 2:
sramBanks = 1;
break;
case 3:
sramBanks = 4;
break;
case 4:
sramBanks = 16;
break;
case 5:
sramBanks = 8;
break;
}
// Last byte of SRAM
if (romType == 6) {
lastByte = 0xA1FF;
}
if (sramSize == 1) {
lastByte = 0xA7FF;
}
else if (sramSize > 1) {
lastByte = 0xBFFF;
}
// Get Checksum as string
eepbit[6] = sdBuffer[0x14E];
eepbit[7] = sdBuffer[0x14F];
sprintf(checksumStr, "%02X%02X", eepbit[6], eepbit[7]);
// Get name
byte myLength = 0;
for (int addr = 0x0134; addr <= 0x13C; addr++) {
if (((char(sdBuffer[addr]) >= 48 && char(sdBuffer[addr]) <= 57) || (char(sdBuffer[addr]) >= 65 && char(sdBuffer[addr]) <= 122)) && myLength < 15) {
romName[myLength] = char(sdBuffer[addr]);
myLength++;
}
}
}
/******************************************
ROM functions
*****************************************/
// Read ROM
void readROM_GB() {
// Get name, add extension and convert to char array for sd lib
strcpy(fileName, romName);
strcat(fileName, ".GB");
// create a new folder for the rom file
EEPROM_readAnything(0, foldern);
sprintf(folder, "GB/ROM/%s/%d", romName, foldern);
sd.mkdir(folder, true);
sd.chdir(folder);
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);
}
word romAddress = 0;
//Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = (uint32_t)(romBanks) * 16384;
draw_progressbar(0, totalProgressBar);
for (word currBank = 1; currBank < romBanks; currBank++) {
// Second bank starts at 0x4000
if (currBank > 1) {
romAddress = 0x4000;
}
// Set ROM bank for MBC2/3/4/5
if (romType >= 5) {
if (romType >= 11 && romType <= 13) {
if ((currBank & 0x1f) == 0) {
// reset MMM01
PORTH &= ~(1 << 0);
PORTH |= (1 << 0);
// remap to higher 4Mbits ROM
writeByte_GB(0x3fff, 0x20);
writeByte_GB(0x5fff, 0x40);
writeByte_GB(0x7fff, 0x01);
writeByte_GB(0x1fff, 0x3a);
writeByte_GB(0x1fff, 0x7a);
// for every 4Mbits ROM, restart from 0x0000
romAddress = 0x0000;
currBank++;
}
else {
writeByte_GB(0x6000, 0);
writeByte_GB(0x2000, (currBank & 0x1f));
}
}
else {
writeByte_GB(0x2100, currBank);
}
}
// Set ROM bank for MBC1
else {
writeByte_GB(0x6000, 0);
writeByte_GB(0x4000, currBank >> 5);
writeByte_GB(0x2000, currBank & 0x1F);
}
// Read banks and save to SD
while (romAddress <= 0x7FFF) {
for (int i = 0; i < 512; i++) {
sdBuffer[i] = readByte_GB(romAddress + i);
}
myFile.write(sdBuffer, 512);
romAddress += 512;
processedProgressBar += 512;
draw_progressbar(processedProgressBar, totalProgressBar);
}
}
// Close the file:
myFile.close();
}
// Calculate checksum
unsigned int calc_checksum_GB (char* fileName, char* folder) {
unsigned int calcChecksum = 0;
// int calcFilesize = 0; // unused
unsigned long i = 0;
int c = 0;
// If file exists
if (myFile.open(fileName, O_READ)) {
//calcFilesize = myFile.fileSize() * 8 / 1024 / 1024; // unused
for (i = 0; i < (myFile.fileSize() / 512); i++) {
myFile.read(sdBuffer, 512);
for (c = 0; c < 512; c++) {
calcChecksum += sdBuffer[c];
}
}
myFile.close();
// Subtract checksum bytes
calcChecksum -= eepbit[6];
calcChecksum -= eepbit[7];
// Return result
return (calcChecksum);
}
// Else show error
else {
print_Error(F("DUMP ROM 1ST"), false);
return 0;
}
}
// Compare checksum
void compare_checksums_GB() {
strcpy(fileName, romName);
strcat(fileName, ".GB");
// last used rom folder
EEPROM_readAnything(0, foldern);
sprintf(folder, "GB/ROM/%s/%d", romName, foldern - 1);
if (strcmp(folder, "root") != 0)
sd.chdir(folder);
// Internal ROM checksum
char calcsumStr[5];
sprintf(calcsumStr, "%04X", calc_checksum_GB(fileName, folder));
if (strcmp(calcsumStr, checksumStr) == 0) {
print_Msg(F("Internal: "));
print_Msg(calcsumStr);
println_Msg(F(" -> OK"));
}
else {
print_Msg(F("Internal: "));
println_Msg(calcsumStr);
print_Error(F("Checksum Error"), false);
}
compareCRC("gb.txt", 0);
display_Update();
//go to root
sd.chdir();
}
/******************************************
SRAM functions
*****************************************/
// Read RAM
void readSRAM_GB() {
// Does cartridge have RAM
if (lastByte > 0) {
// Get name, add extension and convert to char array for sd lib
strcpy(fileName, romName);
strcat(fileName, ".sav");
// create a new folder for the save file
EEPROM_readAnything(0, foldern);
sprintf(folder, "GB/SAVE/%s/%d", romName, foldern);
sd.mkdir(folder, true);
sd.chdir(folder);
// 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);
}
// MBC2 Fix
readByte_GB(0x0134);
if (romType <= 4 || (romType >= 11 && romType <= 13)) {
writeByte_GB(0x6000, 1);
}
// Initialise MBC
writeByte_GB(0x0000, 0x0A);
// Switch SRAM banks
for (byte currBank = 0; currBank < sramBanks; currBank++) {
writeByte_GB(0x4000, currBank);
// Read SRAM
for (word sramAddress = 0xA000; sramAddress <= lastByte; sramAddress += 64) {