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
|
# gameplaySP
#
# Copyright (C) 2006 Exophase <exophase@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
.align 4
.global mips_update_gba
.global mips_indirect_branch_arm
.global mips_indirect_branch_thumb
.global mips_indirect_branch_dual
.global execute_load_u8
.global execute_load_u16
.global execute_load_u32
.global execute_load_s8
.global execute_load_s16
.global execute_store_u8
.global execute_store_u16
.global execute_store_u32
.global execute_aligned_load32
.global execute_aligned_store32
.global execute_read_cpsr
.global execute_read_spsr
.global execute_swi
.global execute_spsr_restore
.global execute_store_cpsr
.global execute_store_spsr
.global execute_lsl_flags_reg
.global execute_lsr_flags_reg
.global execute_asr_flags_reg
.global execute_ror_flags_reg
.global execute_arm_translate
.global invalidate_icache_region
.global invalidate_all_cache
.global reg_check
.global memory_map_read
.global memory_map_write
.global reg
.extern reg
.extern spsr
# MIPS register layout:
# $0 - constant zero
# $1 - temporary
# $2 - temporary / return value
# $3 - ARM r0 (not saved)
# $4 - temporary / function argument 0
# $5 - temporary / function argument 1
# $6 - temporary / function argument 2
# $7 - ARM r1 (not saved)
# $8 - ARM r2 (not saved)
# $9 - ARM r3 (not saved)
# $10 - ARM r4 (not saved)
# $11 - ARM r5 (not saved)
# $12 - ARM r6 (not saved)
# $13 - ARM r7 (not saved)
# $14 - ARM r8 (not saved)
# $15 - ARM r9 (not saved)
# $16 - ARM machine state pointer (saved)
# $17 - cycle counter (saved)
# $18 - ARM r10 (saved)
# $19 - block start address (roughly r15) (saved)
# $20 - ARM negative register (saved)
# $21 - ARM zero register (saved)
# $22 - ARM carry register (saved)
# $23 - ARM overflow register (saved)
# $24 - ARM r11 (not saved)
# $25 - ARM r12 (not saved)
# $26 - kernel temporary 0
# $27 - kernel temporary 1
# $28 - ARM r13 (saved)
# $29 - stack pointer
# $30 - ARM r14 (saved)
# $31 - return address
.equ REG_R0, (0 * 4)
.equ REG_R1, (1 * 4)
.equ REG_R2, (2 * 4)
.equ REG_R3, (3 * 4)
.equ REG_R4, (4 * 4)
.equ REG_R5, (5 * 4)
.equ REG_R6, (6 * 4)
.equ REG_R7, (7 * 4)
.equ REG_R8, (8 * 4)
.equ REG_R9, (9 * 4)
.equ REG_R10, (10 * 4)
.equ REG_R11, (11 * 4)
.equ REG_R12, (12 * 4)
.equ REG_R13, (13 * 4)
.equ REG_R14, (14 * 4)
.equ REG_LR, (14 * 4)
.equ REG_PC, (15 * 4)
.equ REG_N_FLAG, (16 * 4)
.equ REG_Z_FLAG, (17 * 4)
.equ REG_C_FLAG, (18 * 4)
.equ REG_V_FLAG, (19 * 4)
.equ REG_CPSR, (20 * 4)
.equ REG_SAVE, (21 * 4)
.equ REG_SAVE2, (22 * 4)
.equ REG_SAVE3, (23 * 4)
.equ CPU_MODE, (29 * 4)
.equ CPU_HALT_STATE, (30 * 4)
.equ CHANGED_PC_STATUS, (31 * 4)
.equ GP_SAVE, (32 * 4)
.equ SUPERVISOR_LR, (reg_mode + (3 * (7 * 4)) + (6 * 4))
.equ SUPERVISOR_SPSR, (spsr + (3 * 4))
.set noat
.set noreorder
# make sure $16 has the register base for these macros
.macro collapse_flag flag_reg, shift
ins $2, $\flag_reg, \shift, 1 # insert flag into CPSR
.endm
.macro collapse_flags
lw $2, REG_CPSR($16) # load CPSR
andi $2, $2, 0xFF # isolate lower 8bits
collapse_flag 20, 31 # store flags
collapse_flag 21, 30
collapse_flag 22, 29
collapse_flag 23, 28
sw $2, REG_CPSR($16) # store CPSR
.endm
.macro extract_flag shift, flag_reg
ext $\flag_reg, $1, \shift, 1 # extract flag from CPSR
.endm
.macro extract_flags_body # extract flags from $1
extract_flag 31, 20 # load flags
extract_flag 30, 21
extract_flag 29, 22
extract_flag 28, 23
.endm
.macro extract_flags
lw $1, REG_CPSR($16) # load CPSR
extract_flags_body
.endm
.macro save_registers
sw $3, REG_R0($16)
sw $7, REG_R1($16)
sw $8, REG_R2($16)
sw $9, REG_R3($16)
sw $10, REG_R4($16)
sw $11, REG_R5($16)
sw $12, REG_R6($16)
sw $13, REG_R7($16)
sw $14, REG_R8($16)
sw $15, REG_R9($16)
sw $24, REG_R11($16)
sw $25, REG_R12($16)
sw $18, REG_R10($16)
sw $28, REG_R13($16)
sw $30, REG_R14($16)
lw $28, GP_SAVE($16)
.endm
.macro restore_registers
lw $3, REG_R0($16)
lw $7, REG_R1($16)
lw $8, REG_R2($16)
lw $9, REG_R3($16)
lw $10, REG_R4($16)
lw $11, REG_R5($16)
lw $12, REG_R6($16)
lw $13, REG_R7($16)
lw $14, REG_R8($16)
lw $15, REG_R9($16)
lw $24, REG_R11($16)
lw $25, REG_R12($16)
lw $18, REG_R10($16)
lw $28, REG_R13($16)
lw $30, REG_R14($16)
.endm
# Process a hardware event. Since an interrupt might be
# raised we have to check if the PC has changed.
# $4: next address
# $16: register base
# $17: cycle counter
.balign 64
mips_update_gba:
sw $4, REG_PC($16) # current PC = $4
addiu $sp, $sp, -4 # make room on the stack
sw $ra,($sp) # save return address
collapse_flags # update cpsr
save_registers # save registers
jal update_gba # process the next event
sw $0, CHANGED_PC_STATUS($16)
lw $ra, ($sp) # restore return address
addiu $sp, $sp, 4 # fix stack
lw $1, CHANGED_PC_STATUS($16)
bne $1, $0, lookup_pc
addu $17, $2, $0 # $17 = new cycle count (delay slot)
restore_registers
jr $ra # if not, go back to caller
nop
# Perform an indirect branch.
# $4: GBA address to branch to
mips_indirect_branch_arm:
save_registers
jal block_lookup_address_arm # $2 = MIPS address to jump to
nop
restore_registers
jr $2 # jump to it
nop
mips_indirect_branch_thumb:
save_registers
jal block_lookup_address_thumb # $2 = MIPS address to jump to
nop
restore_registers
jr $2 # jump to it
nop
mips_indirect_branch_dual:
save_registers
jal block_lookup_address_dual # $2 = MIPS address to jump to
nop
restore_registers
jr $2 # jump to it
nop
# $4: address to write to
# $5: current PC
# Will patch the return address with a call to the correct handler as
# listed in the given table.
# Value will be set to force_open if it's open
.macro patch_handler ftable, force_open
srl $1, $4, 24 # $1 = address region
sltu $2, $1, 0x0F # check if the value is open
bne $2, $0, 1f
sll $1, $1, 2 # make address word indexed (delay)
addiu $1, $0, (\force_open * 4)
1:
lui $2, %hi(\ftable)
addu $2, $2, $1
lw $2, %lo(\ftable)($2) # new function handler is in $2
srl $2, $2, 2 # remove lower two bits
lui $1, %hi(3 << 26) # $1 = 3 (JAL opcode)
ins $1, $2, 0, 26 # insert offset into jal
addiu $ra, $ra, -8 # rewind return address to function call
sw $1, ($ra) # modify to call new handler
cache 0x1a, ($ra) # hit writeback dcache line
cache 0x08, ($ra) # hit invalidate icache line
jr $ra # return
nop # wary of putting cache here
.endm
# Like the above, but will use the table of the proper alignment,
# The tables should be ordered by alignment
.macro patch_handler_align ftable, alignment
srl $1, $4, 24 # $1 = address region
sltu $2, $1, 0x0F # check if the value is open
bne $2, $0, 1f
sll $1, $1, 2 # make address word indexed (delay)
addiu $1, $0, 4 # force address to 0x1 (open)
1:
ins $1, $4, 6, \alignment # place alignment bits into offset
lui $2, %hi(\ftable)
addu $2, $2, $1
lw $2, %lo(\ftable)($2) # new function handler is in $2
srl $2, $2, 2 # remove lower two bits
lui $1, %hi(3 << 26) # $1 = 3 (JAL opcode)
ins $1, $2, 0, 26 # insert offset into jal
addiu $ra, $ra, -8 # rewind return address to function call
sw $1, ($ra) # modify to call new handler
cache 0x1a, ($ra) # hit writeback dcache line
cache 0x08, ($ra) # hit invalidate icache line
jr $ra # return
nop # wary of putting cache here
.endm
.macro region_check region, patch_handler
srl $1, $4, 24 # check upper 8bits of address
xor $1, $1, \region # see if it is the given region
bne $1, $0, \patch_handler # if not repatch/try again
.endm
.macro region_check_open patch_handler
srl $1, $4, 24 # check upper 8bits of address
sltiu $2, $1, 0x0F # true if it is a low address
addiu $1, $1, -1 # non-zero if it is not a low open
sltu $1, $0, $1 # true if lower bits != 1
and $1, $1, $2 # true if low address and not open
bne $1, $0, \patch_handler # if above is true, patch
.endm
.macro region_check_align region, align_bits, alignment, patch_handler
srl $1, $4, 24 # check upper 8bits of address
ins $1, $4, 8, \align_bits # look at lower bits of address too
# See if it is the given region and alignment
xori $1, $1, (\region | (\alignment << 8))
bne $1, $0, \patch_handler # if not repatch/try again
.endm
.macro region_check_open_align align_bits, alignment, patch_handler
srl $1, $4, 24 # check upper 8bits of address
sltiu $2, $1, 0x0F # true if it is a low address
addiu $1, $1, -1 # non-zero if it is not a low open
sltu $1, $0, $1 # true if $1 != 0
and $1, $1, $2 # true if low address and not open
ext $2, $4, 0, \align_bits # $2 = low bits of 4
xori $2, $2, \alignment # true if alignment doesn't match
or $1, $1, $2 # align failure will trigger too
bne $1, $0, \patch_handler # if above is true, patch
.endm
.macro ignore_region region, patch_handler
region_check \region, \patch_handler
nop
jr $ra
nop
.endm
.macro ignore_high patch_handler
srl $1, $4, 24 # check upper 8bits of address
sltiu $1, $1, 0x0F # see if it is not high
bne $1, $0, \patch_handler # if not repatch/try again
nop
jr $ra
nop
.endm
.macro translate_region_core base, size
lui $2, %hi(\base) # generate upper address
andi $4, $4, \size # generate offset
addu $2, $2, $4 # add ptr upper and offset
.endm
.macro translate_region region, patch_handler, base, size
region_check \region, \patch_handler
translate_region_core \base, \size
.endm
# I refuse to have > 80 char lines, and GAS has a problem with the param
# list spilling over (grumble)
.macro translate_region_align region, a_b, alignment, p_h, base, size
region_check_align \region, \a_b, \alignment, \p_h
translate_region_core \base, \size
.endm
.macro translate_region_ewram_core mask
lui $2, %hi(ewram + 0x8000) # generate upper address (delay)
andi $1, $4, \mask # generate 15bit offset
ext $4, $4, 15, 3 # isolate top 3 bits of offset
ins $1, $4, 16, 3 # reinsert into top 4 bits
addu $2, $2, $1
.endm
.macro translate_region_ewram patch_handler
region_check 2, \patch_handler
translate_region_ewram_core 0x7FFF
.endm
.macro translate_region_ewram_load_align align_bits, alignment, patch_handler
region_check_align 2, \align_bits, \alignment, \patch_handler
translate_region_ewram_core 0x7FFF
.endm
.macro translate_region_ewram_load_align16 align_bits, alignment, patch_handler
region_check_align 2, \align_bits, \alignment, \patch_handler
translate_region_ewram_core 0x7FFE
.endm
.macro translate_region_ewram_load_align32 align_bits, alignment, patch_handler
region_check_align 2, \align_bits, \alignment, \patch_handler
translate_region_ewram_core 0x7FFC
.endm
.macro translate_region_ewram_store_align16 patch_handler
region_check 2, \patch_handler
translate_region_ewram_core 0x7FFE
.endm
.macro translate_region_ewram_store_align32 patch_handler
region_check 2, \patch_handler
translate_region_ewram_core 0x7FFC
.endm
.macro translate_region_vram_core
addiu $2, $2, -3 # see if it's 3
ext $4, $4, 0, 17 # generate 17bit offset
bne $2, $0, 1f
lui $1, %hi(vram) # start loading vram address (delay)
addiu $4, $4, -0x8000 # move address into VRAM region
1:
addu $2, $1, $4 # $2 = (hi)vram + address
.endm
.macro translate_region_vram patch_handler
region_check 6, \patch_handler
ext $2, $4, 15, 2 # $2 = bits 15 and 16 of address (delay)
translate_region_vram_core
.endm
.macro translate_region_vram_load_align align_bits, alignment, patch_handler
region_check_align 6, \align_bits, \alignment, \patch_handler
ext $2, $4, 15, 2 # $2 = bits 15 and 16 of address (delay)
translate_region_vram_core
.endm
.macro translate_region_vram_load_align16 align_bits, alignment, patch_handler
region_check_align 6, \align_bits, \alignment, \patch_handler
ext $2, $4, 15, 2 # $2 = bits 15 and 16 of address (delay)
ins $4, $0, 0, 1 # mask out lower bit of address
translate_region_vram_core
.endm
.macro translate_region_vram_load_align32 align_bits, alignment, patch_handler
region_check_align 6, \align_bits, \alignment, \patch_handler
ext $2, $4, 15, 2 # $2 = bits 15 and 16 of address (delay)
ins $4, $0, 0, 2 # mask out lower two bits of address
translate_region_vram_core
.endm
.macro translate_region_vram_store_align16 patch_handler
region_check 6, \patch_handler
ext $2, $4, 15, 2 # $2 = bits 15 and 16 of address (delay)
ins $4, $0, 0, 1 # mask out lower bit of address
translate_region_vram_core
.endm
.macro translate_region_vram_store_align32 patch_handler
region_check 6, \patch_handler
ext $2, $4, 15, 2 # $2 = bits 15 and 16 of address (delay)
ins $4, $0, 0, 2 # mask out lower two bits of address
translate_region_vram_core
.endm
.macro translate_region_gamepak_core mask
srl $2, $4, 15 # $2 = page number of address (delay)
sll $2, $2, 2 # adjust to word index
addu $2, $2, $16 # $2 = memory_map_read[address >> 15]
lw $2, -32768($2)
bne $2, $0, 1f # if it's non-NULL continue
andi $1, $4, \mask # $1 = low 15bits of address (delay slot)
sw $ra, REG_SAVE2($16) # save return address
save_registers # save the registers
ext $4, $4, 15, 10 # $4 = (address >> 15) & 0x3FF
jal load_gamepak_page # get page in $2
sw $1, REG_SAVE($16) # save offset (delay)
lw $1, REG_SAVE($16) # restore offset (delay)
restore_registers # restore the other registers
lw $ra, REG_SAVE2($16) # restore return address
1:
addu $2, $2, $1 # add the memory map offset
.endm
.macro translate_region_gamepak region, patch_handler
region_check \region, \patch_handler
translate_region_gamepak_core 0x7FFF
.endm
.macro translate_region_gamepak_align region, a_b, alignment, patch_handler
region_check_align \region, \a_b, \alignment, \patch_handler
translate_region_gamepak_core 0x7FFF
.endm
.macro translate_region_gamepak_align16 region, a_b, alignment, patch_handler
region_check_align \region, \a_b, \alignment, \patch_handler
translate_region_gamepak_core 0x7FFE
.endm
.macro translate_region_gamepak_align32 region, a_b, alignment, patch_handler
region_check_align \region, \a_b, \alignment, \patch_handler
translate_region_gamepak_core 0x7FFC
.endm
.macro translate_region_gamepak_a region, patch_handler
region_check \region, \patch_handler
srl $2, $4, 15 # $2 = page number of address (delay)
sll $2, $2, 2 # adjust to word index
addu $2, $2, $16 # $2 = memory_map_read[address >> 15]
lw $2, -32768($2)
bne $2, $0, 1f # if it's non-NULL continue
andi $1, $4, 0x7FFF # $1 = low 15bits of address (delay slot)
sw $ra, REG_SAVE2($16) # save return address
sw $6, REG_SAVE3($16) # save a2
save_registers # save the registers
ext $4, $4, 15, 10 # $4 = (address >> 15) & 0x3FF
jal load_gamepak_page # get page in $2
sw $1, REG_SAVE($16) # save offset (delay)
lw $1, REG_SAVE($16) # restore offset (delay)
restore_registers # restore the other registers
lw $ra, REG_SAVE2($16) # restore return address
lw $6, REG_SAVE3($16) # restore a2
1:
addu $2, $2, $1 # add the memory map offset
.endm
.macro eeprom_load_a patch_handler
region_check 0xD, \patch_handler
sw $ra, REG_SAVE($16) # save the return address (delay)
sw $6, REG_SAVE2($16) # save a2
save_registers # save the registers
jal read_eeprom # get eeprom value in $2
nop
restore_registers # restore the other registers
lw $ra, REG_SAVE($16) # restore return address
jr $ra # return
lw $6, REG_SAVE2($16) # restore a2
.endm
.macro eeprom_load_core
sw $ra, REG_SAVE($16) # save the return address (delay)
save_registers # save the registers
jal read_eeprom # get eeprom value in $2
nop
restore_registers # restore the other registers
lw $ra, REG_SAVE($16) # restore return address
jr $ra # return
nop
.endm
.macro eeprom_load patch_handler
region_check 0xD, \patch_handler
eeprom_load_core
.endm
.macro eeprom_load_align align_bits, alignment, patch_handler
region_check_align 0xD, \align_bits, \alignment, \patch_handler
eeprom_load_core
.endm
.macro eeprom_load_align16 align_bits, alignment, patch_handler
eeprom_load_align \align_bits, \alignment, \patch_handler
.endm
.macro eeprom_load_align32 align_bits, alignment, patch_handler
eeprom_load_align \align_bits, \alignment, \patch_handler
.endm
.macro backup_load_core
save_registers # save the registers
jal read_backup # get backup value in $2
ext $4, $4, 0, 16 # address &= 0xFFFF
restore_registers # restore the other registers
lw $ra, REG_SAVE($16) # restore return address
jr $ra # return
.endm
.macro backup_load_a patch_handler
region_check 0xE, \patch_handler
sw $ra, REG_SAVE($16) # save return address (delay)
sw $6, REG_SAVE2($16) # save a2
save_registers # save the registers
jal read_backup # get backup value in $2
ext $4, $4, 0, 16 # address &= 0xFFFF
restore_registers # restore the other registers
lw $ra, REG_SAVE($16) # restore return address
jr $ra # return
lw $6, REG_SAVE2($16) # restore a2
.endm
.macro backup_load patch_handler
region_check 0xE, \patch_handler
sw $ra, REG_SAVE($16) # save the return address (delay)
backup_load_core
.endm
.macro backup_load_align align_bits, alignment, patch_handler
region_check_align 0xE, \align_bits, \alignment, \patch_handler
sw $ra, REG_SAVE($16) # save the return address (delay)
backup_load_core
.endm
.macro backup_load_align16 align_bits, alignment, patch_handler
region_check_align 0xE, \align_bits, \alignment, \patch_handler
sw $ra, REG_SAVE($16) # save the return address (delay)
ins $4, $0, 0, 1 # mask out lower bit
backup_load_core
.endm
.macro backup_load_align32 align_bits, alignment, patch_handler
region_check_align 0xE, \align_bits, \alignment, \patch_handler
sw $ra, REG_SAVE($16) # save the return address (delay)
ins $4, $0, 0, 2 # mask out lower two bits
backup_load_core
.endm
.macro open_load8_core
lw $2, REG_CPSR($16) # $2 = CPSR (delay)
andi $2, $2, 0x20 # test T bit
beq $2, $0, 1f # branch if ARM mode
andi $4, $4, 0x03 # isolate lower 3bits from address (delay)
andi $4, $4, 0x01 # in Thumb mode, isolate one more bit
1:
sw $ra, REG_SAVE($16) # save the return address (delay)
save_registers # save the registers
jal read_memory8 # get instruction at PC
addu $4, $5, $4 # a0 = PC + low bits of address
restore_registers # restore the other registers
lw $ra, REG_SAVE($16) # restore return address
jr $ra # return
.endm
.macro open_load8 patch_handler
region_check_open \patch_handler
open_load8_core
.endm
.macro open_load16_core
lw $2, REG_CPSR($16) # $2 = CPSR (delay)
andi $2, $2, 0x20 # test T bit
beq $2, $0, 1f # branch if ARM mode
andi $4, $4, 0x02 # isolate bit 1 from address (delay)
addu $4, $0, $0 # zero out address bit
1:
sw $ra, REG_SAVE($16) # save the return address (delay)
save_registers # save the registers
jal read_memory16 # get instruction at PC
addu $4, $5, $4 # a0 = PC + low bits of address
restore_registers # restore the other registers
lw $ra, REG_SAVE($16) # restore return address
jr $ra # return
.endm
.macro open_load16_align align_bits, alignment, patch_handler
region_check_open_align \align_bits, \alignment, \patch_handler
open_load16_core
.endm
.macro open_load16_align16 align_bits, alignment, patch_handler
open_load16_align \align_bits, \alignment, \patch_handler
.endm
.macro open_load32_core
lw $2, REG_CPSR($16) # $2 = CPSR (delay)
andi $2, $2, 0x20 # test T bit
save_registers # save the registers
beq $2, $0, 1f # branch if ARM mode
sw $ra, REG_SAVE($16) # save the return address (delay)
jal read_memory16 # get instruction at PC
addu $4, $5, $0 # a0 = PC
j 2f
ins $2, $2, 16, 16 # result = (result << 16) | result (delay)
1:
jal read_memory32 # get instruction at PC
addu $4, $5, $4 # a0 = PC
2: # join point
restore_registers # restore the other registers
lw $ra, REG_SAVE($16) # restore return address
jr $ra # return
.endm
.macro open_load32_a patch_handler
region_check_open \patch_handler
lw $2, REG_CPSR($16) # $2 = CPSR (delay)
andi $2, $2, 0x20 # test T bit
save_registers # save the registers
sw $6, REG_SAVE2($16) # save a2
beq $2, $0, 1f # branch if ARM mode
sw $ra, REG_SAVE($16) # save the return address (delay)
jal read_memory16 # get instruction at PC
addu $4, $5, $0 # a0 = PC
j 2f
ins $2, $2, 16, 16 # result = (result << 16) | result (delay)
1:
jal read_memory32 # get instruction at PC
addu $4, $5, $4 # a0 = PC
2:
restore_registers # restore the other registers
lw $ra, REG_SAVE($16) # restore return address
jr $ra # return
lw $6, REG_SAVE2($16) # restore a2 (delay)
.endm
.macro open_load32_align align_bits, alignment, patch_handler
region_check_open_align \align_bits, \alignment, \patch_handler
open_load32_core
.endm
.macro open_load32_align32 align_bits, alignment, patch_handler
open_load32_align \align_bits, \alignment, \patch_handler
.endm
.macro store_function function, region, patch_handler, mask
region_check \region, \patch_handler
sw $ra, REG_SAVE($16) # save the return address (delay)
save_registers # save the registers
jal \function # store value out
andi $4, $4, \mask # mask address
restore_registers # restore the other registers
lw $ra, REG_SAVE($16) # restore return address
jr $ra # return
nop
.endm
.macro store_function_a function, region, patch_handler, mask
region_check \region, \patch_handler
sw $ra, REG_SAVE($16) # save the return address (delay)
save_registers # save the registers
jal \function # store value out
andi $4, $4, \mask # mask address
restore_registers # restore the other registers
lw $ra, REG_SAVE($16) # restore return address
jr $ra # return
nop
.endm
.macro load_u8 base
jr $ra # return
lbu $2, %lo(\base)($2) # return base[offset]
.endm
.macro load_s8 base
jr $ra # return
lb $2, %lo(\base)($2) # return base[offset]
.endm
.macro load_u16 base
jr $ra # return
lhu $2, %lo(\base)($2) # return base[offset]
.endm
.macro load_s16 base
jr $ra # return
lh $2, %lo(\base)($2) # return base[offset]
.endm
.macro load_u32 base
jr $ra # return
lw $2, %lo(\base)($2) # return base[offset]
.endm
# 16bit unaligned load will always have a 1 in the LSB;
# should have already been taken care of in indexing.
.macro load_u16_unaligned base
lhu $2, %lo(\base)($2) # load base[offset]
jr $ra # return
ror $2, $2, 8 # rotate value by 8bits
.endm
# This is technically the same as load_s8, but kept to
# avoid confusion.
.macro load_s16_unaligned base
jr $ra # return
lb $2, %lo(\base)($2) # return base[offset]
.endm
# Unalignment must be known statically (use the tables to
# patch correctly)
.macro load_u32_unaligned base, alignment
lw $2, %lo(\base)($2) # load base[offset]
jr $ra # return
ror $2, $2, (\alignment * 8) # rotate value by 8bits
.endm
.macro store_u8 base
jr $ra # return
sb $5, %lo(\base)($2) # store value at base[offset]
.endm
.macro store_u16 base
jr $ra # return
sh $5, %lo(\base)($2) # store value at base[offset]
.endm
.macro store_u32 base
jr $ra # return
sw $5, %lo(\base)($2) # store value at base[offset]
.endm
# Store the value double mirrored (u16)
.macro store_u8_double base
ins $5, $5, 8, 8 # value = (value << 8) | value
jr $ra # return
sh $5, %lo(\base)($2) # store value at base[offset]
.endm
# Store the values and check if it overwrote code there
.macro store_u8_smc base
addiu $2, $2, %lo(\base) # offset the address
lb $1, -32768($2) # load the SMC status
bne $1, $0, smc_write # is there code there?
sb $5, ($2) # store value at base[offset] (delay)
jr $ra # return
nop
.endm
.macro store_u16_smc base
addiu $2, $2, %lo(\base) # offset the address
lh $1, -32768($2) # load the SMC status
bne $1, $0, smc_write # is there code there?
sh $5, ($2) # store value at base[offset] (delay)
jr $ra # return
nop
.endm
.macro store_u32_smc base
addiu $2, $2, %lo(\base) # offset the address
lw $1, -32768($2) # load the SMC status
bne $1, $0, smc_write # is there code there?
sw $5, ($2) # store value at base[offset] (delay)
jr $ra # return
nop
.endm
# Unsigned 8bit load handlers
execute_load_bios_u8:
region_check 0, patch_load_u8
srl $2, $4, 14 # check if address is in BIOS region
bne $2, $0, 2f # if not, perform open read
srl $1, $5, 14 # check if PC is in BIOS region
bne $1, $0, 1f # if not, perform BIOS protected read
lui $2, %hi(bios_rom) # generate upper address (delay)
andi $4, $4, 0x3FFF # generate offset
addu $2, $2, $4
load_u8 bios_rom
1:
lui $2, %hi(bios_read_protect) # generate upper address
ins $2, $4, 0, 2 # lower 2 bits address contributes
load_u8 bios_read_protect
2:
open_load8_core
nop
execute_load_ewram_u8:
translate_region_ewram patch_load_u8
load_u8 (ewram + 0x8000)
# Put the generic address over the handler you want to be default
# IWRAM is typically the most frequently read and written to.
execute_load_u8:
execute_load_iwram_u8:
translate_region 3, patch_load_u8, (iwram + 0x8000), 0x7FFF
load_u8 (iwram + 0x8000)
execute_load_io_u8:
translate_region 4, patch_load_u8, io_registers, 0x3FF
load_u8 io_registers
execute_load_palette_u8:
translate_region 5, patch_load_u8, palette_ram, 0x3FF
load_u8 palette_ram
execute_load_vram_u8:
translate_region_vram patch_load_u8
load_u8 vram
execute_load_oam_u8:
translate_region 7, patch_load_u8, oam_ram, 0x3FF
load_u8 oam_ram
execute_load_gamepak8_u8:
translate_region_gamepak 8, patch_load_u8
load_u8 0
execute_load_gamepak9_u8:
translate_region_gamepak 9, patch_load_u8
load_u8 0
execute_load_gamepakA_u8:
translate_region_gamepak 10, patch_load_u8
load_u8 0
execute_load_gamepakB_u8:
translate_region_gamepak 11, patch_load_u8
load_u8 0
execute_load_gamepakC_u8:
translate_region_gamepak 12, patch_load_u8
load_u8 0
execute_load_eeprom_u8:
eeprom_load patch_load_u8
execute_load_backup_u8:
backup_load patch_load_u8
nop
execute_load_open_u8:
open_load8 patch_load_u8
nop
load_u8_ftable:
.long execute_load_bios_u8 # 0x00 BIOS
.long execute_load_open_u8 # 0x01 open address
.long execute_load_ewram_u8 # 0x02 EWRAM
.long execute_load_iwram_u8 # 0x03 IWRAM
.long execute_load_io_u8 # 0x04 I/O registers
.long execute_load_palette_u8 # 0x05 Palette RAM
.long execute_load_vram_u8 # 0x06 VRAM
.long execute_load_oam_u8 # 0x07 OAM RAM
.long execute_load_gamepak8_u8 # 0x08 gamepak
.long execute_load_gamepak9_u8 # 0x09 gamepak
.long execute_load_gamepakA_u8 # 0x0A gamepak
.long execute_load_gamepakB_u8 # 0x0B gamepak
.long execute_load_gamepakC_u8 # 0x0C gamepak
.long execute_load_eeprom_u8 # 0x0D gamepak/eeprom
.long execute_load_backup_u8 # 0x0E Flash ROM/SRAM
.long execute_load_open_u8 # 0x0F open address
patch_load_u8:
patch_handler load_u8_ftable, 0x01
# Signed 8bit load handlers
execute_load_bios_s8:
region_check 0, patch_load_s8
srl $2, $4, 14 # check if address is in BIOS region
bne $2, $0, 2f
srl $1, $5, 14 # check if PC is in BIOS region
bne $1, $0, 1f # if not, perform BIOS protected read
lui $2, %hi(bios_rom) # generate upper address (delay)
andi $4, $4, 0x3FFF # generate offset
addu $2, $2, $4
load_s8 bios_rom
1:
lui $2, %hi(bios_read_protect) # generate upper address
ins $2, $4, 0, 2 # lower 2 bits contribute
load_s8 bios_read_protect
2:
open_load8_core
seb $2, $2
execute_load_ewram_s8:
translate_region_ewram patch_load_s8
load_s8 (ewram + 0x8000)
execute_load_s8:
execute_load_iwram_s8:
translate_region 3, patch_load_s8, (iwram + 0x8000), 0x7FFF
load_s8 (iwram + 0x8000)
execute_load_io_s8:
translate_region 4, patch_load_s8, io_registers, 0x3FF
load_s8 io_registers
execute_load_palette_s8:
translate_region 5, patch_load_s8, palette_ram, 0x3FF
load_s8 palette_ram
execute_load_vram_s8:
translate_region_vram patch_load_s8
load_s8 vram
execute_load_oam_s8:
translate_region 7, patch_load_s8, oam_ram, 0x3FF
load_s8 oam_ram
execute_load_gamepak8_s8:
translate_region_gamepak 8, patch_load_s8
load_s8 0
execute_load_gamepak9_s8:
translate_region_gamepak 9, patch_load_s8
load_s8 0
execute_load_gamepakA_s8:
translate_region_gamepak 10, patch_load_s8
load_s8 0
execute_load_gamepakB_s8:
translate_region_gamepak 11, patch_load_s8
load_s8 0
execute_load_gamepakC_s8:
translate_region_gamepak 12, patch_load_s8
load_s8 0
execute_load_eeprom_s8:
eeprom_load patch_load_s8
execute_load_backup_s8:
backup_load patch_load_s8
seb $2, $2 # sign extend result (delay)
execute_load_open_s8:
open_load8 patch_load_s8
seb $2, $2 # sign extend result (delay)
load_s8_ftable:
.long execute_load_bios_s8 # 0x00 BIOS
.long execute_load_open_s8 # 0x01 open address
.long execute_load_ewram_s8 # 0x02 EWRAM
.long execute_load_iwram_s8 # 0x03 IWRAM
.long execute_load_io_s8 # 0x04 I/O registers
.long execute_load_palette_s8 # 0x05 Palette RAM
.long execute_load_vram_s8 # 0x06 VRAM
.long execute_load_oam_s8 # 0x07 OAM RAM
.long execute_load_gamepak8_s8 # 0x08 gamepak
.long execute_load_gamepak9_s8 # 0x09 gamepak
.long execute_load_gamepakA_s8 # 0x0A gamepak
.long execute_load_gamepakB_s8 # 0x0B gamepak
.long execute_load_gamepakC_s8 # 0x0C gamepak
.long execute_load_eeprom_s8 # 0x0D gamepak/eeprom
.long execute_load_backup_s8 # 0x0E Flash ROM/SRAM
.long execute_load_open_s8 # 0x0F open address
patch_load_s8:
patch_handler load_s8_ftable, 1
# Unsigned aligned 16bit load handlers
execute_load_bios_u16:
region_check_align 0, 1, 0, patch_load_u16
srl $2, $4, 14 # check if address is in BIOS region
bne $2, $0, 2f # if not, perform open read
srl $1, $5, 14 # check if PC is in BIOS region
bne $1, $0, 1f # if not, perform BIOS protected read
lui $2, %hi(bios_rom) # generate upper address (delay)
andi $4, $4, 0x3FFF # generate offset
addu $2, $2, $4
load_u16 bios_rom
1:
lui $2, %hi(bios_read_protect) # generate upper address
ins $2, $4, 0, 2 # bit 1 contributes
load_u16 bios_read_protect
2:
open_load16_core
nop
execute_load_ewram_u16:
translate_region_ewram_load_align 1, 0, patch_load_u16
load_u16 (ewram + 0x8000)
execute_load_u16:
execute_load_iwram_u16:
translate_region_align 3, 1, 0, patch_load_u16, (iwram + 0x8000), 0x7FFF
load_u16 (iwram + 0x8000)
execute_load_io_u16:
translate_region_align 4, 1, 0, patch_load_u16, io_registers, 0x3FF
load_u16 io_registers
execute_load_palette_u16:
translate_region_align 5, 1, 0, patch_load_u16, palette_ram, 0x3FF
load_u16 palette_ram
execute_load_vram_u16:
translate_region_vram_load_align 1, 0, patch_load_u16
load_u16 vram
execute_load_oam_u16:
translate_region_align 7, 1, 0, patch_load_u16, oam_ram, 0x3FF
load_u16 oam_ram
execute_load_gamepak8_u16:
translate_region_gamepak_align 8, 1, 0, patch_load_u16
load_u16 0
execute_load_gamepak9_u16:
translate_region_gamepak_align 9, 1, 0, patch_load_u16
load_u16 0
execute_load_gamepakA_u16:
translate_region_gamepak_align 10, 1, 0, patch_load_u16
load_u16 0
execute_load_gamepakB_u16:
translate_region_gamepak_align 11, 1, 0, patch_load_u16
load_u16 0
execute_load_gamepakC_u16:
translate_region_gamepak_align 12, 1, 0, patch_load_u16
load_u16 0
execute_load_eeprom_u16:
eeprom_load_align 1, 0, patch_load_u16
execute_load_backup_u16:
backup_load_align 1, 0, patch_load_u16
nop
execute_load_open_u16:
open_load16_align 1, 0, patch_load_u16
nop
# Unsigned unaligned 16bit load handlers
execute_load_bios_u16u:
region_check_align 0, 1, 1, patch_load_u16
srl $2, $4, 14 # check if address is in BIOS region
bne $2, $0, 2f # if not, perform open read
srl $1, $5, 14 # check if PC is in BIOS region
bne $1, $0, 1f # if not, perform BIOS protected read
lui $2, %hi(bios_rom) # generate upper address (delay)
andi $4, $4, 0x3FFE # generate offset
addu $2, $2, $4
load_u16_unaligned bios_rom
1:
lui $2, %hi(bios_read_protect) # generate upper address
ext $1, $4, 1, 1
ins $2, $1, 1, 1 # bit 1 contributes
load_u16_unaligned bios_read_protect
2:
open_load16_core
ror $2, $2, 8
execute_load_ewram_u16u:
translate_region_ewram_load_align16 1, 1, patch_load_u16
load_u16_unaligned (ewram + 0x8000)
execute_load_iwram_u16u:
translate_region_align 3, 1, 1, patch_load_u16, (iwram + 0x8000), 0x7FFE
load_u16_unaligned (iwram + 0x8000)
execute_load_io_u16u:
translate_region_align 4, 1, 1, patch_load_u16, io_registers, 0x3FE
load_u16_unaligned io_registers
execute_load_palette_u16u:
translate_region_align 5, 1, 1, patch_load_u16, palette_ram, 0x3FE
load_u16_unaligned palette_ram
execute_load_vram_u16u:
translate_region_vram_load_align16 1, 1, patch_load_u16
load_u16_unaligned vram
execute_load_oam_u16u:
translate_region_align 7, 1, 1, patch_load_u16, oam_ram, 0x3FE
load_u16_unaligned oam_ram
execute_load_gamepak8_u16u:
translate_region_gamepak_align16 8, 1, 1, patch_load_u16
load_u16_unaligned 0
execute_load_gamepak9_u16u:
translate_region_gamepak_align16 9, 1, 1, patch_load_u16
load_u16_unaligned 0
execute_load_gamepakA_u16u:
translate_region_gamepak_align16 10, 1, 1, patch_load_u16
load_u16_unaligned 0
execute_load_gamepakB_u16u:
translate_region_gamepak_align16 11, 1, 1, patch_load_u16
load_u16_unaligned 0
execute_load_gamepakC_u16u:
translate_region_gamepak_align16 12, 1, 1, patch_load_u16
load_u16_unaligned 0
execute_load_eeprom_u16u:
eeprom_load_align16 1, 1, patch_load_u16
execute_load_backup_u16u:
backup_load_align16 1, 1, patch_load_u16
ror $2, $2, 8 # rotate value by 8bits
execute_load_open_u16u:
open_load16_align16 1, 1, patch_load_u16
ror $2, $2, 8 # rotate value by 8bits
load_u16_ftable:
# .long execute_load_full_u16
.long execute_load_bios_u16 # 0x00 BIOS
.long execute_load_open_u16 # 0x01 open address
.long execute_load_ewram_u16 # 0x02 EWRAM
.long execute_load_iwram_u16 # 0x03 IWRAM
.long execute_load_io_u16 # 0x04 I/O registers
.long execute_load_palette_u16 # 0x05 Palette RAM
.long execute_load_vram_u16 # 0x06 VRAM
.long execute_load_oam_u16 # 0x07 OAM RAM
.long execute_load_gamepak8_u16 # 0x08 gamepak
.long execute_load_gamepak9_u16 # 0x09 gamepak
.long execute_load_gamepakA_u16 # 0x0A gamepak
.long execute_load_gamepakB_u16 # 0x0B gamepak
.long execute_load_gamepakC_u16 # 0x0C gamepak
.long execute_load_eeprom_u16 # 0x0D gamepak/eeprom
.long execute_load_backup_u16 # 0x0E Flash ROM/SRAM
.long execute_load_open_u16 # 0x0F open
.long execute_load_bios_u16u # 0x00 BIOS unaligned
.long execute_load_open_u16u # 0x01 open address unaligned
.long execute_load_ewram_u16u # 0x02 EWRAM unaligned
.long execute_load_iwram_u16u # 0x03 IWRAM unaligned
.long execute_load_io_u16u # 0x04 I/O registers unaligned
.long execute_load_palette_u16u # 0x05 Palette RAM unaligned
.long execute_load_vram_u16u # 0x06 VRAM unaligned
.long execute_load_oam_u16u # 0x07 OAM RAM unaligned
.long execute_load_gamepak8_u16u# 0x08 gamepak unaligned
.long execute_load_gamepak9_u16u# 0x09 gamepak unaligned
.long execute_load_gamepakA_u16u# 0x0A gamepak unaligned
.long execute_load_gamepakB_u16u# 0x0B gamepak unaligned
.long execute_load_gamepakC_u16u# 0x0C gamepak unaligned
.long execute_load_eeprom_u16u # 0x0D gamepak/eeprom unaligned
.long execute_load_backup_u16u # 0x0E Flash ROM/SRAM unaligned
.long execute_load_open_u16u # 0x0F open unaligned
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
.long execute_load_full_u16
patch_load_u16:
patch_handler_align load_u16_ftable, 1
# Signed aligned 16bit load handlers
execute_load_bios_s16:
region_check_align 0, 1, 0, patch_load_s16
srl $2, $4, 14 # check if address is in BIOS region
bne $2, $0, 2f # if not, perform open read
srl $1, $5, 14 # check if PC is in BIOS region
bne $1, $0, 1f # if not, perform BIOS protected read
lui $2, %hi(bios_rom) # generate upper address (delay)
andi $4, $4, 0x3FFF # generate offset
addu $2, $2, $4
load_s16 bios_rom
1:
lui $2, %hi(bios_read_protect) # generate upper address
ins $2, $4, 0, 2 # bit 1 contributes
load_s16 bios_read_protect
2:
open_load16_core
seh $2, $2
execute_load_ewram_s16:
translate_region_ewram_load_align 1, 0, patch_load_s16
load_s16 (ewram + 0x8000)
execute_load_s16:
execute_load_iwram_s16:
translate_region_align 3, 1, 0, patch_load_s16, (iwram + 0x8000), 0x7FFF
load_s16 (iwram + 0x8000)
execute_load_io_s16:
translate_region_align 4, 1, 0, patch_load_s16, io_registers, 0x3FF
load_s16 io_registers
execute_load_palette_s16:
translate_region_align 5, 1, 0, patch_load_s16, palette_ram, 0x3FF
load_s16 palette_ram
execute_load_vram_s16:
translate_region_vram_load_align 1, 0, patch_load_s16
load_s16 vram
execute_load_oam_s16:
translate_region_align 7, 1, 0, patch_load_s16, oam_ram, 0x3FF
load_s16 oam_ram
execute_load_gamepak8_s16:
translate_region_gamepak_align 8, 1, 0, patch_load_s16
load_s16 0
execute_load_gamepak9_s16:
translate_region_gamepak_align 9, 1, 0, patch_load_s16
load_s16 0
execute_load_gamepakA_s16:
translate_region_gamepak_align 10, 1, 0, patch_load_s16
load_s16 0
execute_load_gamepakB_s16:
translate_region_gamepak_align 11, 1, 0, patch_load_s16
load_s16 0
execute_load_gamepakC_s16:
translate_region_gamepak_align 12, 1, 0, patch_load_s16
load_s16 0
execute_load_eeprom_s16:
eeprom_load_align 1, 0, patch_load_s16
execute_load_backup_s16:
backup_load_align 1, 0, patch_load_s16
nop
execute_load_open_s16:
open_load16_align 1, 0, patch_load_s16
nop
# Signed unaligned 16bit load handlers
execute_load_bios_s16u:
region_check_align 0, 1, 1, patch_load_s16
srl $2, $4, 14 # check if address is in BIOS region
bne $2, $0, 2f # if not, perform open read
srl $1, $5, 14 # check if PC is in BIOS region
bne $1, $0, 1f # if not, perform BIOS protected read
lui $2, %hi(bios_rom) # generate upper address (delay)
andi $4, $4, 0x3FFE # generate offset
addu $2, $1, $4
load_s16_unaligned bios_rom
1:
lui $2, %hi(bios_read_protect) # generate upper address
ext $1, $4, 1, 1
ins $2, $1, 1, 1 # bit 1 contributes
load_s16_unaligned bios_read_protect
2:
open_load16_core
seb $2, $2
execute_load_ewram_s16u:
translate_region_ewram_load_align16 1, 1, patch_load_s16
load_s16_unaligned (ewram + 0x8000)
execute_load_iwram_s16u:
translate_region_align 3, 1, 1, patch_load_s16, (iwram + 0x8000), 0x7FFE
load_s16_unaligned (iwram + 0x8000)
execute_load_io_s16u:
translate_region_align 4, 1, 1, patch_load_s16, io_registers, 0x3FE
load_s16_unaligned io_registers
execute_load_palette_s16u:
translate_region_align 5, 1, 1, patch_load_s16, palette_ram, 0x3FE
load_s16_unaligned palette_ram
execute_load_vram_s16u:
translate_region_vram_load_align16 1, 1, patch_load_s16
load_s16_unaligned vram
execute_load_oam_s16u:
translate_region_align 7, 1, 1, patch_load_s16, oam_ram, 0x3FE
load_s16_unaligned oam_ram
execute_load_gamepak8_s16u:
translate_region_gamepak_align16 8, 1, 1, patch_load_s16
load_s16_unaligned 0
execute_load_gamepak9_s16u:
translate_region_gamepak_align16 9, 1, 1, patch_load_s16
load_s16_unaligned 0
execute_load_gamepakA_s16u:
translate_region_gamepak_align16 10, 1, 1, patch_load_s16
load_s16_unaligned 0
execute_load_gamepakB_s16u:
translate_region_gamepak_align16 11, 1, 1, patch_load_s16
load_s16_unaligned 0
execute_load_gamepakC_s16u:
translate_region_gamepak_align16 12, 1, 1, patch_load_s16
load_s16_unaligned 0
execute_load_eeprom_s16u:
eeprom_load_align 1, 1, patch_load_s16
execute_load_backup_s16u:
backup_load_align 1, 1, patch_load_s16
seb $2, $2 # sign extend result from 8bits
execute_load_open_s16u:
open_load16_align 1, 1, patch_load_s16
seb $2, $2 # sign extend result from 8bits
load_s16_ftable:
.long execute_load_bios_s16 # 0x00 BIOS
.long execute_load_open_s16 # 0x01 open address
.long execute_load_ewram_s16 # 0x02 EWRAM
.long execute_load_iwram_s16 # 0x03 IWRAM
.long execute_load_io_s16 # 0x04 I/O registers
.long execute_load_palette_s16 # 0x05 Palette RAM
.long execute_load_vram_s16 # 0x06 VRAM
.long execute_load_oam_s16 # 0x07 OAM RAM
.long execute_load_gamepak8_s16 # 0x08 gamepak
.long execute_load_gamepak9_s16 # 0x09 gamepak
.long execute_load_gamepakA_s16 # 0x0A gamepak
.long execute_load_gamepakB_s16 # 0x0B gamepak
.long execute_load_gamepakC_s16 # 0x0C gamepak
.long execute_load_eeprom_s16 # 0x0D gamepak/eeprom
.long execute_load_backup_s16 # 0x0E Flash ROM/SRAM
.long execute_load_open_s16 # 0x0F open unaligned
.long execute_load_bios_s16u # 0x00 BIOS unaligned
.long execute_load_open_s16u # 0x01 open address unaligned
.long execute_load_ewram_s16u # 0x02 EWRAM unaligned
.long execute_load_iwram_s16u # 0x03 IWRAM unaligned
.long execute_load_io_s16u # 0x04 I/O registers unaligned
.long execute_load_palette_s16u # 0x05 Palette RAM unaligned
.long execute_load_vram_s16u # 0x06 VRAM unaligned
.long execute_load_oam_s16u # 0x07 OAM RAM unaligned
.long execute_load_gamepak8_s16u# 0x08 gamepak unaligned
.long execute_load_gamepak9_s16u# 0x09 gamepak unaligned
.long execute_load_gamepakA_s16u# 0x0A gamepak unaligned
.long execute_load_gamepakB_s16u# 0x0B gamepak unaligned
.long execute_load_gamepakC_s16u# 0x0C gamepak unaligned
.long execute_load_eeprom_s16u # 0x0D gamepak/eeprom unaligned
.long execute_load_backup_s16u # 0x0E Flash ROM/SRAM unaligned
.long execute_load_open_s16u # 0x0F open unaligned
patch_load_s16:
patch_handler_align load_s16_ftable, 1
# Unsigned aligned 32bit load handlers
execute_load_bios_u32:
region_check_align 0, 2, 0, patch_load_u32
srl $2, $4, 14 # check if address is in BIOS region
bne $2, $0, 2f # if not, perform open read
srl $1, $5, 14 # check if PC is in BIOS region
bne $1, $0, 1f # if not, perform BIOS protected read
lui $2, %hi(bios_rom) # generate upper address (delay)
andi $4, $4, 0x3FFF # generate offset
addu $2, $2, $4
load_u32 bios_rom
1:
lui $2, %hi(bios_read_protect) # generate upper address
load_u32 bios_read_protect
2:
open_load32_core
nop
execute_load_ewram_u32:
translate_region_ewram_load_align 2, 0, patch_load_u32
load_u32 (ewram + 0x8000)
execute_load_u32:
execute_load_iwram_u32:
translate_region_align 3, 2, 0, patch_load_u32, (iwram + 0x8000), 0x7FFF
load_u32 (iwram + 0x8000)
execute_load_io_u32:
translate_region_align 4, 2, 0, patch_load_u32, io_registers, 0x3FF
load_u32 io_registers
execute_load_palette_u32:
translate_region_align 5, 2, 0, patch_load_u32, palette_ram, 0x3FF
load_u32 palette_ram
execute_load_vram_u32:
translate_region_vram_load_align 2, 0, patch_load_u32
load_u32 vram
execute_load_oam_u32:
translate_region_align 7, 2, 0, patch_load_u32, oam_ram, 0x3FF
load_u32 oam_ram
execute_load_gamepak8_u32:
translate_region_gamepak_align 8, 2, 0, patch_load_u32
load_u32 0
execute_load_gamepak9_u32:
translate_region_gamepak_align 9, 2, 0, patch_load_u32
load_u32 0
execute_load_gamepakA_u32:
translate_region_gamepak_align 10, 2, 0, patch_load_u32
load_u32 0
execute_load_gamepakB_u32:
translate_region_gamepak_align 11, 2, 0, patch_load_u32
load_u32 0
execute_load_gamepakC_u32:
translate_region_gamepak_align 12, 2, 0, patch_load_u32
load_u32 0
execute_load_eeprom_u32:
eeprom_load_align 2, 0, patch_load_u32
execute_load_backup_u32:
backup_load_align 2, 0, patch_load_u32
nop
execute_load_open_u32:
open_load32_align 2, 0, patch_load_u32
nop
# Unsigned unaligned (by 1) 32bit load handlers
execute_load_bios_u32u1:
region_check_align 0, 2, 1, patch_load_u32
srl $2, $4, 14 # check if address is in BIOS region
bne $2, $0, 2f # if not, perform open read
srl $1, $5, 14 # check if PC is in BIOS region
bne $1, $0, 1f # if not, perform BIOS protected read
lui $2, %hi(bios_rom) # generate upper address (delay)
andi $4, $4, 0x3FFC # generate offset
addu $2, $2, $4
load_u32_unaligned bios_rom, 1
1:
lui $2, %hi(bios_read_protect) # generate upper address
load_u32_unaligned bios_read_protect, 1
2:
open_load32_core
ror $2, $2, 8
execute_load_ewram_u32u1:
translate_region_ewram_load_align32 2, 1, patch_load_u32
load_u32_unaligned (ewram + 0x8000), 1
execute_load_iwram_u32u1:
translate_region_align 3, 2, 1, patch_load_u32, (iwram + 0x8000), 0x7FFC
load_u32_unaligned (iwram + 0x8000), 1
execute_load_io_u32u1:
translate_region_align 4, 2, 1, patch_load_u32, io_registers, 0x3FC
load_u32_unaligned io_registers, 1
execute_load_palette_u32u1:
translate_region_align 5, 2, 1, patch_load_u32, palette_ram, 0x3FC
load_u32_unaligned palette_ram, 1
execute_load_vram_u32u1:
translate_region_vram_load_align32 2, 1, patch_load_u32
load_u32_unaligned vram, 1
execute_load_oam_u32u1:
translate_region_align 7, 2, 1, patch_load_u32, oam_ram, 0x3FC
load_u32_unaligned oam_ram, 1
execute_load_gamepak8_u32u1:
translate_region_gamepak_align32 8, 2, 1, patch_load_u32
load_u32_unaligned 0, 1
execute_load_gamepak9_u32u1:
translate_region_gamepak_align32 9, 2, 1, patch_load_u32
load_u32_unaligned 0, 1
execute_load_gamepakA_u32u1:
translate_region_gamepak_align32 10, 2, 1, patch_load_u32
load_u32_unaligned 0, 1
execute_load_gamepakB_u32u1:
translate_region_gamepak_align32 11, 2, 1, patch_load_u32
load_u32_unaligned 0, 1
execute_load_gamepakC_u32u1:
translate_region_gamepak_align32 12, 2, 1, patch_load_u32
load_u32_unaligned 0, 1
execute_load_eeprom_u32u1:
eeprom_load_align32 2, 1, patch_load_u32
execute_load_backup_u32u1:
backup_load_align32 2, 1, patch_load_u32
ror $2, $2, 8 # rotate value by 8bits
execute_load_open_u32u1:
open_load32_align32 2, 1, patch_load_u32
ror $2, $2, 8 # rotate value by 8bits
# Unsigned unaligned (by 2) 32bit load handlers
execute_load_bios_u32u2:
region_check_align 0, 2, 2, patch_load_u32
srl $2, $4, 14 # check if address is in BIOS region
bne $2, $0, 2f # if not, perform open read
srl $1, $5, 14 # check if PC is in BIOS region
bne $1, $0, 1f # if not, perform BIOS protected read
lui $2, %hi(bios_rom) # generate upper address (delay)
andi $4, $4, 0x3FFC # generate offset
addu $2, $2, $4
load_u32_unaligned bios_rom, 2
1:
lui $2, %hi(bios_read_protect) # generate upper address
load_u32_unaligned bios_read_protect, 2
2:
open_load32_core
ror $2, $2, 16
execute_load_ewram_u32u2:
translate_region_ewram_load_align32 2, 2, patch_load_u32
load_u32_unaligned (ewram + 0x8000), 2
execute_load_iwram_u32u2:
translate_region_align 3, 2, 2, patch_load_u32, (iwram + 0x8000), 0x7FFC
load_u32_unaligned (iwram + 0x8000), 2
execute_load_io_u32u2:
translate_region_align 4, 2, 2, patch_load_u32, io_registers, 0x3FC
load_u32_unaligned io_registers, 2
execute_load_palette_u32u2:
translate_region_align 5, 2, 2, patch_load_u32, palette_ram, 0x3FC
load_u32_unaligned palette_ram, 2
execute_load_vram_u32u2:
translate_region_vram_load_align32 2, 2, patch_load_u32
load_u32_unaligned vram, 2
execute_load_oam_u32u2:
translate_region_align 7, 2, 2, patch_load_u32, oam_ram, 0x3FC
load_u32_unaligned oam_ram, 2
execute_load_gamepak8_u32u2:
translate_region_gamepak_align32 8, 2, 2, patch_load_u32
load_u32_unaligned 0, 2
execute_load_gamepak9_u32u2:
translate_region_gamepak_align32 9, 2, 2, patch_load_u32
load_u32_unaligned 0, 2
execute_load_gamepakA_u32u2:
translate_region_gamepak_align32 10, 2, 2, patch_load_u32
load_u32_unaligned 0, 2
execute_load_gamepakB_u32u2:
translate_region_gamepak_align32 11, 2, 2, patch_load_u32
load_u32_unaligned 0, 2
execute_load_gamepakC_u32u2:
translate_region_gamepak_align32 12, 2, 2, patch_load_u32
load_u32_unaligned 0, 2
execute_load_eeprom_u32u2:
eeprom_load_align32 2, 2, patch_load_u32
execute_load_backup_u32u2:
backup_load_align32 2, 2, patch_load_u32
ror $2, $2, 16 # rotate value by 16bits
execute_load_open_u32u2:
open_load32_align32 2, 2, patch_load_u32
ror $2, $2, 16 # rotate value by 16bits
# Unsigned unaligned (by 1) 32bit load handlers
execute_load_bios_u32u3:
region_check_align 0, 2, 3, patch_load_u32
srl $2, $4, 14 # check if address is in BIOS region
bne $2, $0, 2f # if not, perform open read
srl $1, $5, 14 # check if PC is in BIOS region
bne $1, $0, 1f # if not, perform BIOS protected read
lui $2, %hi(bios_rom) # generate upper address (delay)
andi $4, $4, 0x3FFC # generate offset
addu $2, $2, $4
load_u32_unaligned bios_rom, 3
1:
lui $2, %hi(bios_read_protect) # generate upper address
load_u32_unaligned bios_read_protect, 3
2:
open_load32_core
ror $2, $2, 24
execute_load_ewram_u32u3:
translate_region_ewram_load_align32 2, 3, patch_load_u32
load_u32_unaligned (ewram + 0x8000), 3
execute_load_iwram_u32u3:
translate_region_align 3, 2, 3, patch_load_u32, (iwram + 0x8000), 0x7FFC
load_u32_unaligned (iwram + 0x8000), 3
execute_load_io_u32u3:
translate_region_align 4, 2, 3, patch_load_u32, io_registers, 0x3FC
load_u32_unaligned io_registers, 3
execute_load_palette_u32u3:
translate_region_align 5, 2, 3, patch_load_u32, palette_ram, 0x3FC
load_u32_unaligned palette_ram, 3
execute_load_vram_u32u3:
translate_region_vram_load_align32 2, 3, patch_load_u32
load_u32_unaligned vram, 3
execute_load_oam_u32u3:
translate_region_align 7, 2, 3, patch_load_u32, oam_ram, 0x3FC
load_u32_unaligned oam_ram, 3
execute_load_gamepak8_u32u3:
translate_region_gamepak_align32 8, 2, 3, patch_load_u32
load_u32_unaligned 0, 3
execute_load_gamepak9_u32u3:
translate_region_gamepak_align32 9, 2, 3, patch_load_u32
load_u32_unaligned 0, 3
execute_load_gamepakA_u32u3:
translate_region_gamepak_align32 10, 2, 3, patch_load_u32
load_u32_unaligned 0, 3
execute_load_gamepakB_u32u3:
translate_region_gamepak_align32 11, 2, 3, patch_load_u32
load_u32_unaligned 0, 3
execute_load_gamepakC_u32u3:
translate_region_gamepak_align32 12, 2, 3, patch_load_u32
load_u32_unaligned 0, 3
execute_load_eeprom_u32u3:
eeprom_load_align32 2, 3, patch_load_u32
execute_load_backup_u32u3:
backup_load_align32 2, 3, patch_load_u32
ror $2, $2, 24 # rotate value by 24bits
execute_load_open_u32u3:
open_load32_align32 2, 3, patch_load_u32
ror $2, $2, 24 # rotate value by 24bits
load_u32_ftable:
.long execute_load_bios_u32 # 0x00 BIOS
.long execute_load_open_u32 # 0x01 open address
.long execute_load_ewram_u32 # 0x02 EWRAM
.long execute_load_iwram_u32 # 0x03 IWRAM
.long execute_load_io_u32 # 0x04 I/O registers
.long execute_load_palette_u32 # 0x05 Palette RAM
.long execute_load_vram_u32 # 0x06 VRAM
.long execute_load_oam_u32 # 0x07 OAM RAM
.long execute_load_gamepak8_u32 # 0x08 gamepak
.long execute_load_gamepak9_u32 # 0x09 gamepak
.long execute_load_gamepakA_u32 # 0x0A gamepak
.long execute_load_gamepakB_u32 # 0x0B gamepak
.long execute_load_gamepakC_u32 # 0x0C gamepak
.long execute_load_eeprom_u32 # 0x0D gamepak/eeprom
.long execute_load_backup_u32 # 0x0E Flash ROM/SRAM
.long execute_load_open_u32 # 0x0F open
.long execute_load_bios_u32u1 # 0x00 BIOS unaligned (1b)
.long execute_load_open_u32u1 # 0x01 open address unaligned (1b)
.long execute_load_ewram_u32u1 # 0x02 EWRAM unaligned (1b)
.long execute_load_iwram_u32u1 # 0x03 IWRAM unaligned (1b)
.long execute_load_io_u32u1 # 0x04 I/O registers unaligned (1b)
.long execute_load_palette_u32u1 # 0x05 Palette RAM unaligned (1b)
.long execute_load_vram_u32u1 # 0x06 VRAM unaligned (1b)
.long execute_load_oam_u32u1 # 0x07 OAM RAM unaligned (1b)
.long execute_load_gamepak8_u32u1 # 0x08 gamepak unaligned (1b)
.long execute_load_gamepak9_u32u1 # 0x09 gamepak unaligned (1b)
.long execute_load_gamepakA_u32u1 # 0x0A gamepak unaligned (1b)
.long execute_load_gamepakB_u32u1 # 0x0B gamepak unaligned (1b)
.long execute_load_gamepakC_u32u1 # 0x0C gamepak unaligned (1b)
.long execute_load_eeprom_u32u1 # 0x0D gamepak/eeprom unaligned (1b)
.long execute_load_backup_u32u1 # 0x0E Flash ROM/SRAM unaligned (1b)
.long execute_load_open_u32u1 # 0x0F open unaligned (1b)
.long execute_load_bios_u32u2 # 0x00 BIOS unaligned (2b)
.long execute_load_open_u32u2 # 0x01 open address unaligned (2b)
.long execute_load_ewram_u32u2 # 0x02 EWRAM unaligned (2b)
.long execute_load_iwram_u32u2 # 0x03 IWRAM unaligned (2b)
.long execute_load_io_u32u2 # 0x04 I/O registers unaligned (2b)
.long execute_load_palette_u32u2 # 0x05 Palette RAM unaligned (2b)
.long execute_load_vram_u32u2 # 0x06 VRAM unaligned (2b)
.long execute_load_oam_u32u2 # 0x07 OAM RAM unaligned (2b)
.long execute_load_gamepak8_u32u2 # 0x08 gamepak unaligned (2b)
.long execute_load_gamepak9_u32u2 # 0x09 gamepak unaligned (2b)
.long execute_load_gamepakA_u32u2 # 0x0A gamepak unaligned (2b)
.long execute_load_gamepakB_u32u2 # 0x0B gamepak unaligned (2b)
.long execute_load_gamepakC_u32u2 # 0x0C gamepak unaligned (2b)
.long execute_load_eeprom_u32u2 # 0x0D gamepak/eeprom unaligned (2b)
.long execute_load_backup_u32u2 # 0x0E Flash ROM/SRAM unaligned (2b)
.long execute_load_open_u32u2 # 0x0F open unaligned (2b)
.long execute_load_bios_u32u3 # 0x00 BIOS unaligned (3b)
.long execute_load_open_u32u3 # 0x01 open address unaligned (3b)
.long execute_load_ewram_u32u3 # 0x02 EWRAM unaligned (3b)
.long execute_load_iwram_u32u3 # 0x03 IWRAM unaligned (3b)
.long execute_load_io_u32u3 # 0x04 I/O registers unaligned (3b)
.long execute_load_palette_u32u3 # 0x05 Palette RAM unaligned (3b)
.long execute_load_vram_u32u3 # 0x06 VRAM unaligned (3b)
.long execute_load_oam_u32u3 # 0x07 OAM RAM unaligned (3b)
.long execute_load_gamepak8_u32u3 # 0x08 gamepak unaligned (3b)
.long execute_load_gamepak9_u32u3 # 0x09 gamepak unaligned (3b)
.long execute_load_gamepakA_u32u3 # 0x0A gamepak unaligned (3b)
.long execute_load_gamepakB_u32u3 # 0x0B gamepak unaligned (3b)
.long execute_load_gamepakC_u32u3 # 0x0C gamepak unaligned (3b)
.long execute_load_eeprom_u32u3 # 0x0D gamepak/eeprom unaligned (3b)
.long execute_load_backup_u32u3 # 0x0E Flash ROM/SRAM unaligned (3b)
.long execute_load_open_u32u3 # 0x0F open unaligned (3b)
patch_load_u32:
patch_handler_align load_u32_ftable, 2
# Unsigned always aligned 32bit load handlers
execute_load_bios_u32a:
region_check 0, patch_load_u32a
srl $2, $4, 14 # check if address is in BIOS region
bne $2, $0, 2f # if not, perform open read
srl $1, $5, 14 # check if PC is in BIOS region
bne $1, $0, 1f # if not, perform BIOS protected read
lui $2, %hi(bios_rom) # generate upper address (delay)
andi $4, $4, 0x3FFF # generate offset
addu $2, $2, $4
load_u32 bios_rom
1:
lui $2, %hi(bios_read_protect) # generate upper address
load_u32 bios_read_protect
2:
open_load32_a
nop
execute_load_ewram_u32a:
translate_region_ewram patch_load_u32a
load_u32 (ewram + 0x8000)
execute_aligned_load32:
execute_load_iwram_u32a:
translate_region 3, patch_load_u32a, (iwram + 0x8000), 0x7FFF
load_u32 (iwram + 0x8000)
execute_load_io_u32a:
translate_region 4, patch_load_u32a, io_registers, 0x3FF
load_u32 io_registers
execute_load_palette_u32a:
translate_region 5, patch_load_u32a, palette_ram, 0x3FF
load_u32 palette_ram
execute_load_vram_u32a:
translate_region_vram patch_load_u32a
load_u32 vram
execute_load_oam_u32a:
translate_region 7, patch_load_u32a, oam_ram, 0x3FF
load_u32 oam_ram
execute_load_gamepak8_u32a:
translate_region_gamepak_a 8, patch_load_u32a
load_u32 0
execute_load_gamepak9_u32a:
translate_region_gamepak_a 9, patch_load_u32a
load_u32 0
execute_load_gamepakA_u32a:
translate_region_gamepak_a 10, patch_load_u32a
load_u32 0
execute_load_gamepakB_u32a:
translate_region_gamepak_a 11, patch_load_u32a
load_u32 0
execute_load_gamepakC_u32a:
translate_region_gamepak_a 12, patch_load_u32a
load_u32 0
execute_load_eeprom_u32a:
eeprom_load_a patch_load_u32a
execute_load_backup_u32a:
backup_load_a patch_load_u32a
nop
execute_load_open_u32a:
open_load32_a patch_load_u32a
load_u32a_ftable:
.long execute_load_bios_u32a # 0x00 BIOS unaligned (3b)
.long execute_load_open_u32a # 0x01 open address unaligned (3b)
.long execute_load_ewram_u32a # 0x02 EWRAM unaligned (3b)
.long execute_load_iwram_u32a # 0x03 IWRAM unaligned (3b)
.long execute_load_io_u32a # 0x04 I/O registers unaligned (3b)
.long execute_load_palette_u32a # 0x05 Palette RAM unaligned (3b)
.long execute_load_vram_u32a # 0x06 VRAM unaligned (3b)
.long execute_load_oam_u32a # 0x07 OAM RAM unaligned (3b)
.long execute_load_gamepak8_u32a # 0x08 gamepak unaligned (3b)
.long execute_load_gamepak9_u32a # 0x09 gamepak unaligned (3b)
.long execute_load_gamepakA_u32a # 0x0A gamepak unaligned (3b)
.long execute_load_gamepakB_u32a # 0x0B gamepak unaligned (3b)
.long execute_load_gamepakC_u32a # 0x0C gamepak unaligned (3b)
.long execute_load_eeprom_u32a # 0x0D gamepak/eeprom unaligned (3b)
.long execute_load_backup_u32a # 0x0E Flash ROM/SRAM unaligned (3b)
.long execute_load_open_u32a # 0x0F open unaligned (3b)
patch_load_u32a:
patch_handler load_u32a_ftable, 1
# Unsigned 8bit store handlers
execute_store_ignore0_u8:
ignore_region 0, patch_store_u8
execute_store_ignore1_u8:
ignore_region 1, patch_store_u8
execute_store_ewram_u8:
translate_region_ewram patch_store_u8
store_u8_smc (ewram + 0x8000)
execute_store_u8:
execute_store_iwram_u8:
translate_region 3, patch_store_u8, (iwram + 0x8000), 0x7FFF
store_u8_smc (iwram + 0x8000)
execute_store_io_u8:
region_check 4, patch_store_u8
andi $5, $5, 0xFF # make value 8bit
andi $4, $4, 0x3FF # wrap around address
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp)
save_registers
jal write_io_register8 # write the value out
sw $6, REG_PC($16) # save the PC (delay slot)
j write_io_epilogue # handle any state changes
nop
execute_store_palette_u8:
region_check 5, patch_store_u8
lui $2, %hi(palette_ram) # start loading palette_ram address (delay)
ins $5, $5, 8, 8 # double value
andi $4, $4, 0x3FE # align palette address
addu $2, $2, $4
sh $5, %lo(palette_ram)($2) # palette_ram[address] = value
sll $1, $5, 1 # make green 6bits
ins $1, $0, 0, 6 # make bottom bit 0
ins $1, $5, 0, 5 # insert red channel into $1
lui $2, %hi(palette_ram_converted)
addu $2, $2, $4
jr $ra # return
sh $1, %lo(palette_ram_converted)($2)
execute_store_vram_u8:
translate_region_vram_store_align16 patch_store_u8
store_u8_double vram
execute_store_oam_u8:
translate_region 7, patch_store_u8, oam_ram, 0x3FE
lui $1, %hi(oam_update) # write non-zero to oam_update
sw $1, %lo(oam_update)($1) # cheap, but this is non-zero
store_u8_double oam_ram
execute_store_ignore8_u8:
ignore_region 8, patch_store_u8
execute_store_ignore9_u8:
ignore_region 9, patch_store_u8
execute_store_ignoreA_u8:
ignore_region 10, patch_store_u8
execute_store_ignoreB_u8:
ignore_region 11, patch_store_u8
execute_store_ignoreC_u8:
ignore_region 12, patch_store_u8
execute_store_eeprom_u8:
store_function write_eeprom, 13, patch_store_u8, 0x3FF
execute_store_backup_u8:
store_function write_backup, 14, patch_store_u8, 0xFFFF
execute_store_ignoreF_u8:
ignore_high patch_store_u8
store_u8_ftable:
.long execute_store_ignore0_u8 # 0x00 BIOS
.long execute_store_ignore1_u8 # 0x01 open address
.long execute_store_ewram_u8 # 0x02 EWRAM
.long execute_store_iwram_u8 # 0x03 IWRAM
.long execute_store_io_u8 # 0x04 I/O registers
.long execute_store_palette_u8 # 0x05 Palette RAM
.long execute_store_vram_u8 # 0x06 VRAM
.long execute_store_oam_u8 # 0x07 OAM RAM
.long execute_store_ignore8_u8 # 0x08 gamepak
.long execute_store_ignore9_u8 # 0x09 gamepak
.long execute_store_ignoreA_u8 # 0x0A gamepak
.long execute_store_ignoreB_u8 # 0x0B gamepak
.long execute_store_ignoreC_u8 # 0x0C gamepak
.long execute_store_eeprom_u8 # 0x0D gamepak/eeprom
.long execute_store_backup_u8 # 0x0E Flash ROM/SRAM
.long execute_store_ignoreF_u8 # 0x0F open address
patch_store_u8:
patch_handler store_u8_ftable, 0x0F
# Unsigned 16bit store handlers
execute_store_ignore0_u16:
ignore_region 0, patch_store_u16
execute_store_ignore1_u16:
ignore_region 1, patch_store_u16
execute_store_ewram_u16:
translate_region_ewram_store_align16 patch_store_u16
store_u16_smc (ewram + 0x8000)
execute_store_u16:
execute_store_iwram_u16:
translate_region 3, patch_store_u16, (iwram + 0x8000), 0x7FFE
store_u16_smc (iwram + 0x8000)
execute_store_io_u16:
region_check 4, patch_store_u16
andi $5, $5, 0xFFFF # make value 16bit
andi $4, $4, 0x3FE # wrap around/align address
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp)
save_registers
jal write_io_register16 # write the value out
sw $6, REG_PC($16) # save the PC (delay slot)
j write_io_epilogue # handle any state changes
nop
execute_store_palette_u16:
region_check 5, patch_store_u16
lui $2, %hi(palette_ram) # start loading palette_ram address (delay)
andi $4, $4, 0x3FE # wrap/align palette address
addu $2, $2, $4
sh $5, %lo(palette_ram)($2) # palette_ram[address] = value
sll $1, $5, 1 # make green 6bits
ins $1, $0, 0, 6 # make bottom bit 0
ins $1, $5, 0, 5 # insert red channel into $1
lui $2, %hi(palette_ram_converted)
addu $2, $2, $4
jr $ra # return
sh $1, %lo(palette_ram_converted)($2)
execute_store_vram_u16:
translate_region_vram_store_align16 patch_store_u16
store_u16 vram
execute_store_oam_u16:
translate_region 7, patch_store_u16, oam_ram, 0x3FE
lui $1, %hi(oam_update) # write non-zero to oam_update
sw $1, %lo(oam_update)($1) # cheap, but this is non-zero
store_u16 oam_ram
execute_store_rtc_u16:
store_function write_rtc, 8, patch_store_u16, 0xFE
execute_store_ignore9_u16:
ignore_region 9, patch_store_u16
execute_store_ignoreA_u16:
ignore_region 10, patch_store_u16
execute_store_ignoreB_u16:
ignore_region 11, patch_store_u16
execute_store_ignoreC_u16:
ignore_region 12, patch_store_u16
execute_store_eeprom_u16:
store_function write_eeprom, 13, patch_store_u16, 0x3FE
execute_store_ignoreE_u16:
ignore_region 14, patch_store_u16
execute_store_ignoreF_u16:
ignore_high patch_store_u16
store_u16_ftable:
.long execute_store_ignore0_u16 # 0x00 BIOS
.long execute_store_ignore1_u16 # 0x01 open address
.long execute_store_ewram_u16 # 0x02 EWRAM
.long execute_store_iwram_u16 # 0x03 IWRAM
.long execute_store_io_u16 # 0x04 I/O registers
.long execute_store_palette_u16 # 0x05 Palette RAM
.long execute_store_vram_u16 # 0x06 VRAM
.long execute_store_oam_u16 # 0x07 OAM RAM
.long execute_store_rtc_u16 # 0x08 gamepak
.long execute_store_ignore9_u16 # 0x09 gamepak
.long execute_store_ignoreA_u16 # 0x0A gamepak
.long execute_store_ignoreB_u16 # 0x0B gamepak
.long execute_store_ignoreC_u16 # 0x0C gamepak
.long execute_store_eeprom_u16 # 0x0D gamepak/eeprom
.long execute_store_ignoreE_u16 # 0x0E Flash ROM/SRAM
.long execute_store_ignoreF_u16 # 0x0F open address
patch_store_u16:
patch_handler store_u16_ftable, 0x0F
# Unsigned 32bit store handlers
execute_store_ignore0_u32:
ignore_region 0, patch_store_u32
execute_store_ignore1_u32:
ignore_region 1, patch_store_u32
execute_store_ewram_u32:
translate_region_ewram_store_align32 patch_store_u32
store_u32_smc (ewram + 0x8000)
execute_store_u32:
execute_store_iwram_u32:
translate_region 3, patch_store_u32, (iwram + 0x8000), 0x7FFC
store_u32_smc (iwram + 0x8000)
execute_store_io_u32:
region_check 4, patch_store_u32
nop
andi $4, $4, 0x3FC # wrap around/align address
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp)
save_registers
jal write_io_register32 # write the value out
sw $6, REG_PC($16) # save the PC (delay slot)
j write_io_epilogue # handle any state changes
nop
execute_store_palette_u32:
region_check 5, patch_store_u32
lui $2, %hi(palette_ram) # start loading palette_ram address (delay)
andi $4, $4, 0x3FC # wrap/align palette address
addu $2, $2, $4
sw $5, %lo(palette_ram)($2) # palette_ram[address] = value
sll $1, $5, 1 # make green 6bits
ins $1, $0, 0, 6 # make bottom bit 0
ins $1, $5, 0, 5 # insert red channel into $1
lui $2, %hi(palette_ram_converted)
addu $2, $2, $4
addiu $2, $2, %lo(palette_ram_converted)
sh $1, ($2)
srl $5, $5, 16 # shift down to next palette value
sll $1, $5, 1 # make green 6bits
ins $1, $0, 0, 6 # make bottom bit 0
ins $1, $5, 0, 5 # insert red channel into $1
jr $ra # return
sh $1, 2($2)
execute_store_vram_u32:
translate_region_vram_store_align32 patch_store_u32
store_u32 vram
execute_store_oam_u32:
translate_region 7, patch_store_u32, oam_ram, 0x3FC
lui $1, %hi(oam_update) # write non-zero to oam_update
sw $1, %lo(oam_update)($1) # cheap, but this is non-zero
store_u32 oam_ram
execute_store_ignore8_u32:
ignore_region 8, patch_store_u32
execute_store_ignore9_u32:
ignore_region 9, patch_store_u32
execute_store_ignoreA_u32:
ignore_region 10, patch_store_u32
execute_store_ignoreB_u32:
ignore_region 11, patch_store_u32
execute_store_ignoreC_u32:
ignore_region 12, patch_store_u32
execute_store_eeprom_u32:
store_function write_eeprom, 13, patch_store_u32, 0x3FC
execute_store_ignoreE_u32:
ignore_region 14, patch_store_u32
execute_store_ignoreF_u32:
ignore_high patch_store_u32
store_u32_ftable:
.long execute_store_ignore0_u32 # 0x00 BIOS
.long execute_store_ignore1_u32 # 0x01 open address
.long execute_store_ewram_u32 # 0x02 EWRAM
.long execute_store_iwram_u32 # 0x03 IWRAM
.long execute_store_io_u32 # 0x04 I/O registers
.long execute_store_palette_u32 # 0x05 Palette RAM
.long execute_store_vram_u32 # 0x06 VRAM
.long execute_store_oam_u32 # 0x07 OAM RAM
.long execute_store_ignore8_u32 # 0x08 gamepak
.long execute_store_ignore9_u32 # 0x09 gamepak
.long execute_store_ignoreA_u32 # 0x0A gamepak
.long execute_store_ignoreB_u32 # 0x0B gamepak
.long execute_store_ignoreC_u32 # 0x0C gamepak
.long execute_store_eeprom_u32 # 0x0D gamepak/eeprom
.long execute_store_ignoreE_u32 # 0x0E Flash ROM/SRAM
.long execute_store_ignoreF_u32 # 0x0F open address
patch_store_u32:
patch_handler store_u32_ftable, 0x0F
# Unsigned always aligned, a2 safe 32bit store handlers
execute_store_ignore0_u32a:
ignore_region 0, patch_store_u32a
execute_store_ignore1_u32a:
ignore_region 1, patch_store_u32a
execute_store_ewram_u32a:
translate_region_ewram_store_align32 patch_store_u32a
store_u32 (ewram + 0x8000)
execute_aligned_store32:
execute_store_iwram_u32a:
translate_region 3, patch_store_u32a, (iwram + 0x8000), 0x7FFC
store_u32 (iwram + 0x8000)
execute_store_io_u32a:
region_check 4, patch_store_u32a
nop
sw $6, REG_SAVE($16) # save a2
sw $ra, REG_SAVE2($16) # save ra
andi $4, $4, 0x3FC # wrap around/align address
save_registers
jal write_io_register32 # write the value out
nop
restore_registers
lw $ra, REG_SAVE2($16) # restore ra
jr $ra
lw $6, REG_SAVE($16) # restore a2
execute_store_palette_u32a:
region_check 5, patch_store_u32a
lui $2, %hi(palette_ram) # start loading palette_ram address (delay)
andi $4, $4, 0x3FC # wrap/align palette address
addu $2, $2, $4
sw $5, %lo(palette_ram)($2) # palette_ram[address] = value
sll $1, $5, 1 # make green 6bits
ins $1, $0, 0, 6 # make bottom bit 0
ins $1, $5, 0, 5 # insert red channel into $1
lui $2, %hi(palette_ram_converted)
addu $2, $2, $4
addiu $2, $2, %lo(palette_ram_converted)
sh $1, ($2)
srl $5, $5, 16 # shift down to next palette value
sll $1, $5, 1 # make green 6bits
ins $1, $0, 0, 6 # make bottom bit 0
ins $1, $5, 0, 5 # insert red channel into $1
jr $ra # return
sh $1, 2($2)
execute_store_vram_u32a:
translate_region_vram_store_align32 patch_store_u32a
store_u32 vram
execute_store_oam_u32a:
translate_region 7, patch_store_u32a, oam_ram, 0x3FC
lui $1, %hi(oam_update) # write non-zero to oam_update
sw $1, %lo(oam_update)($1) # cheap, but this is non-zero
store_u32 oam_ram
execute_store_ignore8_u32a:
ignore_region 8, patch_store_u32a
execute_store_ignore9_u32a:
ignore_region 9, patch_store_u32a
execute_store_ignoreA_u32a:
ignore_region 10, patch_store_u32a
execute_store_ignoreB_u32a:
ignore_region 11, patch_store_u32a
execute_store_ignoreC_u32a:
ignore_region 12, patch_store_u32a
execute_store_eeprom_u32a:
store_function_a write_eeprom, 13, patch_store_u32a, 0x3FC
execute_store_ignoreE_u32a:
ignore_region 14, patch_store_u32a
execute_store_ignoreF_u32a:
ignore_high patch_store_u32a
store_u32a_ftable:
.long execute_store_ignore0_u32a# 0x00 BIOS
.long execute_store_ignore1_u32a# 0x01 open address
.long execute_store_ewram_u32a # 0x02 EWRAM
.long execute_store_iwram_u32a # 0x03 IWRAM
.long execute_store_io_u32a # 0x04 I/O registers
.long execute_store_palette_u32a# 0x05 Palette RAM
.long execute_store_vram_u32a # 0x06 VRAM
.long execute_store_oam_u32a # 0x07 OAM RAM
.long execute_store_ignore8_u32a# 0x08 gamepak
.long execute_store_ignore9_u32a# 0x09 gamepak
.long execute_store_ignoreA_u32a# 0x0A gamepak
.long execute_store_ignoreB_u32a# 0x0B gamepak
.long execute_store_ignoreC_u32a# 0x0C gamepak
.long execute_store_eeprom_u32a # 0x0D gamepak/eeprom
.long execute_store_ignoreE_u32a# 0x0E Flash ROM/SRAM
.long execute_store_ignoreF_u32a# 0x0F open address
patch_store_u32a:
patch_handler store_u32a_ftable, 0x0F
#execute_load_u8:
execute_load_full_u8:
srl $1, $4, 28 # check if the address is out of range
bne $1, $0, ext_load_u8 # if it is, perform an extended read
srl $2, $4, 15 # $1 = page number of address
sll $2, $2, 2 # adjust to word index
addu $2, $2, $16 # $1 = memory_map_read[address >> 15]
lw $1, -32768($2)
beq $1, $0, ext_load_u8 # if it's NULL perform an extended read
andi $2, $4, 0x7FFF # $2 = low 15bits of address (delay slot)
addu $1, $1, $2 # add the memory map offset
jr $ra # return
lbu $2, ($1) # read the value
ext_load_u8:
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp) # store return address
save_registers
jal read_memory8 # read the value
nop
restore_registers
lw $ra, ($sp) # restore return address
jr $ra # return
addiu $sp, $sp, 4 # fix stack (delay slot)
#execute_load_s8:
execute_load_full_s8:
srl $1, $4, 28 # check if the address is out of range
bne $1, $0, ext_load_s8 # if it is, perform an extended read
srl $2, $4, 15 # $1 = page number of address
sll $2, $2, 2 # adjust to word index
addu $2, $2, $16 # $1 = memory_map_read[address >> 15]
lw $1, -32768($2)
beq $1, $0, ext_load_s8 # if it's NULL perform an extended read
andi $2, $4, 0x7FFF # $2 = low 15bits of address (delay slot)
addu $1, $1, $2 # add the memory map offset
jr $ra # return
lb $2, ($1) # read the value
ext_load_s8:
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp) # store return address
save_registers
jal read_memory8 # read the value
nop
restore_registers
seb $2, $2 # sign extend the read value
lw $ra, ($sp) # restore return address
jr $ra # return
addiu $sp, $sp, 4 # fix stack (delay slot)
#execute_load_u16:
execute_load_full_u16:
srl $1, $4, 28 # check if the address is out of range
ins $1, $4, 4, 1 # or unaligned (bottom bit)
bne $1, $0, ext_load_u16 # if it is, perform an extended read
srl $2, $4, 15 # $1 = page number of address
sll $2, $2, 2 # adjust to word index
addu $2, $2, $16 # $1 = memory_map_read[address >> 15]
lw $1, -32768($2)
beq $1, $0, ext_load_u16 # if it's NULL perform an extended read
andi $2, $4, 0x7FFF # $2 = low 15bits of address (delay slot)
addu $1, $1, $2 # add the memory map offset
jr $ra # return
lhu $2, ($1) # read the value
ext_load_u16:
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp) # store return address
save_registers
jal read_memory16 # read the value
nop
restore_registers
lw $ra, ($sp) # restore return address
jr $ra # return
addiu $sp, $sp, 4 # fix stack (delay slot)
#execute_load_s16:
execute_load_full_s16:
srl $1, $4, 28 # check if the address is out of range
ins $1, $4, 4, 1 # or unaligned (bottom bit)
bne $1, $0, ext_load_s16 # if it is, perform an extended read
srl $2, $4, 15 # $1 = page number of address
sll $2, $2, 2 # adjust to word index
addu $2, $2, $16 # $1 = memory_map_read[address >> 15]
lw $1, -32768($2)
beq $1, $0, ext_load_s16 # if it's NULL perform an extended read
andi $2, $4, 0x7FFF # $2 = low 15bits of address (delay slot)
addu $1, $1, $2 # add the memory map offset
jr $ra # return
lh $2, ($1) # read the value
ext_load_s16:
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp) # store return address
save_registers
jal read_memory16_signed # read the value
nop
restore_registers
seh $2, $2 # sign extend the return value
lw $ra, ($sp) # restore return address
jr $ra # return
addiu $sp, $sp, 4 # fix stack (delay slot)
#execute_load_u32:
execute_load_full_u32:
srl $1, $4, 28 # check if the address is out of range
ins $1, $4, 4, 2 # or unaligned (bottom two bits)
bne $1, $0, ext_load_u32 # if it is, perform an extended read
srl $2, $4, 15 # $1 = page number of address
sll $2, $2, 2 # adjust to word index
addu $2, $2, $16 # $1 = memory_map_read[address >> 15]
lw $1, -32768($2)
beq $1, $0, ext_load_u32 # if it's NULL perform an extended read
andi $2, $4, 0x7FFF # $2 = low 15bits of address (delay slot)
addu $1, $1, $2 # add the memory map offset
jr $ra # return
lw $2, ($1) # read the value
ext_load_u32:
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp) # store return address
save_registers
jal read_memory32 # read the value
nop
restore_registers
lw $ra, ($sp) # restore return address
jr $ra # return
addiu $sp, $sp, 4 # fix stack (delay slot)
#execute_aligned_load32:
srl $2, $4, 28 # check if the address is out of range
bne $2, $0, ext_aligned_load32 # if it is, perform an extended load
srl $1, $4, 15 # $1 = page number of address
sll $1, $1, 2 # adjust to word index
addu $1, $1, $16 # $1 = memory_map_read[address >> 15]
lw $1, -32768($1)
beq $1, $0, ext_aligned_load32 # if it's NULL perform an extended read
andi $2, $4, 0x7FFF # $2 = low 15bits of address (delay slot)
addu $1, $1, $2 # add the memory map offset
jr $ra # return
lw $2, ($1) # read the value
ext_aligned_load32:
addiu $sp, $sp, -8 # make room on the stack for $ra
sw $6, 4($sp)
sw $ra, ($sp) # store return address
save_registers
jal read_memory32 # read the value
nop
restore_registers
lw $6, 4($sp)
lw $ra, ($sp) # restore return address
jr $ra # return
addiu $sp, $sp, 8 # fix stack (delay slot)
# General ext memory routines
ext_store_ignore:
jr $ra # ignore these writes
nop
write_io_epilogue:
beq $2, $0, no_alert # 0 means nothing happened
addiu $4, $2, -2 # see if return value is 2 (delay slot)
beq $4, $0, smc_dma # is it an SMC alert? (return value = 2)
nop
addiu $4, $2, -3 # see if return value is 3
beq $4, $0, irq_alert # is it an IRQ alert? (return value = 3)
nop
collapse_flags # make sure flags are good for update_gba
alert_loop:
jal update_gba # process the next event
nop
lw $1, CPU_HALT_STATE($16) # check if CPU is sleeping
bne $1, $0, alert_loop # see if it hasn't changed
nop
addu $17, $2, $0 # $17 = new cycle counter
lw $4, REG_PC($16) # $4 = new PC
j lookup_pc
addiu $sp, $sp, 4 # fix the stack (delay slot)
irq_alert:
restore_registers
j lookup_pc # PC has changed, get a new one
addiu $sp, $sp, 4 # fix the stack
no_alert:
restore_registers
lw $ra, ($sp) # restore return address
jr $ra # we can return
addiu $sp, $sp, 4 # fix the stack
smc_dma:
addiu $sp, $sp, 4 # fix the stack
jal flush_translation_cache_ram # flush translation cache
nop
j lookup_pc
nop
ext_store_eeprom:
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp)
save_registers
jal write_eeprom # write the value out
sw $6, REG_PC($16) # save the PC (delay slot)
restore_registers
lw $ra, ($sp) # restore return address
jr $ra # we can return
addiu $sp, $sp, 4 # fix the stack
# 8bit ext memory routines
ext_store_io8:
andi $5, $5, 0xFF # make value 8bit
andi $4, $4, 0x3FF # wrap around address
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp)
save_registers
jal write_io_register8 # write the value out
sw $6, REG_PC($16) # save the PC (delay slot)
j write_io_epilogue # handle any state changes
nop
ext_store_palette8:
j ext_store_palette16b # perform 16bit palette write
andi $4, $4, 0x3FE # wrap + align (delay)
ext_store_vram8:
ins $5, $5, 8, 8 # value = (value << 8) | value
ext $4, $4, 0, 17 # address = adress & 0x1FFFF
ins $4, $0, 0, 1 # align out bottom bit
lui $1, %hi(0x18000) # $1 = 0x18000
sltu $1, $4, $1 # see if address < 0x18000
bne $1, $0, ext_store_vram8b
lui $2, %hi(vram) # start loading vram address (delay)
addiu $4, $4, -0x8000 # move address into VRAM region
ext_store_vram8b:
addu $2, $2, $4 # $2 = (hi)vram + address
jr $ra # return
sh $5, %lo(vram)($2) # vram[address] = value (delay)
ext_store_oam8:
lui $1, %hi(oam_update) # $1 = oam_update
addiu $1, %lo(oam_update)
li $2, 1 # $2 = 1
sw $2, ($1) # *oam_update = 1
andi $4, $4, 0x3FE # wrap around address and align to 16bits
ins $5, $5, 8, 8 # value = (value << 8) | value
lui $1, %hi(oam_ram) # $1 = (hi)oam_ram
addu $1, $1, $4 # $1 = (hi)oam_ram + address
jr $ra # return
sh $5, %lo(oam_ram)($1) # oam_ram[address] = value (delay)
ext_store_backup:
andi $5, $5, 0xFF # make value 8bit
andi $4, $4, 0xFFFF # mask value
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp)
save_registers
jal write_backup # write the value out
sw $6, REG_PC($16) # save the PC (delay slot)
restore_registers
lw $ra, ($sp) # restore return address
jr $ra # we can return
addiu $sp, $sp, 4 # fix the stack
ext_store_u8_jtable:
.long ext_store_ignore # 0x00 BIOS
.long ext_store_ignore # 0x01 invalid
.long ext_store_ignore # 0x02 EWRAM
.long ext_store_ignore # 0x03 IWRAM
.long ext_store_io8 # 0x04 I/O registers
.long ext_store_palette8 # 0x05 Palette RAM
.long ext_store_vram8 # 0x06 VRAM
.long ext_store_oam8 # 0x07 OAM RAM
.long ext_store_ignore # 0x08 gamepak (no RTC accepted in 8bit)
.long ext_store_ignore # 0x09 gamepak, ignore
.long ext_store_ignore # 0x0A gamepak, ignore
.long ext_store_ignore # 0x0B gamepak, ignore
.long ext_store_ignore # 0x0C gamepak, ignore
.long ext_store_eeprom # 0x0D EEPROM (possibly)
.long ext_store_backup # 0x0E Flash ROM/SRAM
.long ext_store_ignore # 0x0F invalid
ext_store_u8:
srl $1, $4, 24 # $1 = address >> 24
sltu $2, $1, 16 # check if the value is out of range
beq $2, $0, ext_store_ignore
sll $1, $1, 2 # make address word indexed (delay)
lui $2, %hi(ext_store_u8_jtable)
addu $2, $2, $1
# $2 = ext_store_u8_jtable[address >> 24]
lw $2, %lo(ext_store_u8_jtable)($2)
jr $2 # jump to table location
nop
# $4: address to write to
# $5: value to write
# $6: current PC
#execute_store_u8:
srl $1, $4, 28 # check if the address is out of range
bne $1, $0, ext_store_u8 # if it is, perform an extended write
srl $2, $4, 15 # $1 = page number of address (delay slot)
sll $2, $2, 2 # adjust to word index
addu $2, $2, $16
lw $1, 256($2) # $1 = memory_map_write[address >> 15]
beq $1, $0, ext_store_u8 # if it's NULL perform an extended write
andi $2, $4, 0x7FFF # $2 = low 15bits of address (delay slot)
addu $1, $1, $2 # add the memory map offset
lb $2, -32768($1) # load the SMC status
bne $2, $0, smc_write # is there code there?
sb $5, ($1) # store the value (delay slot)
jr $ra # return
nop
# 16bit ext memory routines
ext_store_io16:
andi $4, $4, 0x3FF # wrap around address
andi $5, $5, 0xFFFF # make value 16bit
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp)
save_registers
jal write_io_register16 # write the value out
sw $6, REG_PC($16) # save the PC (delay slot)
j write_io_epilogue # handle any state changes
nop
ext_store_palette16:
andi $4, 0x3FF # wrap address
ext_store_palette16b:
lui $2, %hi(palette_ram)
addu $2, $2, $4
sh $5, %lo(palette_ram)($2) # palette_ram[address] = value
sll $1, $5, 1 # make green 6bits
ins $1, $0, 0, 6 # make bottom bit 0
ins $1, $5, 0, 5 # insert red channel into $1
lui $2, %hi(palette_ram_converted)
addu $2, $2, $4
jr $ra # return
sh $1, %lo(palette_ram_converted)($2)
ext_store_vram16:
ext $4, $4, 0, 17 # address = adress & 0x1FFFF
lui $1, %hi(0x18000) # $1 = 0x18000
sltu $1, $4, $1 # see if address < 0x18000
bne $1, $0, ext_store_vram16b
lui $2, %hi(vram) # start loading vram address (delay)
addiu $4, $4, -0x8000 # move address into VRAM region
ext_store_vram16b:
addu $2, $2, $4 # $2 = (hi)vram + address
jr $ra # return
sh $5, %lo(vram)($2) # vram[address] = value (delay)
ext_store_oam16:
lui $1, %hi(oam_update) # $1 = oam_update
addiu $1, %lo(oam_update)
li $2, 1 # $2 = 1
sw $2, ($1) # *oam_update = 1
andi $4, $4, 0x3FF # wrap around address
lui $1, %hi(oam_ram) # $1 = (hi)oam_ram
addu $1, $1, $4 # $1 = (hi)oam_ram + address
jr $ra # return
sh $5, %lo(oam_ram)($1) # oam_ram[address] = value (delay)
ext_store_rtc:
andi $5, $5, 0xFFFF # make value 16bit
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp)
save_registers
jal write_rtc # write the value out
sw $6, REG_PC($16) # save the PC (delay slot)
restore_registers
lw $ra, ($sp) # restore return address
jr $ra # we can return
addiu $sp, $sp, 4 # fix the stack
ext_store_u16_jtable:
.long ext_store_ignore # 0x00 BIOS, ignore
.long ext_store_ignore # 0x01 invalid, ignore
.long ext_store_ignore # 0x02 EWRAM, should have been hit already
.long ext_store_ignore # 0x03 IWRAM, should have been hit already
.long ext_store_io16 # 0x04 I/O registers
.long ext_store_palette16 # 0x05 Palette RAM
.long ext_store_vram16 # 0x06 VRAM
.long ext_store_oam16 # 0x07 OAM RAM
.long ext_store_rtc # 0x08 gamepak, RTC
.long ext_store_ignore # 0x09 gamepak, ignore
.long ext_store_ignore # 0x0A gamepak, ignore
.long ext_store_ignore # 0x0B gamepak, ignore
.long ext_store_ignore # 0x0C gamepak, ignore
.long ext_store_eeprom # 0x0D EEPROM (possibly)
.long ext_store_ignore # 0x0E Flash ROM/SRAM
ext_store_u16:
srl $1, $4, 24 # $1 = address >> 24
sltu $2, $1, 16 # check if the value is out of range
beq $2, $0, ext_store_ignore
sll $1, $1, 2 # make address word indexed (delay)
lui $2, %hi(ext_store_u16_jtable)
addu $2, $2, $1
# $2 = ext_store_u16_jtable[address >> 24]
lw $2, %lo(ext_store_u16_jtable)($2)
jr $2 # jump to table location
nop
#execute_store_u16:
srl $1, $4, 28 # check if the address is out of range
bne $1, $0, ext_store_u16 # if it is, perform an extended write
srl $2, $4, 15 # $1 = page number of address (delay slot)
sll $2, $2, 2 # adjust to word index
addu $2, $2, $16
lw $1, 256($2) # $1 = memory_map_write[address >> 15]
beq $1, $0, ext_store_u16 # if it's NULL perform an extended write
andi $2, $4, 0x7FFE # $2 = low 15bits of address (delay slot)
addu $1, $1, $2 # add the memory map offset
lh $2, -32768($1) # load the SMC status
bne $2, $0, smc_write # is there code there?
sh $5, ($1) # store the value (delay slot)
jr $ra # return
nop
# 32bit ext memory routines
ext_store_io32:
andi $4, $4, 0x3FF # wrap around address
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp)
save_registers
jal write_io_register32 # write the value out
sw $6, REG_PC($16) # save the PC (delay slot)
j write_io_epilogue # handle any state changes
nop
ext_store_palette32:
addu $6, $ra, $0 # save return address in $6
jal ext_store_palette16b # write out palette entry
andi $4, 0x3FF # wrap address (delay)
addiu $4, $4, 2 # go to next location
srl $5, $5, 16 # shift to next 16bit value
j ext_store_palette16b # write out next palette entry
addu $ra, $6, $0 # restore return address (delay)
ext_store_vram32:
ext $4, $4, 0, 17 # address = adress & 0x1FFFF
lui $1, %hi(0x18000) # $1 = 0x18000
sltu $1, $4, $1 # see if address < 0x18000
bne $1, $0, ext_store_vram32b
lui $2, %hi(vram) # start loading vram address (delay)
addiu $4, $4, -0x8000 # move address into VRAM region
ext_store_vram32b:
addu $2, $2, $4 # $2 = (hi)vram + address
jr $ra # return
sw $5, %lo(vram)($2) # vram[address] = value (delay)
ext_store_oam32:
lui $1, %hi(oam_update) # $1 = oam_update
addiu $1, %lo(oam_update)
li $2, 1 # $2 = 1
sw $2, ($1) # *oam_update = 1
andi $4, $4, 0x3FF # wrap around address
lui $1, %hi(oam_ram) # $1 = (hi)oam_ram
addu $1, $1, $4 # $1 = (hi)oam_ram + address
jr $ra # return
sw $5, %lo(oam_ram)($1) # oam_ram[address] = value (delay)
ext_store_u32_jtable:
.long ext_store_ignore # 0x00 BIOS, ignore
.long ext_store_ignore # 0x01 invalid, ignore
.long ext_store_ignore # 0x02 EWRAM, should have been hit already
.long ext_store_ignore # 0x03 IWRAM, should have been hit already
.long ext_store_io32 # 0x04 I/O registers
.long ext_store_palette32 # 0x05 Palette RAM
.long ext_store_vram32 # 0x06 VRAM
.long ext_store_oam32 # 0x07 OAM RAM
.long ext_store_ignore # 0x08 gamepak, ignore
.long ext_store_ignore # 0x09 gamepak, ignore
.long ext_store_ignore # 0x0A gamepak, ignore
.long ext_store_ignore # 0x0B gamepak, ignore
.long ext_store_ignore # 0x0C gamepak, ignore
.long ext_store_eeprom # 0x0D EEPROM (possibly)
.long ext_store_ignore # 0x0E Flash ROM/SRAM
ext_store_u32:
srl $1, $4, 24 # $1 = address >> 24
sltu $2, $1, 16 # check if the value is out of range
beq $2, $0, ext_store_ignore
sll $1, $1, 2 # make address word indexed (delay)
lui $2, %hi(ext_store_u32_jtable)
addu $2, $2, $1
# $2 = ext_store_u32_jtable[address >> 24]
lw $2, %lo(ext_store_u32_jtable)($2)
jr $2 # jump to table location
nop
#execute_store_u32:
execute_store_full_u32:
srl $1, $4, 28 # check if the address is out of range
bne $1, $0, ext_store_u32 # if it is, perform an extended write
srl $2, $4, 15 # $1 = page number of address (delay slot)
sll $2, $2, 2 # adjust to word index
addu $2, $2, $16
lw $1, 256($2) # $1 = memory_map_write[address >> 15]
beq $1, $0, ext_store_u32 # if it's NULL perform an extended write
andi $2, $4, 0x7FFC # $2 = low 15bits of address (delay slot)
addu $1, $1, $2 # add the memory map offset
lw $2, -32768($1) # load the SMC status
bne $2, $0, smc_write # is there code there?
sw $5, ($1) # store the value (delay slot)
jr $ra # return
nop
# 32bit ext aligned, non a2 destroying routines
ext_store_io32a:
andi $4, $4, 0x3FF # wrap around address
addiu $sp, $sp, -4 # make room on the stack for $ra
sw $ra, ($sp)
save_registers
jal write_io_register32 # write the value out
sw $6, REG_SAVE($16) # save a2
lw $6, REG_SAVE($16) # restore a2
j write_io_epilogue # handle any state changes
nop
ext_store_palette32a:
sw $ra, REG_SAVE($16) # save return address
jal ext_store_palette16b # write out palette entry
andi $4, 0x3FF # wrap address (delay)
addiu $4, $4, 2 # go to next location
srl $5, $5, 16 # shift to next 16bit value
j ext_store_palette16b # write out next palette entry
lw $ra, REG_SAVE($16) # restore return address (delay)
ext_store_u32a_jtable:
.long ext_store_ignore # 0x00 BIOS, ignore
.long ext_store_ignore # 0x01 invalid, ignore
.long ext_store_ignore # 0x02 EWRAM, should have been hit already
.long ext_store_ignore # 0x03 IWRAM, should have been hit already
.long ext_store_io32a # 0x04 I/O registers
.long ext_store_palette32a # 0x05 Palette RAM
.long ext_store_vram32 # 0x06 VRAM
.long ext_store_oam32 # 0x07 OAM RAM
.long ext_store_ignore # 0x08 gamepak, ignore
.long ext_store_ignore # 0x09 gamepak, ignore
.long ext_store_ignore # 0x0A gamepak, ignore
.long ext_store_ignore # 0x0B gamepak, ignore
.long ext_store_ignore # 0x0C gamepak, ignore
.long ext_store_ignore # 0x0D EEPROM (nothing will write this)
.long ext_store_ignore # 0x0E Flash ROM/SRAM
ext_aligned_store32:
srl $1, $4, 24 # $1 = address >> 24
sltu $2, $1, 16 # check if the value is out of range
beq $2, $0, ext_store_ignore
sll $1, $1, 2 # make address word indexed (delay)
lui $2, %hi(ext_store_u32a_jtable)
addu $2, $2, $1
# $2 = ext_store_u32a_jtable[address >> 24]
lw $2, %lo(ext_store_u32a_jtable)($2)
jr $2 # jump to table location
nop
#execute_aligned_store32:
srl $2, $4, 28 # check if the address is out of range
bne $2, $0, ext_aligned_store32 # if it is, perform an extended load
srl $1, $4, 15 # $1 = page number of address
sll $1, $1, 2 # adjust to word index
addu $1, $1, $16 # $1 = memory_map_write[address >> 15]
lw $1, 256($1)
beq $1, $0, ext_aligned_store32 # if it's NULL perform an extended write
andi $2, $4, 0x7FFF # $2 = low 15bits of address (delay slot)
addu $1, $1, $2 # add the memory map offset
jr $ra # return
sw $5, ($1) # write the value
smc_write:
save_registers
jal flush_translation_cache_ram # flush translation cache
sw $6, REG_PC($16) # save PC (delay slot)
lookup_pc:
lw $2, REG_CPSR($16) # $2 = cpsr
andi $2, $2, 0x20 # isolate mode bit
beq $2, $0, lookup_pc_arm # if T bit is zero use arm handler
nop
lookup_pc_thumb:
jal block_lookup_address_thumb # get Thumb address
lw $4, REG_PC($16) # load PC as arg 0 (delay slot)
restore_registers
jr $2 # jump to result
nop
lookup_pc_arm:
jal block_lookup_address_arm # get ARM address
lw $4, REG_PC($16) # load PC as arg 0 (delay slot)
restore_registers
jr $2 # jump to result
nop
# Return the current cpsr
execute_read_cpsr:
collapse_flags # fold flags into cpsr, put cpsr into $2
jr $ra # return
nop
# Return the current spsr
execute_read_spsr:
lw $1, CPU_MODE($16) # $1 = cpu_mode
lui $2, %hi(spsr)
sll $1, $1, 2 # adjust to word offset size
addu $2, $2, $1
jr $ra # return
lw $2, %lo(spsr)($2) # $2 = spsr[cpu_mode] (delay slot)
# Switch into SWI, has to collapse flags
# $4: Current pc
execute_swi:
add $sp, $sp, -4 # push $ra
sw $ra, ($sp)
lui $1, %hi(SUPERVISOR_LR)
sw $4, %lo(SUPERVISOR_LR)($1) # store next PC in the supervisor's LR
collapse_flags # get cpsr in $2
lui $5, %hi(SUPERVISOR_SPSR)
sw $2, %lo(SUPERVISOR_SPSR)($5) # save cpsr in SUPERVISOR_CPSR
ins $2, $0, 0, 6 # zero out bottom 6 bits of CPSR
ori $2, 0x13 # set mode to supervisor
sw $2, REG_CPSR($16) # write back CPSR
save_registers
jal set_cpu_mode # set the CPU mode to supervisor
li $4, 3 # 3 is supervisor mode (delay slot)
restore_registers
lw $ra, ($sp) # pop $ra
jr $ra # return
add $sp, $sp, 4 # fix stack (delay slot)
# $4: pc to restore to
# returns in $4
execute_spsr_restore:
lw $1, CPU_MODE($16) # $1 = cpu_mode
beq $1, $0, no_spsr_restore # only restore if the cpu isn't usermode
lui $2, %hi(spsr) # start loading SPSR (delay)
sll $1, $1, 2 # adjust to word offset size
addu $2, $2, $1
lw $1, %lo(spsr)($2) # $1 = spsr[cpu_mode]
sw $1, REG_CPSR($16) # cpsr = spsr[cpu_mode]
extract_flags_body # extract flags from $1
addiu $sp, $sp, -4
sw $ra, ($sp)
save_registers
jal execute_spsr_restore_body # do the dirty work in this C function
nop
restore_registers
addu $4, $2, $0 # move return value to $4
lw $ra, ($sp)
jr $ra
addiu $sp, $sp, 4
no_spsr_restore:
jr $ra
nop
# $4: new cpsr
# $5: store mask
# $6: current PC
execute_store_cpsr:
and $1, $4, $5 # $1 = new_cpsr & store_mask
lw $2, REG_CPSR($16) # $2 = current cpsr
nor $4, $5, $0 # $4 = ~store_mask
and $2, $2, $4 # $2 = (cpsr & (~store_mask))
or $1, $1, $2 # $1 = new cpsr combined with old
extract_flags_body # extract flags from $1
addiu $sp, $sp, -4
sw $ra, ($sp)
save_registers
jal execute_store_cpsr_body # do the dirty work in this C function
addu $4, $1, $0 # load the new CPSR (delay slot)
bne $2, $0, changed_pc_cpsr # this could have changed the pc
nop
restore_registers
lw $ra, ($sp)
jr $ra
addiu $sp, $sp, 4
changed_pc_cpsr:
jal block_lookup_address_arm # GBA address is in $4
addu $4, $2, $0 # load new address in $4 (delay slot)
restore_registers # restore registers
jr $2 # jump to the new address
addiu $sp, $sp, 4 # get rid of the old ra (delay slot)
# $4: new spsr
# $5: store mask
execute_store_spsr:
lw $1, CPU_MODE($16) # $1 = cpu_mode
lui $2, %hi(spsr)
sll $1, $1, 2 # adjust to word offset size
addu $1, $2, $1
lw $2, %lo(spsr)($1) # $2 = spsr[cpu_mode]
and $4, $4, $5 # $4 = new_spsr & store_mask
nor $5, $5, $0 # $5 = ~store_mask
and $2, $2, $5 # $2 = (spsr & (~store_mask))
or $4, $4, $2 # $4 = new spsr combined with old
jr $ra # return
sw $4, %lo(spsr)($1) # spsr[cpu_mode] = $4 (delay slot)
# $4: value
# $5: shift
execute_lsl_flags_reg:
beq $5, $0, lsl_shift_zero # is the shift zero?
sltiu $1, $5, 32 # $1 = (shift < 32) (delay)
beq $1, $0, lsl_shift_high # is the shift >= 32?
li $2, 32
subu $2, $2, $5 # $2 = (32 - shift)
srlv $2, $4, $2 # $2 = (value >> (32 - shift))
andi $22, $2, 1 # c flag = (value >> (32 - shift)) & 0x01
lsl_shift_zero:
jr $ra # return
sllv $4, $4, $5 # return (value << shift) (delay)
lsl_shift_high:
sltiu $1, $5, 33 # $1 = (shift < 33) (delay)
bne $1, $0, lsl_shift_done # jump if shift == 32
andi $22, $4, 1 # c flag = value & 0x01 (delay)
add $22, $0, $0 # c flag = 0 otherwise
lsl_shift_done:
jr $ra # return
add $4, $0, $0 # value = 0 no matter what
execute_lsr_flags_reg:
beq $5, $0, lsr_shift_zero # is the shift zero?
sltiu $1, $5, 32 # $1 = (shift < 32) (delay)
beq $1, $0, lsr_shift_high # is the shift >= 32?
addiu $2, $5, -1 # $2 = shift - 1 (delay)
srlv $2, $4, $2 # $2 = (value >> (shift - 1))
andi $22, $2, 1 # c flag = (value >> (32 - shift)) & 0x01
lsr_shift_zero:
jr $ra # return
srlv $4, $4, $5 # return (value >> shift) (delay)
lsr_shift_high:
sltiu $1, $5, 33 # $1 = (shift < 33) (delay)
bne $1, $0, lsr_shift_done # jump if shift == 32
srl $22, $4, 31 # c flag = value >> 31 (delay)
add $22, $0, $0 # c flag = 0 otherwise
lsr_shift_done:
jr $ra # return
add $4, $0, $0 # value = 0 no matter what
execute_asr_flags_reg:
beq $5, $0, asr_shift_zero # is the shift zero?
sltiu $1, $5, 32 # $1 = (shift < 32) (delay)
beq $1, $0, asr_shift_high # is the shift >= 32?
addiu $2, $5, -1 # $2 = shift - 1 (delay)
srlv $2, $4, $2 # $2 = (value >> (shift - 1))
andi $22, $2, 1 # c flag = (value >> (32 - shift)) & 0x01
asr_shift_zero:
jr $ra # return
srav $4, $4, $5 # return (value >> shift) (delay)
asr_shift_high:
sra $4, $4, 31 # value >>= 31
jr $ra # return
andi $22, $4, 1 # c flag = value & 0x01
execute_ror_flags_reg:
beq $5, $0, ror_zero_shift # is the shift zero?
addiu $1, $5, -1 # $1 = (shift - 1) (delay)
srav $1, $4, $1 # $1 = (value >> (shift - 1))
andi $22, $1, 1 # c flag = $1 & 1
ror_zero_shift:
jr $ra # return
rotrv $4, $4, $5 # return (value ror shift) delay
# $4: cycle counter argument
execute_arm_translate:
addu $17, $4, $0 # load cycle counter register
lui $16, %hi(reg) # load base register
addiu $16, %lo(reg)
extract_flags # load flag variables
and $1, $1, 0x20 # see if Thumb bit is set in flags
bne $1, $0, 1f
lw $4, REG_PC($16) # load PC into $4 (delay)
jal block_lookup_address_arm # lookup initial jump address
nop
restore_registers # load initial register values
jr $2 # jump to return
nop
1:
jal block_lookup_address_thumb # lookup initial jump address
nop
restore_registers # load initial register values
jr $2 # jump to return
nop
# sceKernelInvalidateIcacheRange gives me problems, trying this instead
# Invalidates an n byte region starting at the start address
# $4: start location
# $5: length
invalidate_icache_region:
ins $4, $0, 0, 6 # align to 64 bytes
addiu $2, $5, 63 # align up to 64 bytes
srl $2, $2, 6 # divide by 64
beq $2, $0, done # exit early on 0
nop
iir_loop:
cache 0x08, ($4) # hit invalidate icache line
addiu $2, $2, -1 # next loop iteration
bne $2, $0, iir_loop # loop
addiu $4, $4, 64 # go to next cache line (delay slot)
done:
jr $ra # return
nop
# Writes back dcache and invalidates icache.
invalidate_all_cache:
addu $4, $0, $0 # $4 = 0
addiu $5, $0, 0x4000 # $5 = 0x4000
iac_loop:
cache 0x14, 0($4) # index invalidate/writeback dcache index
addiu $4, $4, 0x40 # goto next cache line
bne $4, $5, iac_loop # next iteration
cache 0x04, -0x40($4) # index invalidate icache index.. maybe?
jr $ra # return
nop
memory_map_read:
.space 0x8000
reg:
.space 0x100
memory_map_write:
.space 0x8000
|