File: GekkoDisassembler.cpp

package info (click to toggle)
dolphin-emu 5.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 29,052 kB
  • sloc: cpp: 213,146; java: 6,252; asm: 2,277; xml: 1,998; ansic: 1,514; python: 462; sh: 279; pascal: 247; makefile: 124; perl: 97
file content (2289 lines) | stat: -rw-r--r-- 42,681 bytes parent folder | download | duplicates (2)
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
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

/* $VER: ppc_disasm.c V1.5 (27.05.2009)
 *
 * Disassembler module for the PowerPC microprocessor family
 * Copyright (c) 1998-2001,2009,2011 Frank Wille
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

// Modified for use with Dolphin

#include <string>

#include "Common/CommonTypes.h"
#include "Common/GekkoDisassembler.h"
#include "Common/StringUtil.h"

// version/revision
#define PPCDISASM_VER 1
#define PPCDISASM_REV 6

// general defines
#define PPCIDXMASK      0xfc000000
#define PPCIDX2MASK     0x000007fe
#define PPCDMASK        0x03e00000
#define PPCAMASK        0x001f0000
#define PPCBMASK        0x0000f800
#define PPCCMASK        0x000007c0
#define PPCMMASK        0x0000003e
#define PPCCRDMASK      0x03800000
#define PPCCRAMASK      0x001c0000
#define PPCLMASK        0x00600000
#define PPCOE           0x00000400
#define PPCVRC          0x00000400
#define PPCDST          0x02000000
#define PPCSTRM         0x00600000

#define PPCIDXSH        26
#define PPCDSH          21
#define PPCASH          16
#define PPCBSH          11
#define PPCCSH          6
#define PPCMSH          1
#define PPCCRDSH        23
#define PPCCRASH        18
#define PPCLSH          21
#define PPCIDX2SH       1

#define PPCGETIDX(x)    (((x)&PPCIDXMASK)>>PPCIDXSH)
#define PPCGETD(x)      (((x)&PPCDMASK)>>PPCDSH)
#define PPCGETA(x)      (((x)&PPCAMASK)>>PPCASH)
#define PPCGETB(x)      (((x)&PPCBMASK)>>PPCBSH)
#define PPCGETC(x)      (((x)&PPCCMASK)>>PPCCSH)
#define PPCGETM(x)      (((x)&PPCMMASK)>>PPCMSH)
#define PPCGETCRD(x)    (((x)&PPCCRDMASK)>>PPCCRDSH)
#define PPCGETCRA(x)    (((x)&PPCCRAMASK)>>PPCCRASH)
#define PPCGETL(x)      (((x)&PPCLMASK)>>PPCLSH)
#define PPCGETIDX2(x)   (((x)&PPCIDX2MASK)>>PPCIDX2SH)
#define PPCGETSTRM(x)   (((x)&PPCSTRM)>>PPCDSH)


static const char* trap_condition[32] = {
	nullptr, "lgt",   "llt",   nullptr, "eq",    "lge",   "lle",   nullptr,
	"gt",    nullptr, nullptr, nullptr, "ge",    nullptr, nullptr, nullptr,
	"lt",    nullptr, nullptr, nullptr, "le",    nullptr, nullptr, nullptr,
	"ne",    nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr
};

static const char* cmpname[4] = {
	"cmpw", "cmpd", "cmplw", "cmpld"
};

static const char* ps_cmpname[4] = {
	"ps_cmpu0", "ps_cmpo0", "ps_cmpu1", "ps_cmpo1"
};

static const char* b_ext[4] = {
	"", "l", "a", "la"
};

static const char* b_condition[8] = {
	"ge", "le", "ne", "ns", "lt", "gt", "eq", "so"
};

static const char* b_decr[16] = {
	"nzf", "zf", nullptr, nullptr, "nzt", "zt", nullptr, nullptr,
	"nz",  "z",  nullptr, nullptr, "nz",  "z",  nullptr, nullptr
};

static const char* regsel[2] = {
	"", "r"
};

static const char* oesel[2] = {
	"", "o"
};

static const char* rcsel[2] = {
	"", "."
};

static const char* ldstnames[24] = {
	"lwz", "lwzu", "lbz", "lbzu", "stw", "stwu", "stb", "stbu", "lhz", "lhzu",
	"lha", "lhau", "sth", "sthu", "lmw", "stmw", "lfs", "lfsu", "lfd", "lfdu",
	"stfs", "stfsu", "stfd", "stfdu"
};

static const char* regnames[32] = {
	"r0", "sp", "rtoc", "r3", "r4", "r5", "r6", "r7",
	"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
	"r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
	"r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
};

// Initialize static class variables.
u32* GekkoDisassembler::m_instr = nullptr;
u32* GekkoDisassembler::m_iaddr = nullptr;
std::string GekkoDisassembler::m_opcode = "";
std::string GekkoDisassembler::m_operands = "";
unsigned char GekkoDisassembler::m_type = 0;
unsigned char GekkoDisassembler::m_flags = PPCF_ILLEGAL;
unsigned short GekkoDisassembler::m_sreg = 0;
u32 GekkoDisassembler::m_displacement = 0;


static std::string spr_name(int i)
{
	switch (i)
	{
	case 1: return "XER";
	case 8: return "LR";
	case 9: return "CTR";
	case 18: return "DSIR";
	case 19: return "DAR";
	case 22: return "DEC";
	case 25: return "SDR1";
	case 26: return "SRR0";
	case 27: return "SRR1";
	case 272: return "SPRG0";
	case 273: return "SPRG1";
	case 274: return "SPRG2";
	case 275: return "SPRG3";
	case 282: return "EAR";
	case 287: return "PVR";
	case 528: return "IBAT0U";
	case 529: return "IBAT0L";
	case 530: return "IBAT1U";
	case 531: return "IBAT1L";
	case 532: return "IBAT2U";
	case 533: return "IBAT2L";
	case 534: return "IBAT3U";
	case 535: return "IBAT3L";
	case 536: return "DBAT0U";
	case 537: return "DBAT0L";
	case 538: return "DBAT1U";
	case 539: return "DBAT1L";
	case 540: return "DBAT2U";
	case 541: return "DBAT2L";
	case 542: return "DBAT3U";
	case 543: return "DBAT3L";
	case 912: return "GQR0";
	case 913: return "GQR1";
	case 914: return "GQR2";
	case 915: return "GQR3";
	case 916: return "GQR4";
	case 917: return "GQR5";
	case 918: return "GQR6";
	case 919: return "GQR7";
	case 920: return "HID2";
	case 921: return "WPAR";
	case 922: return "DMA_U";
	case 923: return "DMA_L";
	case 924: return "ECID_U";
	case 925: return "ECID_M";
	case 926: return "ECID_L";
	case 936: return "UMMCR0";
	case 937: return "UPMC1";
	case 938: return "UPMC2";
	case 939: return "USIA";
	case 940: return "UMMCR1";
	case 941: return "UPMC3";
	case 942: return "UPMC4";
	case 943: return "USDA";
	case 952: return "MMCR0";
	case 953: return "PMC1";
	case 954: return "PMC2";
	case 955: return "SIA";
	case 956: return "MMCR1";
	case 957: return "PMC3";
	case 958: return "PMC4";
	case 959: return "SDA";
	case 1008: return "HID0";
	case 1009: return "HID1";
	case 1010: return "IABR";
	case 1011: return "HID4";
	case 1013: return "DABR";
	case 1017: return "L2CR";
	case 1019: return "ICTC";
	case 1020: return "THRM1";
	case 1021: return "THRM2";
	case 1022: return "THRM3";
	}

	return StringFromFormat("%d", i);
}


static u32 swapda(u32 w)
{
	return ((w & 0xfc00ffff) | ((w&PPCAMASK) << 5) | ((w&PPCDMASK) >> 5));
}


static u32 swapab(u32 w)
{
	return ((w & 0xffe007ff) | ((w&PPCBMASK) << 5) | ((w&PPCAMASK) >> 5));
}


void GekkoDisassembler::ill(u32 in)
{
	if (in == 0)
	{
		m_opcode = "";
		m_operands = "---";
	}
	else
	{
		m_opcode = "(ill)";
		m_operands =  StringFromFormat("%08x", in);
	}

	m_flags |= PPCF_ILLEGAL;
}

// Generate immediate instruction operand.
//
// Type 0: D-mode, D,A,imm
// Type 1: S-mode, A,S,imm
// Type 2: S/D register is ignored (trap,cmpi)
// Type 3: A register is ignored (li)
std::string GekkoDisassembler::imm(u32 in, int uimm, int type, bool hex)
{
	int i = (int)(in & 0xffff);

	m_type = PPCINSTR_IMM;

	if (uimm == 0)
	{
		if (i > 0x7fff)
			i -= 0x10000;
	}
	else
	{
		m_flags |= PPCF_UNSIGNED;
	}
	m_displacement = i;

	switch (type)
	{
	case 0:
		return StringFromFormat("%s, %s, %d", regnames[(int)PPCGETD(in)], regnames[(int)PPCGETA(in)], i);

	case 1:
		if (hex)
			return StringFromFormat("%s, %s, 0x%.4X", regnames[(int)PPCGETA(in)], regnames[(int)PPCGETD(in)], i);
		else
			return StringFromFormat("%s, %s, %d", regnames[(int)PPCGETA(in)], regnames[(int)PPCGETD(in)], i);

	case 2:
		return StringFromFormat("%s, %d", regnames[(int)PPCGETA(in)], i);

	case 3:
		if (hex)
			return StringFromFormat("%s, 0x%.4X", regnames[(int)PPCGETD(in)], i);
		else
			return StringFromFormat("%s, %d", regnames[(int)PPCGETD(in)], i);

	default:
		return StringFromFormat("%s", "imm(): Wrong type");
	}
}


std::string GekkoDisassembler::ra_rb(u32 in)
{
	return StringFromFormat("%s, %s", regnames[(int)PPCGETA(in)], regnames[(int)PPCGETB(in)]);
}


std::string GekkoDisassembler::rd_ra_rb(u32 in, int mask)
{
	std::string result;

	if (mask)
	{
		if (mask & 4)
			result += StringFromFormat("%s, ", regnames[(int)PPCGETD(in)]);
		if (mask & 2)
			result += StringFromFormat("%s, ", regnames[(int)PPCGETA(in)]);
		if (mask & 1)
			result += StringFromFormat("%s, ", regnames[(int)PPCGETB(in)]);

		size_t pos = result.rfind(", ");
		if (pos != std::string::npos)
		{
			result.erase(pos, result.length() - pos);
		}
	}

	return result;
}


std::string GekkoDisassembler::fd_ra_rb(u32 in, int mask)
{
	std::string result;

	if (mask)
	{
		if (mask & 4)
			result += StringFromFormat("f%d,", (int)PPCGETD(in));
		if (mask & 2)
			result += StringFromFormat("%s,", regnames[(int)PPCGETA(in)]);
		if (mask & 1)
			result += StringFromFormat("%s,", regnames[(int)PPCGETB(in)]);

		// Drop the trailing comma
		result.pop_back();
	}

	return result;
}


void GekkoDisassembler::trapi(u32 in, unsigned char dmode)
{
	const char* cnd = trap_condition[PPCGETD(in)];


	m_flags |= dmode;
	if (cnd != nullptr)
	{
		m_opcode = StringFromFormat("t%c%s", dmode ? 'd' : 'w', cnd);
	}
	else
	{
		m_opcode = StringFromFormat("t%ci", dmode ? 'd' : 'w');
		m_operands = StringFromFormat("%d, ", PPCGETD(in));
	}
	m_operands += imm(in, 0, 2, false);
}


void GekkoDisassembler::cmpi(u32 in, int uimm)
{
	int i = (int)PPCGETL(in);

	if (i < 2)
	{
		if (i != 0)
			m_flags |= PPCF_64;

		m_opcode = StringFromFormat("%si", cmpname[uimm * 2 + i]);

		i = (int)PPCGETCRD(in);
		if (i != 0)
		{
			m_operands += StringFromFormat("cr%c, ", '0' + i);
		}

		m_operands += imm(in, uimm, 2, false);
	}
	else
	{
		ill(in);
	}
}


void GekkoDisassembler::addi(u32 in, const std::string& ext)
{
	if ((in & 0x08000000) && !PPCGETA(in))
	{
		m_opcode = StringFromFormat("l%s", ext.c_str());  // li, lis

		if (ext == "i")
			m_operands = imm(in, 0, 3, false);
		else
			m_operands = imm(in, 1, 3, true);
	}
	else
	{
		m_opcode = StringFromFormat("%s%s", (in & 0x8000) ? "sub" : "add", ext.c_str());

		if (in & 0x8000)
			in = (in ^ 0xffff) + 1;

		m_operands = imm(in, 1, 0, false);
	}
}

// Build a branch instr. and return number of chars written to operand.
size_t GekkoDisassembler::branch(u32 in, const char* bname, int aform, int bdisp)
{
	int bo = (int)PPCGETD(in);
	int bi = (int)PPCGETA(in);
	char y = (char)(bo & 1);
	const char* ext = b_ext[aform * 2 + (int)(in & 1)];

	if (bdisp < 0)
		y ^= 1;
	y = (y != 0) ? '+' : '-';

	if (bo & 4)
	{
		// standard case - no decrement
		if (bo & 16)
		{
			// branch always
			if (PPCGETIDX(in) != 16)
			{
				m_opcode = StringFromFormat("b%s%s", bname, ext);
			}
			else
			{
				m_opcode = StringFromFormat("bc%s", ext);
				m_operands = StringFromFormat("%d, %d", bo, bi);
			}
		}
		else // Branch conditional
		{
			m_opcode = StringFromFormat("b%s%s%s%c", b_condition[((bo & 8) >> 1) + (bi & 3)], bname, ext, y);

			if (bi >= 4)
			{
				m_operands = StringFromFormat("cr%d", bi >> 2);
			}
		}
	}
	else
	{
		// CTR is decremented and checked
		m_opcode = StringFromFormat("bd%s%s%s%c", b_decr[bo >> 1], bname, ext, y);

		if ((bo & 16) == 0)
		{
			m_operands = StringFromFormat("%d", bi);
		}
	}

	return m_operands.length();
}


void GekkoDisassembler::bc(u32 in)
{
	unsigned int d = (int)(in & 0xfffc);

	if (d & 0x8000)
		d |= 0xffff0000;

	branch(in, "", (in & 2) ? 1 : 0, d);

	if (in & 2)  // AA ?
		m_operands = StringFromFormat("%s ->0x%.8X", m_operands.c_str(), d);
	else
		m_operands = StringFromFormat("%s ->0x%.8X", m_operands.c_str(), *m_iaddr + d);

	m_type = PPCINSTR_BRANCH;
	m_displacement = d;
}


void GekkoDisassembler::bli(u32 in)
{
	unsigned int d = (unsigned int)(in & 0x3fffffc);

	if (d & 0x02000000)
		d |= 0xfc000000;

	m_opcode = StringFromFormat("b%s", b_ext[in & 3]);

	if (in & 2)  // AA ?
		m_operands = StringFromFormat("->0x%.8X", d);
	else
		m_operands = StringFromFormat("->0x%.8X", *m_iaddr + d);

	m_type = PPCINSTR_BRANCH;
	m_displacement = d;
}


void GekkoDisassembler::mcrf(u32 in, char c)
{
	if ((in & 0x0063f801) == 0)
	{
		m_opcode = StringFromFormat("mcrf%c", c);
		m_operands = StringFromFormat("cr%d, cr%d", (int)PPCGETCRD(in), (int)PPCGETCRA(in));
	}
	else
	{
		ill(in);
	}
}


void GekkoDisassembler::crop(u32 in, const char* n1, const char* n2)
{
	int crd = (int)PPCGETD(in);
	int cra = (int)PPCGETA(in);
	int crb = (int)PPCGETB(in);

	if ((in & 1) == 0)
	{
		m_opcode = StringFromFormat("cr%s", (cra == crb && n2) ? n2 : n1);
		if (cra == crb && n2)
			m_operands = StringFromFormat("%d, %d", crd, cra);
		else
			m_operands = StringFromFormat("%d, %d, %d", crd, cra, crb);
	}
	else
	{
		ill(in);
	}
}


void GekkoDisassembler::nooper(u32 in, const char* name, unsigned char dmode)
{
	if (in & (PPCDMASK | PPCAMASK | PPCBMASK | 1))
	{
		ill(in);
	}
	else
	{
		m_flags |= dmode;
		m_opcode = name;
	}
}

void GekkoDisassembler::rlw(u32 in, const char* name, int i)
{
	int s = (int)PPCGETD(in);
	int a = (int)PPCGETA(in);
	int bsh = (int)PPCGETB(in);
	int mb = (int)PPCGETC(in);
	int me = (int)PPCGETM(in);

	m_opcode = StringFromFormat("rlw%s%c", name, in & 1 ? '.' : '\0');
	m_operands = StringFromFormat("%s, %s, %s%d, %d, %d (%08x)", regnames[a], regnames[s], regsel[i], bsh, mb, me, HelperRotateMask(bsh, mb, me));
}


void GekkoDisassembler::ori(u32 in, const char* name)
{
	m_opcode = name;
	m_operands = imm(in, 1, 1, true);
}


void GekkoDisassembler::rld(u32 in, const char* name, int i)
{
	int s = (int)PPCGETD(in);
	int a = (int)PPCGETA(in);
	int bsh = i ? (int)PPCGETB(in) : (int)(((in & 2) << 4) + PPCGETB(in));
	int m = (int)(in & 0x7e0) >> 5;

	m_flags |= PPCF_64;
	m_opcode = StringFromFormat("rld%s%c", name, in & 1 ? '.' : '\0');
	m_operands = StringFromFormat("%s, %s, %s%d, %d", regnames[a], regnames[s], regsel[i], bsh, m);
}


void GekkoDisassembler::cmp(u32 in)
{
	int i = (int)PPCGETL(in);

	if (i < 2)
	{
		if (i != 0)
			m_flags |= PPCF_64;

		m_opcode = cmpname[((in&PPCIDX2MASK) ? 2 : 0) + i];

		i = (int)PPCGETCRD(in);
		if (i != 0)
			m_operands += StringFromFormat("cr%c,", '0' + i);

		m_operands += ra_rb(in);
	}
	else
	{
		ill(in);
	}
}


void GekkoDisassembler::trap(u32 in, unsigned char dmode)
{
	int to = (int)PPCGETD(in);
	const char* cnd = trap_condition[to];

	if (cnd != nullptr)
	{
		m_flags |= dmode;
		m_opcode = StringFromFormat("t%c%s", dmode ? 'd' : 'w', cnd);
		m_operands = ra_rb(in);
	}
	else
	{
		if (to == 31)
		{
			if (dmode)
			{
				m_flags |= dmode;
				m_opcode = "td";
				m_operands = "31,0,0";
			}
			else
			{
				m_opcode = "trap";
			}
		}
		else
		{
			ill(in);
		}
	}
}

// Standard instruction: xxxx rD,rA,rB
void GekkoDisassembler::dab(u32 in, const char* name, int mask, int smode, int chkoe, int chkrc, unsigned char dmode)
{
	if (chkrc >= 0 && ((in & 1) != (unsigned int)chkrc))
	{
		ill(in);
	}
	else
	{
		m_flags |= dmode;

		// rA,rS,rB
		if (smode)
			in = swapda(in);

		m_opcode = StringFromFormat("%s%s%s", name, oesel[chkoe && (in&PPCOE)], rcsel[(chkrc < 0) && (in & 1)]);
		m_operands = rd_ra_rb(in, mask);
	}
}

// Last operand is no register: xxxx rD,rA,NB
void GekkoDisassembler::rrn(u32 in, const char* name, int smode, int chkoe, int chkrc, unsigned char dmode)
{
	if (chkrc >= 0 && ((in & 1) != (unsigned int)chkrc))
	{
		ill(in);
	}
	else
	{
		m_flags |= dmode;

		// rA,rS,NB
		if (smode)
			in = swapda(in);

		m_opcode = StringFromFormat("%s%s%s", name, oesel[chkoe && (in&PPCOE)], rcsel[(chkrc < 0) && (in & 1)]);

		m_operands = rd_ra_rb(in, 6);
		m_operands += StringFromFormat(",%d",(int)PPCGETB(in));
	}
}


void GekkoDisassembler::mtcr(u32 in)
{
	int s = (int)PPCGETD(in);
	int crm = (int)(in & 0x000ff000) >> 12;

	if (in & 0x00100801)
	{
		ill(in);
	}
	else
	{
		m_opcode = StringFromFormat("mtcr%c", crm == 0xff ? '\0' : 'f');

		if (crm != 0xff)
			m_operands += StringFromFormat("0x%02x,", crm);

		m_operands += regnames[s];
	}
}


void GekkoDisassembler::msr(u32 in, int smode)
{
	int s = (int)PPCGETD(in);
	int sr = (int)(in & 0x000f0000) >> 16;

	if (in & 0x0010f801)
	{
		ill(in);
	}
	else
	{
		m_flags |= PPCF_SUPER;
		m_opcode = StringFromFormat("m%csr", smode ? 't' : 'f');

		if (smode)
			m_operands = StringFromFormat("%d, %s", sr, regnames[s]);
		else
			m_operands = StringFromFormat("%s, %d", regnames[s], sr);
	}
}


void GekkoDisassembler::mspr(u32 in, int smode)
{
	int d = (int)PPCGETD(in);
	int spr = (int)((PPCGETB(in) << 5) + PPCGETA(in));
	int fmt = 0;

	if (in & 1)
	{
		ill(in);
	}
	else
	{
		if (spr != 1 && spr != 8 && spr != 9)
			m_flags |= PPCF_SUPER;

		const char* x;
		switch (spr)
		{
		case 1:
			x = "xer";
			break;

		case 8:
			x = "lr";
			break;

		case 9:
			x = "ctr";
			break;

		default:
			x = "spr";
			fmt = 1;
			break;
		}

		m_opcode = StringFromFormat("m%c%s", smode ? 't' : 'f', x);

		if (fmt)
		{
			if (smode)
				m_operands = StringFromFormat("%s, %s", spr_name(spr).c_str(), regnames[d]);
			else
				m_operands = StringFromFormat("%s, %s", regnames[d], spr_name(spr).c_str());
		}
		else
		{
			m_operands = regnames[d];
		}
	}
}


void GekkoDisassembler::mtb(u32 in)
{
	int d = (int)PPCGETD(in);
	int tbr = (int)((PPCGETB(in) << 5) + PPCGETA(in));

	if (in & 1)
	{
		ill(in);
	}
	else
	{
		m_operands += regnames[d];

		char x;
		switch (tbr)
		{
		case 268:
			x = 'l';
			break;

		case 269:
			x = 'u';
			break;

		default:
			x = '\0';
			m_flags |= PPCF_SUPER;
			m_operands += StringFromFormat(",%d", tbr);
			break;
		}

		m_opcode = StringFromFormat("mftb%c", x);
	}
}


void GekkoDisassembler::sradi(u32 in)
{
	int s = (int)PPCGETD(in);
	int a = (int)PPCGETA(in);
	int bsh = (int)(((in & 2) << 4) + PPCGETB(in));

	m_flags |= PPCF_64;
	m_opcode = StringFromFormat("sradi%c", in & 1 ? '.' : '\0');
	m_operands = StringFromFormat("%s, %s, %d", regnames[a], regnames[s], bsh);
}

void GekkoDisassembler::ldst(u32 in, const char* name, char reg, unsigned char dmode)
{
	int s = (int)PPCGETD(in);
	int a = (int)PPCGETA(in);
	int d = (u32)(in & 0xffff);

	m_type = PPCINSTR_LDST;
	m_flags |= dmode;
	m_sreg = (short)a;
	//  if (d >= 0x8000)
	//    d -= 0x10000;
	m_displacement = (u32)d;
	m_opcode = name;

	if (reg == 'r')
	{
		m_operands = StringFromFormat("%s, %s (%s)", regnames[s], ldst_offs(d).c_str(), regnames[a]);
	}
	else
	{
		m_operands = StringFromFormat("%c%d, %s (%s)", reg, s, ldst_offs(d).c_str(), regnames[a]);
	}
}

// Standard floating point instruction: xxxx fD,fA,fC,fB
void GekkoDisassembler::fdabc(u32 in, const char* name, int mask, unsigned char dmode)
{
	int err = 0;

	m_flags |= dmode;
	m_opcode = StringFromFormat("f%s%s", name, rcsel[in & 1]);
	m_operands += StringFromFormat("f%d,", (int)PPCGETD(in));

	if (mask & 4)
		m_operands += StringFromFormat("f%d,", (int)PPCGETA(in));
	else
		err |= (int)PPCGETA(in);

	if (mask & 2)
		m_operands += StringFromFormat("f%d,", (int)PPCGETC(in));
	else if (PPCGETC(in))
		err |= (int)PPCGETC(in);

	if (mask & 1)
		m_operands += StringFromFormat("f%d,", (int)PPCGETB(in));
	else if (!(mask & 8))
		err |= (int)PPCGETB(in);

	// Drop the trailing comma
	m_operands.pop_back();

	if (err)
		ill(in);
}

void GekkoDisassembler::fmr(u32 in)
{
	m_opcode = StringFromFormat("fmr%s", rcsel[in & 1]);
	m_operands = StringFromFormat("f%d, f%d", (int)PPCGETD(in), (int)PPCGETB(in));
}

// Indexed float instruction: xxxx fD,rA,rB
void GekkoDisassembler::fdab(u32 in, const char* name, int mask)
{
	m_opcode = name;
	m_operands = fd_ra_rb(in, mask);
}


void GekkoDisassembler::fcmp(u32 in, char c)
{
	if (in & 0x00600001)
	{
		ill(in);
	}
	else
	{
		m_opcode = StringFromFormat("fcmp%c", c);
		m_operands = StringFromFormat("cr%d,f%d,f%d", (int)PPCGETCRD(in), (int)PPCGETA(in), (int)PPCGETB(in));
	}
}


void GekkoDisassembler::mtfsb(u32 in, int n)
{
	if (in & (PPCAMASK | PPCBMASK))
	{
		ill(in);
	}
	else
	{
		m_opcode = StringFromFormat("mtfsb%d%s", n, rcsel[in & 1]);
		m_operands = StringFromFormat("%d", (int)PPCGETD(in));
	}
}


// Paired instructions

#define RA ((inst >> 16) & 0x1f)
#define RB ((inst >> 11) & 0x1f)
#define RC ((inst >> 6) & 0x1f)
#define RD ((inst >> 21) & 0x1f)
#define RS ((inst >> 21) & 0x1f)
#define FA ((inst >> 16) & 0x1f)
#define FB ((inst >> 11) & 0x1f)
#define FC ((inst >> 6) & 0x1f)
#define FD ((inst >> 21) & 0x1f)
#define FS ((inst >> 21) & 0x1f)
#define IMM (inst & 0xffff)
#define UIMM (inst & 0xffff)
#define OFS (inst & 0xffff)
#define OPCD ((inst >> 26) & 0x3f)
#define XO_10 ((inst >> 1) & 0x3ff)
#define XO_9 ((inst >> 1) & 0x1ff)
#define XO_5 ((inst >> 1) & 0x1f)
#define Rc (inst & 1)
#define SH ((inst >> 11) & 0x1f)
#define MB ((inst >> 6) & 0x1f)
#define ME ((inst >> 1) & 0x1f)
#define OE ((inst >> 10) & 1)
#define TO ((inst >> 21) & 0x1f)
#define CRFD ((inst >> 23) & 0x7)
#define CRFS ((inst >> 18) & 0x7)
#define CRBD ((inst >> 21) & 0x1f)
#define CRBA ((inst >> 16) & 0x1f)
#define CRBB ((inst >> 11) & 0x1f)
#define L ((inst >> 21) & 1)
#define NB ((inst >> 11) & 0x1f)
#define AA ((inst >> 1) & 1)
#define LK (inst & 1)
#define LI ((inst >> 2) & 0xffffff)
#define BO ((inst >> 21) & 0x1f)
#define BI ((inst >> 16) & 0x1f)
#define BD ((inst >> 2) & 0x3fff)

#define MTFSFI_IMM ((inst >> 12) & 0xf)
#define FM ((inst >> 17) & 0xff)
#define SR ((inst >> 16) & 0xf)
#define SPR ((inst >> 11) & 0x3ff)
#define TBR ((inst >> 11) & 0x3ff)
#define CRM ((inst >> 12) & 0xff)

#define I ((inst >> 12) & 0x7)
#define W ((inst >> 15) & 0x1)
#define IX ((inst >> 7) & 0x7)
#define WX ((inst >> 10) & 0x1)


void GekkoDisassembler::ps(u32 inst)
{
	switch ((inst >> 1) & 0x1F)
	{
	case 6:
		m_opcode = inst & 0x40 ? "psq_lux" : "psq_lx";
		m_operands = StringFromFormat("p%u, (r%u + r%u), %d, qr%d", FD, RA, RB, WX, IX);
		return;

	case 7:
		m_opcode = inst & 0x40 ? "psq_stux" : "psq_stx";
		m_operands = StringFromFormat("p%u, r%u, r%u, %d, qr%d", FS, RA, RB, WX, IX);
		return;

	case 18:
		m_opcode = "ps_div";
		m_operands = StringFromFormat("p%u, p%u/p%u", FD, FA, FB);
		return;

	case 20:
		m_opcode = "ps_sub";
		m_operands = StringFromFormat("p%u, p%u-p%u", FD, FA, FB);
		return;

	case 21:
		m_opcode = "ps_add";
		m_operands = StringFromFormat("p%u, p%u+p%u", FD, FA, FB);
		return;

	case 23:
		m_opcode = "ps_sel";
		m_operands = StringFromFormat("p%u>=0?p%u:p%u", FD, FA, FC);
		return;

	case 24:
		m_opcode = "ps_res";
		m_operands = StringFromFormat("p%u, (1/p%u)", FD, FB);
		return;

	case 25:
		m_opcode = "ps_mul";
		m_operands = StringFromFormat("p%u, p%u*p%u", FD, FA, FC);
		return;

	case 26: // rsqrte
		m_opcode = "ps_rsqrte";
		m_operands = StringFromFormat("p%u, p%u", FD, FB);
		return;

	case 28: // msub
		m_opcode = "ps_msub";
		m_operands = StringFromFormat("p%u, p%u*p%u-p%u", FD, FA, FC, FB);
		return;

	case 29: // madd
		m_opcode = "ps_madd";
		m_operands = StringFromFormat("p%u, p%u*p%u+p%u", FD, FA, FC, FB);
		return;

	case 30: // nmsub
		m_opcode = "ps_nmsub";
		m_operands = StringFromFormat("p%u, -(p%u*p%u-p%u)", FD, FA, FC, FB);
		return;

	case 31: // nmadd
		m_opcode = "ps_nmadd";
		m_operands = StringFromFormat("p%u, -(p%u*p%u+p%u)", FD, FA, FC, FB);
		return;

	case 10:
		m_opcode = "ps_sum0";
		m_operands = StringFromFormat("p%u, 0=p%u+p%u, 1=p%u", FD, FA, FB, FC);
		return;

	case 11:
		m_opcode = "ps_sum1";
		m_operands = StringFromFormat("p%u, 0=p%u, 1=p%u+p%u", FD, FC, FA, FB);
		return;

	case 12:
		m_opcode = "ps_muls0";
		m_operands = StringFromFormat("p%u, p%u*p%u[0]", FD, FA, FC);
		return;

	case 13:
		m_opcode = "ps_muls1";
		m_operands = StringFromFormat("p%u, p%u*p%u[1]", FD, FA, FC);
		return;

	case 14:
		m_opcode = "ps_madds0";
		m_operands = StringFromFormat("p%u, p%u*p%u[0]+p%u", FD, FA, FC, FB);
		return;

	case 15:
		m_opcode = "ps_madds1";
		m_operands = StringFromFormat("p%u, p%u*p%u[1]+p%u", FD, FA, FC, FB);
		return;
	}

	switch ((inst >> 1) & 0x3FF)
	{
		// 10-bit suckers  (?)
	case 40: // nmadd
		m_opcode = "ps_neg";
		m_operands = StringFromFormat("p%u, -p%u", FD, FB);
		return;

	case 72: // nmadd
		m_opcode = "ps_mr";
		m_operands = StringFromFormat("p%u, p%u", FD, FB);
		return;

	case 136:
		m_opcode = "ps_nabs";
		m_operands = StringFromFormat("p%u, -|p%u|", FD, FB);
		return;

	case 264:
		m_opcode = "ps_abs";
		m_operands = StringFromFormat("p%u, |p%u|", FD, FB);
		return;

	case 0:
	case 32:
	case 64:
	case 96:
	{
		m_opcode = ps_cmpname[(inst >> 6) & 0x3];

		int i = (int)PPCGETCRD(inst);
		if (i != 0)
			m_operands += StringFromFormat("cr%c, ", '0' + i);
		m_operands += StringFromFormat("p%u, p%u", FA, FB);
		return;
	}
	case 528:
		m_opcode = "ps_merge00";
		m_operands = StringFromFormat("p%u, p%u[0],p%u[0]", FD, FA, FB);
		return;

	case 560:
		m_opcode = "ps_merge01";
		m_operands = StringFromFormat("p%u, p%u[0],p%u[1]", FD, FA, FB);
		return;

	case 592:
		m_opcode = "ps_merge10";
		m_operands = StringFromFormat("p%u, p%u[1],p%u[0]", FD, FA, FB);
		return;

	case 624:
		m_opcode = "ps_merge11";
		m_operands = StringFromFormat("p%u, p%u[1],p%u[1]", FD, FA, FB);
		return;

	case 1014:
		if (inst & PPCDMASK)
			ill(inst);
		else
			dab(inst, "dcbz_l", 3, 0, 0, 0, 0);
	}

	//	default:
	m_opcode = StringFromFormat("ps_%i", ((inst >> 1) & 0x1f));
	m_operands = "---";
}

void GekkoDisassembler::ps_mem(u32 inst)
{
	switch (PPCGETIDX(inst))
	{
	case 56:
		m_opcode = "psq_l";
		m_operands = StringFromFormat("p%u, %i(r%u), %d, qr%d", RS, SEX12(inst & 0xFFF), RA, W, I);
		break;

	case 57:
		m_opcode = "psq_lu";
		m_operands = StringFromFormat("p%u, %i(r%u), %d, qr%d", RS, SEX12(inst & 0xFFF), RA, W, I);;
		break;

	case 60:
		m_opcode = "psq_st";
		m_operands = StringFromFormat("p%u, %i(r%u), %d, qr%d", RS, SEX12(inst & 0xFFF), RA, W, I);
		break;

	case 61:
		m_opcode = "psq_stu";
		m_operands = StringFromFormat("p%u, %i(r%u), %d, qr%d", RS, SEX12(inst & 0xFFF), RA, W, I);
		break;
	}
}

// Disassemble PPC instruction and return a pointer to the next
// instruction, or nullptr if an error occurred.
u32* GekkoDisassembler::DoDisassembly(bool big_endian)
{
	u32 in = *m_instr;

	if (!big_endian)
	{
		in = (in & 0xff) << 24 | (in & 0xff00) << 8 | (in & 0xff0000) >> 8 |
		     (in & 0xff000000) >> 24;
	}

	m_opcode.clear();
	m_operands.clear();
	m_type = PPCINSTR_OTHER;
	m_flags = 0;

	switch (PPCGETIDX(in))
	{
	case 2:
		trapi(in, PPCF_64); // tdi
		break;

	case 3:
		trapi(in, 0); // twi
		break;

	case 4:
		ps(in);
		break;

	case 56:
	case 57:
	case 60:
	case 61:
		ps_mem(in);
		break;

	case 7:
		m_opcode = "mulli";
		m_operands = imm(in, 0, 0, false);
		break;

	case 8:
		m_opcode = "subfic";
		m_operands = imm(in, 0, 0, false);
		break;

	case 10:
		cmpi(in, 1);      // cmpli
		break;

	case 11:
		cmpi(in, 0);      // cmpi
		break;

	case 12:
		addi(in, "ic");   // addic
		break;

	case 13:
		addi(in, "ic.");  // addic.
		break;

	case 14:
		addi(in, "i");    // addi
		break;

	case 15:
		addi(in, "is");   // addis
		break;

	case 16:
		bc(in);
		break;

	case 17:
		if ((in & ~PPCIDXMASK) == 2)
			m_opcode = "sc";
		else
			ill(in);
		break;

	case 18:
		bli(in);
		break;

	case 19:
		switch (PPCGETIDX2(in))
		{
		case 0:
			mcrf(in, '\0');  // mcrf
			break;

		case 16:
			branch(in, "lr", 0, 0);    // bclr
			break;

		case 33:
			crop(in, "nor", "not");    // crnor
			break;

		case 50:
			nooper(in, "rfi", PPCF_SUPER);
			break;

		case 129:
			crop(in, "andc", nullptr); // crandc
			break;

		case 150:
			nooper(in, "isync", 0);
			break;

		case 193:
			crop(in, "xor", "clr");    // crxor
			break;

		case 225:
			crop(in, "nand", nullptr); // crnand
			break;

		case 257:
			crop(in, "and", nullptr);  // crand
			break;

		case 289:
			crop(in, "eqv", "set");    // creqv
			break;

		case 417:
			crop(in, "orc", nullptr);  // crorc
			break;

		case 449:
			crop(in, "or", "move");    // cror
			break;

		case 528:
			branch(in, "ctr", 0, 0);   // bcctr
			break;

		default:
			ill(in);
			break;
		}
		break;

	case 20:
		rlw(in, "imi", 0); // rlwimi
		break;

	case 21:
		rlw(in, "inm", 0); // rlwinm
		break;

	case 23:
		rlw(in, "nm", 1);  // rlwnm
		break;

	case 24:
		if (in & ~PPCIDXMASK)
			ori(in, "ori");
		else
			m_opcode = "nop";
		break;

	case 25:
		ori(in, "oris");
		break;

	case 26:
		ori(in, "xori");
		break;

	case 27:
		ori(in, "xoris");
		break;

	case 28:
		ori(in, "andi.");
		break;

	case 29:
		ori(in, "andis.");
		break;

	case 30:
		switch (in & 0x1c)
		{
		case 0:
			rld(in, "icl", 0);  // rldicl
			break;
		case 1:
			rld(in, "icr", 0);  // rldicr
			break;
		case 2:
			rld(in, "ic", 0);   // rldic
			break;
		case 3:
			rld(in, "imi", 0);  // rldimi
			break;
		case 4:
			rld(in, in & 2 ? "cl" : "cr", 1); // rldcl, rldcr
			break;
		default:
			ill(in);
			break;
		}
		break;

	case 31:
		switch (PPCGETIDX2(in))
		{
		case 0:
		case 32:
			if (in & 1)
				ill(in);
			else
				cmp(in); // cmp, cmpl
			break;

		case 4:
			if (in & 1)
				ill(in);
			else
				trap(in, 0); // tw
			break;

		case 8:
		case (PPCOE >> 1) + 8:
			dab(swapab(in), "subc", 7, 0, 1, -1, 0);
			break;

		case 9:
			dab(in, "mulhdu", 7, 0, 0, -1, PPCF_64);
			break;

		case 10:
		case (PPCOE >> 1) + 10:
			dab(in, "addc", 7, 0, 1, -1, 0);
			break;

		case 11:
			dab(in, "mulhwu", 7, 0, 0, -1, 0);
			break;

		case 19:
			if (in & (PPCAMASK | PPCBMASK))
				ill(in);
			else
				dab(in, "mfcr", 4, 0, 0, 0, 0);
			break;

		case 20:
			dab(in, "lwarx", 7, 0, 0, 0, 0);
			break;

		case 21:
			dab(in, "ldx", 7, 0, 0, 0, PPCF_64);
			break;

		case 23:
			dab(in, "lwzx", 7, 0, 0, 0, 0);
			break;

		case 24:
			dab(in, "slw", 7, 1, 0, -1, 0);
			break;

		case 26:
			if (in & PPCBMASK)
				ill(in);
			else
				dab(in, "cntlzw", 6, 1, 0, -1, 0);
			break;

		case 27:
			dab(in, "sld", 7, 1, 0, -1, PPCF_64);
			break;

		case 28:
			dab(in, "and", 7, 1, 0, -1, 0);
			break;

		case 40:
		case (PPCOE >> 1) + 40:
			dab(swapab(in), "sub", 7, 0, 1, -1, 0);
			break;

		case 53:
			dab(in, "ldux", 7, 0, 0, 0, PPCF_64);
			break;

		case 54:
			if (in & PPCDMASK)
				ill(in);
			else
				dab(in, "dcbst", 3, 0, 0, 0, 0);
			break;

		case 55:
			dab(in, "lwzux", 7, 0, 0, 0, 0);
			break;

		case 58:
			if (in & PPCBMASK)
				ill(in);
			else
				dab(in, "cntlzd", 6, 1, 0, -1, PPCF_64);
			break;

		case 60:
			dab(in, "andc", 7, 1, 0, -1, 0);
			break;

		case 68:
			trap(in, PPCF_64); // td
			break;

		case 73:
			dab(in, "mulhd", 7, 0, 0, -1, PPCF_64);
			break;

		case 75:
			dab(in, "mulhw", 7, 0, 0, -1, 0);
			break;

		case 83:
			if (in & (PPCAMASK | PPCBMASK))
				ill(in);
			else
				dab(in, "mfmsr", 4, 0, 0, 0, PPCF_SUPER);
			break;

		case 84:
			dab(in, "ldarx", 7, 0, 0, 0, PPCF_64);
			break;

		case 86:
			if (in & PPCDMASK)
				ill(in);
			else
				dab(in, "dcbf", 3, 0, 0, 0, 0);
			break;

		case 87:
			dab(in, "lbzx", 7, 0, 0, 0, 0);
			break;

		case 104:
		case (PPCOE >> 1) + 104:
			if (in & PPCBMASK)
				ill(in);
			else
				dab(in, "neg", 6, 0, 1, -1, 0);
			break;

		case 119:
			dab(in, "lbzux", 7, 0, 0, 0, 0);
			break;

		case 124:
			if (PPCGETD(in) == PPCGETB(in))
				dab(in, "not", 6, 1, 0, -1, 0);
			else
				dab(in, "nor", 7, 1, 0, -1, 0);
			break;

		case 136:
		case (PPCOE >> 1) + 136:
			dab(in, "subfe", 7, 0, 1, -1, 0);
			break;

		case 138:
		case (PPCOE >> 1) + 138:
			dab(in, "adde", 7, 0, 1, -1, 0);
			break;

		case 144:
			mtcr(in);
			break;

		case 146:
			if (in & (PPCAMASK | PPCBMASK))
				ill(in);
			else
				dab(in, "mtmsr", 4, 0, 0, 0, PPCF_SUPER);
			break;

		case 149:
			dab(in, "stdx", 7, 0, 0, 0, PPCF_64);
			break;

		case 150:
			dab(in, "stwcx.", 7, 0, 0, 1, 0);
			break;

		case 151:
			dab(in, "stwx", 7, 0, 0, 0, 0);
			break;

		case 181:
			dab(in, "stdux", 7, 0, 0, 0, PPCF_64);
			break;

		case 183:
			dab(in, "stwux", 7, 0, 0, 0, 0);
			break;

		case 200:
		case (PPCOE >> 1) + 200:
			if (in & PPCBMASK)
				ill(in);
			else
				dab(in, "subfze", 6, 0, 1, -1, 0);
			break;

		case 202:
		case (PPCOE >> 1) + 202:
			if (in & PPCBMASK)
				ill(in);
			else
				dab(in, "addze", 6, 0, 1, -1, 0);
			break;

		case 210:
			msr(in, 1); // mfsr
			break;

		case 214:
			dab(in, "stdcx.", 7, 0, 0, 1, PPCF_64);
			break;

		case 215:
			dab(in, "stbx", 7, 0, 0, 0, 0);
			break;

		case 232:
		case (PPCOE >> 1) + 232:
			if (in & PPCBMASK)
				ill(in);
			else
				dab(in, "subfme", 6, 0, 1, -1, 0);
			break;

		case 233:
		case (PPCOE >> 1) + 233:
			dab(in, "mulld", 7, 0, 1, -1, PPCF_64);
			break;

		case 234:
		case (PPCOE >> 1) + 234:
			if (in & PPCBMASK)
				ill(in);
			else
				dab(in, "addme", 6, 0, 1, -1, 0);
			break;

		case 235:
		case (PPCOE >> 1) + 235:
			dab(in, "mullw", 7, 0, 1, -1, 0);
			break;

		case 242:
			if (in & PPCAMASK)
				ill(in);
			else
				dab(in, "mtsrin", 5, 0, 0, 0, PPCF_SUPER);
			break;

		case 246:
			if (in & PPCDMASK)
				ill(in);
			else
				dab(in, "dcbtst", 3, 0, 0, 0, 0);
			break;

		case 247:
			dab(in, "stbux", 7, 0, 0, 0, 0);
			break;

		case 266:
		case (PPCOE >> 1) + 266:
			dab(in, "add", 7, 0, 1, -1, 0);
			break;

		case 278:
			if (in & PPCDMASK)
				ill(in);
			else
				dab(in, "dcbt", 3, 0, 0, 0, 0);
			break;

		case 279:
			dab(in, "lhzx", 7, 0, 0, 0, 0);
			break;

		case 284:
			dab(in, "eqv", 7, 1, 0, -1, 0);
			break;

		case 306:
			if (in & (PPCDMASK | PPCAMASK))
				ill(in);
			else
				dab(in, "tlbie", 1, 0, 0, 0, PPCF_SUPER);
			break;

		case 310:
			dab(in, "eciwx", 7, 0, 0, 0, 0);
			break;

		case 311:
			dab(in, "lhzux", 7, 0, 0, 0, 0);
			break;

		case 316:
			dab(in, "xor", 7, 1, 0, -1, 0);
			break;

		case 339:
			mspr(in, 0); // mfspr
			break;

		case 341:
			dab(in, "lwax", 7, 0, 0, 0, PPCF_64);
			break;

		case 343:
			dab(in, "lhax", 7, 0, 0, 0, 0);
			break;

		case 370:
			nooper(in, "tlbia", PPCF_SUPER);
			break;

		case 371:
			mtb(in); // mftb
			break;

		case 373:
			dab(in, "lwaux", 7, 0, 0, 0, PPCF_64);
			break;

		case 375:
			dab(in, "lhaux", 7, 0, 0, 0, 0);
			break;

		case 407:
			dab(in, "sthx", 7, 0, 0, 0, 0);
			break;

		case 412:
			dab(in, "orc", 7, 1, 0, -1, 0);
			break;

		case 413:
			sradi(in); // sradi
			break;

		case 434:
			if (in & (PPCDMASK | PPCAMASK))
				ill(in);
			else
				dab(in, "slbie", 1, 0, 0, 0, PPCF_SUPER | PPCF_64);
			break;

		case 438:
			dab(in, "ecowx", 7, 0, 0, 0, 0);
			break;

		case 439:
			dab(in, "sthux", 7, 0, 0, 0, 0);
			break;

		case 444:
			if (PPCGETD(in) == PPCGETB(in))
				dab(in, "mr", 6, 1, 0, -1, 0);
			else
				dab(in, "or", 7, 1, 0, -1, 0);
			break;

		case 457:
		case (PPCOE >> 1) + 457:
			dab(in, "divdu", 7, 0, 1, -1, PPCF_64);
			break;

		case 459:
		case (PPCOE >> 1) + 459:
			dab(in, "divwu", 7, 0, 1, -1, 0);
			break;

		case 467:
			mspr(in, 1); // mtspr
			break;

		case 470:
			if (in & PPCDMASK)
				ill(in);
			else
				dab(in, "dcbi", 3, 0, 0, 0, 0);
			break;

		case 476:
			dab(in, "nand", 7, 1, 0, -1, 0);
			break;

		case 489:
		case (PPCOE >> 1) + 489:
			dab(in, "divd", 7, 0, 1, -1, PPCF_64);
			break;

		case 491:
		case (PPCOE >> 1) + 491:
			dab(in, "divw", 7, 0, 1, -1, 0);
			break;

		case 498:
			nooper(in, "slbia", PPCF_SUPER | PPCF_64);
			break;

		case 512:
			if (in & 0x007ff801)
			{
				ill(in);
			}
			else
			{
				m_opcode = "mcrxr";
				m_operands = StringFromFormat("cr%d", (int)PPCGETCRD(in));
			}
			break;

		case 533:
			dab(in, "lswx", 7, 0, 0, 0, 0);
			break;

		case 534:
			dab(in, "lwbrx", 7, 0, 0, 0, 0);
			break;

		case 535:
			fdab(in, "lfsx", 7);
			break;

		case 536:
			dab(in, "srw", 7, 1, 0, -1, 0);
			break;

		case 539:
			dab(in, "srd", 7, 1, 0, -1, PPCF_64);
			break;

		case 566:
			nooper(in, "tlbsync", PPCF_SUPER);
			break;

		case 567:
			fdab(in, "lfsux", 7);
			break;

		case 595:
			msr(in, 0); // mfsr
			break;

		case 597:
			rrn(in, "lswi", 0, 0, 0, 0);
			break;

		case 598:
			nooper(in, "sync", PPCF_SUPER);
			break;

		case 599:
			fdab(in, "lfdx", 7);
			break;

		case 631:
			fdab(in, "lfdux", 7);
			break;

		case 659:
			if (in & PPCAMASK)
				ill(in);
			else
				dab(in, "mfsrin", 5, 0, 0, 0, PPCF_SUPER);
			break;

		case 661:
			dab(in, "stswx", 7, 0, 0, 0, 0);
			break;

		case 662:
			dab(in, "stwbrx", 7, 0, 0, 0, 0);
			break;

		case 663:
			fdab(in, "stfsx", 7);
			break;

		case 695:
			fdab(in, "stfsux", 7);
			break;

		case 725:
			rrn(in, "stswi", 0, 0, 0, 0);
			break;

		case 727:
			fdab(in, "stfdx", 7);
			break;

		case 759:
			fdab(in, "stfdux", 7);
			break;

		case 790:
			dab(in, "lhbrx", 7, 0, 0, 0, 0);
			break;

		case 792:
			dab(in, "sraw", 7, 1, 0, -1, 0);
			break;

		case 794:
			dab(in, "srad", 7, 1, 0, -1, PPCF_64);
			break;

		case 824:
			rrn(in, "srawi", 1, 0, -1, 0);
			break;

		case 854:
			nooper(in, "eieio", PPCF_SUPER);
			break;

		case 918:
			dab(in, "sthbrx", 7, 0, 0, 0, 0);
			break;

		case 922:
			if (in & PPCBMASK)
				ill(in);
			else
				dab(in, "extsh", 6, 1, 0, -1, 0);
			break;

		case 954:
			if (in & PPCBMASK)
				ill(in);
			else
				dab(in, "extsb", 6, 1, 0, -1, 0);
			break;

		case 982:
			if (in & PPCDMASK)
				ill(in);
			else
				dab(in, "icbi", 3, 0, 0, 0, 0);
			break;

		case 983:
			fdab(in, "stfiwx", 7);
			break;

		case 986:
			if (in & PPCBMASK)
				ill(in);
			else
				dab(in, "extsw", 6, 1, 0, -1, PPCF_64);
			break;

		case 1014:
			if (in & PPCDMASK)
				ill(in);
			else
				dab(in, "dcbz", 3, 0, 0, 0, 0);
			break;

		default:
			ill(in);
			break;
		}
		break;

	case 32:
	case 33:
	case 34:
	case 35:
	case 36:
	case 37:
	case 38:
	case 39:
	case 40:
	case 41:
	case 42:
	case 43:
	case 44:
	case 45:
	case 46:
	case 47:
		ldst(in, ldstnames[PPCGETIDX(in) - 32], 'r', 0);
		break;

	case 48:
	case 49:
	case 50:
	case 51:
	case 52:
	case 53:
	case 54:
	case 55:
		ldst(in, ldstnames[PPCGETIDX(in) - 32], 'f', 0);
		break;

	case 58:
		switch (in & 3)
		{
		case 0:
			ldst(in&~3, "ld", 'r', PPCF_64);
			break;
		case 1:
			ldst(in&~3, "ldu", 'r', PPCF_64);
			break;
		case 2:
			ldst(in&~3, "lwa", 'r', PPCF_64);
			break;
		default:
			ill(in);
			break;
		}
		break;

	case 59:
		switch (in & 0x3e)
		{
		case 36:
			fdabc(in, "divs", 5, 0);
			break;

		case 40:
			fdabc(in, "subs", 5, 0);
			break;

		case 42:
			fdabc(in, "adds", 5, 0);
			break;

		case 44:
			fdabc(in, "sqrts", 1, 0);
			break;

		case 48:
			fdabc(in, "res", 1, 0);
			break;

		case 50:
			fdabc(in, "muls", 6, 0);
			break;

		case 56:
			fdabc(in, "msubs", 7, 0);
			break;

		case 58:
			fdabc(in, "madds", 7, 0);
			break;

		case 60:
			fdabc(in, "nmsubs", 7, 0);
			break;

		case 62:
			fdabc(in, "nmadds", 7, 0);
			break;

		default:
			ill(in);
			break;
		}
		break;

	case 62:
		switch (in & 3)
		{
		case 0:
			ldst(in&~3, "std", 'r', PPCF_64);
			break;
		case 1:
			ldst(in&~3, "stdu", 'r', PPCF_64);
			break;
		default:
			ill(in);
			break;
		}
		break;

	case 63:
		if (in & 32)
		{
			switch (in & 0x1e)
			{
			case 4:
				fdabc(in, "div", 5, 0);
				break;

			case 8:
				fdabc(in, "sub", 5, 0);
				break;

			case 10:
				fdabc(in, "add", 5, 0);
				break;

			case 12:
				fdabc(in, "sqrt", 1, 0);
				break;

			case 14:
				fdabc(in, "sel", 7, 0);
				break;

			case 18:
				fdabc(in, "mul", 6, 0);
				break;

			case 20:
				fdabc(in, "rsqrte", 1, 0);
				break;

			case 24:
				fdabc(in, "msub", 7, 0);
				break;

			case 26:
				fdabc(in, "madd", 7, 0);
				break;

			case 28:
				fdabc(in, "nmsub", 7, 0);
				break;

			case 30:
				fdabc(in, "nmadd", 7, 0);
				break;

			default:
				ill(in);
				break;
			}
		}
		else
		{
			switch (PPCGETIDX2(in))
			{
			case 0:
				fcmp(in, 'u');
				break;

			case 12:
				fdabc(in, "rsp", 1, 0);
				break;

			case 14:
				fdabc(in, "ctiw", 1, 0);
				break;

			case 15:
				fdabc(in, "ctiwz", 1, 0);
				break;

			case 32:
				fcmp(in, 'o');
				break;

			case 38:
				mtfsb(in, 1);
				break;

			case 40:
				fdabc(in, "neg", 10, 0);
				break;

			case 64:
				mcrf(in, 's'); // mcrfs
				break;

			case 70:
				mtfsb(in, 0);
				break;

			case 72:
				fmr(in);
				break;

			case 134:
				if ((in & 0x006f0800) == 0)
				{
					m_opcode = StringFromFormat("mtfsfi%s", rcsel[in & 1]);
					m_operands = StringFromFormat("cr%d,%d", (int)PPCGETCRD(in), (int)(in & 0xf000) >> 12);
				}
				else
				{
					ill(in);
				}
				break;

			case 136:
				fdabc(in, "nabs", 10, 0);
				break;

			case 264:
				fdabc(in, "abs", 10, 0);
				break;

			case 583:
				if (in & (PPCAMASK | PPCBMASK))
					ill(in);
				else
					dab(in, "mffs", 4, 0, 0, -1, 0);
				break;

			case 711:
				if ((in & 0x02010000) == 0)
				{
					m_opcode = StringFromFormat("mtfsf%s", rcsel[in & 1]);
					m_operands = StringFromFormat("0x%x,%u", (unsigned int)(in >> 17) & 0x01fe, (unsigned int)PPCGETB(in));
				}
				else
				{
					ill(in);
				}
				break;

			case 814:
				fdabc(in, "fctid", 10, PPCF_64);
				break;

			case 815:
				fdabc(in, "fctidz", 10, PPCF_64);
				break;

			case 846:
				fdabc(in, "fcfid", 10, PPCF_64);
				break;

			default:
				ill(in);
				break;
			}
		}
		break;

	default:
		ill(in);
		break;
	}
	return (m_instr + 1);
}

// simplified interface
std::string GekkoDisassembler::Disassemble(u32 opcode, u32 current_instruction_address, bool big_endian)
{
	u32 opc = opcode;
	u32 addr = current_instruction_address;

	m_instr = (u32*)&opc;
	m_iaddr = (u32*)&addr;

	DoDisassembly(big_endian);

	return m_opcode.append("\t").append(m_operands);
}

static const char* gprnames[] =
{
	" r0", " r1", " r2", " r3", " r4", " r5", " r6", " r7",
	" r8", " r9", "r10", "r11", "r12", "r13", "r14", "r15",
	"r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
	"r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
};

const char* GekkoDisassembler::GetGPRName(u32 index)
{
	if (index < 32)
		return gprnames[index];

	return nullptr;
}

static const char* fprnames[] =
{
	" f0", " f1", " f2", " f3", " f4", " f5", " f6", " f7",
	" f8", " f9", "f10", "f11", "f12", "f13", "f14", "f15",
	"f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
	"f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31"
};

const char* GekkoDisassembler::GetFPRName(u32 index)
{
	if (index < 32)
		return fprnames[index];

	return nullptr;
}