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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
|
//******************************************
// GAME BOY MODULE
//******************************************
#ifdef enable_GBX
/******************************************
Variables
*****************************************/
// Game Boy
word sramBanks;
word romBanks;
word lastByte = 0;
/******************************************
Menu
*****************************************/
// GBx start menu
static const char gbxMenuItem1[] PROGMEM = "Game Boy (Color)";
static const char gbxMenuItem2[] PROGMEM = "GB Advance (3V)";
static const char gbxMenuItem3[] PROGMEM = "Flash GBC Cart";
static const char gbxMenuItem4[] PROGMEM = "NPower GB Memory";
static const char gbxMenuItem5[] PROGMEM = "Flash Codebreaker";
static const char gbxMenuItem6[] PROGMEM = "Flash Datel Device";
//static const char gbxMenuItem5[] PROGMEM = "Reset"; (stored in common strings array)
static const char* const menuOptionsGBx[] PROGMEM = { gbxMenuItem1, gbxMenuItem2, gbxMenuItem3, gbxMenuItem4, gbxMenuItem5, gbxMenuItem6, string_reset2 };
// 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"; (stored in common strings array)
static const char* const menuOptionsGB[] PROGMEM = { GBMenuItem1, GBMenuItem2, GBMenuItem3, string_reset2 };
// 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"; (stored in common strings array)
static const char* const menuOptionsGBFlash[] PROGMEM = { GBFlashItem1, GBFlashItem2, GBFlashItem3, GBFlashItem4, GBFlashItem5, GBFlashItem6, string_reset2 };
// Pelican Codebreaker, Brainboy, and Monster Brain Operation Menu
static const char PelicanRead[] PROGMEM = "Read Device";
static const char PelicanWrite[] PROGMEM = "Write Device";
static const char* const menuOptionsGBPelican[] PROGMEM = { PelicanRead, PelicanWrite };
// Datel Device Operation Menu
static const char MegaMemRead[] PROGMEM = "Read Mega Memory";
static const char MegaMemWrite[] PROGMEM = "Write Mega Memory";
static const char GameSharkRead[] PROGMEM = "Read GBC GameShark";
static const char GameSharkWrite[] PROGMEM = "Write GBC GameShark";
static const char* const menuOptionsGBDatel[] PROGMEM = { MegaMemRead, MegaMemWrite, GameSharkRead, GameSharkWrite };
// Start menu for both GB and GBA
void gbxMenu() {
// create menu with title and 5 options to choose from
unsigned char gbType;
// Copy menuOptions out of progmem
convertPgm(menuOptionsGBx, 7);
gbType = question_box(F("Select Game Boy"), menuOptions, 7, 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
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
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
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
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);
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
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(""));
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
// Flash second bank without erase
// Change working dir to root
sd.chdir("/");
//MBC3
writeFlash29F_GB(3, 0);
// Reset
println_Msg(F(""));
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
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!"));
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
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!"));
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
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_STR(error_STR, 0);
print_Msg(wrErrors);
print_STR(_bytes_STR, 1);
print_Error(did_not_verify_STR);
}
break;
}
}
}
if (!saveFound) {
println_Msg(F("Error: No save found."));
}
} else {
print_Error(F("Cart has no Sram"));
}
// 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:
// Read or Write a Pelican Codebreaker or MonsterBrain
// 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);
mode = mode_GB;
display_Clear();
display_Update();
unsigned char gbPelican;
// Copy menuOptions out of progmem
convertPgm(menuOptionsGBPelican, 2);
gbPelican = question_box(F("Select operation:"), menuOptions, 2, 0);
// wait for user choice to come back from the question box menu
switch (gbPelican) {
case 0:
readPelican_GB();
// Reset
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
resetArduino();
break;
case 1:
writePelican_GB();
// Reset
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
resetArduino();
break;
}
break;
case 5:
// Read or Write a Datel Device (Mega Memory Card and Gameshark)
// 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);
mode = mode_GB;
display_Clear();
display_Update();
unsigned char gbDatel;
// Copy menuOptions out of progmem
convertPgm(menuOptionsGBDatel, 4);
gbDatel = question_box(F("Select operation:"), menuOptions, 4, 0);
// wait for user choice to come back from the question box menu
switch (gbDatel) {
case 0:
readMegaMem_GB();
// Reset
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
resetArduino();
break;
case 1:
writeMegaMem_GB();
// Reset
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
resetArduino();
break;
case 2:
readGameshark_GB();
// Reset
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
resetArduino();
break;
case 3:
writeGameshark_GB();
// Reset
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
resetArduino();
break;
}
break;
case 6:
resetArduino();
break;
}
}
void gbMenu() {
// create menu with title and 4 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("/");
if (romType == 32)
readSRAMFLASH_MBC6_GB();
else if (romType == 34)
readEEPROM_MBC7_GB();
else
readSRAM_GB();
} else {
print_Error(F("No save or unsupported type"));
}
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"));
if (romType == 32)
writeSRAMFLASH_MBC6_GB();
else if (romType == 34)
writeEEPROM_MBC7_GB();
else {
writeSRAM_GB();
unsigned long wrErrors;
wrErrors = verifySRAM_GB();
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 {
print_Error(F("No save or unsupported type"));
}
println_Msg(F(""));
break;
case 3:
resetArduino();
break;
}
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
}
/******************************************
Setup
*****************************************/
void setup_GB() {
// Request 5V
setVoltage(VOLTS_SET_5V);
// 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) {
print_Msg(F("Title: "));
println_Msg(romName);
if (cartID[0] != 0) {
print_Msg(F("Serial: "));
println_Msg(cartID);
}
print_Msg(F("Revision: "));
println_Msg(romVersion);
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"));
else if (romType == 32)
print_Msg(F("MBC6"));
else if (romType == 34)
print_Msg(F("MBC7"));
else if (romType == 252)
print_Msg(F("Camera"));
else if (romType == 253)
print_Msg(F("TAMA5"));
else if (romType == 254)
print_Msg(F("HuC-3"));
else if (romType == 255)
print_Msg(F("HuC-1"));
else if ((romType == 0x101) || (romType == 0x103))
print_Msg(F("MBC1M"));
else if (romType == 0x104)
print_Msg(F("M161"));
println_Msg(F(""));
print_Msg(F("ROM Size: "));
switch (romSize) {
case 0:
print_Msg(F("32 KB"));
break;
case 1:
print_Msg(F("64 KB"));
break;
case 2:
print_Msg(F("128 KB"));
break;
case 3:
print_Msg(F("256 KB"));
break;
case 4:
print_Msg(F("512 KB"));
break;
case 5:
print_Msg(F("1 MB"));
break;
case 6:
print_Msg(F("2 MB"));
break;
case 7:
print_Msg(F("4 MB"));
break;
case 8:
print_Msg(F("8 MB"));
break;
}
println_Msg(F(""));
//print_Msg(F("Banks: "));
//println_Msg(romBanks);
print_Msg(F("Save Size: "));
switch (sramSize) {
case 0:
if (romType == 6) {
print_Msg(F("512 Byte"));
} else if (romType == 0x22) {
print_Msg(lastByte);
print_Msg(F(" Byte"));
} else if (romType == 0xFD) {
print_Msg(F("32 Byte"));
} else {
print_Msg(F("None"));
}
break;
case 1:
print_Msg(F("2 KB"));
break;
case 2:
print_Msg(F("8 KB"));
break;
case 3:
if (romType == 0x20) {
print_Msg(F("1.03 MB"));
} else {
print_Msg(F("32 KB"));
}
break;
case 4:
print_Msg(F("128 KB"));
break;
case 5:
print_Msg(F("64 KB"));
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(""));
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
} else {
print_FatalError(F("GAMEPAK ERROR"));
}
}
/******************************************
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 || romType == 253) {
// 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 || romType == 253) {
// 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"));
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"));
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];
// Get Checksum as string
eepbit[6] = sdBuffer[0x14E];
eepbit[7] = sdBuffer[0x14F];
sprintf(checksumStr, "%02X%02X", eepbit[6], eepbit[7]);
// 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;
case 0x08:
romBanks = 512;
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;
}
// MBC6
if (romType == 32) {
sramBanks = 8;
lastByte = 0xAFFF;
} else if (romType == 34) { // MBC7
lastByte = (*((uint16_t*)(eepbit + 6)) == 0xa5be ? 512 : 256); // Only "Command Master" use LC66 EEPROM
}
// Get name
byte myByte = 0;
byte myLength = 0;
byte x = 0;
if (sdBuffer[0x143] == 0x80 || sdBuffer[0x143] == 0xC0) {
x++;
}
for (int addr = 0x0134; addr <= 0x0143 - x; addr++) {
myByte = sdBuffer[addr];
if (isprint(myByte) && myByte != '<' && myByte != '>' && myByte != ':' && myByte != '"' && myByte != '/' && myByte != '\\' && myByte != '|' && myByte != '?' && myByte != '*') {
romName[myLength++] = char(myByte);
} else if (myLength == 0 || romName[myLength - 1] != '_') {
romName[myLength++] = '_';
}
}
// Find Game Serial
cartID[0] = 0;
if (sdBuffer[0x143] == 0x80 || sdBuffer[0x143] == 0xC0) {
if ((romName[myLength - 4] == 'A' || romName[myLength - 4] == 'B' || romName[myLength - 4] == 'H' || romName[myLength - 4] == 'K' || romName[myLength - 4] == 'V') && (romName[myLength - 1] == 'A' || romName[myLength - 1] == 'B' || romName[myLength - 1] == 'D' || romName[myLength - 1] == 'E' || romName[myLength - 1] == 'F' || romName[myLength - 1] == 'I' || romName[myLength - 1] == 'J' || romName[myLength - 1] == 'K' || romName[myLength - 1] == 'P' || romName[myLength - 1] == 'S' || romName[myLength - 1] == 'U' || romName[myLength - 1] == 'X' || romName[myLength - 1] == 'Y')) {
cartID[0] = romName[myLength - 4];
cartID[1] = romName[myLength - 3];
cartID[2] = romName[myLength - 2];
cartID[3] = romName[myLength - 1];
myLength -= 4;
romName[myLength] = 0;
}
}
// Strip trailing white space
while (
myLength && (romName[myLength - 1] == '_' || romName[myLength - 1] == ' ')) {
myLength--;
}
romName[myLength] = 0;
// M161 (Mani 4 in 1)
if (strncmp(romName, "TETRIS SET", 10) == 0 && sdBuffer[0x14D] == 0x3F) {
romType = 0x104;
}
// MMM01 (Mani 4 in 1)
if (
(
strncmp(romName, "BOUKENJIMA2 SET", 15) == 0 && sdBuffer[0x14D] == 0)
|| (strncmp(romName, "BUBBLEBOBBLE SET", 16) == 0 && sdBuffer[0x14D] == 0xC6) || (strncmp(romName, "GANBARUGA SET", 13) == 0 && sdBuffer[0x14D] == 0x90) || (strncmp(romName, "RTYPE 2 SET", 11) == 0 && sdBuffer[0x14D] == 0x32)) {
romType = 0x0B;
}
// MBC1M
if (
(
strncmp(romName, "MOMOCOL", 7) == 0 && sdBuffer[0x14D] == 0x28)
|| (strncmp(romName, "BOMCOL", 6) == 0 && sdBuffer[0x14D] == 0x86) || (strncmp(romName, "GENCOL", 6) == 0 && sdBuffer[0x14D] == 0x8A) || (strncmp(romName, "SUPERCHINESE 123", 16) == 0 && sdBuffer[0x14D] == 0xE4) || (strncmp(romName, "MORTALKOMBATI&II", 16) == 0 && sdBuffer[0x14D] == 0xB9) || (strncmp(romName, "MORTALKOMBAT DUO", 16) == 0 && sdBuffer[0x14D] == 0xA7)) {
romType += 0x100;
}
// ROM revision
romVersion = sdBuffer[0x14C];
}
/******************************************
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_STR(saving_to_STR, 0);
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_FatalError(create_file_STR);
}
word endAddress = 0x7FFF;
word romAddress = 0;
word startBank = 1;
//Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = (uint32_t)(romBanks)*16384;
draw_progressbar(0, totalProgressBar);
// M161 banks are double size and start with 0
if (romType == 0x104) {
startBank = 0;
romBanks >>= 1;
endAddress = 0x7FFF;
}
// MBC6 banks are half size
else if (romType == 32) {
romBanks <<= 1;
endAddress = 0x3FFF;
}
for (word currBank = startBank; currBank < romBanks; currBank++) {
// Second bank starts at 0x4000
if (currBank > 1) {
romAddress = 0x4000;
// MBC6 banks are half size
if (romType == 32) {
endAddress = 0x5FFF;
}
}
// Set ROM bank for M161
if (romType == 0x104) {
romAddress = 0;
PORTH &= ~(1 << 0);
delay(50);
PORTH |= (1 << 0);
writeByte_GB(0x4000, currBank & 0x7);
}
// Set ROM bank for MBC1M
else if (romType == 0x101 || romType == 0x103) {
if (currBank < 10) {
writeByte_GB(0x4000, currBank >> 4);
writeByte_GB(0x2000, (currBank & 0x1f));
} else {
writeByte_GB(0x4000, currBank >> 4);
writeByte_GB(0x2000, 0x10 | (currBank & 0x1f));
}
}
// Set ROM bank for MBC6
else if (romType == 32) {
writeByte_GB(0x2800, 0);
writeByte_GB(0x3800, 0);
writeByte_GB(0x2000, currBank);
writeByte_GB(0x3000, currBank);
}
// Set ROM bank for TAMA5
else if (romType == 0xFD) {
writeByteSRAM_GB(0xA001, 0);
writeByteSRAM_GB(0xA000, currBank & 0x0f);
writeByteSRAM_GB(0xA001, 1);
writeByteSRAM_GB(0xA000, (currBank >> 4) & 0x0f);
}
// Set ROM bank for MBC2/3/4/5
else 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 {
if ((romType >= 0x19 && romType <= 0x1E) && (currBank == 0 || currBank == 256)) {
writeByte_GB(0x3000, (currBank >> 8) & 0xFF);
}
writeByte_GB(0x2100, currBank & 0xFF);
}
}
// 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 <= endAddress) {
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) {
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"));
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));
print_Msg(F("Checksum: "));
print_Msg(calcsumStr);
if (strcmp(calcsumStr, checksumStr) == 0) {
println_Msg(F(" -> OK"));
} else {
print_Msg(F(" != "));
println_Msg(checksumStr);
print_Error(F("Invalid Checksum"));
}
compareCRC("gb.txt", 0, 1, 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_FatalError(sd_error_STR);
}
// 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) {
for (byte i = 0; i < 64; i++) {
sdBuffer[i] = readByteSRAM_GB(sramAddress + i);
}
myFile.write(sdBuffer, 64);
}
}
// Disable SRAM
writeByte_GB(0x0000, 0x00);
// Close the file:
myFile.close();
// Signal end of process
print_Msg(F("Saved to "));
print_Msg(folder);
println_Msg(F("/"));
display_Update();
} else {
print_Error(F("Cart has no SRAM"));
}
}
// Write RAM
void writeSRAM_GB() {
// Does cartridge have SRAM
if (lastByte > 0) {
// Create filepath
sprintf(filePath, "%s/%s", filePath, fileName);
//open file on sd card
if (myFile.open(filePath, O_READ)) {
// MBC2 Fix
readByte_GB(0x0134);
// Enable SRAM for MBC1
if (romType <= 4 || (romType >= 11 && romType <= 13)) {
writeByte_GB(0x6000, 1);
}
// Initialise MBC
writeByte_GB(0x0000, 0x0A);
// Switch RAM banks
for (byte currBank = 0; currBank < sramBanks; currBank++) {
writeByte_GB(0x4000, currBank);
// Write RAM
for (word sramAddress = 0xA000; sramAddress <= lastByte; sramAddress++) {
writeByteSRAM_GB(sramAddress, myFile.read());
}
}
// Disable SRAM
writeByte_GB(0x0000, 0x00);
// Close the file:
myFile.close();
display_Clear();
println_Msg(F("SRAM writing finished"));
display_Update();
} else {
print_Error(F("File doesnt exist"));
}
} else {
print_Error(F("Cart has no SRAM"));
}
}
// Check if the SRAM was written without any error
unsigned long verifySRAM_GB() {
//open file on sd card
if (myFile.open(filePath, O_READ)) {
// Variable for errors
writeErrors = 0;
// MBC2 Fix
readByte_GB(0x0134);
// Check SRAM size
if (lastByte > 0) {
if (romType <= 4) { // MBC1
writeByte_GB(0x6000, 1); // Set RAM Mode
}
// 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) {
//fill sdBuffer
myFile.read(sdBuffer, 64);
for (int c = 0; c < 64; c++) {
if (readByteSRAM_GB(sramAddress + c) != sdBuffer[c]) {
writeErrors++;
}
}
}
}
// Disable RAM
writeByte_GB(0x0000, 0x00);
}
// Close the file:
myFile.close();
return writeErrors;
} else {
print_FatalError(open_file_STR);
return 1;
}
}
// Read SRAM + FLASH save data of MBC6
void readSRAMFLASH_MBC6_GB() {
// 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);
display_Clear();
print_STR(saving_to_STR, 0);
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_FatalError(sd_error_STR);
}
//Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = 0x108000;
draw_progressbar(0, totalProgressBar);
// Enable Mapper and SRAM
writeByte_GB(0x0000, 0x0A);
// Switch SRAM banks
for (byte currBank = 0; currBank < sramBanks; currBank++) {
writeByte_GB(0x0400, currBank);
writeByte_GB(0x0800, currBank);
// Read SRAM
for (word sramAddress = 0xA000; sramAddress <= lastByte; sramAddress += 64) {
for (byte i = 0; i < 64; i++) {
sdBuffer[i] = readByteSRAM_GB(sramAddress + i);
}
myFile.write(sdBuffer, 64);
processedProgressBar += 64;
draw_progressbar(processedProgressBar, totalProgressBar);
}
}
// Disable SRAM
writeByte_GB(0x0000, 0x00);
// Enable flash save memory (map to ROM)
writeByte_GB(0x1000, 0x01);
writeByte_GB(0x0C00, 0x01);
writeByte_GB(0x1000, 0x00);
writeByte_GB(0x2800, 0x08);
writeByte_GB(0x3800, 0x08);
// Switch FLASH banks
for (byte currBank = 0; currBank < 128; currBank++) {
word romAddress = 0x4000;
writeByte_GB(0x2000, currBank);
writeByte_GB(0x3000, currBank);
// Read banks and save to SD
while (romAddress <= 0x5FFF) {
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);
}
}
// Disable flash save memory
writeByte_GB(0x1000, 0x01);
writeByte_GB(0x0C00, 0x00);
writeByte_GB(0x1000, 0x00);
writeByte_GB(0x2800, 0x00);
writeByte_GB(0x3800, 0x00);
// Close the file:
myFile.close();
// Signal end of process
println_Msg(F("OK"));
display_Update();
}
// Write RAM
void writeSRAMFLASH_MBC6_GB() {
// Create filepath
sprintf(filePath, "%s/%s", filePath, fileName);
//open file on sd card
if (myFile.open(filePath, O_READ)) {
display_Clear();
println_Msg(F("Writing MBC6 save..."));
display_Update();
//Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = 0x108000;
draw_progressbar(0, totalProgressBar);
// Enable Mapper and SRAM
writeByte_GB(0x0000, 0x0A);
// Switch SRAM banks
for (byte currBank = 0; currBank < sramBanks; currBank++) {
writeByte_GB(0x0400, currBank);
writeByte_GB(0x0800, currBank);
// Write SRAM
for (word sramAddress = 0xA000; sramAddress <= lastByte; sramAddress++) {
writeByteSRAM_GB(sramAddress, myFile.read());
}
processedProgressBar += (lastByte + 1) - 0xA000;
draw_progressbar(processedProgressBar, totalProgressBar);
}
// Disable SRAM
writeByte_GB(0x0000, 0x00);
// Enable flash save memory (map to ROM)
writeByte_GB(0x1000, 0x01);
writeByte_GB(0x0C00, 0x01);
writeByte_GB(0x1000, 0x01);
writeByte_GB(0x2800, 0x08);
writeByte_GB(0x3800, 0x08);
for (byte currBank = 0; currBank < 128; currBank++) {
word romAddress = 0x4000;
// Erase FLASH sector
if (((processedProgressBar - 0x8000) % 0x20000) == 0) {
writeByte_GB(0x2800, 0x08);
writeByte_GB(0x3800, 0x08);
writeByte_GB(0x2000, 0x01);
writeByte_GB(0x3000, 0x02);
writeByte_GB(0x7555, 0xAA);
writeByte_GB(0x4AAA, 0x55);
writeByte_GB(0x7555, 0x80);
writeByte_GB(0x7555, 0xAA);
writeByte_GB(0x4AAA, 0x55);
writeByte_GB(0x2800, 0x08);
writeByte_GB(0x3800, 0x08);
writeByte_GB(0x2000, currBank);
writeByte_GB(0x3000, currBank);
writeByte_GB(0x4000, 0x30);
byte lives = 100;
while (1) {
byte sr = readByte_GB(0x4000);
if (sr == 0x80) break;
delay(1);
if (lives-- <= 0) {
// Disable flash save memory
writeByte_GB(0x1000, 0x01);
writeByte_GB(0x0C00, 0x00);
writeByte_GB(0x1000, 0x00);
writeByte_GB(0x2800, 0x00);
writeByte_GB(0x3800, 0x00);
myFile.close();
display_Clear();
print_FatalError(F("Error erasing FLASH sector."));
}
}
} else {
writeByte_GB(0x2800, 0x08);
writeByte_GB(0x3800, 0x08);
writeByte_GB(0x2000, currBank);
writeByte_GB(0x3000, currBank);
}
// Write to FLASH
while (romAddress <= 0x5FFF) {
writeByte_GB(0x2000, 0x01);
writeByte_GB(0x3000, 0x02);
writeByte_GB(0x7555, 0xAA);
writeByte_GB(0x4AAA, 0x55);
writeByte_GB(0x7555, 0xA0);
writeByte_GB(0x2800, 0x08);
writeByte_GB(0x3800, 0x08);
writeByte_GB(0x2000, currBank);
writeByte_GB(0x3000, currBank);
for (int i = 0; i < 128; i++) {
writeByte_GB(romAddress++, myFile.read());
}
writeByte_GB(romAddress - 1, 0x00);
byte lives = 100;
while (1) {
byte sr = readByte_GB(romAddress - 1);
if (sr == 0x80) break;
delay(1);
if (lives-- <= 0) {
// Disable flash save memory
writeByte_GB(0x1000, 0x01);
writeByte_GB(0x0C00, 0x00);
writeByte_GB(0x1000, 0x00);
writeByte_GB(0x2800, 0x00);
writeByte_GB(0x3800, 0x00);
myFile.close();
display_Clear();
print_FatalError(F("Error writing to FLASH."));
}
}
writeByte_GB(romAddress - 1, 0xF0);
processedProgressBar += 128;
draw_progressbar(processedProgressBar, totalProgressBar);
}
}
// Disable flash save memory
writeByte_GB(0x1000, 0x01);
writeByte_GB(0x0C00, 0x00);
writeByte_GB(0x1000, 0x00);
writeByte_GB(0x2800, 0x00);
writeByte_GB(0x3800, 0x00);
// Close the file:
myFile.close();
println_Msg(F("Save writing finished"));
display_Update();
} else {
print_Error(F("File doesnt exist"));
}
}
void readEEPROM_MBC7_GB() {
// 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_FatalError(sd_error_STR);
}
display_Clear();
print_STR(saving_to_STR, 0);
print_Msg(folder);
println_Msg(F("/..."));
display_Update();
uint16_t* data = (uint16_t*)sdBuffer;
// enable 0xa000 - 0xbfff access
writeByte_GB(0x0000, 0x0a);
writeByte_GB(0x4000, 0x40);
// set CS high
writeByte_GB(0xa080, 0x00);
writeByte_GB(0xa080, 0x80);
sendMBC7EEPROM_Inst_GB(2, 0, 0);
// read data from EEPROM
for (uint16_t i = 0; i < lastByte; i += 2, data++) {
*data = 0;
for (uint8_t j = 0; j < 16; j++) {
writeByte_GB(0xa080, 0x80);
writeByte_GB(0xa080, 0xc0);
*data <<= 1;
*data |= (readByte_GB(0xa080) & 0x01);
}
}
// deselect chip and ram access
writeByte_GB(0xa080, 0x00);
writeByte_GB(0x0000, 0x00);
writeByte_GB(0x4000, 0x00);
myFile.write(sdBuffer, lastByte);
myFile.close();
print_STR(done_STR, true);
display_Update();
}
void writeEEPROM_MBC7_GB() {
sprintf(filePath, "%s/%s", filePath, fileName);
// open file on sd card
if (!myFile.open(filePath, O_READ))
print_Error(F("File doesnt exist"));
myFile.read(sdBuffer, lastByte);
myFile.close();
display_Clear();
uint16_t* data = (uint16_t*)sdBuffer;
// enable 0xa000 - 0xbfff access
writeByte_GB(0x0000, 0x0a);
writeByte_GB(0x4000, 0x40);
// set CS high
writeByte_GB(0xa080, 0x00);
writeByte_GB(0xa080, 0x80);
println_Msg(F("Unlocking EEPROM..."));
display_Update();
// unlock EEPROM by sending EWEN instruction
sendMBC7EEPROM_Inst_GB(0, 0xc0, 0);
// set CS low
writeByte_GB(0xa080, 0x00);
println_Msg(F("Erasing EEPROM..."));
display_Update();
// erase entire EEPROM by sending ERAL instruction
sendMBC7EEPROM_Inst_GB(0, 0x80, 0);
// set CS low
writeByte_GB(0xa080, 0x00);
// set CS high
writeByte_GB(0xa080, 0x80);
// wait for erase complete
do {
writeByte_GB(0xa080, 0x80);
writeByte_GB(0xa080, 0xc0);
} while ((readByte_GB(0xa080) & 0x01) == 0);
println_Msg(F("Writing EEPROM..."));
display_Update();
// write data to EEPROM by sending WRITE instruction word by word
for (uint16_t i = 0; i < lastByte; i += 2, data++) {
sendMBC7EEPROM_Inst_GB(1, (i >> 1), *data);
// set CS low
writeByte_GB(0xa080, 0x00);
// set CS high
writeByte_GB(0xa080, 0x80);
// wait for writing complete
do {
writeByte_GB(0xa080, 0x80);
writeByte_GB(0xa080, 0xc0);
} while ((readByte_GB(0xa080) & 0x01) == 0);
}
println_Msg(F("Locking EEPROM..."));
display_Update();
// re-lock EEPROM
sendMBC7EEPROM_Inst_GB(0, 0, 0);
writeByte_GB(0xa080, 0x00);
// disable ram access
writeByte_GB(0x0000, 0x00);
writeByte_GB(0x4000, 0x00);
// done
print_STR(done_STR, true);
display_Update();
}
void sendMBC7EEPROM_Inst_GB(uint8_t op, uint8_t addr, uint16_t data) {
boolean send_data = false;
uint8_t i;
op = op & 0x03;
if (op == 0) {
addr &= 0xc0;
if (addr == 0x40)
send_data = true;
} else if (op == 1) {
send_data = true;
}
// send start bit
writeByte_GB(0xa080, 0x82);
writeByte_GB(0xa080, 0xc2);
// send op
for (i = 0; i < 2; i++, op <<= 1) {
eepbit[1] = (op & 0x02);
eepbit[0] = (eepbit[1] | 0x80);
eepbit[1] |= 0xc0;
writeByte_GB(0xa080, eepbit[0]);
writeByte_GB(0xa080, eepbit[1]);
}
// send addr
for (i = 0; i < 8; i++, addr <<= 1) {
eepbit[1] = ((addr & 0x80) >> 6);
eepbit[0] = (eepbit[1] | 0x80);
eepbit[1] |= 0xc0;
writeByte_GB(0xa080, eepbit[0]);
writeByte_GB(0xa080, eepbit[1]);
}
if (send_data) {
for (i = 0; i < 16; i++, data <<= 1) {
eepbit[1] = ((data & 0x8000) >> 14);
eepbit[0] = (eepbit[1] | 0x80);
eepbit[1] |= 0xc0;
writeByte_GB(0xa080, eepbit[0]);
writeByte_GB(0xa080, eepbit[1]);
}
}
}
/******************************************
29F016/29F032/29F033 flashrom functions
*****************************************/
// Write 29F032 flashrom
// A0-A13 directly connected to cart edge -> 16384(0x0-0x3FFF) bytes per bank -> 256(0x0-0xFF) banks
// A14-A21 connected to MBC5
void writeFlash29F_GB(byte MBC, boolean flashErase) {
// Launch filebrowser
filePath[0] = '\0';
sd.chdir("/");
fileBrowser(F("Select file"));
display_Clear();
// Create filepath
sprintf(filePath, "%s/%s", filePath, fileName);
// Open file on sd card
if (myFile.open(filePath, O_READ)) {
// Get rom size from file
myFile.seekCur(0x147);
romType = myFile.read();
romSize = myFile.read();
// Go back to file beginning
myFile.seekSet(0);
// 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;
}
// Set ROM bank hi 0
writeByte_GB(0x3000, 0);
// Set ROM bank low 0
writeByte_GB(0x2000, 0);
delay(100);
// Reset flash
writeByte_GB(0x555, 0xf0);
delay(100);
// ID command sequence
writeByte_GB(0x555, 0xaa);
writeByte_GB(0x2aa, 0x55);
writeByte_GB(0x555, 0x90);
// Read the two id bytes into a string
flashid = readByte_GB(0) << 8;
flashid |= readByte_GB(1);
if (flashid == 0x04D4) {
println_Msg(F("MBM29F033C"));
print_Msg(F("Banks: "));
print_Msg(romBanks);
println_Msg(F("/256"));
display_Update();
} else if (flashid == 0x0141) {
println_Msg(F("AM29F032B"));
print_Msg(F("Banks: "));
print_Msg(romBanks);
println_Msg(F("/256"));
display_Update();
} else if (flashid == 0x01AD) {
println_Msg(F("AM29F016B"));
print_Msg(F("Banks: "));
print_Msg(romBanks);
println_Msg(F("/128"));
display_Update();
} else if (flashid == 0x04AD) {
println_Msg(F("AM29F016D"));
print_Msg(F("Banks: "));
print_Msg(romBanks);
println_Msg(F("/128"));
display_Update();
} else if (flashid == 0x01D5) {
println_Msg(F("AM29F080B"));
print_Msg(F("Banks: "));
print_Msg(romBanks);
println_Msg(F("/64"));
display_Update();
} else {
print_Msg(F("Flash ID: "));
sprintf(flashid_str, "%04X", flashid);
println_Msg(flashid_str);
display_Update();
print_FatalError(F("Unknown flashrom"));
}
// Reset flash
writeByte_GB(0x555, 0xf0);
delay(100);
if (flashErase) {
println_Msg(F("Erasing flash"));
display_Update();
// Erase flash
writeByte_GB(0x555, 0xaa);
writeByte_GB(0x2aa, 0x55);
writeByte_GB(0x555, 0x80);
writeByte_GB(0x555, 0xaa);
writeByte_GB(0x2aa, 0x55);
writeByte_GB(0x555, 0x10);
// Read the status register
byte statusReg = readByte_GB(0);
// After a completed erase D7 will output 1
while ((statusReg & 0x80) != 0x80) {
// Update Status
statusReg = readByte_GB(0);
}
// Blankcheck
println_Msg(F("Blankcheck"));
display_Update();
// Read x number of banks
for (word currBank = 0; currBank < romBanks; currBank++) {
// Blink led
blinkLED();
// Set ROM bank
writeByte_GB(0x2000, currBank);
for (unsigned int currAddr = 0x4000; currAddr < 0x7FFF; currAddr += 512) {
for (int currByte = 0; currByte < 512; currByte++) {
sdBuffer[currByte] = readByte_GB(currAddr + currByte);
}
for (int j = 0; j < 512; j++) {
if (sdBuffer[j] != 0xFF) {
println_Msg(F("Not empty"));
print_FatalError(F("Erase failed"));
}
}
}
}
}
if (MBC == 3) {
println_Msg(F("Writing flash MBC3"));
display_Update();
// Write flash
word currAddr = 0;
word endAddr = 0x3FFF;
//Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = (uint32_t)(romBanks)*16384;
draw_progressbar(0, totalProgressBar);
for (word currBank = 0; currBank < romBanks; currBank++) {
// Blink led
blinkLED();
// Set ROM bank
writeByte_GB(0x2100, currBank);
if (currBank > 0) {
currAddr = 0x4000;
endAddr = 0x7FFF;
}
while (currAddr <= endAddr) {
myFile.read(sdBuffer, 512);
for (int currByte = 0; currByte < 512; currByte++) {
// Write command sequence
writeByte_GB(0x555, 0xaa);
writeByte_GB(0x2aa, 0x55);
writeByte_GB(0x555, 0xa0);
// Write current byte
writeByte_GB(currAddr + currByte, sdBuffer[currByte]);
// Set OE/RD(PH6) LOW
PORTH &= ~(1 << 6);
// Busy check
while ((PINC & 0x80) != (sdBuffer[currByte] & 0x80)) {
}
// Switch OE/RD(PH6) to HIGH
PORTH |= (1 << 6);
}
currAddr += 512;
processedProgressBar += 512;
draw_progressbar(processedProgressBar, totalProgressBar);
}
}
}
else if (MBC == 5) {
println_Msg(F("Writing flash MBC5"));
display_Update();
// Write flash
//Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = (uint32_t)(romBanks)*16384;
draw_progressbar(0, totalProgressBar);
for (word currBank = 0; currBank < romBanks; currBank++) {
// Blink led
blinkLED();
// Set ROM bank
writeByte_GB(0x2000, currBank);
// 0x2A8000 fix
writeByte_GB(0x4000, 0x0);
for (unsigned int currAddr = 0x4000; currAddr < 0x7FFF; currAddr += 512) {
myFile.read(sdBuffer, 512);
for (int currByte = 0; currByte < 512; currByte++) {
// Write command sequence
writeByte_GB(0x555, 0xaa);
writeByte_GB(0x2aa, 0x55);
writeByte_GB(0x555, 0xa0);
// Write current byte
writeByte_GB(currAddr + currByte, sdBuffer[currByte]);
// Set OE/RD(PH6) LOW
PORTH &= ~(1 << 6);
// Busy check
while ((PINC & 0x80) != (sdBuffer[currByte] & 0x80)) {
}
// Switch OE/RD(PH6) to HIGH
PORTH |= (1 << 6);
}
processedProgressBar += 512;
draw_progressbar(processedProgressBar, totalProgressBar);
}
}
}
print_STR(verifying_STR, 0);
display_Update();
// Go back to file beginning
myFile.seekSet(0);
//unsigned int addr = 0; // unused
writeErrors = 0;
// Verify flashrom
word romAddress = 0;
// Read number of banks and switch banks
for (word bank = 1; bank < romBanks; bank++) {
if (romType >= 5) { // MBC2 and above
writeByte_GB(0x2100, bank); // Set ROM bank
} else { // MBC1
writeByte_GB(0x6000, 0); // Set ROM Mode
writeByte_GB(0x4000, bank >> 5); // Set bits 5 & 6 (01100000) of ROM bank
writeByte_GB(0x2000, bank & 0x1F); // Set bits 0 & 4 (00011111) of ROM bank
}
if (bank > 1) {
romAddress = 0x4000;
}
// Blink led
blinkLED();
// Read up to 7FFF per bank
while (romAddress <= 0x7FFF) {
// Fill sdBuffer
myFile.read(sdBuffer, 512);
// Compare
for (int i = 0; i < 512; i++) {
if (readByte_GB(romAddress + i) != sdBuffer[i]) {
writeErrors++;
}
}
romAddress += 512;
}
}
// Close the file:
myFile.close();
if (writeErrors == 0) {
println_Msg(F("OK"));
display_Update();
} else {
println_Msg(F("Error"));
print_Msg(writeErrors);
print_STR(_bytes_STR, 1);
print_FatalError(did_not_verify_STR);
}
} else {
print_STR(open_file_STR, 1);
display_Update();
}
}
/******************************************
CFU flashrom functions
*****************************************/
/*
Flash chips can either be in x8 mode or x16 mode and sometimes the two
least significant bits on flash cartridges' data lines are swapped.
This function reads a byte and compensates for the differences.
This is only necessary for commands to the flash, not for data read from the flash, the MBC or SRAM.
address needs to be the x8 mode address of the flash register that should be read.
*/
byte readByteCompensated(int address) {
byte data = readByte_GB(address >> (flashX16Mode ? 1 : 0));
if (flashSwitchLastBits) {
return (data & 0b11111100) | ((data << 1) & 0b10) | ((data >> 1) & 0b01);
}
return data;
}
/*
Flash chips can either be in x8 mode or x16 mode and sometimes the two
least significant bits on flash cartridges' data lines are swapped.
This function writes a byte and compensates for the differences.
This is only necessary for commands to the flash, not for data written to the flash, the MBC or SRAM.
.
address needs to be the x8 mode address of the flash register that should be read.
*/
void writeByteCompensated(int address, byte data) {
if (flashSwitchLastBits) {
data = (data & 0b11111100) | ((data << 1) & 0b10) | ((data >> 1) & 0b01);
}
writeByte_GB(address >> (flashX16Mode ? 1 : 0), data);
}
void startCFIMode(boolean x16Mode) {
if (x16Mode) {
writeByte_GB(0x555, 0xf0); //x16 mode reset command
delay(500);
writeByte_GB(0x555, 0xf0); //Double reset to get out of possible Autoselect + CFI mode
delay(500);
writeByte_GB(0x55, 0x98); //x16 CFI Query command
} else {
writeByte_GB(0xAAA, 0xf0); //x8 mode reset command
delay(100);
writeByte_GB(0xAAA, 0xf0); //Double reset to get out of possible Autoselect + CFI mode
delay(100);
writeByte_GB(0xAA, 0x98); //x8 CFI Query command
}
}
/* Identify the different flash chips.
Sets the global variables flashBanks, flashX16Mode and flashSwitchLastBits
*/
void identifyCFI_GB() {
// Reset flash
display_Clear();
writeByte_GB(0x6000, 0); // Set ROM Mode
writeByte_GB(0x2000, 0); // Set Bank to 0
writeByte_GB(0x3000, 0);
startCFIMode(false); // Trying x8 mode first
display_Clear();
// Try x8 mode first
char cfiQRYx8[7];
char cfiQRYx16[7];
sprintf(cfiQRYx8, "%02X%02X%02X", readByte_GB(0x20), readByte_GB(0x22), readByte_GB(0x24));
sprintf(cfiQRYx16, "%02X%02X%02X", readByte_GB(0x10), readByte_GB(0x11), readByte_GB(0x12)); // some devices use x8-style CFI Query command even though they are in x16 command mode
if (strcmp(cfiQRYx8, "515259") == 0) { // QRY in x8 mode
println_Msg(F("Normal CFI x8 Mode"));
flashX16Mode = false;
flashSwitchLastBits = false;
} else if (strcmp(cfiQRYx8, "52515A") == 0) { // QRY in x8 mode with switched last bit
println_Msg(F("Switched CFI x8 Mode"));
flashX16Mode = false;
flashSwitchLastBits = true;
} else if (strcmp(cfiQRYx16, "515259") == 0) { // QRY in x16 mode
println_Msg(F("Normal CFI x16 Mode"));
flashX16Mode = true;
flashSwitchLastBits = false;
} else if (strcmp(cfiQRYx16, "52515A") == 0) { // QRY in x16 mode with switched last bit
println_Msg(F("Switched CFI x16 Mode"));
flashX16Mode = true;
flashSwitchLastBits = true;
} else {
startCFIMode(true); // Try x16 mode next
sprintf(cfiQRYx16, "%02X%02X%02X", readByte_GB(0x10), readByte_GB(0x11), readByte_GB(0x12));
if (strcmp(cfiQRYx16, "515259") == 0) { // QRY in x16 mode
println_Msg(F("Normal CFI x16 Mode"));
flashX16Mode = true;
flashSwitchLastBits = false;
} else if (strcmp(cfiQRYx16, "52515A") == 0) { // QRY in x16 mode with switched last bit
println_Msg(F("Switched CFI x16 Mode"));
flashX16Mode = true;
flashSwitchLastBits = true;
} else {
println_Msg(F("CFI Query failed!"));
display_Update();
wait();
return;
}
}
flashBanks = 1 << (readByteCompensated(0x4E) - 14); // - flashX16Mode);
// Reset flash
writeByteCompensated(0xAAA, 0xf0);
delay(100);
}
// Write 29F032 flashrom
// A0-A13 directly connected to cart edge -> 16384(0x0-0x3FFF) bytes per bank -> 256(0x0-0xFF) banks
// A14-A21 connected to MBC5
// identifyFlash_GB() needs to be run before this!
bool writeCFI_GB() {
// Create filepath
sprintf(filePath, "%s/%s", filePath, fileName);
// Open file on sd card
if (myFile.open(filePath, O_READ)) {
// Get rom size from file
myFile.seekCur(0x147);
romType = myFile.read();
romSize = myFile.read();
// Go back to file beginning
myFile.seekSet(0);
// 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;
}
if (romBanks <= flashBanks) {
print_Msg(F("Using "));
print_Msg(romBanks);
print_Msg(F("/"));
print_Msg(flashBanks);
println_Msg(F(" Banks"));
display_Update();
} else {
println_Msg(F("Error: Flash has too few banks!"));
print_Msg(F("Has "));
print_Msg(flashBanks);
println_Msg(F(" banks,"));
print_Msg(F("but needs "));
print_Msg(romBanks);
println_Msg(F("."));
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
resetArduino();
}
// Set ROM bank hi 0
writeByte_GB(0x3000, 0);
// Set ROM bank low 0
writeByte_GB(0x2000, 0);
delay(100);
// Reset flash
writeByteCompensated(0xAAA, 0xf0);
delay(100);
// Reset flash
writeByte_GB(0x555, 0xf0);
delay(100);
println_Msg(F("Erasing flash"));
display_Update();
// Erase flash
writeByteCompensated(0xAAA, 0xaa);
writeByteCompensated(0x555, 0x55);
writeByteCompensated(0xAAA, 0x80);
writeByteCompensated(0xAAA, 0xaa);
writeByteCompensated(0x555, 0x55);
writeByteCompensated(0xAAA, 0x10);
// Read the status register
byte statusReg = readByte_GB(0);
// After a completed erase D7 will output 1
while ((statusReg & 0x80) != 0x80) {
// Blink led
blinkLED();
delay(100);
// Update Status
statusReg = readByte_GB(0);
}
// Blankcheck
println_Msg(F("Blankcheck"));
display_Update();
// Read x number of banks
for (word currBank = 0; currBank < romBanks; currBank++) {
// Blink led
blinkLED();
// Set ROM bank
writeByte_GB(0x2000, currBank);
for (unsigned int currAddr = 0x4000; currAddr < 0x7FFF; currAddr += 512) {
for (int currByte = 0; currByte < 512; currByte++) {
sdBuffer[currByte] = readByte_GB(currAddr + currByte);
}
for (int j = 0; j < 512; j++) {
if (sdBuffer[j] != 0xFF) {
println_Msg(F("Not empty"));
print_FatalError(F("Erase failed"));
}
}
}
}
println_Msg(F("Writing flash MBC3/5"));
display_Update();
// Write flash
word currAddr = 0;
word endAddr = 0x3FFF;
for (word currBank = 0; currBank < romBanks; currBank++) {
// Blink led
blinkLED();
// Set ROM bank
writeByte_GB(0x2100, currBank);
// 0x2A8000 fix
writeByte_GB(0x4000, 0x0);
if (currBank > 0) {
currAddr = 0x4000;
endAddr = 0x7FFF;
}
while (currAddr <= endAddr) {
myFile.read(sdBuffer, 512);
for (int currByte = 0; currByte < 512; currByte++) {
// Write command sequence
writeByteCompensated(0xAAA, 0xaa);
writeByteCompensated(0x555, 0x55);
writeByteCompensated(0xAAA, 0xa0);
// Write current byte
writeByte_GB(currAddr + currByte, sdBuffer[currByte]);
// Setting CS(PH3) and OE/RD(PH6) LOW
PORTH &= ~((1 << 3) | (1 << 6));
// Busy check
short i = 0;
while ((PINC & 0x80) != (sdBuffer[currByte] & 0x80)) {
i++;
if (i > 500) {
if (currAddr < 0x4000) { // This happens when trying to flash an MBC5 as if it was an MBC3. Retry to flash as MBC5, starting from last successfull byte.
currByte--;
currAddr += 0x4000;
endAddr = 0x7FFF;
break;
} else { // If a timeout happens while trying to flash MBC5-style, flashing failed.
return false;
}
}
}
// Switch CS(PH3) and OE/RD(PH6) to HIGH
PORTH |= (1 << 3) | (1 << 6);
__asm__("nop\n\tnop\n\tnop\n\t"); // Waste a few CPU cycles to remove write errors
}
currAddr += 512;
}
}
display_Clear();
println_Msg(F("Verifying"));
display_Update();
// Go back to file beginning
myFile.seekSet(0);
//unsigned int addr = 0; // unused
writeErrors = 0;
// Verify flashrom
word romAddress = 0;
// Read number of banks and switch banks
for (word bank = 1; bank < romBanks; bank++) {
if (romType >= 5) { // MBC2 and above
writeByte_GB(0x2100, bank); // Set ROM bank
} else { // MBC1
writeByte_GB(0x6000, 0); // Set ROM Mode
writeByte_GB(0x4000, bank >> 5); // Set bits 5 & 6 (01100000) of ROM bank
writeByte_GB(0x2000, bank & 0x1F); // Set bits 0 & 4 (00011111) of ROM bank
}
if (bank > 1) {
romAddress = 0x4000;
}
// Blink led
blinkLED();
// Read up to 7FFF per bank
while (romAddress <= 0x7FFF) {
// Fill sdBuffer
myFile.read(sdBuffer, 512);
// Compare
for (int i = 0; i < 512; i++) {
if (readByte_GB(romAddress + i) != sdBuffer[i]) {
writeErrors++;
}
}
romAddress += 512;
}
}
// Close the file:
myFile.close();
if (writeErrors == 0) {
println_Msg(F("OK"));
display_Update();
} else {
print_STR(error_STR, 0);
print_Msg(writeErrors);
print_STR(_bytes_STR, 1);
print_Error(did_not_verify_STR);
}
} else {
print_STR(open_file_STR, 1);
display_Update();
}
return true;
}
/**************************************************
Pelican Gameboy Device Read Function
**************************************************/
// Read Pelican GBC Device - All Brainboys, MonsterBrains, Codebreakers
void readPelican_GB() {
// Get name, add extension and convert to char array for sd lib
strcpy(fileName, "Pelican");
strcat(fileName, ".GB");
// create a new folder for the rom file
EEPROM_readAnything(0, foldern);
sprintf(folder, "GB/ROM/Pelican/%d", foldern);
sd.mkdir(folder, true);
sd.chdir(folder);
display_Clear();
print_STR(saving_to_STR, 0);
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_FatalError(create_file_STR);
}
word finalAddress = 0x3FFF;
word startAddress= 0x2000;
word bankAddress = 0xA000;
//Enable bank addressing in the CPLD
readByte_GB(0x100);
// W29C020 ID command sequence
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xAA);
writeByteSRAM_GB(0xA000, 0x1);
writeByte_GB(0x2AAA, 0x55);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0x80);
writeByte_GB(0x3555, 0xAA);
writeByteSRAM_GB(0xA000, 0x1);
writeByte_GB(0x2AAA, 0x55);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0x60);
delay(10);
// Read the two id bytes into a string
writeByteSRAM_GB(0xA000, 0x0);
flashid = readByte_GB(0) << 8;
flashid |= readByte_GB(1);
// W29C020 Flash ID Mode Exit
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xAA);
writeByteSRAM_GB(0xA000, 0x1);
writeByte_GB(0x2AAA, 0x55);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xF0);
delay(100);
if (flashid == 0xDA45 || flashid == 0xBF10) {
println_Msg(F("29EE020 / W29C020"));
println_Msg(F("Banks Used: 32/64"));
println_Msg(F("Rom Size: 256 KB"));
romBanks = 32;
display_Update();
} else {
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xFF);
delay(100);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0x90);
delay(100);
flashid = readByte_GB(0) << 8;
flashid |= readByte_GB(1);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xFF);
delay(100);
if (flashid != 0xBF04) {
println_Msg(F("Unknown Flash ID"));
println_Msg(flashid);
print_STR(press_button_STR, 1);
display_Update();
wait();
mainMenu();
}
}
if (flashid == 0xBF04) {
println_Msg(F("SST 28LF040"));
println_Msg(F("Banks Used: 64/64"));
println_Msg(F("Rom Size: 512 KB"));
romBanks = 64;
display_Update();
}
// Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = (uint32_t)(romBanks)*8192;
draw_progressbar(0, totalProgressBar);
for (size_t workBank = 0; workBank < romBanks; workBank++) { // Loop over banks
startAddress = 0x2000;
writeByteSRAM_GB(bankAddress, (workBank & 0xFF));
// Read banks and save to SD
while (startAddress <= finalAddress) {
for (int i = 0; i < 512; i++) {
sdBuffer[i] = readByte_GB(startAddress + i);
}
myFile.write(sdBuffer, 512);
startAddress += 512;
processedProgressBar += 512;
draw_progressbar(processedProgressBar, totalProgressBar);
}
}
// Close the file:
myFile.close();
}
/******************************************
Pelican Gameboy Device Write Function
*****************************************/
// Write Pelican GBC Device - All Brainboys, MonsterBrains, Codebreakers
void writePelican_GB() {
// Launch filebrowser
filePath[0] = '\0';
sd.chdir("/");
fileBrowser(F("Select file"));
display_Clear();
// Create filepath
sprintf(filePath, "%s/%s", filePath, fileName);
// Open file on sd card
if (myFile.open(filePath, O_READ)) {
// Enable Rom Banks
readByte_GB(0x100);
delay(100);
// W29C020 ID command sequence
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xAA);
writeByteSRAM_GB(0xA000, 0x1);
writeByte_GB(0x2AAA, 0x55);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0x80);
writeByte_GB(0x3555, 0xAA);
writeByteSRAM_GB(0xA000, 0x1);
writeByte_GB(0x2AAA, 0x55);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0x60);
delay(10);
// Read the two id bytes into a string
writeByteSRAM_GB(0xA000, 0x0);
flashid = readByte_GB(0) << 8;
flashid |= readByte_GB(1);
// W29C020 Flash ID Mode Exit
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xAA);
writeByteSRAM_GB(0xA000, 0x1);
writeByte_GB(0x2AAA, 0x55);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xF0);
delay(100);
if (flashid == 0xDA45 || flashid == 0xBF10) {
println_Msg(F("29EE020 / W29C020"));
println_Msg(F("Banks Used: 32/64"));
romBanks = 32;
display_Update();
println_Msg(F("Erasing flash..."));
display_Update();
if (flashid == 0xDA45) {
// Disable BootBlock
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xAA);
writeByteSRAM_GB(0xA000, 0x1);
writeByte_GB(0x2AAA, 0x55);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0x80);
writeByte_GB(0x3555, 0xAA);
writeByteSRAM_GB(0xA000, 0x1);
writeByte_GB(0x2AAA, 0x55);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0x40);
writeByteSRAM_GB(0xA000, 0x1);
writeByte_GB(0x2AAA, 0xAA);
delay(100);
}
// Erase flash
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xAA);
writeByteSRAM_GB(0xA000, 0x1);
writeByte_GB(0x2AAA, 0x55);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0x80);
writeByte_GB(0x3555, 0xAA);
writeByteSRAM_GB(0xA000, 0x1);
writeByte_GB(0x2AAA, 0x55);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0x10);
delay(1000);
} else {
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xFF);
delay(100);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0x90);
delay(100);
flashid = readByte_GB(0) << 8;
flashid |= readByte_GB(1);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xFF);
delay(100);
if (flashid != 0xBF04) {
println_Msg(F("Unknown Flash ID"));
println_Msg(flashid);
print_STR(press_button_STR, 1);
display_Update();
wait();
mainMenu();
}
}
if (flashid == 0xBF04) {
println_Msg(F("SST 28LF040"));
println_Msg(F("Banks Used: 64/64"));
romBanks = 64;
display_Update();
println_Msg(F("Erasing flash..."));
display_Update();
//Unprotect flash
writeByteSRAM_GB(0xA000, 0x0);
readByte_GB(0x3823);
readByte_GB(0x3820);
readByte_GB(0x3822);
readByte_GB(0x2418);
readByte_GB(0x241B);
readByte_GB(0x2419);
readByte_GB(0x241A);
delay(100);
//Erase flash
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0x30);
writeByte_GB(0x3555, 0x30);
delay(100);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xFF);
delay(100);
}
// Blankcheck
println_Msg(F("Blankcheck..."));
display_Update();
// Read x number of banks
for (word currBank = 0; currBank < romBanks; currBank++) {
// Blink led
blinkLED();
// Set ROM bank
writeByteSRAM_GB(0xA000, currBank);
for (word currAddr = 0x2000; currAddr < 0x4000; currAddr += 0x200) {
for (int currByte = 0; currByte < 512; currByte++) {
sdBuffer[currByte] = readByte_GB(currAddr + currByte);
}
for (int j = 0; j < 512; j++) {
if (sdBuffer[j] != 0xFF) {
println_Msg(F("Not empty"));
print_FatalError(F("Erase failed"));
}
}
}
}
}
println_Msg(F("Writing flash..."));
display_Update();
// Write flash
word currAddr = 0x2000;
word endAddr = 0x3FFF;
byte byte1;
byte byte2;
bool toggle = true;
//Unprotect flash
writeByteSRAM_GB(0xA000, 0x0);
readByte_GB(0x3823);
readByte_GB(0x3820);
readByte_GB(0x3822);
readByte_GB(0x2418);
readByte_GB(0x241B);
readByte_GB(0x2419);
readByte_GB(0x241A);
delay(100);
//Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = (uint32_t)(romBanks)*8192;
draw_progressbar(0, totalProgressBar);
for (word currBank = 0; currBank < romBanks; currBank++) {
// Blink led
blinkLED();
currAddr = 0x2000;
if (flashid == 0xDA45 || flashid == 0xBF10) {
while (currAddr <= endAddr) {
myFile.read(sdBuffer, 128);
// Write command sequence
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xAA);
writeByteSRAM_GB(0xA000, 0x1);
writeByte_GB(0x2AAA, 0x55);
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0xA0);
// Set ROM bank
writeByteSRAM_GB(0xA000, currBank);
for (int currByte = 0; currByte < 128; currByte++) {
// Write current byte
writeByte_GB(currAddr + currByte, sdBuffer[currByte]);
}
currAddr += 128;
processedProgressBar += 128;
draw_progressbar(processedProgressBar, totalProgressBar);
delay(10);
}
}
if (flashid == 0xBF04) {
while (currAddr <= endAddr) {
myFile.read(sdBuffer, 512);
for (int currByte = 0; currByte < 512; currByte++) {
toggle = true;
// Write current byte
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0x10);
writeByteSRAM_GB(0xA000, currBank);
writeByte_GB(currAddr + currByte, sdBuffer[currByte]);
while (toggle) {
byte1 = readByte_GB(currAddr + currByte);
byte2 = readByte_GB(currAddr + currByte);
toggle = isToggle(byte1, byte2);
}
byte1 = readByte_GB(currAddr + currByte);
if (byte1 != sdBuffer[currByte]) {
writeByteSRAM_GB(0xA000, 0x2);
writeByte_GB(0x3555, 0x10);
writeByteSRAM_GB(0xA000, currBank);
writeByte_GB(currAddr + currByte, sdBuffer[currByte]);
while (toggle) {
byte1 = readByte_GB(currAddr + currByte);
byte2 = readByte_GB(currAddr + currByte);
toggle = isToggle(byte1, byte2);
}
}
}
currAddr += 512;
processedProgressBar += 512;
draw_progressbar(processedProgressBar, totalProgressBar);
}
}
}
if (flashid == 0xBF04) {
//Protect flash
writeByteSRAM_GB(0xA000, 0x0);
readByte_GB(0x3823);
readByte_GB(0x3820);
readByte_GB(0x3822);
readByte_GB(0x2418);
readByte_GB(0x241B);
readByte_GB(0x2419);
readByte_GB(0x240A);
delay(100);
}
display_Clear();
print_STR(verifying_STR, 0);
display_Update();
// Go back to file beginning
myFile.seekSet(0);
//unsigned int addr = 0; // unused
writeErrors = 0;
// Verify flashrom
word romAddress = 0x2000;
// Read number of banks and switch banks
for (word bank = 0; bank < romBanks; bank++) {
writeByteSRAM_GB(0xA000, bank); // Set ROM bank
romAddress = 0x2000;
// Blink led
blinkLED();
// Read up to 3FFF per bank
while (romAddress < 0x4000) {
// Fill sdBuffer
myFile.read(sdBuffer, 512);
// Compare
for (int i = 0; i < 512; i++) {
if (readByte_GB(romAddress + i) != sdBuffer[i]) {
writeErrors++;
}
}
romAddress += 512;
}
}
// Close the file:
myFile.close();
if (writeErrors == 0) {
println_Msg(F("OK"));
println_Msg(F("Please turn off the power."));
display_Update();
} else {
println_Msg(F("Error"));
print_Msg(writeErrors);
print_STR(_bytes_STR, 1);
print_FatalError(did_not_verify_STR);
}
}
bool isToggle(byte byte1, byte byte2) {
// XOR the two bytes to get the bits that are different
byte difference = byte1 ^ byte2;
difference = difference & 0b00100000;
// Check if only the 6th bit is different
return difference == 0b00100000;
}
/******************************************************
Datel Mega Memory Card Gameboy Device Read Function
******************************************************/
// Read Mega Memory Card Rom and Save Backup Data
void readMegaMem_GB() {
// Dump the Rom
strcpy(fileName, "Rom");
strcat(fileName, ".GB");
// create a new folder for the rom file
EEPROM_readAnything(0, foldern);
sprintf(folder, "GB/ROM/MegaMem/%d", foldern);
sd.mkdir(folder, true);
sd.chdir(folder);
display_Clear();
print_STR(saving_to_STR, 0);
print_Msg(folder);
println_Msg(F("/..."));
println_Msg(F("Rom.GB"));
display_Update();
//open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(create_file_STR);
}
word finalAddress = 0x3FFF;
word startAddress= 0x0;
// Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = (uint32_t)16384;
draw_progressbar(0, totalProgressBar);
// Read banks and save to SD
while (startAddress <= finalAddress) {
for (int i = 0; i < 512; i++) {
sdBuffer[i] = readByte_GB(startAddress + i);
}
myFile.write(sdBuffer, 512);
startAddress += 512;
processedProgressBar += 512;
draw_progressbar(processedProgressBar, totalProgressBar);
}
// Close the file:
myFile.close();
// Dump the Save Data SST28LF040
strcpy(fileName, "SaveData");
strcat(fileName, ".bin");
display_Clear();
print_STR(saving_to_STR, 0);
print_Msg(folder);
println_Msg(F("/..."));
println_Msg(F("SaveData.bin"));
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_FatalError(create_file_STR);
}
finalAddress = 0x7FFF;
startAddress= 0x4000;
word bankAddress = 0x2000;
romBanks = 32;
// Initialize progress bar
processedProgressBar = 0;
totalProgressBar = (uint32_t)(romBanks)*8192;
draw_progressbar(0, totalProgressBar);
for (size_t workBank = 0; workBank < romBanks; workBank++) { // Loop over banks
startAddress = 0x4000;
writeByte_GB(bankAddress, (workBank & 0xFF));
// Read banks and save to SD
while (startAddress <= finalAddress) {
for (int i = 0; i < 512; i++) {
sdBuffer[i] = readByte_GB(startAddress + i);
}
myFile.write(sdBuffer, 512);
startAddress += 512;
processedProgressBar += 512;
draw_progressbar(processedProgressBar, totalProgressBar);
}
}
// Close the file:
myFile.close();
}
/*******************************************************
Datel Mega Memory Card Gameboy Device Write Function
*******************************************************/
// Read Mega Memory Card Rom and Save Backup Data
void writeMegaMem_GB() {
// Write Datel Mega Memory Card Save Storage Chip SST28LF040
filePath[0] = '\0';
sd.chdir("/");
fileBrowser(F("Select file"));
display_Clear();
// Create filepath
sprintf(filePath, "%s/%s", filePath, fileName);
// Open file on sd card
if (myFile.open(filePath, O_READ)) {
writeByte_GB(0x2000, 0x1);
writeByte_GB(0x5555, 0xFF);
delay(100);
writeByte_GB(0x2000, 0x1);
writeByte_GB(0x5555, 0x90);
delay(100);
writeByte_GB(0x2000, 0x0);
flashid = readByte_GB(0x4000) << 8;
flashid |= readByte_GB(0x4001);
writeByte_GB(0x2000, 0x1);
writeByte_GB(0x5555, 0xFF);
delay(100);
if (flashid != 0xBF04) {
println_Msg(F("Unknown Flash ID"));
println_Msg(flashid);
print_STR(press_button_STR, 1);
display_Update();
wait();
mainMenu();
}
}
if (flashid == 0xBF04) {
println_Msg(F("SST 28LF040"));
romBanks = 32;
display_Update();
println_Msg(F("Erasing flash..."));
display_Update();
//Unprotect flash
writeByte_GB(0x2000, 0x0);
readByte_GB(0x5823);
readByte_GB(0x5820);
readByte_GB(0x5822);
readByte_GB(0x4418);
readByte_GB(0x441B);
readByte_GB(0x4419);
readByte_GB(0x441A);
delay(100);
//Erase flash
writeByte_GB(0x2000, 0x1);
writeByte_GB(0x5555, 0x30);
writeByte_GB(0x5555, 0x30);
delay(100);
writeByte_GB(0x2000, 0x1);
writeByte_GB(0x5555, 0xFF);
delay(100);
}
// Blankcheck
println_Msg(F("Blankcheck..."));
display_Update();
// Read x number of banks
for (word currBank = 0; currBank < romBanks; currBank++) {
// Blink led
blinkLED();
// Set ROM bank
writeByte_GB(0x2000, currBank);
for (word currAddr = 0x4000; currAddr < 0x8000; currAddr += 0x200) {
for (int currByte = 0; currByte < 512; currByte++) {
sdBuffer[currByte] = readByte_GB(currAddr + currByte);
}
for (int j = 0; j < 512; j++) {
if (sdBuffer[j] != 0xFF) {
println_Msg(F("Not empty"));
print_FatalError(F("Erase failed"));
}
}
}
}
println_Msg(F("Writing flash..."));
display_Update();
// Write flash
word currAddr = 0x4000;
word endAddr = 0x7FFF;
byte byte1;
byte byte2;
bool toggle = true;
//Unprotect flash
writeByte_GB(0x2000, 0x0);
readByte_GB(0x5823);
readByte_GB(0x5820);
readByte_GB(0x5822);
readByte_GB(0x4418);
readByte_GB(0x441B);
readByte_GB(0x4419);
readByte_GB(0x441A);
delay(100);
//Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = (uint32_t)(romBanks)*8192;
draw_progressbar(0, totalProgressBar);
for (word currBank = 0; currBank < romBanks; currBank++) {
// Blink led
blinkLED();
currAddr = 0x4000;
if (flashid == 0xBF04) {
while (currAddr <= endAddr) {
myFile.read(sdBuffer, 512);
for (int currByte = 0; currByte < 512; currByte++) {
toggle = true;
// Write current byte
writeByte_GB(0x2000, 0x1);
writeByte_GB(0x5555, 0x10);
writeByte_GB(0x2000, currBank);
writeByte_GB(currAddr + currByte, sdBuffer[currByte]);
while (toggle) {
byte1 = readByte_GB(currAddr + currByte);
byte2 = readByte_GB(currAddr + currByte);
toggle = isToggle(byte1, byte2);
}
byte1 = readByte_GB(currAddr + currByte);
if (byte1 != sdBuffer[currByte]) {
writeByte_GB(0x2000, 0x1);
writeByte_GB(0x5555, 0x10);
writeByte_GB(0x2000, currBank);
writeByte_GB(currAddr + currByte, sdBuffer[currByte]);
while (toggle) {
byte1 = readByte_GB(currAddr + currByte);
byte2 = readByte_GB(currAddr + currByte);
toggle = isToggle(byte1, byte2);
}
}
}
currAddr += 512;
processedProgressBar += 512;
draw_progressbar(processedProgressBar, totalProgressBar);
}
}
}
if (flashid == 0xBF04) {
//Protect flash
writeByte_GB(0x2000, 0x0);
readByte_GB(0x5823);
readByte_GB(0x5820);
readByte_GB(0x5822);
readByte_GB(0x4418);
readByte_GB(0x441B);
readByte_GB(0x4419);
readByte_GB(0x440A);
delay(100);
}
display_Clear();
print_STR(verifying_STR, 0);
display_Update();
// Go back to file beginning
myFile.seekSet(0);
//unsigned int addr = 0; // unused
writeErrors = 0;
// Verify flashrom
word romAddress = 0x4000;
// Read number of banks and switch banks
for (word bank = 0; bank < romBanks; bank++) {
writeByte_GB(0x2000, bank); // Set ROM bank
romAddress = 0x4000;
// Blink led
blinkLED();
// Read up to 3FFF per bank
while (romAddress < 0x8000) {
// Fill sdBuffer
myFile.read(sdBuffer, 512);
// Compare
for (int i = 0; i < 512; i++) {
if (readByte_GB(romAddress + i) != sdBuffer[i]) {
writeErrors++;
}
}
romAddress += 512;
}
}
// Close the file:
myFile.close();
if (writeErrors == 0) {
println_Msg(F("OK"));
println_Msg(F("Please turn off the power."));
display_Update();
} else {
println_Msg(F("Error"));
print_Msg(writeErrors);
print_STR(_bytes_STR, 1);
print_FatalError(did_not_verify_STR);
}
}
/***************************************************
Datel GBC Gameshark Gameboy Device Read Function
***************************************************/
// Read Datel GBC Gameshark Device
void readGameshark_GB() {
// Get name, add extension and convert to char array for sd lib
strcpy(fileName, "Gameshark");
strcat(fileName, ".GB");
// create a new folder for the rom file
EEPROM_readAnything(0, foldern);
sprintf(folder, "GB/ROM/Gameshark/%d", foldern);
sd.mkdir(folder, true);
sd.chdir(folder);
display_Clear();
print_STR(saving_to_STR, 0);
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_FatalError(create_file_STR);
}
word finalAddress = 0x5FFF;
word startAddress= 0x4000;
word bankAddress = 0x7FE1;
romBanks = 16;
//Enable bank addressing in the CPLD
readByte_GB(0x101);
readByte_GB(0x108);
readByte_GB(0x101);
// SST 39SF010 ID command sequence
writeByte_GB(bankAddress, 0x2);
writeByte_GB(0x5555, 0xAA);
writeByte_GB(bankAddress, 0x1);
writeByte_GB(0x4AAA, 0x55);
writeByte_GB(bankAddress, 0x2);
writeByte_GB(0x5555, 0x90);
delay(10);
// Read the two id bytes into a string
writeByte_GB(bankAddress, 0x0);
flashid = readByte_GB(0x4000) << 8;
flashid |= readByte_GB(0x4001);
// SST 39SF010 Flash ID Mode Exit
writeByte_GB(bankAddress, 0x2);
writeByte_GB(0x5555, 0xAA);
writeByte_GB(bankAddress, 0x1);
writeByte_GB(0x4AAA, 0x55);
writeByte_GB(bankAddress, 0x2);
writeByte_GB(0x5555, 0xF0);
delay(100);
if (flashid == 0xBFB5) {
println_Msg(F("SST 39SF010"));
println_Msg(F("Rom Size: 128 KB"));
display_Update();
}
// Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = (uint32_t)(romBanks)*8192;
draw_progressbar(0, totalProgressBar);
for (size_t workBank = 0; workBank < romBanks; workBank++) { // Loop over banks
startAddress = 0x4000;
writeByte_GB(bankAddress, (workBank & 0xFF));
// Read banks and save to SD
while (startAddress <= finalAddress) {
for (int i = 0; i < 512; i++) {
sdBuffer[i] = readByte_GB(startAddress + i);
}
myFile.write(sdBuffer, 512);
startAddress += 512;
processedProgressBar += 512;
draw_progressbar(processedProgressBar, totalProgressBar);
}
}
// Close the file:
myFile.close();
}
/****************************************************
Datel GBC Gameshark Gameboy Device Write Function
****************************************************/
// Write Datel GBC Gameshark Device
void writeGameshark_GB() {
// Launch filebrowser
filePath[0] = '\0';
sd.chdir("/");
fileBrowser(F("Select file"));
display_Clear();
byte byte1;
bool toggle = true;
// Create filepath
sprintf(filePath, "%s/%s", filePath, fileName);
// Open file on sd card
if (myFile.open(filePath, O_READ)) {
// Enable Rom Banks
readByte_GB(0x101);
readByte_GB(0x108);
readByte_GB(0x101);
delay(100);
// SST 39SF010 ID command sequence
writeByte_GB(0x7FE1, 0x2);
writeByte_GB(0x5555, 0xAA);
writeByte_GB(0x7FE1, 0x1);
writeByte_GB(0x4AAA, 0x55);
writeByte_GB(0x7FE1, 0x2);
writeByte_GB(0x5555, 0x90);
delay(10);
// Read the two id bytes into a string
writeByte_GB(0x7FE1, 0x0);
flashid = readByte_GB(0x4000) << 8;
flashid |= readByte_GB(0x4001);
// SST 39SF010 Flash ID Mode Exit
writeByte_GB(0x7FE1, 0x2);
writeByte_GB(0x5555, 0xAA);
writeByte_GB(0x7FE1, 0x1);
writeByte_GB(0x4AAA, 0x55);
writeByte_GB(0x7FE1, 0x2);
writeByte_GB(0x5555, 0xF0);
if (flashid != 0xBFB5) {
println_Msg(F("Unknown Flash ID"));
println_Msg(flashid);
print_STR(press_button_STR, 1);
display_Update();
wait();
mainMenu();
}
}
if (flashid == 0xBFB5) {
println_Msg(F("SST 39SF010"));
romBanks = 16;
display_Update();
println_Msg(F("Erasing flash..."));
display_Update();
//Erase flash
writeByte_GB(0x7FE1, 0x2);
writeByte_GB(0x5555, 0xAA);
writeByte_GB(0x7FE1, 0x1);
writeByte_GB(0x4AAA, 0x55);
writeByte_GB(0x7FE1, 0x2);
writeByte_GB(0x5555, 0x80);
writeByte_GB(0x7FE1, 0x2);
writeByte_GB(0x5555, 0xAA);
writeByte_GB(0x7FE1, 0x1);
writeByte_GB(0x4AAA, 0x55);
writeByte_GB(0x7FE1, 0x2);
writeByte_GB(0x5555, 0x10);
delay(100);
}
// Blankcheck
println_Msg(F("Blankcheck..."));
display_Update();
// Read x number of banks
for (word currBank = 0; currBank < romBanks; currBank++) {
// Blink led
blinkLED();
// Set ROM bank
writeByte_GB(0x7FE1, currBank);
for (word currAddr = 0x4000; currAddr < 0x6000; currAddr += 0x200) {
for (int currByte = 0; currByte < 512; currByte++) {
sdBuffer[currByte] = readByte_GB(currAddr + currByte);
}
for (int j = 0; j < 512; j++) {
if (sdBuffer[j] != 0xFF) {
println_Msg(F("Not empty"));
print_FatalError(F("Erase failed"));
}
}
}
}
println_Msg(F("Writing flash..."));
display_Update();
// Write flash
word currAddr = 0x4000;
word endAddr = 0x5FFF;
//Initialize progress bar
uint32_t processedProgressBar = 0;
uint32_t totalProgressBar = (uint32_t)(romBanks)*8192;
draw_progressbar(0, totalProgressBar);
for (word currBank = 0; currBank < romBanks; currBank++) {
// Blink led
blinkLED();
currAddr = 0x4000;
while (currAddr <= endAddr) {
myFile.read(sdBuffer, 512);
for (int currByte = 0; currByte < 512; currByte++) {
// Write command sequence
writeByte_GB(0x7FE1, 0x2);
writeByte_GB(0x5555, 0xAA);
writeByte_GB(0x7FE1, 0x1);
writeByte_GB(0x4AAA, 0x55);
writeByte_GB(0x7FE1, 0x2);
writeByte_GB(0x5555, 0xA0);
// Set ROM bank
writeByte_GB(0x7FE1, currBank);
toggle = true;
// Write current byte
writeByte_GB(currAddr + currByte, sdBuffer[currByte]);
while (toggle) {
byte1 = readByte_GB(currAddr + currByte);
if (byte1 == sdBuffer[currByte]) {
toggle = false;
}
}
}
currAddr += 512;
processedProgressBar += 512;
draw_progressbar(processedProgressBar, totalProgressBar);
}
}
display_Clear();
print_STR(verifying_STR, 0);
display_Update();
// Go back to file beginning
myFile.seekSet(0);
//unsigned int addr = 0; // unused
writeErrors = 0;
// Verify flashrom
word romAddress = 0x4000;
// Read number of banks and switch banks
for (word bank = 0; bank < romBanks; bank++) {
writeByte_GB(0x7FE1, bank); // Set ROM bank
romAddress = 0x4000;
// Blink led
blinkLED();
// Read up to 1FFF per bank
while (romAddress < 0x6000) {
// Fill sdBuffer
myFile.read(sdBuffer, 512);
// Compare
for (int i = 0; i < 512; i++) {
if (readByte_GB(romAddress + i) != sdBuffer[i]) {
writeErrors++;
}
}
romAddress += 512;
}
}
// Close the file:
myFile.close();
if (writeErrors == 0) {
println_Msg(F("OK"));
println_Msg(F("Please turn off the power."));
display_Update();
} else {
println_Msg(F("Error"));
print_Msg(writeErrors);
print_STR(_bytes_STR, 1);
print_FatalError(did_not_verify_STR);
}
}
#endif
//******************************************
// End of File
//******************************************
|