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
|
/* 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
*/
#ifndef X86_EMIT_H
#define X86_EMIT_H
u32 x86_update_gba(u32 pc);
// Although these are defined as a function, don't call them as
// such (jump to it instead)
void x86_indirect_branch_arm(u32 address);
void x86_indirect_branch_thumb(u32 address);
void x86_indirect_branch_dual(u32 address);
void execute_store_cpsr(u32 new_cpsr, u32 store_mask);
typedef enum
{
x86_reg_number_eax,
x86_reg_number_ecx,
x86_reg_number_edx,
x86_reg_number_ebx,
x86_reg_number_esp,
x86_reg_number_ebp,
x86_reg_number_esi,
x86_reg_number_edi
} x86_reg_number;
#define x86_emit_byte(value) \
*translation_ptr = value; \
translation_ptr++ \
#define x86_emit_dword(value) \
*((u32 *)translation_ptr) = value; \
translation_ptr += 4 \
typedef enum
{
x86_mod_mem = 0,
x86_mod_mem_disp8 = 1,
x86_mod_mem_disp32 = 2,
x86_mod_reg = 3
} x86_mod;
#define x86_emit_mod_rm(mod, rm, spare) \
x86_emit_byte((mod << 6) | (spare << 3) | rm) \
#define x86_emit_mem_op(dest, base, offset) \
if(offset == 0) \
{ \
x86_emit_mod_rm(x86_mod_mem, base, dest); \
} \
else \
\
if(((s32)offset < 127) && ((s32)offset > -128)) \
{ \
x86_emit_mod_rm(x86_mod_mem_disp8, base, dest); \
x86_emit_byte((s8)offset); \
} \
else \
{ \
x86_emit_mod_rm(x86_mod_mem_disp32, base, dest); \
x86_emit_dword(offset); \
} \
#define x86_emit_reg_op(dest, source) \
x86_emit_mod_rm(x86_mod_reg, source, dest) \
typedef enum
{
x86_opcode_mov_rm_reg = 0x89,
x86_opcode_mov_reg_rm = 0x8B,
x86_opcode_mov_reg_imm = 0xB8,
x86_opcode_mov_rm_imm = 0x00C7,
x86_opcode_ror_reg_imm = 0x01C1,
x86_opcode_shl_reg_imm = 0x04C1,
x86_opcode_shr_reg_imm = 0x05C1,
x86_opcode_sar_reg_imm = 0x07C1,
x86_opcode_push_reg = 0x50,
x86_opcode_push_rm = 0xFF,
x86_opcode_push_imm = 0x0668,
x86_opcode_call_offset = 0xE8,
x86_opcode_ret = 0xC3,
x86_opcode_test_rm_imm = 0x00F7,
x86_opcode_test_reg_rm = 0x85,
x86_opcode_mul_eax_rm = 0x04F7,
x86_opcode_imul_eax_rm = 0x05F7,
x86_opcode_idiv_eax_rm = 0x07F7,
x86_opcode_add_rm_imm = 0x0081,
x86_opcode_and_rm_imm = 0x0481,
x86_opcode_sub_rm_imm = 0x0581,
x86_opcode_xor_rm_imm = 0x0681,
x86_opcode_add_reg_rm = 0x03,
x86_opcode_adc_reg_rm = 0x13,
x86_opcode_or_reg_rm = 0x0B,
x86_opcode_sub_reg_rm = 0x2B,
x86_opcode_xor_reg_rm = 0x33,
x86_opcode_cmp_reg_rm = 0x39,
x86_opcode_cmp_rm_imm = 0x053B,
x86_opcode_lea_reg_rm = 0x8D,
x86_opcode_j = 0x80,
x86_opcode_jmp = 0xE9,
x86_opcode_jmp_reg = 0x04FF,
x86_opcode_ext = 0x0F
} x86_opcodes;
typedef enum
{
x86_condition_code_o = 0x00,
x86_condition_code_no = 0x01,
x86_condition_code_c = 0x02,
x86_condition_code_nc = 0x03,
x86_condition_code_z = 0x04,
x86_condition_code_nz = 0x05,
x86_condition_code_na = 0x06,
x86_condition_code_a = 0x07,
x86_condition_code_s = 0x08,
x86_condition_code_ns = 0x09,
x86_condition_code_p = 0x0A,
x86_condition_code_np = 0x0B,
x86_condition_code_l = 0x0C,
x86_condition_code_nl = 0x0D,
x86_condition_code_ng = 0x0E,
x86_condition_code_g = 0x0F
} x86_condition_codes;
#define x86_relative_offset(source, offset, next) \
((u32)offset - ((u32)source + next)) \
#define x86_unequal_operands(op_a, op_b) \
(x86_reg_number_##op_a != x86_reg_number_##op_b) \
#define x86_emit_opcode_1b_reg(opcode, dest, source) \
{ \
x86_emit_byte(x86_opcode_##opcode); \
x86_emit_reg_op(x86_reg_number_##dest, x86_reg_number_##source); \
} \
#define x86_emit_opcode_1b_mem(opcode, dest, base, offset) \
{ \
x86_emit_byte(x86_opcode_##opcode); \
x86_emit_mem_op(x86_reg_number_##dest, x86_reg_number_##base, offset); \
} \
#define x86_emit_opcode_1b(opcode, reg) \
x86_emit_byte(x86_opcode_##opcode | x86_reg_number_##reg) \
#define x86_emit_opcode_1b_ext_reg(opcode, dest) \
x86_emit_byte(x86_opcode_##opcode & 0xFF); \
x86_emit_reg_op(x86_opcode_##opcode >> 8, x86_reg_number_##dest) \
#define x86_emit_opcode_1b_ext_mem(opcode, base, offset) \
x86_emit_byte(x86_opcode_##opcode & 0xFF); \
x86_emit_mem_op(x86_opcode_##opcode >> 8, x86_reg_number_##base, offset) \
#define x86_emit_mov_reg_mem(dest, base, offset) \
x86_emit_opcode_1b_mem(mov_reg_rm, dest, base, offset) \
#define x86_emit_mov_mem_reg(source, base, offset) \
x86_emit_opcode_1b_mem(mov_rm_reg, source, base, offset) \
#define x86_emit_mov_reg_reg(dest, source) \
if(x86_unequal_operands(dest, source)) \
{ \
x86_emit_opcode_1b_reg(mov_reg_rm, dest, source) \
} \
#define x86_emit_mov_reg_imm(dest, imm) \
x86_emit_opcode_1b(mov_reg_imm, dest); \
x86_emit_dword(imm) \
#define x86_emit_mov_mem_imm(imm, base, offset) \
x86_emit_opcode_1b_ext_mem(mov_rm_imm, base, offset); \
x86_emit_dword(imm) \
#define x86_emit_shl_reg_imm(dest, imm) \
x86_emit_opcode_1b_ext_reg(shl_reg_imm, dest); \
x86_emit_byte(imm) \
#define x86_emit_shr_reg_imm(dest, imm) \
x86_emit_opcode_1b_ext_reg(shr_reg_imm, dest); \
x86_emit_byte(imm) \
#define x86_emit_sar_reg_imm(dest, imm) \
x86_emit_opcode_1b_ext_reg(sar_reg_imm, dest); \
x86_emit_byte(imm) \
#define x86_emit_ror_reg_imm(dest, imm) \
x86_emit_opcode_1b_ext_reg(ror_reg_imm, dest); \
x86_emit_byte(imm) \
#define x86_emit_add_reg_reg(dest, source) \
x86_emit_opcode_1b_reg(add_reg_rm, dest, source) \
#define x86_emit_adc_reg_reg(dest, source) \
x86_emit_opcode_1b_reg(adc_reg_rm, dest, source) \
#define x86_emit_sub_reg_reg(dest, source) \
x86_emit_opcode_1b_reg(sub_reg_rm, dest, source) \
#define x86_emit_or_reg_reg(dest, source) \
x86_emit_opcode_1b_reg(or_reg_rm, dest, source) \
#define x86_emit_xor_reg_reg(dest, source) \
x86_emit_opcode_1b_reg(xor_reg_rm, dest, source) \
#define x86_emit_add_reg_imm(dest, imm) \
if(imm != 0) \
{ \
x86_emit_opcode_1b_ext_reg(add_rm_imm, dest); \
x86_emit_dword(imm); \
} \
#define x86_emit_sub_reg_imm(dest, imm) \
if(imm != 0) \
{ \
x86_emit_opcode_1b_ext_reg(sub_rm_imm, dest); \
x86_emit_dword(imm); \
} \
#define x86_emit_and_reg_imm(dest, imm) \
x86_emit_opcode_1b_ext_reg(and_rm_imm, dest); \
x86_emit_dword(imm) \
#define x86_emit_xor_reg_imm(dest, imm) \
x86_emit_opcode_1b_ext_reg(xor_rm_imm, dest); \
x86_emit_dword(imm) \
#define x86_emit_test_reg_imm(dest, imm) \
x86_emit_opcode_1b_ext_reg(test_rm_imm, dest); \
x86_emit_dword(imm) \
#define x86_emit_cmp_reg_reg(dest, source) \
x86_emit_opcode_1b_reg(cmp_reg_rm, dest, source) \
#define x86_emit_test_reg_reg(dest, source) \
x86_emit_opcode_1b_reg(test_reg_rm, dest, source) \
#define x86_emit_cmp_reg_imm(dest, imm) \
x86_emit_opcode_1b_ext_reg(cmp_rm_imm, dest); \
x86_emit_dword(imm) \
#define x86_emit_mul_eax_reg(source) \
x86_emit_opcode_1b_ext_reg(mul_eax_rm, source) \
#define x86_emit_imul_eax_reg(source) \
x86_emit_opcode_1b_ext_reg(imul_eax_rm, source) \
#define x86_emit_idiv_eax_reg(source) \
x86_emit_opcode_1b_ext_reg(idiv_eax_rm, source) \
#define x86_emit_push_mem(base, offset) \
x86_emit_opcode_1b_mem(push_rm, 0x06, base, offset) \
#define x86_emit_push_imm(imm) \
x86_emit_byte(x86_opcode_push_imm); \
x86_emit_dword(imm) \
#define x86_emit_call_offset(relative_offset) \
x86_emit_byte(x86_opcode_call_offset); \
x86_emit_dword(relative_offset) \
#define x86_emit_ret() \
x86_emit_byte(x86_opcode_ret) \
#define x86_emit_lea_reg_mem(dest, base, offset) \
x86_emit_opcode_1b_mem(lea_reg_rm, dest, base, offset) \
#define x86_emit_j_filler(condition_code, writeback_location) \
x86_emit_byte(x86_opcode_ext); \
x86_emit_byte(x86_opcode_j | condition_code); \
(writeback_location) = translation_ptr; \
translation_ptr += 4 \
#define x86_emit_j_offset(condition_code, offset) \
x86_emit_byte(x86_opcode_ext); \
x86_emit_byte(x86_opcode_j | condition_code); \
x86_emit_dword(offset) \
#define x86_emit_jmp_filler(writeback_location) \
x86_emit_byte(x86_opcode_jmp); \
(writeback_location) = translation_ptr; \
translation_ptr += 4 \
#define x86_emit_jmp_offset(offset) \
x86_emit_byte(x86_opcode_jmp); \
x86_emit_dword(offset) \
#define x86_emit_jmp_reg(source) \
x86_emit_opcode_1b_ext_reg(jmp_reg, source) \
#define reg_base ebx
#define reg_cycles edi
#define reg_a0 eax
#define reg_a1 edx
#define reg_a2 ecx
#define reg_rv eax
#define reg_s0 esi
#define generate_load_reg(ireg, reg_index) \
x86_emit_mov_reg_mem(reg_##ireg, reg_base, reg_index * 4); \
#define generate_load_pc(ireg, new_pc) \
x86_emit_mov_reg_imm(reg_##ireg, new_pc) \
#define generate_load_imm(ireg, imm) \
x86_emit_mov_reg_imm(reg_##ireg, imm) \
#define generate_store_reg(ireg, reg_index) \
x86_emit_mov_mem_reg(reg_##ireg, reg_base, reg_index * 4) \
#define generate_shift_left(ireg, imm) \
x86_emit_shl_reg_imm(reg_##ireg, imm) \
#define generate_shift_right(ireg, imm) \
x86_emit_shr_reg_imm(reg_##ireg, imm) \
#define generate_shift_right_arithmetic(ireg, imm) \
x86_emit_sar_reg_imm(reg_##ireg, imm) \
#define generate_rotate_right(ireg, imm) \
x86_emit_ror_reg_imm(reg_##ireg, imm) \
#define generate_add(ireg_dest, ireg_src) \
x86_emit_add_reg_reg(reg_##ireg_dest, reg_##ireg_src) \
#define generate_sub(ireg_dest, ireg_src) \
x86_emit_sub_reg_reg(reg_##ireg_dest, reg_##ireg_src) \
#define generate_or(ireg_dest, ireg_src) \
x86_emit_or_reg_reg(reg_##ireg_dest, reg_##ireg_src) \
#define generate_xor(ireg_dest, ireg_src) \
x86_emit_xor_reg_reg(reg_##ireg_dest, reg_##ireg_src) \
#define generate_add_imm(ireg, imm) \
x86_emit_add_reg_imm(reg_##ireg, imm) \
#define generate_sub_imm(ireg, imm) \
x86_emit_sub_reg_imm(reg_##ireg, imm) \
#define generate_xor_imm(ireg, imm) \
x86_emit_xor_reg_imm(reg_##ireg, imm) \
#define generate_add_reg_reg_imm(ireg_dest, ireg_src, imm) \
x86_emit_lea_reg_mem(reg_##ireg_dest, reg_##ireg_src, imm) \
#define generate_and_imm(ireg, imm) \
x86_emit_and_reg_imm(reg_##ireg, imm) \
#define generate_mov(ireg_dest, ireg_src) \
x86_emit_mov_reg_reg(reg_##ireg_dest, reg_##ireg_src) \
#define generate_multiply(ireg) \
x86_emit_imul_eax_reg(reg_##ireg) \
#define generate_multiply_s64(ireg) \
x86_emit_imul_eax_reg(reg_##ireg) \
#define generate_multiply_u64(ireg) \
x86_emit_mul_eax_reg(reg_##ireg) \
#define generate_multiply_s64_add(ireg_src, ireg_lo, ireg_hi) \
x86_emit_imul_eax_reg(reg_##ireg_src); \
x86_emit_add_reg_reg(reg_a0, reg_##ireg_lo); \
x86_emit_adc_reg_reg(reg_a1, reg_##ireg_hi) \
#define generate_multiply_u64_add(ireg_src, ireg_lo, ireg_hi) \
x86_emit_mul_eax_reg(reg_##ireg_src); \
x86_emit_add_reg_reg(reg_a0, reg_##ireg_lo); \
x86_emit_adc_reg_reg(reg_a1, reg_##ireg_hi) \
#define generate_function_call(function_location) \
x86_emit_call_offset(x86_relative_offset(translation_ptr, \
function_location, 4)); \
#define generate_exit_block() \
x86_emit_ret(); \
#define generate_branch_filler_true(ireg_dest, ireg_src, writeback_location) \
x86_emit_test_reg_imm(reg_##ireg_dest, 1); \
x86_emit_j_filler(x86_condition_code_z, writeback_location) \
#define generate_branch_filler_false(ireg_dest, ireg_src, writeback_location) \
x86_emit_test_reg_imm(reg_##ireg_dest, 1); \
x86_emit_j_filler(x86_condition_code_nz, writeback_location) \
#define generate_branch_filler_equal(ireg_dest, ireg_src, writeback_location) \
x86_emit_cmp_reg_reg(reg_##ireg_dest, reg_##ireg_src); \
x86_emit_j_filler(x86_condition_code_nz, writeback_location) \
#define generate_branch_filler_not_equal(ireg_dest, ireg_src, \
writeback_location) \
x86_emit_cmp_reg_reg(reg_##ireg_dest, reg_##ireg_src); \
x86_emit_j_filler(x86_condition_code_z, writeback_location) \
#define generate_update_pc(new_pc) \
x86_emit_mov_reg_imm(eax, new_pc) \
#define generate_update_pc_reg() \
generate_update_pc(pc); \
generate_store_reg(a0, REG_PC) \
#define generate_cycle_update() \
x86_emit_sub_reg_imm(reg_cycles, cycle_count); \
cycle_count = 0 \
#define generate_branch_patch_conditional(dest, offset) \
*((u32 *)(dest)) = x86_relative_offset(dest, offset, 4) \
#define generate_branch_patch_unconditional(dest, offset) \
*((u32 *)(dest)) = x86_relative_offset(dest, offset, 4) \
#define generate_branch_no_cycle_update(writeback_location, new_pc) \
if(pc == idle_loop_target_pc) \
{ \
x86_emit_mov_reg_imm(eax, new_pc); \
generate_function_call(x86_update_gba); \
x86_emit_jmp_filler(writeback_location); \
} \
else \
{ \
x86_emit_test_reg_reg(reg_cycles, reg_cycles); \
x86_emit_j_offset(x86_condition_code_ns, 10); \
x86_emit_mov_reg_imm(eax, new_pc); \
generate_function_call(x86_update_gba); \
x86_emit_jmp_filler(writeback_location); \
} \
#define generate_branch_cycle_update(writeback_location, new_pc) \
generate_cycle_update(); \
generate_branch_no_cycle_update(writeback_location, new_pc) \
#define generate_conditional_branch(ireg_a, ireg_b, type, writeback_location) \
generate_branch_filler_##type(ireg_a, ireg_b, writeback_location) \
// a0 holds the destination
#define generate_indirect_branch_cycle_update(type) \
generate_cycle_update(); \
x86_emit_jmp_offset(x86_relative_offset(translation_ptr, \
x86_indirect_branch_##type, 4)) \
#define generate_indirect_branch_no_cycle_update(type) \
x86_emit_jmp_offset(x86_relative_offset(translation_ptr, \
x86_indirect_branch_##type, 4)) \
#define generate_block_prologue() \
#define generate_block_extra_vars_arm() \
void generate_indirect_branch_arm(void) \
{ \
if(condition == 0x0E) \
{ \
generate_indirect_branch_cycle_update(arm); \
} \
else \
{ \
generate_indirect_branch_no_cycle_update(arm); \
} \
} \
\
void generate_indirect_branch_dual() \
{ \
if(condition == 0x0E) \
{ \
generate_indirect_branch_cycle_update(dual); \
} \
else \
{ \
generate_indirect_branch_no_cycle_update(dual); \
} \
} \
#define generate_block_extra_vars_thumb() \
#define block_prologue_size 0
#define calculate_z_flag(dest) \
reg[REG_Z_FLAG] = (dest == 0) \
#define calculate_n_flag(dest) \
reg[REG_N_FLAG] = ((signed)dest < 0) \
#define calculate_c_flag_sub(dest, src_a, src_b) \
reg[REG_C_FLAG] = ((unsigned)src_b <= (unsigned)src_a) \
#define calculate_v_flag_sub(dest, src_a, src_b) \
reg[REG_V_FLAG] = ((signed)src_b > (signed)src_a) != ((signed)dest < 0) \
#define calculate_c_flag_add(dest, src_a, src_b) \
reg[REG_C_FLAG] = ((unsigned)dest < (unsigned)src_a) \
#define calculate_v_flag_add(dest, src_a, src_b) \
reg[REG_V_FLAG] = ((signed)dest < (signed)src_a) != ((signed)src_b < 0) \
#define get_shift_imm() \
u32 shift = (opcode >> 7) & 0x1F \
#define generate_shift_reg(ireg, name, flags_op) \
generate_load_reg_pc(ireg, rm, 12); \
generate_load_reg(a1, ((opcode >> 8) & 0x0F)); \
generate_function_call(execute_##name##_##flags_op##_reg); \
generate_mov(ireg, rv) \
u32 execute_lsl_no_flags_reg(u32 value, u32 shift)
{
if(shift != 0)
{
if(shift > 31)
value = 0;
else
value <<= shift;
}
return value;
}
u32 execute_lsr_no_flags_reg(u32 value, u32 shift)
{
if(shift != 0)
{
if(shift > 31)
value = 0;
else
value >>= shift;
}
return value;
}
u32 execute_asr_no_flags_reg(u32 value, u32 shift)
{
if(shift != 0)
{
if(shift > 31)
value = (s32)value >> 31;
else
value = (s32)value >> shift;
}
return value;
}
u32 execute_ror_no_flags_reg(u32 value, u32 shift)
{
if(shift != 0)
{
ror(value, value, shift);
}
return value;
}
u32 execute_lsl_flags_reg(u32 value, u32 shift)
{
if(shift != 0)
{
if(shift > 31)
{
reg[REG_C_FLAG] = value & 0x01;
if(shift != 32)
reg[REG_C_FLAG] = 0;
value = 0;
}
else
{
reg[REG_C_FLAG] = (value >> (32 - shift)) & 0x01;
value <<= shift;
}
}
return value;
}
u32 execute_lsr_flags_reg(u32 value, u32 shift)
{
if(shift != 0)
{
if(shift > 31)
{
reg[REG_C_FLAG] = value >> 31;
if(shift != 32)
reg[REG_C_FLAG] = 0;
value = 0;
}
else
{
reg[REG_C_FLAG] = (value >> (shift - 1)) & 0x01;
value >>= shift;
}
}
return value;
}
u32 execute_asr_flags_reg(u32 value, u32 shift)
{
if(shift != 0)
{
if(shift > 31)
{
value = (s32)value >> 31;
reg[REG_C_FLAG] = value & 0x01;
}
else
{
reg[REG_C_FLAG] = (value >> (shift - 1)) & 0x01;
value = (s32)value >> shift;
}
}
return value;
}
u32 execute_ror_flags_reg(u32 value, u32 shift)
{
if(shift != 0)
{
reg[REG_C_FLAG] = (value >> (shift - 1)) & 0x01;
ror(value, value, shift);
}
return value;
}
u32 execute_rrx_flags(u32 value)
{
u32 c_flag = reg[REG_C_FLAG];
reg[REG_C_FLAG] = value & 0x01;
return (value >> 1) | (c_flag << 31);
}
u32 execute_rrx(u32 value)
{
return (value >> 1) | (reg[REG_C_FLAG] << 31);
}
#define generate_shift_imm_lsl_no_flags(ireg) \
generate_load_reg_pc(ireg, rm, 8); \
if(shift != 0) \
{ \
generate_shift_left(ireg, shift); \
} \
#define generate_shift_imm_lsr_no_flags(ireg) \
if(shift != 0) \
{ \
generate_load_reg_pc(ireg, rm, 8); \
generate_shift_right(ireg, shift); \
} \
else \
{ \
generate_load_imm(ireg, 0); \
} \
#define generate_shift_imm_asr_no_flags(ireg) \
generate_load_reg_pc(ireg, rm, 8); \
if(shift != 0) \
{ \
generate_shift_right_arithmetic(ireg, shift); \
} \
else \
{ \
generate_shift_right_arithmetic(ireg, 31); \
} \
#define generate_shift_imm_ror_no_flags(ireg) \
if(shift != 0) \
{ \
generate_load_reg_pc(ireg, rm, 8); \
generate_rotate_right(ireg, shift); \
} \
else \
{ \
generate_load_reg_pc(a0, rm, 8); \
generate_function_call(execute_rrx); \
generate_mov(ireg, rv); \
} \
#define generate_shift_imm_lsl_flags(ireg) \
generate_load_reg_pc(ireg, rm, 8); \
if(shift != 0) \
{ \
generate_mov(a1, ireg); \
generate_shift_right(a1, (32 - shift)); \
generate_and_imm(a1, 1); \
generate_store_reg(a1, REG_C_FLAG); \
generate_shift_left(ireg, shift); \
} \
#define generate_shift_imm_lsr_flags(ireg) \
if(shift != 0) \
{ \
generate_load_reg_pc(ireg, rm, 8); \
generate_mov(a1, ireg); \
generate_shift_right(a1, shift - 1); \
generate_and_imm(a1, 1); \
generate_store_reg(a1, REG_C_FLAG); \
generate_shift_right(ireg, shift); \
} \
else \
{ \
generate_load_reg_pc(a1, rm, 8); \
generate_shift_right(a1, 31); \
generate_store_reg(a1, REG_C_FLAG); \
generate_load_imm(ireg, 0); \
} \
#define generate_shift_imm_asr_flags(ireg) \
if(shift != 0) \
{ \
generate_load_reg_pc(ireg, rm, 8); \
generate_mov(a1, ireg); \
generate_shift_right_arithmetic(a1, shift - 1); \
generate_and_imm(a1, 1); \
generate_store_reg(a1, REG_C_FLAG); \
generate_shift_right_arithmetic(ireg, shift); \
} \
else \
{ \
generate_load_reg_pc(a0, rm, 8); \
generate_shift_right_arithmetic(ireg, 31); \
generate_mov(a1, ireg); \
generate_and_imm(a1, 1); \
generate_store_reg(a1, REG_C_FLAG); \
} \
#define generate_shift_imm_ror_flags(ireg) \
generate_load_reg_pc(ireg, rm, 8); \
if(shift != 0) \
{ \
generate_mov(a1, ireg); \
generate_shift_right(a1, shift - 1); \
generate_and_imm(a1, 1); \
generate_store_reg(a1, REG_C_FLAG); \
generate_rotate_right(ireg, shift); \
} \
else \
{ \
generate_function_call(execute_rrx_flags); \
generate_mov(ireg, rv); \
} \
#define generate_shift_imm(ireg, name, flags_op) \
get_shift_imm(); \
generate_shift_imm_##name##_##flags_op(ireg) \
#define generate_load_rm_sh(flags_op) \
switch((opcode >> 4) & 0x07) \
{ \
/* LSL imm */ \
case 0x0: \
{ \
generate_shift_imm(a0, lsl, flags_op); \
break; \
} \
\
/* LSL reg */ \
case 0x1: \
{ \
generate_shift_reg(a0, lsl, flags_op); \
break; \
} \
\
/* LSR imm */ \
case 0x2: \
{ \
generate_shift_imm(a0, lsr, flags_op); \
break; \
} \
\
/* LSR reg */ \
case 0x3: \
{ \
generate_shift_reg(a0, lsr, flags_op); \
break; \
} \
\
/* ASR imm */ \
case 0x4: \
{ \
generate_shift_imm(a0, asr, flags_op); \
break; \
} \
\
/* ASR reg */ \
case 0x5: \
{ \
generate_shift_reg(a0, asr, flags_op); \
break; \
} \
\
/* ROR imm */ \
case 0x6: \
{ \
generate_shift_imm(a0, ror, flags_op); \
break; \
} \
\
/* ROR reg */ \
case 0x7: \
{ \
generate_shift_reg(a0, ror, flags_op); \
break; \
} \
} \
#define generate_load_offset_sh() \
switch((opcode >> 5) & 0x03) \
{ \
/* LSL imm */ \
case 0x0: \
{ \
generate_shift_imm(a1, lsl, no_flags); \
break; \
} \
\
/* LSR imm */ \
case 0x1: \
{ \
generate_shift_imm(a1, lsr, no_flags); \
break; \
} \
\
/* ASR imm */ \
case 0x2: \
{ \
generate_shift_imm(a1, asr, no_flags); \
break; \
} \
\
/* ROR imm */ \
case 0x3: \
{ \
generate_shift_imm(a1, ror, no_flags); \
break; \
} \
} \
#define calculate_flags_add(dest, src_a, src_b) \
calculate_z_flag(dest); \
calculate_n_flag(dest); \
calculate_c_flag_add(dest, src_a, src_b); \
calculate_v_flag_add(dest, src_a, src_b) \
#define calculate_flags_sub(dest, src_a, src_b) \
calculate_z_flag(dest); \
calculate_n_flag(dest); \
calculate_c_flag_sub(dest, src_a, src_b); \
calculate_v_flag_sub(dest, src_a, src_b) \
#define calculate_flags_logic(dest) \
calculate_z_flag(dest); \
calculate_n_flag(dest) \
#define extract_flags() \
reg[REG_N_FLAG] = reg[REG_CPSR] >> 31; \
reg[REG_Z_FLAG] = (reg[REG_CPSR] >> 30) & 0x01; \
reg[REG_C_FLAG] = (reg[REG_CPSR] >> 29) & 0x01; \
reg[REG_V_FLAG] = (reg[REG_CPSR] >> 28) & 0x01; \
#define collapse_flags() \
reg[REG_CPSR] = (reg[REG_N_FLAG] << 31) | (reg[REG_Z_FLAG] << 30) | \
(reg[REG_C_FLAG] << 29) | (reg[REG_V_FLAG] << 28) | \
(reg[REG_CPSR] & 0xFF) \
// It should be okay to still generate result flags, spsr will overwrite them.
// This is pretty infrequent (returning from interrupt handlers, et al) so
// probably not worth optimizing for.
#define check_for_interrupts() \
if((io_registers[REG_IE] & io_registers[REG_IF]) && \
io_registers[REG_IME] && ((reg[REG_CPSR] & 0x80) == 0)) \
{ \
reg_mode[MODE_IRQ][6] = reg[REG_PC] + 4; \
spsr[MODE_IRQ] = reg[REG_CPSR]; \
reg[REG_CPSR] = 0xD2; \
address = 0x00000018; \
set_cpu_mode(MODE_IRQ); \
} \
#define generate_load_reg_pc(ireg, reg_index, pc_offset) \
if(reg_index == 15) \
{ \
generate_load_pc(ireg, pc + pc_offset); \
} \
else \
{ \
generate_load_reg(ireg, reg_index); \
} \
#define generate_store_reg_pc_no_flags(ireg, reg_index) \
generate_store_reg(ireg, reg_index); \
if(reg_index == 15) \
{ \
generate_mov(a0, ireg); \
generate_indirect_branch_arm(); \
} \
u32 execute_spsr_restore(u32 address)
{
if(reg[CPU_MODE] != MODE_USER)
{
reg[REG_CPSR] = spsr[reg[CPU_MODE]];
extract_flags();
set_cpu_mode(cpu_modes[reg[REG_CPSR] & 0x1F]);
check_for_interrupts();
if(reg[REG_CPSR] & 0x20)
address |= 0x01;
}
return address;
}
#define generate_store_reg_pc_flags(ireg, reg_index) \
generate_store_reg(ireg, reg_index); \
if(reg_index == 15) \
{ \
generate_mov(a0, ireg); \
generate_function_call(execute_spsr_restore); \
generate_mov(a0, rv); \
generate_indirect_branch_dual(); \
} \
typedef enum
{
CONDITION_TRUE,
CONDITION_FALSE,
CONDITION_EQUAL,
CONDITION_NOT_EQUAL
} condition_check_type;
#define generate_condition_eq(ireg_a, ireg_b) \
generate_load_reg(ireg_a, REG_Z_FLAG); \
condition_check = CONDITION_TRUE \
#define generate_condition_ne(ireg_a, ireg_b) \
generate_load_reg(ireg_a, REG_Z_FLAG); \
condition_check = CONDITION_FALSE \
#define generate_condition_cs(ireg_a, ireg_b) \
generate_load_reg(ireg_a, REG_C_FLAG); \
condition_check = CONDITION_TRUE \
#define generate_condition_cc(ireg_a, ireg_b) \
generate_load_reg(ireg_a, REG_C_FLAG); \
condition_check = CONDITION_FALSE \
#define generate_condition_mi(ireg_a, ireg_b) \
generate_load_reg(ireg_a, REG_N_FLAG); \
condition_check = CONDITION_TRUE \
#define generate_condition_pl(ireg_a, ireg_b) \
generate_load_reg(ireg_a, REG_N_FLAG); \
condition_check = CONDITION_FALSE \
#define generate_condition_vs(ireg_a, ireg_b) \
generate_load_reg(ireg_a, REG_V_FLAG); \
condition_check = CONDITION_TRUE \
#define generate_condition_vc(ireg_a, ireg_b) \
generate_load_reg(ireg_a, REG_V_FLAG); \
condition_check = CONDITION_FALSE \
#define generate_condition_hi(ireg_a, ireg_b) \
generate_load_reg(ireg_a, REG_C_FLAG); \
generate_xor_imm(ireg_a, 1); \
generate_load_reg(ireg_b, REG_Z_FLAG); \
generate_or(ireg_a, ireg_b); \
condition_check = CONDITION_FALSE \
#define generate_condition_ls(ireg_a, ireg_b) \
generate_load_reg(ireg_a, REG_C_FLAG); \
generate_xor_imm(ireg_a, 1); \
generate_load_reg(ireg_b, REG_Z_FLAG); \
generate_or(ireg_a, ireg_b); \
condition_check = CONDITION_TRUE \
#define generate_condition_ge(ireg_a, ireg_b) \
generate_load_reg(ireg_a, REG_N_FLAG); \
generate_load_reg(ireg_b, REG_V_FLAG); \
condition_check = CONDITION_EQUAL \
#define generate_condition_lt(ireg_a, ireg_b) \
generate_load_reg(ireg_a, REG_N_FLAG); \
generate_load_reg(ireg_b, REG_V_FLAG); \
condition_check = CONDITION_NOT_EQUAL \
#define generate_condition_gt(ireg_a, ireg_b) \
generate_load_reg(ireg_a, REG_N_FLAG); \
generate_load_reg(ireg_b, REG_V_FLAG); \
generate_xor(ireg_b, ireg_a); \
generate_load_reg(a0, REG_Z_FLAG); \
generate_or(ireg_a, ireg_b); \
condition_check = CONDITION_FALSE \
#define generate_condition_le(ireg_a, ireg_b) \
generate_load_reg(ireg_a, REG_N_FLAG); \
generate_load_reg(ireg_b, REG_V_FLAG); \
generate_xor(ireg_b, ireg_a); \
generate_load_reg(a0, REG_Z_FLAG); \
generate_or(ireg_a, ireg_b); \
condition_check = CONDITION_TRUE \
#define generate_condition(ireg_a, ireg_b) \
switch(condition) \
{ \
case 0x0: \
generate_condition_eq(ireg_a, ireg_b); \
break; \
\
case 0x1: \
generate_condition_ne(ireg_a, ireg_b); \
break; \
\
case 0x2: \
generate_condition_cs(ireg_a, ireg_b); \
break; \
\
case 0x3: \
generate_condition_cc(ireg_a, ireg_b); \
break; \
\
case 0x4: \
generate_condition_mi(ireg_a, ireg_b); \
break; \
\
case 0x5: \
generate_condition_pl(ireg_a, ireg_b); \
break; \
\
case 0x6: \
generate_condition_vs(ireg_a, ireg_b); \
break; \
\
case 0x7: \
generate_condition_vc(ireg_a, ireg_b); \
break; \
\
case 0x8: \
generate_condition_hi(ireg_a, ireg_b); \
break; \
\
case 0x9: \
generate_condition_ls(ireg_a, ireg_b); \
break; \
\
case 0xA: \
generate_condition_ge(ireg_a, ireg_b); \
break; \
\
case 0xB: \
generate_condition_lt(ireg_a, ireg_b); \
break; \
\
case 0xC: \
generate_condition_gt(ireg_a, ireg_b); \
break; \
\
case 0xD: \
generate_condition_le(ireg_a, ireg_b); \
break; \
\
case 0xE: \
/* AL */ \
break; \
\
case 0xF: \
/* Reserved */ \
break; \
} \
generate_cycle_update() \
#define generate_conditional_branch_type(ireg_a, ireg_b) \
switch(condition_check) \
{ \
case CONDITION_TRUE: \
generate_conditional_branch(ireg_a, ireg_b, true, backpatch_address); \
break; \
\
case CONDITION_FALSE: \
generate_conditional_branch(ireg_a, ireg_b, false, backpatch_address); \
break; \
\
case CONDITION_EQUAL: \
generate_conditional_branch(ireg_a, ireg_b, equal, backpatch_address); \
break; \
\
case CONDITION_NOT_EQUAL: \
generate_conditional_branch(ireg_a, ireg_b, not_equal, \
backpatch_address); \
break; \
} \
#define generate_branch() \
{ \
if(condition == 0x0E) \
{ \
generate_branch_cycle_update( \
block_exits[block_exit_position].branch_source, \
block_exits[block_exit_position].branch_target); \
} \
else \
{ \
generate_branch_no_cycle_update( \
block_exits[block_exit_position].branch_source, \
block_exits[block_exit_position].branch_target); \
} \
block_exit_position++; \
} \
#define rm_op_reg rm
#define rm_op_imm imm
#define arm_data_proc_reg_flags() \
arm_decode_data_proc_reg(opcode); \
if(flag_status & 0x02) \
{ \
generate_load_rm_sh(flags) \
} \
else \
{ \
generate_load_rm_sh(no_flags); \
} \
#define arm_data_proc_reg() \
arm_decode_data_proc_reg(opcode); \
generate_load_rm_sh(no_flags) \
#define arm_data_proc_imm() \
arm_decode_data_proc_imm(opcode); \
ror(imm, imm, imm_ror); \
generate_load_imm(a0, imm) \
#define arm_data_proc_imm_flags() \
arm_decode_data_proc_imm(opcode); \
if((flag_status & 0x02) && (imm_ror != 0)) \
{ \
/* Generate carry flag from integer rotation */ \
generate_load_imm(a0, ((imm >> (imm_ror - 1)) & 0x01)); \
generate_store_reg(a0, REG_C_FLAG); \
} \
ror(imm, imm, imm_ror); \
generate_load_imm(a0, imm) \
#define arm_data_proc(name, type, flags_op) \
{ \
arm_data_proc_##type(); \
generate_load_reg_pc(a1, rn, 8); \
generate_function_call(execute_##name); \
generate_store_reg_pc_##flags_op(rv, rd); \
} \
#define arm_data_proc_test(name, type) \
{ \
arm_data_proc_##type(); \
generate_load_reg_pc(a1, rn, 8); \
generate_function_call(execute_##name); \
} \
#define arm_data_proc_unary(name, type, flags_op) \
{ \
arm_data_proc_##type(); \
generate_function_call(execute_##name); \
generate_store_reg_pc_##flags_op(rv, rd); \
} \
#define arm_data_proc_mov(type) \
{ \
arm_data_proc_##type(); \
generate_store_reg_pc_no_flags(a0, rd); \
} \
static void execute_mul_flags(u32 dest)
{
calculate_z_flag(dest);
calculate_n_flag(dest);
}
#define arm_multiply_flags_yes() \
generate_function_call(execute_mul_flags) \
#define arm_multiply_flags_no(_dest) \
#define arm_multiply_add_no() \
#define arm_multiply_add_yes() \
generate_load_reg(a1, rn); \
generate_add(a0, a1) \
#define arm_multiply(add_op, flags) \
{ \
arm_decode_multiply(); \
generate_load_reg(a0, rm); \
generate_load_reg(a1, rs); \
generate_multiply(a1); \
arm_multiply_add_##add_op(); \
generate_store_reg(a0, rd); \
arm_multiply_flags_##flags(); \
} \
static void execute_mul_long_flags(u32 dest_lo, u32 dest_hi)
{
reg[REG_Z_FLAG] = (dest_lo == 0) & (dest_hi == 0);
calculate_n_flag(dest_hi);
}
#define arm_multiply_long_flags_yes() \
generate_function_call(execute_mul_long_flags) \
#define arm_multiply_long_flags_no(_dest) \
#define arm_multiply_long_add_yes(name) \
generate_load_reg(a2, rdlo); \
generate_load_reg(s0, rdhi); \
generate_multiply_##name(a1, a2, s0) \
#define arm_multiply_long_add_no(name) \
generate_multiply_##name(a1) \
#define arm_multiply_long(name, add_op, flags) \
{ \
arm_decode_multiply_long(); \
generate_load_reg(a0, rm); \
generate_load_reg(a1, rs); \
arm_multiply_long_add_##add_op(name); \
generate_store_reg(a0, rdlo); \
generate_store_reg(a1, rdhi); \
arm_multiply_long_flags_##flags(); \
} \
u32 execute_read_cpsr(void)
{
collapse_flags();
return reg[REG_CPSR];
}
u32 execute_read_spsr(void)
{
collapse_flags();
return spsr[reg[CPU_MODE]];
}
#define arm_psr_read(op_type, psr_reg) \
generate_function_call(execute_read_##psr_reg); \
generate_store_reg(rv, rd) \
// store_mask and address are stored in the SAVE slots, since there's no real
// register space to nicely pass them.
u32 execute_store_cpsr_body(u32 _cpsr)
{
reg[REG_CPSR] = _cpsr;
if(reg[REG_SAVE] & 0xFF)
{
set_cpu_mode(cpu_modes[_cpsr & 0x1F]);
if((io_registers[REG_IE] & io_registers[REG_IF]) &&
io_registers[REG_IME] && ((_cpsr & 0x80) == 0))
{
reg_mode[MODE_IRQ][6] = reg[REG_SAVE2] + 4;
spsr[MODE_IRQ] = _cpsr;
reg[REG_CPSR] = (_cpsr & 0xFFFFFF00) | 0xD2;
set_cpu_mode(MODE_IRQ);
return 0x00000018;
}
}
return 0;
}
void execute_store_spsr(u32 new_spsr, u32 store_mask)
{
u32 _spsr = spsr[reg[CPU_MODE]];
spsr[reg[CPU_MODE]] = (new_spsr & store_mask) | (_spsr & (~store_mask));
}
#define arm_psr_load_new_reg() \
generate_load_reg(a0, rm) \
#define arm_psr_load_new_imm() \
ror(imm, imm, imm_ror); \
generate_load_imm(a0, imm) \
#define arm_psr_store(op_type, psr_reg) \
arm_psr_load_new_##op_type(); \
generate_load_imm(a1, psr_masks[psr_field]); \
generate_load_pc(a2, (pc + 4)); \
generate_function_call(execute_store_##psr_reg) \
#define arm_psr(op_type, transfer_type, psr_reg) \
{ \
arm_decode_psr_##op_type(opcode); \
arm_psr_##transfer_type(op_type, psr_reg); \
} \
#define aligned_address_mask8 0xF0000000
#define aligned_address_mask16 0xF0000001
#define aligned_address_mask32 0xF0000003
#define read_memory(size, type, address, dest) \
{ \
u8 *map; \
\
if(((address >> 24) == 0) && (reg[REG_PC] >= 0x4000)) \
{ \
dest = *((type *)((u8 *)&bios_read_protect + (address & 0x03))); \
} \
else \
\
if(((address & aligned_address_mask##size) == 0) && \
(map = memory_map_read[address >> 15])) \
{ \
dest = *((type *)((u8 *)map + (address & 0x7FFF))); \
} \
else \
{ \
dest = (type)read_memory##size(address); \
} \
} \
#define read_memory_s16(address, dest) \
{ \
u8 *map; \
\
if(((address >> 24) == 0) && (reg[REG_PC] >= 0x4000)) \
{ \
dest = *((s16 *)((u8 *)&bios_read_protect + (address & 0x03))); \
} \
else \
\
if(((address & aligned_address_mask16) == 0) && \
(map = memory_map_read[address >> 15])) \
{ \
dest = *((s16 *)((u8 *)map + (address & 0x7FFF))); \
} \
else \
{ \
dest = (s16)read_memory16_signed(address); \
} \
} \
#define access_memory_generate_read_function(mem_size, mem_type) \
u32 execute_load_##mem_type(u32 address) \
{ \
u32 dest; \
read_memory(mem_size, mem_type, address, dest); \
return dest; \
} \
access_memory_generate_read_function(8, u8);
access_memory_generate_read_function(8, s8);
access_memory_generate_read_function(16, u16);
access_memory_generate_read_function(32, u32);
u32 execute_load_s16(u32 address)
{
u32 dest;
read_memory_s16(address, dest);
return dest;
}
#define access_memory_generate_write_function(mem_size, mem_type) \
void execute_store_##mem_type(u32 address, u32 source) \
{ \
u8 *map; \
\
if(((address & aligned_address_mask##mem_size) == 0) && \
(map = memory_map_write[address >> 15])) \
{ \
*((mem_type *)((u8 *)map + (address & 0x7FFF))) = source; \
} \
else \
{ \
write_memory##mem_size(address, source); \
} \
} \
#define arm_access_memory_load(mem_type) \
cycle_count += 2; \
generate_function_call(execute_load_##mem_type); \
generate_store_reg_pc_no_flags(rv, rd) \
#define arm_access_memory_store(mem_type) \
cycle_count++; \
generate_load_reg_pc(a1, rd, 12); \
generate_load_pc(a2, (pc + 4)); \
generate_function_call(execute_store_##mem_type) \
#define no_op \
#define arm_access_memory_writeback_yes(off_op) \
reg[rn] = address off_op \
#define arm_access_memory_writeback_no(off_op) \
#define load_reg_op reg[rd] \
#define store_reg_op reg_op \
#define arm_access_memory_adjust_op_up add
#define arm_access_memory_adjust_op_down sub
#define arm_access_memory_reverse_op_up sub
#define arm_access_memory_reverse_op_down add
#define arm_access_memory_reg_pre(adjust_dir_op, reverse_dir_op) \
generate_load_reg_pc(a0, rn, 8); \
generate_##adjust_dir_op(a0, a1) \
#define arm_access_memory_reg_pre_wb(adjust_dir_op, reverse_dir_op) \
arm_access_memory_reg_pre(adjust_dir_op, reverse_dir_op); \
generate_store_reg(a0, rn) \
#define arm_access_memory_reg_post(adjust_dir_op, reverse_dir_op) \
generate_load_reg(a0, rn); \
generate_##adjust_dir_op(a0, a1); \
generate_store_reg(a0, rn); \
generate_##reverse_dir_op(a0, a1) \
#define arm_access_memory_imm_pre(adjust_dir_op, reverse_dir_op) \
generate_load_reg_pc(a0, rn, 8); \
generate_##adjust_dir_op##_imm(a0, offset) \
#define arm_access_memory_imm_pre_wb(adjust_dir_op, reverse_dir_op) \
arm_access_memory_imm_pre(adjust_dir_op, reverse_dir_op); \
generate_store_reg(a0, rn) \
#define arm_access_memory_imm_post(adjust_dir_op, reverse_dir_op) \
generate_load_reg(a0, rn); \
generate_##adjust_dir_op##_imm(a0, offset); \
generate_store_reg(a0, rn); \
generate_##reverse_dir_op##_imm(a0, offset) \
#define arm_data_trans_reg(adjust_op, adjust_dir_op, reverse_dir_op) \
arm_decode_data_trans_reg(); \
generate_load_offset_sh(); \
arm_access_memory_reg_##adjust_op(adjust_dir_op, reverse_dir_op) \
#define arm_data_trans_imm(adjust_op, adjust_dir_op, reverse_dir_op) \
arm_decode_data_trans_imm(); \
arm_access_memory_imm_##adjust_op(adjust_dir_op, reverse_dir_op) \
#define arm_data_trans_half_reg(adjust_op, adjust_dir_op, reverse_dir_op) \
arm_decode_half_trans_r(); \
generate_load_reg(a1, rm); \
arm_access_memory_reg_##adjust_op(adjust_dir_op, reverse_dir_op) \
#define arm_data_trans_half_imm(adjust_op, adjust_dir_op, reverse_dir_op) \
arm_decode_half_trans_of(); \
arm_access_memory_imm_##adjust_op(adjust_dir_op, reverse_dir_op) \
#define arm_access_memory(access_type, direction, adjust_op, mem_type, \
offset_type) \
{ \
arm_data_trans_##offset_type(adjust_op, \
arm_access_memory_adjust_op_##direction, \
arm_access_memory_reverse_op_##direction); \
\
arm_access_memory_##access_type(mem_type); \
} \
#define word_bit_count(word) \
(bit_count[word >> 8] + bit_count[word & 0xFF]) \
#define sprint_no(access_type, pre_op, post_op, wb) \
#define sprint_yes(access_type, pre_op, post_op, wb) \
printf("sbit on %s %s %s %s\n", #access_type, #pre_op, #post_op, #wb) \
u32 execute_aligned_load32(u32 address)
{
u8 *map;
if(!(address & 0xF0000000) && (map = memory_map_read[address >> 15]))
return address32(map, address & 0x7FFF);
else
return read_memory32(address);
}
void execute_aligned_store32(u32 address, u32 source)
{
u8 *map;
if(!(address & 0xF0000000) && (map = memory_map_write[address >> 15]))
address32(map, address & 0x7FFF) = source;
else
write_memory32(address, source);
}
#define arm_block_memory_load() \
generate_function_call(execute_aligned_load32); \
generate_store_reg(rv, i) \
#define arm_block_memory_store() \
generate_load_reg_pc(a1, i, 8); \
generate_function_call(execute_aligned_store32) \
#define arm_block_memory_final_load() \
arm_block_memory_load() \
#define arm_block_memory_final_store() \
generate_load_reg_pc(a1, i, 12); \
generate_load_pc(a2, (pc + 4)); \
generate_function_call(execute_store_u32) \
#define arm_block_memory_adjust_pc_store() \
#define arm_block_memory_adjust_pc_load() \
if(reg_list & 0x8000) \
{ \
generate_mov(a0, rv); \
generate_indirect_branch_arm(); \
} \
#define arm_block_memory_offset_down_a() \
generate_add_imm(s0, -((word_bit_count(reg_list) * 4) - 4)) \
#define arm_block_memory_offset_down_b() \
generate_add_imm(s0, -(word_bit_count(reg_list) * 4)) \
#define arm_block_memory_offset_no() \
#define arm_block_memory_offset_up() \
generate_add_imm(s0, 4) \
#define arm_block_memory_writeback_down() \
generate_load_reg(a0, rn) \
generate_add_imm(a0, -(word_bit_count(reg_list) * 4)); \
generate_store_reg(a0, rn) \
#define arm_block_memory_writeback_up() \
generate_load_reg(a0, rn); \
generate_add_imm(a0, (word_bit_count(reg_list) * 4)); \
generate_store_reg(a0, rn) \
#define arm_block_memory_writeback_no()
// Only emit writeback if the register is not in the list
#define arm_block_memory_writeback_load(writeback_type) \
if(!((reg_list >> rn) & 0x01)) \
{ \
arm_block_memory_writeback_##writeback_type(); \
} \
#define arm_block_memory_writeback_store(writeback_type) \
arm_block_memory_writeback_##writeback_type() \
#define arm_block_memory(access_type, offset_type, writeback_type, s_bit) \
{ \
arm_decode_block_trans(); \
u32 offset = 0; \
u32 i; \
\
generate_load_reg(s0, rn); \
arm_block_memory_offset_##offset_type(); \
arm_block_memory_writeback_##access_type(writeback_type); \
generate_and_imm(s0, ~0x03); \
\
for(i = 0; i < 16; i++) \
{ \
if((reg_list >> i) & 0x01) \
{ \
cycle_count++; \
generate_add_reg_reg_imm(a0, s0, offset) \
if(reg_list & ~((2 << i) - 1)) \
{ \
arm_block_memory_##access_type(); \
offset += 4; \
} \
else \
{ \
arm_block_memory_final_##access_type(); \
} \
} \
} \
\
arm_block_memory_adjust_pc_##access_type(); \
} \
#define arm_swap(type) \
{ \
arm_decode_swap(); \
cycle_count += 3; \
generate_load_reg(a0, rn); \
generate_function_call(execute_load_##type); \
generate_mov(s0, rv); \
generate_load_reg(a0, rn); \
generate_load_reg(a1, rm); \
generate_function_call(execute_store_##type); \
generate_store_reg(s0, rd); \
} \
#define thumb_rn_op_reg(_rn) \
generate_load_reg(a0, _rn) \
#define thumb_rn_op_imm(_imm) \
generate_load_imm(a0, _imm) \
// Types: add_sub, add_sub_imm, alu_op, imm
// Affects N/Z/C/V flags
#define thumb_data_proc(type, name, rn_type, _rd, _rs, _rn) \
{ \
thumb_decode_##type(); \
thumb_rn_op_##rn_type(_rn); \
generate_load_reg(a1, _rs); \
generate_function_call(execute_##name); \
generate_store_reg(rv, _rd); \
} \
#define thumb_data_proc_test(type, name, rn_type, _rs, _rn) \
{ \
thumb_decode_##type(); \
thumb_rn_op_##rn_type(_rn); \
generate_load_reg(a1, _rs); \
generate_function_call(execute_##name); \
} \
#define thumb_data_proc_unary(type, name, rn_type, _rd, _rn) \
{ \
thumb_decode_##type(); \
thumb_rn_op_##rn_type(_rn); \
generate_function_call(execute_##name); \
generate_store_reg(rv, _rd); \
} \
#define thumb_data_proc_mov(type, rn_type, _rd, _rn) \
{ \
thumb_decode_##type(); \
thumb_rn_op_##rn_type(_rn); \
generate_store_reg(a0, _rd); \
} \
#define generate_store_reg_pc_thumb(ireg) \
generate_store_reg(ireg, rd); \
if(rd == 15) \
{ \
generate_indirect_branch_cycle_update(thumb); \
} \
#define thumb_data_proc_hi(name) \
{ \
thumb_decode_hireg_op(); \
generate_load_reg_pc(a0, rs, 4); \
generate_load_reg_pc(a1, rd, 4); \
generate_function_call(execute_##name); \
generate_store_reg_pc_thumb(rv); \
} \
#define thumb_data_proc_test_hi(name) \
{ \
thumb_decode_hireg_op(); \
generate_load_reg_pc(a0, rs, 4); \
generate_load_reg_pc(a1, rd, 4); \
generate_function_call(execute_##name); \
} \
#define thumb_data_proc_unary_hi(name) \
{ \
thumb_decode_hireg_op(); \
generate_load_reg_pc(a0, rn, 4); \
generate_function_call(execute_##name); \
generate_store_reg_pc_thumb(rv); \
} \
#define thumb_data_proc_mov_hi() \
{ \
thumb_decode_hireg_op(); \
generate_load_reg_pc(a0, rs, 4); \
generate_store_reg_pc_thumb(a0); \
} \
#define thumb_load_pc(_rd) \
{ \
thumb_decode_imm(); \
generate_load_pc(a0, (((pc & ~2) + 4) + (imm * 4))); \
generate_store_reg(a0, _rd); \
} \
#define thumb_load_sp(_rd) \
{ \
thumb_decode_imm(); \
generate_load_reg(a0, 13); \
generate_add_imm(a0, (imm * 4)); \
generate_store_reg(a0, _rd); \
} \
#define thumb_adjust_sp_up() \
generate_add_imm(a0, imm * 4) \
#define thumb_adjust_sp_down() \
generate_sub_imm(a0, imm * 4) \
#define thumb_adjust_sp(direction) \
{ \
thumb_decode_add_sp(); \
generate_load_reg(a0, REG_SP); \
thumb_adjust_sp_##direction(); \
generate_store_reg(a0, REG_SP); \
} \
// Decode types: shift, alu_op
// Operation types: lsl, lsr, asr, ror
// Affects N/Z/C flags
u32 execute_lsl_reg_op(u32 value, u32 shift)
{
if(shift != 0)
{
if(shift > 31)
{
if(shift == 32)
reg[REG_C_FLAG] = value & 0x01;
else
reg[REG_C_FLAG] = 0;
value = 0;
}
else
{
reg[REG_C_FLAG] = (value >> (32 - shift)) & 0x01;
value <<= shift;
}
}
calculate_flags_logic(value);
return value;
}
u32 execute_lsr_reg_op(u32 value, u32 shift)
{
if(shift != 0)
{
if(shift > 31)
{
if(shift == 32)
reg[REG_C_FLAG] = (value >> 31) & 0x01;
else
reg[REG_C_FLAG] = 0;
value = 0;
}
else
{
reg[REG_C_FLAG] = (value >> (shift - 1)) & 0x01;
value >>= shift;
}
}
calculate_flags_logic(value);
return value;
}
u32 execute_asr_reg_op(u32 value, u32 shift)
{
if(shift != 0)
{
if(shift > 31)
{
value = (s32)value >> 31;
reg[REG_C_FLAG] = value & 0x01;
}
else
{
reg[REG_C_FLAG] = (value >> (shift - 1)) & 0x01;
value = (s32)value >> shift;
}
}
calculate_flags_logic(value);
return value;
}
u32 execute_ror_reg_op(u32 value, u32 shift)
{
if(shift != 0)
{
reg[REG_C_FLAG] = (value >> (shift - 1)) & 0x01;
ror(value, value, shift);
}
calculate_flags_logic(value);
return value;
}
u32 execute_lsl_imm_op(u32 value, u32 shift)
{
if(shift != 0)
{
reg[REG_C_FLAG] = (value >> (32 - shift)) & 0x01;
value <<= shift;
}
calculate_flags_logic(value);
return value;
}
u32 execute_lsr_imm_op(u32 value, u32 shift)
{
if(shift != 0)
{
reg[REG_C_FLAG] = (value >> (shift - 1)) & 0x01;
value >>= shift;
}
else
{
reg[REG_C_FLAG] = value >> 31;
value = 0;
}
calculate_flags_logic(value);
return value;
}
u32 execute_asr_imm_op(u32 value, u32 shift)
{
if(shift != 0)
{
reg[REG_C_FLAG] = (value >> (shift - 1)) & 0x01;
value = (s32)value >> shift;
}
else
{
value = (s32)value >> 31;
reg[REG_C_FLAG] = value & 0x01;
}
calculate_flags_logic(value);
return value;
}
u32 execute_ror_imm_op(u32 value, u32 shift)
{
if(shift != 0)
{
reg[REG_C_FLAG] = (value >> (shift - 1)) & 0x01;
ror(value, value, shift);
}
else
{
u32 c_flag = reg[REG_C_FLAG];
reg[REG_C_FLAG] = value & 0x01;
value = (value >> 1) | (c_flag << 31);
}
calculate_flags_logic(value);
return value;
}
#define generate_shift_load_operands_reg() \
generate_load_reg(a0, rd); \
generate_load_reg(a1, rs) \
#define generate_shift_load_operands_imm() \
generate_load_reg(a0, rs); \
generate_load_imm(a1, imm) \
#define thumb_shift(decode_type, op_type, value_type) \
{ \
thumb_decode_##decode_type(); \
generate_shift_load_operands_##value_type(); \
generate_function_call(execute_##op_type##_##value_type##_op); \
generate_store_reg(rv, rd); \
} \
// Operation types: imm, mem_reg, mem_imm
#define thumb_access_memory_load(mem_type, reg_rd) \
cycle_count += 2; \
generate_function_call(execute_load_##mem_type); \
generate_store_reg(rv, reg_rd) \
#define thumb_access_memory_store(mem_type, reg_rd) \
cycle_count++; \
generate_load_reg(a1, reg_rd); \
generate_load_pc(a2, (pc + 2)); \
generate_function_call(execute_store_##mem_type) \
#define thumb_access_memory_generate_address_pc_relative(offset, _rb, _ro) \
generate_load_pc(a0, (offset)) \
#define thumb_access_memory_generate_address_reg_imm_sp(offset, _rb, _ro) \
generate_load_reg(a0, _rb); \
generate_add_imm(a0, (offset * 4)) \
#define thumb_access_memory_generate_address_reg_imm(offset, _rb, _ro) \
generate_load_reg(a0, _rb); \
generate_add_imm(a0, (offset)) \
#define thumb_access_memory_generate_address_reg_reg(offset, _rb, _ro) \
generate_load_reg(a0, _rb); \
generate_load_reg(a1, _ro); \
generate_add(a0, a1) \
#define thumb_access_memory(access_type, op_type, _rd, _rb, _ro, \
address_type, offset, mem_type) \
{ \
thumb_decode_##op_type(); \
thumb_access_memory_generate_address_##address_type(offset, _rb, _ro); \
thumb_access_memory_##access_type(mem_type, _rd); \
} \
#define thumb_block_address_preadjust_up() \
generate_add_imm(s0, (bit_count[reg_list] * 4)) \
#define thumb_block_address_preadjust_down() \
generate_sub_imm(s0, (bit_count[reg_list] * 4)) \
#define thumb_block_address_preadjust_push_lr() \
generate_sub_imm(s0, ((bit_count[reg_list] + 1) * 4)) \
#define thumb_block_address_preadjust_no() \
#define thumb_block_address_postadjust_no(base_reg) \
generate_store_reg(s0, base_reg) \
#define thumb_block_address_postadjust_up(base_reg) \
generate_add_reg_reg_imm(a0, s0, (bit_count[reg_list] * 4)); \
generate_store_reg(a0, base_reg) \
#define thumb_block_address_postadjust_down(base_reg) \
generate_mov(a0, s0); \
generate_sub_imm(a0, (bit_count[reg_list] * 4)); \
generate_store_reg(a0, base_reg) \
#define thumb_block_address_postadjust_pop_pc(base_reg) \
generate_add_reg_reg_imm(a0, s0, ((bit_count[reg_list] + 1) * 4)); \
generate_store_reg(a0, base_reg) \
#define thumb_block_address_postadjust_push_lr(base_reg) \
generate_store_reg(s0, base_reg) \
#define thumb_block_memory_extra_no() \
#define thumb_block_memory_extra_up() \
#define thumb_block_memory_extra_down() \
#define thumb_block_memory_extra_pop_pc() \
generate_add_reg_reg_imm(a0, s0, (bit_count[reg_list] * 4)); \
generate_function_call(execute_aligned_load32); \
generate_store_reg(rv, REG_PC); \
generate_mov(a0, rv); \
generate_indirect_branch_cycle_update(thumb) \
#define thumb_block_memory_extra_push_lr(base_reg) \
generate_add_reg_reg_imm(a0, s0, (bit_count[reg_list] * 4)); \
generate_load_reg(a1, REG_LR); \
generate_function_call(execute_aligned_store32) \
#define thumb_block_memory_load() \
generate_function_call(execute_aligned_load32); \
generate_store_reg(rv, i) \
#define thumb_block_memory_store() \
generate_load_reg(a1, i); \
generate_function_call(execute_aligned_store32) \
#define thumb_block_memory_final_load() \
thumb_block_memory_load() \
#define thumb_block_memory_final_store() \
generate_load_reg(a1, i); \
generate_load_pc(a2, (pc + 2)); \
generate_function_call(execute_store_u32) \
#define thumb_block_memory_final_no(access_type) \
thumb_block_memory_final_##access_type() \
#define thumb_block_memory_final_up(access_type) \
thumb_block_memory_final_##access_type() \
#define thumb_block_memory_final_down(access_type) \
thumb_block_memory_final_##access_type() \
#define thumb_block_memory_final_push_lr(access_type) \
thumb_block_memory_##access_type() \
#define thumb_block_memory_final_pop_pc(access_type) \
thumb_block_memory_##access_type() \
#define thumb_block_memory(access_type, pre_op, post_op, base_reg) \
{ \
thumb_decode_rlist(); \
u32 i; \
u32 offset = 0; \
\
generate_load_reg(s0, base_reg); \
generate_and_imm(s0, ~0x03); \
thumb_block_address_preadjust_##pre_op(); \
thumb_block_address_postadjust_##post_op(base_reg); \
\
for(i = 0; i < 8; i++) \
{ \
if((reg_list >> i) & 0x01) \
{ \
cycle_count++; \
generate_add_reg_reg_imm(a0, s0, offset) \
if(reg_list & ~((2 << i) - 1)) \
{ \
thumb_block_memory_##access_type(); \
offset += 4; \
} \
else \
{ \
thumb_block_memory_final_##post_op(access_type); \
} \
} \
} \
\
thumb_block_memory_extra_##post_op(); \
} \
#define thumb_conditional_branch(condition) \
{ \
condition_check_type condition_check = CONDITION_TRUE; \
generate_cycle_update(); \
generate_condition_##condition(a0, a1); \
generate_conditional_branch_type(a0, a1); \
generate_branch_no_cycle_update( \
block_exits[block_exit_position].branch_source, \
block_exits[block_exit_position].branch_target); \
generate_branch_patch_conditional(backpatch_address, translation_ptr); \
block_exit_position++; \
} \
#define flags_vars(src_a, src_b) \
u32 dest; \
const u32 _sa = src_a; \
const u32 _sb = src_b \
#define data_proc_generate_logic_function(name, expr) \
u32 execute_##name(u32 rm, u32 rn) \
{ \
return expr; \
} \
\
u32 execute_##name##s(u32 rm, u32 rn) \
{ \
u32 dest = expr; \
calculate_z_flag(dest); \
calculate_n_flag(dest); \
return expr; \
} \
#define data_proc_generate_logic_unary_function(name, expr) \
u32 execute_##name(u32 rm) \
{ \
return expr; \
} \
\
u32 execute_##name##s(u32 rm) \
{ \
u32 dest = expr; \
calculate_z_flag(dest); \
calculate_n_flag(dest); \
return expr; \
} \
#define data_proc_generate_sub_function(name, src_a, src_b) \
u32 execute_##name(u32 rm, u32 rn) \
{ \
return (src_a) - (src_b); \
} \
\
u32 execute_##name##s(u32 rm, u32 rn) \
{ \
flags_vars(src_a, src_b); \
dest = _sa - _sb; \
calculate_flags_sub(dest, _sa, _sb); \
return dest; \
} \
#define data_proc_generate_add_function(name, src_a, src_b) \
u32 execute_##name(u32 rm, u32 rn) \
{ \
return (src_a) + (src_b); \
} \
\
u32 execute_##name##s(u32 rm, u32 rn) \
{ \
flags_vars(src_a, src_b); \
dest = _sa + _sb; \
calculate_flags_add(dest, _sa, _sb); \
return dest; \
} \
#define data_proc_generate_sub_test_function(name, src_a, src_b) \
void execute_##name(u32 rm, u32 rn) \
{ \
flags_vars(src_a, src_b); \
dest = _sa - _sb; \
calculate_flags_sub(dest, _sa, _sb); \
} \
#define data_proc_generate_add_test_function(name, src_a, src_b) \
void execute_##name(u32 rm, u32 rn) \
{ \
flags_vars(src_a, src_b); \
dest = _sa + _sb; \
calculate_flags_add(dest, _sa, _sb); \
} \
#define data_proc_generate_logic_test_function(name, expr) \
void execute_##name(u32 rm, u32 rn) \
{ \
u32 dest = expr; \
calculate_z_flag(dest); \
calculate_n_flag(dest); \
} \
u32 execute_neg(u32 rm) \
{ \
u32 dest = 0 - rm; \
calculate_flags_sub(dest, 0, rm); \
return dest; \
} \
// Execute functions
data_proc_generate_logic_function(and, rn & rm);
data_proc_generate_logic_function(eor, rn ^ rm);
data_proc_generate_logic_function(orr, rn | rm);
data_proc_generate_logic_function(bic, rn & (~rm));
data_proc_generate_logic_function(mul, rn * rm);
data_proc_generate_logic_unary_function(mov, rm);
data_proc_generate_logic_unary_function(mvn, ~rm);
data_proc_generate_sub_function(sub, rn, rm);
data_proc_generate_sub_function(rsb, rm, rn);
data_proc_generate_sub_function(sbc, rn, (rm + (reg[REG_C_FLAG] ^ 1)));
data_proc_generate_sub_function(rsc, (rm + reg[REG_C_FLAG] - 1), rn);
data_proc_generate_add_function(add, rn, rm);
data_proc_generate_add_function(adc, rn, rm + reg[REG_C_FLAG]);
data_proc_generate_logic_test_function(tst, rn & rm);
data_proc_generate_logic_test_function(teq, rn ^ rm);
data_proc_generate_sub_test_function(cmp, rn, rm);
data_proc_generate_add_test_function(cmn, rn, rm);
static void execute_swi(u32 pc)
{
reg_mode[MODE_SUPERVISOR][6] = pc;
collapse_flags();
spsr[MODE_SUPERVISOR] = reg[REG_CPSR];
reg[REG_CPSR] = (reg[REG_CPSR] & ~0x3F) | 0x13;
set_cpu_mode(MODE_SUPERVISOR);
}
#define arm_conditional_block_header() \
{ \
condition_check_type condition_check = CONDITION_TRUE; \
generate_condition(a0, a1); \
generate_conditional_branch_type(a0, a1); \
}
#define arm_b() \
generate_branch() \
#define arm_bl() \
generate_update_pc((pc + 4)); \
generate_store_reg(a0, REG_LR); \
generate_branch() \
#define arm_bx() \
arm_decode_branchx(opcode); \
generate_load_reg(a0, rn); \
generate_indirect_branch_dual(); \
#define arm_swi() \
generate_swi_hle_handler((opcode >> 16) & 0xFF); \
generate_update_pc((pc + 4)); \
generate_function_call(execute_swi); \
generate_branch() \
#define thumb_b() \
generate_branch_cycle_update( \
block_exits[block_exit_position].branch_source, \
block_exits[block_exit_position].branch_target); \
block_exit_position++ \
#define thumb_bl() \
generate_update_pc(((pc + 2) | 0x01)); \
generate_store_reg(a0, REG_LR); \
generate_branch_cycle_update( \
block_exits[block_exit_position].branch_source, \
block_exits[block_exit_position].branch_target); \
block_exit_position++ \
#define thumb_blh() \
{ \
thumb_decode_branch(); \
generate_update_pc(((pc + 2) | 0x01)); \
generate_load_reg(a1, REG_LR); \
generate_store_reg(a0, REG_LR); \
generate_mov(a0, a1); \
generate_add_imm(a0, (offset * 2)); \
generate_indirect_branch_cycle_update(thumb); \
} \
#define thumb_bx() \
{ \
thumb_decode_hireg_op(); \
generate_load_reg_pc(a0, rs, 4); \
generate_indirect_branch_cycle_update(dual); \
} \
#define thumb_swi() \
generate_swi_hle_handler(opcode & 0xFF); \
generate_update_pc((pc + 2)); \
generate_function_call(execute_swi); \
generate_branch_cycle_update( \
block_exits[block_exit_position].branch_source, \
block_exits[block_exit_position].branch_target); \
block_exit_position++ \
u8 swi_hle_handle[256] =
{
0x0, // SWI 0: SoftReset
0x0, // SWI 1: RegisterRAMReset
0x0, // SWI 2: Halt
0x0, // SWI 3: Stop/Sleep
0x0, // SWI 4: IntrWait
0x0, // SWI 5: VBlankIntrWait
0x1, // SWI 6: Div
0x0, // SWI 7: DivArm
0x0, // SWI 8: Sqrt
0x0, // SWI 9: ArcTan
0x0, // SWI A: ArcTan2
0x0, // SWI B: CpuSet
0x0, // SWI C: CpuFastSet
0x0, // SWI D: GetBIOSCheckSum
0x0, // SWI E: BgAffineSet
0x0, // SWI F: ObjAffineSet
0x0, // SWI 10: BitUnpack
0x0, // SWI 11: LZ77UnCompWram
0x0, // SWI 12: LZ77UnCompVram
0x0, // SWI 13: HuffUnComp
0x0, // SWI 14: RLUnCompWram
0x0, // SWI 15: RLUnCompVram
0x0, // SWI 16: Diff8bitUnFilterWram
0x0, // SWI 17: Diff8bitUnFilterVram
0x0, // SWI 18: Diff16bitUnFilter
0x0, // SWI 19: SoundBias
0x0, // SWI 1A: SoundDriverInit
0x0, // SWI 1B: SoundDriverMode
0x0, // SWI 1C: SoundDriverMain
0x0, // SWI 1D: SoundDriverVSync
0x0, // SWI 1E: SoundChannelClear
0x0, // SWI 1F: MidiKey2Freq
0x0, // SWI 20: SoundWhatever0
0x0, // SWI 21: SoundWhatever1
0x0, // SWI 22: SoundWhatever2
0x0, // SWI 23: SoundWhatever3
0x0, // SWI 24: SoundWhatever4
0x0, // SWI 25: MultiBoot
0x0, // SWI 26: HardReset
0x0, // SWI 27: CustomHalt
0x0, // SWI 28: SoundDriverVSyncOff
0x0, // SWI 29: SoundDriverVSyncOn
0x0 // SWI 2A: SoundGetJumpList
};
void swi_hle_div(void)
{
s32 result = (s32)reg[0] / (s32)reg[1];
reg[1] = (s32)reg[0] % (s32)reg[1];
reg[0] = result;
reg[3] = (result ^ (result >> 31)) - (result >> 31);
}
#define generate_swi_hle_handler(_swi_number) \
{ \
u32 swi_number = _swi_number; \
if(swi_hle_handle[swi_number]) \
{ \
/* Div */ \
if(swi_number == 0x06) \
{ \
generate_function_call(swi_hle_div); \
} \
break; \
} \
} \
#define generate_translation_gate(type) \
generate_update_pc(pc); \
generate_indirect_branch_no_cycle_update(type) \
#endif
|