summaryrefslogtreecommitdiffstats
path: root/src/VBox/ValidationKit/utils/cpu/cidet-core.cpp
blob: ff350861b0a5add67e54c6ec9d938b4511f6938a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
/* $Id: cidet-core.cpp $ */
/** @file
 * CPU Instruction Decoding & Execution Tests - Simple Instructions.
 */

/*
 * Copyright (C) 2014-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * 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, in version 3 of the
 * License.
 *
 * 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, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
#define CIDET_INSTR_TEST_OP_FLAG(a_pInstr, a_fFlag) \
    (   ((a_pInstr)->afOperands[0] & (a_fFlag)) \
     || ((a_pInstr)->afOperands[1] & (a_fFlag)) \
     || (   (a_pInstr)->cOperands > 2 \
         && (   ((a_pInstr)->afOperands[2] & (a_fFlag)) \
             || ((a_pInstr)->afOperands[3] & (a_fFlag))  ) ) )

#define CIDET_INSTR_TEST_OP_MASK_VALUE(a_pInstr, a_fMask, a_fValue) \
    (   ((a_pInstr)->afOperands[0] & (a_fMask)) == (a_fValue) \
     || ((a_pInstr)->afOperands[1] & (a_fMask)) == (a_fValue) \
     || (   (a_pInstr)->cOperands > 2 \
         && (   ((a_pInstr)->afOperands[2] & (a_fMask)) == (a_fValue) \
             || ((a_pInstr)->afOperands[3] & (a_fMask)) == (a_fValue) ) ) )

/** @def CIDET_DPRINTF
 * Debug printf. */
#if 1 //def DEBUG_bird
# define CIDET_DPRINTF(a)   do { RTPrintf a; } while (0)
# define CIDET_DPRINTF_ENABLED
#else
# define CIDET_DPRINTF(a)   do { } while (0)
#endif

/** @def CIDET_DEBUG_DISAS
 * Enables instruction disassembly. */
#if defined(DOXYGEN_RUNNING)
# define CIDET_DEBUG_DISAS 1
#endif


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include "cidet.h"

#include <iprt/assert.h>
#include <iprt/rand.h>
#include <iprt/param.h>
#include <iprt/string.h>
#include <iprt/errcore.h>
#if defined(CIDET_DPRINTF_ENABLED) || defined(CIDET_DEBUG_DISAS)
# include <VBox/dis.h>
# include <iprt/stream.h>
#endif


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
/** For translating CIDET_OF_Z_XXX values (after shifting). */
uint16_t const g_acbCidetOfSizes[] =
{
    /* [CIDET_OF_Z_NONE]    = */  0,
    /* [CIDET_OF_Z_BYTE]    = */  1,
    /* [CIDET_OF_Z_WORD]    = */  2,
    /* [CIDET_OF_Z_DWORD]   = */  4,
    /* [CIDET_OF_Z_QWORD]   = */  8,
    /* [CIDET_OF_Z_TBYTE]   = */  10,
    /* [CIDET_OF_Z_OWORD]   = */  16,
    /* [CIDET_OF_Z_YWORD]   = */  32,
    /* [CIDET_OF_Z_ZWORD]   = */  64,
    /* [CIDET_OF_Z_VAR_WDQ] = */  UINT16_MAX,
    /* [0xa] = */                 0,
    /* [0xb] = */                 0,
    /* [0xc] = */                 0,
    /* [0xd] = */                 0,
    /* [0xe] = */                 0,
    /* [CIDET_OF_Z_SPECIAL] = */  UINT16_MAX - 1,
};


/** Converts operand sizes in bytes to 64-bit masks. */
static const uint64_t g_au64ByteSizeToMask[] =
{
    UINT64_C(0x0000000000000000),
    UINT64_C(0x00000000000000ff),
    UINT64_C(0x000000000000ffff),
    UINT64_C(0x0000000000ffffff),
    UINT64_C(0x00000000ffffffff),
    UINT64_C(0x000000ffffffffff),
    UINT64_C(0x0000ffffffffffff),
    UINT64_C(0x00ffffffffffffff),
    UINT64_C(0xffffffffffffffff),
};

/** Converts operand sizes in bytes to 64-bit signed max values. */
static const int64_t g_ai64ByteSizeToMax[] =
{
    INT64_C(0x0000000000000000),
    INT64_C(0x000000000000007f),
    INT64_C(0x0000000000007fff),
    INT64_C(0x00000000007fffff),
    INT64_C(0x000000007fffffff),
    INT64_C(0x0000007fffffffff),
    INT64_C(0x00007fffffffffff),
    INT64_C(0x007fffffffffffff),
    INT64_C(0x7fffffffffffffff),
};


bool CidetInstrHasMrmMemOperand(PCCIDETINSTR pInstr)
{
    return CIDET_INSTR_TEST_OP_FLAG(pInstr, CIDET_OF_M_RM_ONLY_M);
}


bool CidetInstrHasMrmRegOperand(PCCIDETINSTR pInstr)
{
    return CIDET_INSTR_TEST_OP_FLAG(pInstr, CIDET_OF_M_RM_ONLY_R);
}


bool CidetInstrRespondsToOperandSizePrefixes(PCCIDETINSTR pInstr)
{
    return CIDET_INSTR_TEST_OP_MASK_VALUE(pInstr, CIDET_OF_Z_MASK, CIDET_OF_Z_VAR_WDQ);
}




int CidetCoreInit(PCIDETCORE pThis, RTRAND hRand)
{
    AssertPtr(pThis);
    AssertPtr(hRand);

    RT_ZERO(*pThis);
    pThis->u32Magic  = CIDETCORE_MAGIC;
    pThis->hRand = hRand;
    return VINF_SUCCESS;
}


void CidetCoreDelete(PCIDETCORE pThis)
{
    AssertPtr(pThis); Assert(pThis->u32Magic == CIDETCORE_MAGIC);

    RTRandAdvDestroy(pThis->hRand);
    RT_ZERO(*pThis);
}


/**
 * Report a test failure via CIDET::pfnFailure
 *
 * @returns false
 * @param   pThis           Pointer to the core structure.
 * @param   pszFormat       Format string containing failure details.
 * @param   va              Arguments referenced in @a pszFormat.
 */
int CidetCoreSetErrorV(PCIDETCORE pThis, const char *pszFormat, va_list va)
{
    pThis->pfnFailure(pThis, pszFormat, va);
    return false;
}


/**
 * Report a test failure via CIDET::pfnFailure
 *
 * @returns false
 * @param   pThis           Pointer to the core structure.
 * @param   pszFormat       Format string containing failure details.
 * @param   ...             Arguments referenced in @a pszFormat.
 */
bool CidetCoreSetError(PCIDETCORE pThis, const char *pszFormat, ...)
{
    va_list va;
    va_start(va, pszFormat);
    CidetCoreSetErrorV(pThis, pszFormat, va);
    va_end(va);
    return false;
}


/**
 * Get a signed random number, with a given number of significant bytes.
 *
 * @returns Random number.
 * @param   pThis           Pointer to the core structure.
 * @param   cbSignificant   The number of significant bytes.
 */
int64_t CidetCoreGetRandS64(PCIDETCORE pThis, uint8_t cbSignificant)
{
    int64_t iVal = RTRandAdvS64(pThis->hRand);
    switch (cbSignificant)
    {
        case 1: return (int8_t)iVal;
        case 2: return (int16_t)iVal;
        case 4: return (int32_t)iVal;
        case 8: return iVal;
        default:
            AssertReleaseFailed();
            return iVal;
    }
}


/**
 * Get an unsigned random number, with a given number of significant bytes.
 *
 * @returns Random number.
 * @param   pThis           Pointer to the core structure.
 * @param   cbSignificant   The number of significant bytes.
 */
uint64_t CidetCoreGetRandU64(PCIDETCORE pThis, uint8_t cbSignificant)
{
    Assert(cbSignificant == 1 || cbSignificant == 2 || cbSignificant == 4 || cbSignificant == 8);

    uint64_t uVal = RTRandAdvU64(pThis->hRand);
    uVal &= g_au64ByteSizeToMask[cbSignificant];

    return uVal;
}



void CidetCoreInitializeCtxTemplate(PCIDETCORE pThis)
{
    pThis->InTemplateCtx.rip = UINT64_MAX;
    pThis->InTemplateCtx.rfl = X86_EFL_1 | X86_EFL_ID | X86_EFL_IF;

    unsigned i = RT_ELEMENTS(pThis->InTemplateCtx.aGRegs);
    if (CIDETMODE_IS_LM(pThis->bMode))
        while (i-- > 0)
            pThis->InTemplateCtx.aGRegs[i] = UINT64_C(0x3fefcc00daba005d)
                                           | ((uint64_t)i << 32)
                                           | ((uint32_t)i << 8);
    else
        while (i-- > 0)
            pThis->InTemplateCtx.aGRegs[i] = UINT64_C(0xfada009b)
                                           | ((uint32_t)i << 12)
                                           | ((uint32_t)i << 8);
    i = RT_ELEMENTS(pThis->InTemplateCtx.aSRegs);
    while (i-- > 0)
        pThis->InTemplateCtx.aSRegs[i] = 0; /* Front end sets these afterwards. */
    pThis->InTemplateCtx.cr2  = 0;
#ifndef CIDET_REDUCED_CTX
    pThis->InTemplateCtx.tr   = 0;
    pThis->InTemplateCtx.ldtr = 0;
    pThis->InTemplateCtx.cr0  = 0;
    pThis->InTemplateCtx.cr3  = 0;
    pThis->InTemplateCtx.cr4  = 0;
    pThis->InTemplateCtx.cr8  = 0;
#endif
    pThis->InTemplateCtx.fIgnoredRFlags = 0;
    pThis->InTemplateCtx.uXcpt          = UINT32_MAX;
    pThis->InTemplateCtx.uErr           = UINT64_MAX;
    pThis->InTemplateCtx.fTrickyStack   = false;
}


/**
 * Sets the target mode.
 *
 * Caller must set up default selector values after calling this function.
 *
 * @returns VBox status code.
 * @param   pThis           Pointer to the core structure.
 * @param   bMode           The new mode.
 */
int CidetCoreSetTargetMode(PCIDETCORE pThis, uint8_t bMode)
{
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE); AssertReturn(pThis->u32Magic == CIDETCORE_MAGIC, VERR_INVALID_HANDLE);
    switch (bMode)
    {
        //case CIDETMODE_RM:
        //case CIDETMODE_PE_16:
        //case CIDETMODE_PE_32:
        //case CIDETMODE_PE_V86:
        //case CIDETMODE_PP_16:
        case CIDETMODE_PP_32:
        //case CIDETMODE_PP_V86:
        //case CIDETMODE_PAE_16:
        case CIDETMODE_PAE_32:
        //case CIDETMODE_PAE_V86:
        //case CIDETMODE_LM_S16:
        //case CIDETMODE_LM_32:
        case CIDETMODE_LM_64:
            break;
        default:
            return VERR_NOT_IMPLEMENTED;
    }
    pThis->bMode = bMode;
    CidetCoreInitializeCtxTemplate(pThis);
    return VINF_SUCCESS;
}


bool CidetCoreIsEncodingCompatibleWithInstruction(PCIDETCORE pThis)
{
    RT_NOREF_PV(pThis);
    return true;
}


/**
 * Selects the next address size mode.
 *
 * @returns @c true if done, @c false if the next wheel needs to be moved.
 * @param   pThis               The core state structure.
 */
static bool cidetCoreSetupNextBaseEncoding_AddressSize(PCIDETCORE pThis)
{
    if (pThis->fAddrSizePrf)
    {
        /*
         * Reset to default.
         */
        pThis->cbAddrMode   = CIDETMODE_GET_BYTE_COUNT(pThis->bMode);
        pThis->fAddrSizePrf = false;
    }
    else
    {
        /*
         * The other addressing size.
         */
        if (CIDETMODE_IS_64BIT(pThis->bMode))
            pThis->cbAddrMode = 4;
        else if (CIDETMODE_IS_32BIT(pThis->bMode))
            pThis->cbAddrMode = 2;
        else
        {
            AssertRelease(CIDETMODE_IS_16BIT(pThis->bMode));
            pThis->cbAddrMode = 2;
        }
        pThis->fAddrSizePrf = true;
    }
    return pThis->fAddrSizePrf;
}


/**
 * Selects the first REG encoding.
 *
 * @param   pThis               The core state structure.
 */
static void cidetCoreSetupFirstBaseEncoding_MrmReg(PCIDETCORE pThis)
{
    pThis->aOperands[pThis->idxMrmRegOp].iReg            = 0;
    pThis->aOperands[pThis->idxMrmRegOp].fIsMem          = false;
    pThis->aOperands[pThis->idxMrmRegOp].fIsRipRelative  = false;
    pThis->aOperands[pThis->idxMrmRegOp].fIsHighByteRegister = false;
    pThis->aOperands[pThis->idxMrmRegOp].cbMemDisp       = 0;
    pThis->aOperands[pThis->idxMrmRegOp].iMemBaseReg     = UINT8_MAX;
    pThis->aOperands[pThis->idxMrmRegOp].iMemIndexReg    = UINT8_MAX;
    pThis->aOperands[pThis->idxMrmRegOp].uMemScale       = 1;
    pThis->aOperands[pThis->idxMrmRegOp].iEffSeg         = UINT8_MAX;
    pThis->bModRm  &= ~X86_MODRM_REG_MASK;
    pThis->fRexR    = false;
}


/**
 * Selects the next REG (ModR/M) encoding.
 *
 * @returns @c true if done, @c false if the next wheel needs to be moved.
 * @param   pThis               The core state structure.
 * @param   iReg                The value of MODRM.REG /w REX.R applied.
 */
static bool cidetCoreSetupNextBaseEncoding_MrmReg(PCIDETCORE pThis, uint8_t iReg)
{
    Assert(pThis->idxMrmRegOp < RT_ELEMENTS(pThis->aOperands) && !pThis->aOperands[pThis->idxMrmRegOp].fIsMem);
    Assert(iReg < 16);

    /*
     * Clear the collision flags here because of the byte register kludge.
     */
    pThis->fHasRegCollisionDirect   = false;
    pThis->fHasRegCollisionMemBase  = false;
    pThis->fHasRegCollisionMemIndex = false;
    pThis->fHasRegCollisionMem      = false;

    /*
     * Clear the REX prefix and high byte register tracking too.  ASSUMES MrmReg is after MrmRmMod.
     */
    Assert(!pThis->fNoRexPrefixMrmRm);
    Assert(!pThis->fHasHighByteRegInMrmRm);
    pThis->fNoRexPrefixMrmReg = false;
    pThis->fNoRexPrefix = false;
    pThis->fHasHighByteRegInMrmReg = false;
    pThis->aOperands[pThis->idxMrmRegOp].fIsHighByteRegister = false;

    /*
     * Special kludge for ah, ch, dh, bh, spl, bpl, sil, and dil.
     * Needs extra care in 64-bit mode and special collision detection code.
     */
    CIDET_DPRINTF(("aOperands[%u].cb=%u fGpr=%u iReg=%d fRex=%d fRexW=%u fRexX=%u fRexB=%u fRexR=%d\n",
                   pThis->idxMrmRegOp, pThis->aOperands[pThis->idxMrmRegOp].cb, CIDET_OF_K_IS_GPR(pThis->fMrmRegOp), iReg,
                   pThis->fRex, pThis->fRexW, pThis->fRexX, pThis->fRexB, pThis->fRexR));
    if (   pThis->aOperands[pThis->idxMrmRegOp].cb == 1
        && CIDET_OF_K_IS_GPR(pThis->fMrmRegOp)
        && iReg >= 3
        && (   iReg <= 6
            || (CIDETMODE_IS_64BIT(pThis->bMode) && iReg == 7 && !pThis->fRex)) )

    {
        if (!pThis->fRex && iReg >= 4 && CIDETMODE_IS_64BIT(pThis->bMode) && !pThis->fNoRexPrefix)
        {
            /* The AMD64 low variants: spl, bpl, sil and dil. */
            pThis->fRex = true;
            pThis->fHasStackRegInMrmReg = iReg == X86_GREG_xSP;

            /* Check for collisions. */
            if (pThis->idxMrmRmOp < RT_ELEMENTS(pThis->aOperands))
            {
                Assert(!pThis->fHasHighByteRegInMrmRm);
                if (!pThis->aOperands[pThis->idxMrmRmOp].fIsMem)
                    pThis->fHasRegCollisionDirect = CIDET_OF_K_IS_GPR(pThis->fMrmRmOp)
                                                 && iReg == pThis->aOperands[pThis->idxMrmRmOp].iReg;
                else
                {
                    Assert(!pThis->fUsesVexIndexRegs || pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg == UINT8_MAX);

                    pThis->fHasRegCollisionMemBase  = iReg == pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg;
                    pThis->fHasRegCollisionMemIndex = iReg == pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg;
                    pThis->fHasRegCollisionMem = pThis->fHasRegCollisionMemBase || pThis->fHasRegCollisionMemIndex;
                }
            }
        }
        else
        {
            /* Next register: ah, ch, dh and bh. */
            iReg++;
            pThis->aOperands[pThis->idxMrmRegOp].iReg = iReg;
            pThis->bModRm &= ~X86_MODRM_REG_MASK;
            pThis->bModRm |= (iReg & X86_MODRM_REG_SMASK) << X86_MODRM_REG_SHIFT;
            pThis->fRex    = false;
            pThis->fRexR   = false;
            pThis->fNoRexPrefixMrmReg       = true;
            pThis->fNoRexPrefix             = true;
            pThis->fHasHighByteRegInMrmReg  = true;
            pThis->fHasStackRegInMrmReg     = false;
            pThis->aOperands[pThis->idxMrmRegOp].fIsHighByteRegister = true;
            Assert(!pThis->fRexW); Assert(!pThis->fRexX); Assert(!pThis->fRexB);

            /* Check for collisions. */
            if (pThis->idxMrmRmOp < RT_ELEMENTS(pThis->aOperands))
            {
                if (!pThis->aOperands[pThis->idxMrmRmOp].fIsMem)
                    pThis->fHasRegCollisionDirect = CIDET_OF_K_IS_GPR(pThis->fMrmRmOp)
                                                 && (   (   pThis->aOperands[pThis->idxMrmRmOp].cb == 1
                                                         && iReg == pThis->aOperands[pThis->idxMrmRmOp].iReg
                                                         && pThis->fHasHighByteRegInMrmRm)
                                                     || (   pThis->aOperands[pThis->idxMrmRmOp].cb > 1
                                                         && iReg - 4 == pThis->aOperands[pThis->idxMrmRmOp].iReg));
                else
                {
                    Assert(!pThis->fUsesVexIndexRegs || pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg == UINT8_MAX);

                    pThis->fHasRegCollisionMemBase  = iReg - 4 == pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg;
                    pThis->fHasRegCollisionMemIndex = iReg - 4 == pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg;
                    pThis->fHasRegCollisionMem = pThis->fHasRegCollisionMemBase || pThis->fHasRegCollisionMemIndex;
                }
            }
        }
        return true;
    }

    Assert(!pThis->fRex || (iReg == 7 && CIDETMODE_IS_64BIT(pThis->bMode)));
    pThis->fRex = false;

    /*
     * Next register.
     */
    iReg = (iReg + 1) & (CIDETMODE_IS_64BIT(pThis->bMode) ? 15 : 7);

    pThis->aOperands[pThis->idxMrmRegOp].iReg = iReg;
    pThis->bModRm &= ~X86_MODRM_REG_MASK;
    pThis->bModRm |= (iReg & X86_MODRM_REG_SMASK) << X86_MODRM_REG_SHIFT;
    pThis->fRexR   = iReg >= 8;
    pThis->fHasStackRegInMrmReg = iReg == X86_GREG_xSP && CIDET_OF_K_IS_GPR(pThis->fMrmRegOp);

    /*
     * Register collision detection.
     */
    if (pThis->idxMrmRmOp < RT_ELEMENTS(pThis->aOperands))
    {
        if (!pThis->aOperands[pThis->idxMrmRmOp].fIsMem)
            pThis->fHasRegCollisionDirect = iReg == pThis->aOperands[pThis->idxMrmRmOp].iReg
                                         && CIDET_OF_K_IS_SAME(pThis->fMrmRmOp, pThis->fMrmRegOp);
        else if (CIDET_OF_K_IS_GPR(pThis->fMrmRegOp))
        {
            Assert(!pThis->fUsesVexIndexRegs || pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg == UINT8_MAX);
            pThis->fHasRegCollisionMemBase  = iReg == pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg;
            pThis->fHasRegCollisionMemIndex = iReg == pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg;
            pThis->fHasRegCollisionMem = pThis->fHasRegCollisionMemBase || pThis->fHasRegCollisionMemIndex;
        }
    }
    Assert(!pThis->fSib);

    return iReg != 0;
}


/**
 * Selects the next MOD & R/M encoding, 16-bit addressing variant.
 *
 * @param   pThis               The core state structure.
 * @param   iReg                The value of MODRM.REG /w REX.R applied.
 */
static void cidetCoreSetupFirstBaseEncoding_MrmRmMod_16bit(PCIDETCORE pThis, uint8_t iReg)
{
    if (CidetInstrHasMrmRegOperand(pThis->pCurInstr))
    {
        pThis->aOperands[pThis->idxMrmRmOp].iReg            = 0;
        pThis->aOperands[pThis->idxMrmRmOp].fIsMem          = false;
        pThis->aOperands[pThis->idxMrmRmOp].fIsRipRelative  = false;
        pThis->aOperands[pThis->idxMrmRmOp].fIsHighByteRegister = false;
        pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp       = 0;
        pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg     = UINT8_MAX;
        pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg    = UINT8_MAX;
        pThis->aOperands[pThis->idxMrmRmOp].uMemScale       = 1;
        pThis->aOperands[pThis->idxMrmRmOp].iEffSeg         = UINT8_MAX;
        pThis->bModRm                     &= ~(X86_MODRM_RM_MASK | X86_MODRM_MOD_MASK);
        pThis->bModRm                     |= 3 << X86_MODRM_MOD_SHIFT;
        pThis->fRexB                       = false;
        pThis->fRexX                       = false;
        pThis->fHasMemoryOperand           = false;
        pThis->fHasRegCollisionDirect      = iReg == 0
                                          && CIDET_OF_K_IS_SAME(pThis->fMrmRmOp, pThis->fMrmRegOp);
        pThis->fHasRegCollisionMem         = false;
        pThis->fHasRegCollisionMemBase     = false;
        pThis->fHasRegCollisionMemIndex    = false;
        pThis->fHasStackRegInMrmRmBase     = false;
    }
    else
    {
        Assert(CidetInstrHasMrmMemOperand(pThis->pCurInstr));
        pThis->aOperands[pThis->idxMrmRmOp].iReg            = UINT8_MAX;
        pThis->aOperands[pThis->idxMrmRmOp].fIsMem          = true;
        pThis->aOperands[pThis->idxMrmRmOp].fIsRipRelative  = false;
        pThis->aOperands[pThis->idxMrmRmOp].fIsHighByteRegister = false;
        pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp       = 0;
        pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg     = X86_GREG_xBX;
        pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg    = X86_GREG_xSI;
        pThis->aOperands[pThis->idxMrmRmOp].uMemScale       = 1;
        pThis->aOperands[pThis->idxMrmRmOp].iEffSeg         = UINT8_MAX;
        pThis->bModRm                     &= ~(X86_MODRM_RM_MASK | X86_MODRM_MOD_MASK);
        pThis->fRexB                       = false;
        pThis->fRexX                       = false;
        pThis->fHasMemoryOperand           = true;
        pThis->fHasRegCollisionDirect      = false;
        iReg -= pThis->fHasHighByteRegInMrmReg * 4;
        pThis->fHasRegCollisionMemBase     = iReg == X86_GREG_xBX && CIDET_OF_K_IS_GPR(pThis->fMrmRegOp);
        pThis->fHasRegCollisionMemIndex    = iReg == X86_GREG_xSI && CIDET_OF_K_IS_GPR(pThis->fMrmRegOp);
        pThis->fHasRegCollisionMem         = pThis->fHasRegCollisionMemBase || pThis->fHasRegCollisionMemIndex;
        pThis->fHasStackRegInMrmRmBase     = false;
    }
}


/**
 * Selects the next MOD & R/M encoding, 16-bit addressing variant.
 *
 * @returns @c true if done, @c false if the next wheel needs to be moved.
 * @param   pThis               The core state structure.
 * @param   iReg                The value of MODRM.REG /w REX.R applied.
 */
static bool cidetCoreSetupNextBaseEncoding_MrmRmMod_16bit(PCIDETCORE pThis, uint8_t iReg)
{
    AssertRelease(!pThis->fRexB);
    AssertRelease(!pThis->fRexX);
    uint8_t iRm  = pThis->bModRm & X86_MODRM_RM_MASK;
    uint8_t iMod = (pThis->bModRm >> X86_MODRM_MOD_SHIFT) & X86_MODRM_MOD_SMASK;
    if (iMod == 3)
    {
        /*
         * Register access mode.
         */
        Assert(pThis->idxMrmRmOp < RT_ELEMENTS(pThis->aOperands) && !pThis->aOperands[pThis->idxMrmRmOp].fIsMem);
        Assert(!pThis->fHasMemoryOperand);
        Assert(!pThis->fHasRegCollisionMem);
        Assert(!pThis->fHasRegCollisionMemBase);
        Assert(!pThis->fHasRegCollisionMemIndex);
        if (iRm < 7)
        {
            iRm++;
            pThis->aOperands[pThis->idxMrmRmOp].iReg = iRm;
            pThis->bModRm &= ~X86_MODRM_RM_MASK;
            pThis->bModRm |= iRm;
            pThis->fHasRegCollisionDirect = iRm == iReg
                                        && CIDET_OF_K_IS_SAME(pThis->fMrmRmOp, pThis->fMrmRegOp);
            pThis->fHasStackRegInMrmRmBase = iRm == X86_GREG_xSP && CIDET_OF_K_IS_GPR(pThis->fMrmRmOp);
            return true;
        }

        /* If no memory modes, we're done.  */
        if (!CidetInstrHasMrmMemOperand(pThis->pCurInstr))
        {
            cidetCoreSetupFirstBaseEncoding_MrmRmMod_16bit(pThis, iReg);
            return false;
        }

        /* Next mode: 16-bit memory addressing without displacement. */
        pThis->aOperands[pThis->idxMrmRmOp].fIsMem = true;
        pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp = 0;
        iMod = 0;
    }
    else
    {
        /*
         * Memory access mode.
         */
        Assert(pThis->idxMrmRmOp < RT_ELEMENTS(pThis->aOperands) && pThis->aOperands[pThis->idxMrmRmOp].fIsMem);
        Assert(pThis->fHasMemoryOperand);
        if (iRm < 7)
        {
            iRm++;
            switch (iRm)
            {
                case 1:
                    pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg  = X86_GREG_xBX;
                    pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg = X86_GREG_xDI;
                    break;
                case 2:
                    pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg  = X86_GREG_xBP;
                    pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg = X86_GREG_xSI;
                    break;
                case 3:
                    pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg  = X86_GREG_xBP;
                    pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg = X86_GREG_xDI;
                    break;
                case 4:
                    pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg  = UINT8_MAX;
                    pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg = X86_GREG_xSI;
                    break;
                case 5:
                    pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg  = UINT8_MAX;
                    pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg = X86_GREG_xDI;
                    break;
                case 6:
                    if (iMod == 0)
                    {
                        pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp = 2;
                        pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg = UINT8_MAX;
                    }
                    else
                        pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg = X86_GREG_xBP;
                    pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg = UINT8_MAX;
                    break;
                case 7:
                    if (iMod == 0)
                        pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp = 0;
                    pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg  = X86_GREG_xBX;
                    pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg = UINT8_MAX;
                    break;
                default: AssertReleaseFailed();
            }
            pThis->bModRm &= ~X86_MODRM_RM_MASK;
            pThis->bModRm |= iRm;
            if (CIDET_OF_K_IS_GPR(pThis->fMrmRegOp))
            {
                iReg -= pThis->fHasHighByteRegInMrmReg * 4;
                pThis->fHasRegCollisionMemBase = iReg == pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg;
                pThis->fHasRegCollisionMemIndex = iReg == pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg;
                pThis->fHasRegCollisionMem = pThis->fHasRegCollisionMemBase || pThis->fHasRegCollisionMemIndex;
            }
            return true;
        }

        /* Last mode? */
        if (iMod >= 2)
        {
            cidetCoreSetupFirstBaseEncoding_MrmRmMod_16bit(pThis, iReg);
            return false;
        }

        /* Next memory addressing mode (if any). */
        iMod++;
        pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp++;
    }
    pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg  = X86_GREG_xBX;
    pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg = X86_GREG_xSI;
    pThis->aOperands[pThis->idxMrmRmOp].uMemScale    = 1;
    pThis->bModRm &= ~(X86_MODRM_RM_MASK | X86_MODRM_MOD_MASK);
    pThis->bModRm |= iMod << X86_MODRM_MOD_SHIFT;
    pThis->fHasMemoryOperand = true;
    pThis->fHasRegCollisionDirect = false;
    pThis->fHasStackRegInMrmRmBase = false;
    if (CIDET_OF_K_IS_GPR(pThis->fMrmRmOp))
    {
        iReg -= pThis->fHasHighByteRegInMrmReg * 4;
        pThis->fHasRegCollisionMemBase  = iReg == X86_GREG_xBX;
        pThis->fHasRegCollisionMemIndex = iReg == X86_GREG_xSI;
        pThis->fHasRegCollisionMem = pThis->fHasRegCollisionMemBase || pThis->fHasRegCollisionMemIndex;
    }
    return true;
}


/**
 * Selects the first MOD & R/M encoding, 32-bit and 64-bit addressing variant.
 *
 * @param   pThis               The core state structure.
 * @param   iReg                The value of MODRM.REG /w REX.R applied.
 * @param   f64Bit              Set if 64-bit, clear if 32-bit.
 */
static void cidetCoreSetupFirstBaseEncoding_MrmRmMod_32bit64bit(PCIDETCORE pThis, uint8_t iReg, bool f64Bit)
{
    RT_NOREF_PV(f64Bit);
    if (CidetInstrHasMrmRegOperand(pThis->pCurInstr))
    {
        pThis->aOperands[pThis->idxMrmRmOp].iReg            = 0;
        pThis->aOperands[pThis->idxMrmRmOp].fIsMem          = false;
        pThis->aOperands[pThis->idxMrmRmOp].fIsRipRelative  = false;
        pThis->aOperands[pThis->idxMrmRmOp].fIsHighByteRegister = false;
        pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp       = 0;
        pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg     = UINT8_MAX;
        pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg    = UINT8_MAX;
        pThis->aOperands[pThis->idxMrmRmOp].uMemScale       = 1;
        pThis->aOperands[pThis->idxMrmRmOp].iEffSeg         = UINT8_MAX;
        pThis->bModRm                     &= ~(X86_MODRM_RM_MASK | X86_MODRM_MOD_MASK);
        pThis->bModRm                     |= 3 << X86_MODRM_MOD_SHIFT;
        pThis->fRexB                       = false;
        pThis->fRexX                       = false;
        pThis->fHasMemoryOperand           = false;
        pThis->fHasRegCollisionDirect      = iReg == 0
                                          && CIDET_OF_K_IS_SAME(pThis->fMrmRmOp, pThis->fMrmRegOp);
        pThis->fHasRegCollisionMem         = false;
        pThis->fHasRegCollisionMemBase     = false;
        pThis->fHasRegCollisionMemIndex    = false;
        pThis->fHasStackRegInMrmRmBase     = false;
    }
    else
    {
        Assert(CidetInstrHasMrmMemOperand(pThis->pCurInstr));
        pThis->aOperands[pThis->idxMrmRmOp].iReg            = UINT8_MAX;
        pThis->aOperands[pThis->idxMrmRmOp].fIsMem          = true;
        pThis->aOperands[pThis->idxMrmRmOp].fIsRipRelative  = false;
        pThis->aOperands[pThis->idxMrmRmOp].fIsHighByteRegister = false;
        pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp       = 0;
        pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg     = 0;
        pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg    = UINT8_MAX;
        pThis->aOperands[pThis->idxMrmRmOp].uMemScale       = 1;
        pThis->aOperands[pThis->idxMrmRmOp].iEffSeg         = UINT8_MAX;
        pThis->bModRm                     &= ~(X86_MODRM_RM_MASK | X86_MODRM_MOD_MASK);
        pThis->fRexB                       = false;
        pThis->fRexX                       = false;
        pThis->fHasMemoryOperand           = true;
        pThis->fHasRegCollisionDirect      = false;
        pThis->fHasRegCollisionMemIndex    = false;
        pThis->fHasRegCollisionMemBase     = iReg == pThis->fHasHighByteRegInMrmReg * 4 && CIDET_OF_K_IS_GPR(pThis->fMrmRegOp);
        pThis->fHasRegCollisionMem         = pThis->fHasRegCollisionMemBase;
        pThis->fHasStackRegInMrmRmBase     = false;
    }
}


/**
 * Selects the next MOD & R/M encoding, 32-bit and 64-bit addressing variant.
 *
 * @returns @c true if done, @c false if the next wheel needs to be moved.
 * @param   pThis               The core state structure.
 * @param   iReg                The value of MODRM.REG /w REX.R applied.
 * @param   f64Bit              Set if 64-bit, clear if 32-bit.
 */
static bool cidetCoreSetupNextBaseEncoding_MrmRmMod_32bit64bit(PCIDETCORE pThis, uint8_t iReg, bool f64Bit)
{
    AssertRelease(!pThis->fRexX || CIDETMODE_IS_64BIT(pThis->bMode));
    AssertRelease(!pThis->fRexB || CIDETMODE_IS_64BIT(pThis->bMode));
    uint8_t iRm  = (pThis->bModRm & X86_MODRM_RM_MASK) + pThis->fRexB * 8;
    uint8_t iMod = (pThis->bModRm >> X86_MODRM_MOD_SHIFT) & X86_MODRM_MOD_SMASK;
    if (iMod == 3)
    {
        /*
         * Register access mode.
         */
        Assert(pThis->idxMrmRmOp < RT_ELEMENTS(pThis->aOperands) && !pThis->aOperands[pThis->idxMrmRmOp].fIsMem);
        Assert(!pThis->fHasMemoryOperand);
        Assert(!pThis->fHasRegCollisionMem);
        Assert(!pThis->fHasRegCollisionMemBase);
        Assert(!pThis->fHasRegCollisionMemIndex);

        if (CIDETMODE_IS_64BIT(pThis->bMode) && !pThis->fRexX && !pThis->fNoRexPrefix) /* should be ignored. */
        {
            pThis->fRexX = true;
            return true;
        }

        /* Reset the byte register kludges variables. */
        pThis->aOperands[pThis->idxMrmRmOp].fIsHighByteRegister = false;
        pThis->fHasHighByteRegInMrmRm = false;
        pThis->fNoRexPrefixMrmRm = false;
        pThis->fNoRexPrefix = pThis->fNoRexPrefixMrmReg;

        if (iRm < (CIDETMODE_IS_64BIT(pThis->bMode) && !pThis->fNoRexPrefix ? 15 : 7))
        {
            /*
             * Byte register kludge.
             */
            if (   pThis->aOperands[pThis->idxMrmRmOp].cb == 1
                && CIDET_OF_K_IS_GPR(pThis->fMrmRegOp)
                && iRm >= 3
                && (   iRm <= 6
                    || (iRm == 7 && CIDETMODE_IS_64BIT(pThis->bMode) && !pThis->fRexX) ) )
            {
                if (!pThis->fRexX && iRm >= 4 && CIDETMODE_IS_64BIT(pThis->bMode) && !pThis->fNoRexPrefix)
                {
                    /* The AMD64 low variants: spl, bpl, sil and dil. (Using fRexX here as REG covers fRex.) */
                    pThis->fRexX = true;
                    pThis->fHasRegCollisionDirect = CIDET_OF_K_IS_GPR(pThis->fMrmRegOp)
                                                 && iRm == iReg - pThis->fHasHighByteRegInMrmReg * 4;
                    pThis->fHasStackRegInMrmRmBase = iRm == X86_GREG_xSP && CIDET_OF_K_IS_GPR(pThis->fMrmRegOp);
                }
                else
                {
                    /* Next register: ah, ch, dh and bh. */
                    iRm++;
                    pThis->aOperands[pThis->idxMrmRmOp].iReg = iRm;
                    pThis->bModRm &= ~X86_MODRM_RM_MASK;
                    pThis->bModRm |= iRm & X86_MODRM_RM_MASK;
                    pThis->fRexB   = false;
                    pThis->fRexX   = false;
                    if (!pThis->fRexR && !pThis->fRexW && !pThis->fRex)
                    {
                        pThis->fNoRexPrefixMrmRm = true;
                        pThis->fNoRexPrefix = true;
                        pThis->fHasHighByteRegInMrmRm = true;
                        pThis->aOperands[pThis->idxMrmRmOp].fIsHighByteRegister = true;
                        pThis->fHasRegCollisionDirect = CIDET_OF_K_IS_GPR(pThis->fMrmRegOp)
                                                     && iRm - 4 == iReg - pThis->fHasHighByteRegInMrmReg * 4;
                        pThis->fHasStackRegInMrmRmBase = false;

                    }
                    else
                    {
                        /* Can't do the high stuff, so do the spl, bpl, sil and dil variation instead.
                           Note! We don't set the RexX yet since the base register or operand width holds it down. */
                        pThis->fHasRegCollisionDirect = CIDET_OF_K_IS_GPR(pThis->fMrmRegOp)
                                                     && iRm == iReg - pThis->fHasHighByteRegInMrmReg * 4;
                        pThis->fHasStackRegInMrmRmBase = iRm == X86_GREG_xSP && CIDET_OF_K_IS_GPR(pThis->fMrmRegOp);
                    }
                }
            }
            /*
             * Normal register.
             */
            else
            {
                iRm++;
                pThis->aOperands[pThis->idxMrmRmOp].iReg = iRm;
                pThis->bModRm &= ~X86_MODRM_RM_MASK;
                pThis->bModRm |= iRm & X86_MODRM_RM_MASK;
                pThis->fRexB   = iRm >= 8;
                pThis->fRexX   = false;
                pThis->fHasRegCollisionDirect = iRm == iReg && CIDET_OF_K_IS_SAME(pThis->fMrmRmOp, pThis->fMrmRegOp);
                pThis->fHasStackRegInMrmRmBase = iRm == X86_GREG_xSP && CIDET_OF_K_IS_GPR(pThis->fMrmRegOp);
            }
            return true;
        }

        /* If no memory modes, we're done.  */
        if (!CidetInstrHasMrmMemOperand(pThis->pCurInstr))
        {
            cidetCoreSetupFirstBaseEncoding_MrmRmMod_32bit64bit(pThis, iReg, f64Bit);
            return false;
        }

        /* Next mode: 32-bit/64-bit memory addressing without displacement. */
        pThis->aOperands[pThis->idxMrmRmOp].fIsMem = true;
        pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp = 0;
        iMod = 0;
    }
    else
    {
        /*
         * Memory access mode.
         */
        Assert(pThis->idxMrmRmOp < RT_ELEMENTS(pThis->aOperands) && pThis->aOperands[pThis->idxMrmRmOp].fIsMem);
        Assert(pThis->fHasMemoryOperand);
        Assert(!pThis->fHasStackRegInMrmRmBase);
        if (iRm < (CIDETMODE_IS_64BIT(pThis->bMode) && !pThis->fNoRexPrefix ? 15 : 7))
        {
            iRm++;
            if (iRm == 12)
                iRm++; /* Leave REX.B=1 to the next-sib-base function. */
            if (iRm == 4)
            {
                /* SIB */
                pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg     = 0;
                pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg    = 0;
                pThis->fSib = true;
                pThis->bSib = 0;
            }
            else if ((iRm & 7) == 5 && iMod == 0)
            {
                /* Absolute or wrt rip addressing. */
                pThis->aOperands[pThis->idxMrmRmOp].fIsRipRelative  = CIDETMODE_IS_64BIT(pThis->bMode);
                pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg     = UINT8_MAX;
                pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg    = UINT8_MAX;
                pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp       = 4;
            }
            else
            {
                if ((iRm & 7) == 6 && iMod == 0)
                {
                    pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp   = 0;
                    pThis->aOperands[pThis->idxMrmRmOp].fIsRipRelative = false;
                }
                pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg     = iRm;
                pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg    = UINT8_MAX;
            }
            pThis->aOperands[pThis->idxMrmRmOp].uMemScale = 1;
            pThis->bModRm &= ~X86_MODRM_RM_MASK;
            pThis->bModRm |= iRm & X86_MODRM_RM_MASK;
            pThis->fRexB   = iRm >= 8;
            pThis->fRexX   = false;
            if (CIDET_OF_K_IS_GPR(pThis->fMrmRegOp))
            {
                iReg -= pThis->fHasHighByteRegInMrmReg * 4;
                pThis->fHasRegCollisionMemBase = iReg == pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg;
                pThis->fHasRegCollisionMemIndex = iReg == pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg;
                pThis->fHasRegCollisionMem = pThis->fHasRegCollisionMemBase || pThis->fHasRegCollisionMemIndex;
            }
            return true;
        }

        /* Last mode? */
        if (iMod >= 2)
        {
            cidetCoreSetupFirstBaseEncoding_MrmRmMod_32bit64bit(pThis, iReg, f64Bit);
            return false;
        }

        /* Next memory addressing mode (if any). */
        iMod++;
        pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp = iMod == 1 ? 1 : 4;
    }
    pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg  = 0;
    pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg = UINT8_MAX;
    pThis->aOperands[pThis->idxMrmRmOp].uMemScale    = 1;
    pThis->bModRm &= ~(X86_MODRM_RM_MASK | X86_MODRM_MOD_MASK);
    pThis->bModRm |= iMod << X86_MODRM_MOD_SHIFT;
    pThis->fRexB   = false;
    pThis->fRexX   = false;
    pThis->fHasMemoryOperand = true;
    pThis->fHasRegCollisionDirect = false;
    pThis->fHasRegCollisionMemIndex = false;
    pThis->fHasRegCollisionMemBase = iReg == pThis->fHasHighByteRegInMrmReg * 4
                                  && CIDET_OF_K_IS_GPR(pThis->fMrmRmOp);
    pThis->fHasRegCollisionMem = pThis->fHasRegCollisionMemBase;
    pThis->fHasStackRegInMrmRmBase = false;
    return true;
}


/**
 * Selects the next MOD & R/M encoding.
 *
 * @returns @c true if done, @c false if the next wheel needs to be moved.
 * @param   pThis               The core state structure.
 * @param   iReg                The value of MODRM.REG /w REX.R applied.
 */
static bool cidetCoreSetupNextBaseEncoding_MrmRmMod(PCIDETCORE pThis, uint8_t iReg)
{
    if (pThis->cbAddrMode == 2)
        return cidetCoreSetupNextBaseEncoding_MrmRmMod_16bit(pThis, iReg);
    if (pThis->cbAddrMode == 4)
        return cidetCoreSetupNextBaseEncoding_MrmRmMod_32bit64bit(pThis, iReg, false);
    if (pThis->cbAddrMode == 8)
        return cidetCoreSetupNextBaseEncoding_MrmRmMod_32bit64bit(pThis, iReg, true);
    AssertReleaseFailedReturn(false);
}



/**
 * Selects the next SIB base register (/ encoding).
 *
 * @returns @c true if done, @c false if the next wheel needs to be moved.
 * @param   pThis               The core state structure.
 * @param   iReg                The value of MODRM.REG /w REX.R applied.
 */
static bool cidetCoreSetupNextBaseEncoding_SibBase(PCIDETCORE pThis, uint8_t iReg)
{
    AssertRelease(!pThis->fRexB || CIDETMODE_IS_64BIT(pThis->bMode));

    uint8_t iBase = (pThis->bSib & X86_SIB_BASE_MASK) + pThis->fRexB * 8;
    iBase = (iBase + 1) & (CIDETMODE_IS_64BIT(pThis->bMode) && !pThis->fNoRexPrefix ? 15 : 7);

    if ((iBase & 7) == 5 && (pThis->bModRm & X86_MODRM_MOD_MASK) == 0)
    {
        pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp   = 4;
        pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg = UINT8_MAX;
    }
    else
    {
        if ((iBase & 7) == 6 && (pThis->bModRm & X86_MODRM_MOD_MASK) == 0)
            pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp = 0;
        pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg = iBase;
    }
    pThis->bSib  &= ~X86_SIB_BASE_MASK;
    pThis->bSib  |= iBase & X86_SIB_BASE_MASK;
    pThis->fRexB  = iBase >= 8;
    pThis->fHasRegCollisionMemBase =    pThis->aOperands[pThis->idxMrmRmOp].iMemBaseReg
                                     == iReg - pThis->fHasHighByteRegInMrmReg * 4
                                  && CIDET_OF_K_IS_GPR(pThis->fMrmRegOp);
    pThis->fHasRegCollisionMem = pThis->fHasRegCollisionMemBase || pThis->fHasRegCollisionMemIndex;
    pThis->fHasStackRegInMrmRmBase = iBase == X86_GREG_xSP;

    return iBase != 0;
}


/**
 * Selects the next SIB index register (/ encoding).
 *
 * @returns @c true if done, @c false if the next wheel needs to be moved.
 * @param   pThis               The core state structure.
 * @param   iReg                The value of MODRM.REG /w REX.R applied.
 */
static bool cidetCoreSetupNextBaseEncoding_SibIndex(PCIDETCORE pThis, uint8_t iReg)
{
    AssertRelease(!pThis->fRexX || CIDETMODE_IS_64BIT(pThis->bMode));
    Assert(pThis->idxMrmRmOp < RT_ELEMENTS(pThis->aOperands) && pThis->aOperands[pThis->idxMrmRmOp].fIsMem);

    uint8_t iIndex = ((pThis->bSib >> X86_SIB_INDEX_SHIFT) & X86_SIB_INDEX_SMASK) + pThis->fRexX * 8;
    iIndex = (iIndex + 1) & (CIDETMODE_IS_64BIT(pThis->bMode) && !pThis->fNoRexPrefix ? 15 : 7);

    if (iIndex == 4 && !pThis->fUsesVexIndexRegs)
        pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg = UINT8_MAX;
    else
        pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg = iIndex;
    pThis->bSib &= ~X86_SIB_INDEX_MASK;
    pThis->bSib |= (iIndex & X86_SIB_INDEX_SMASK) << X86_SIB_INDEX_SHIFT;
    pThis->fRexX = iIndex >= 8;
    pThis->fHasRegCollisionMemIndex =     pThis->aOperands[pThis->idxMrmRmOp].iMemIndexReg
                                       == iReg - pThis->fHasHighByteRegInMrmReg * 4
                                   && (  !pThis->fUsesVexIndexRegs
                                       ? CIDET_OF_K_IS_GPR(pThis->fMrmRegOp) : CIDET_OF_K_IS_VRX(pThis->fMrmRegOp) );
    pThis->fHasRegCollisionMem = pThis->fHasRegCollisionMemBase || pThis->fHasRegCollisionMemIndex;

    return iIndex != 0;
}


/**
 * Selects the next SIB scale.
 *
 * @returns @c true if done, @c false if the next wheel needs to be moved.
 * @param   pThis               The core state structure.
 * @param   iReg                The value of MODRM.REG /w REX.R applied.
 */
static bool cidetCoreSetupNextBaseEncoding_SibScale(PCIDETCORE pThis, uint8_t iReg)
{
    RT_NOREF_PV(iReg);
    switch ((pThis->bSib >> X86_SIB_SCALE_SHIFT) & X86_SIB_SCALE_SMASK)
    {
        case 0:
            pThis->bSib |= 1 << X86_SIB_SCALE_SHIFT;
            pThis->aOperands[pThis->idxMrmRmOp].uMemScale = 2;
            return true;
        case 1:
            pThis->bSib &= ~X86_SIB_SCALE_MASK;
            pThis->bSib |= 2 << X86_SIB_SCALE_SHIFT;
            pThis->aOperands[pThis->idxMrmRmOp].uMemScale = 4;
            return true;
        case 2:
            pThis->bSib |= 3 << X86_SIB_SCALE_SHIFT;
            pThis->aOperands[pThis->idxMrmRmOp].uMemScale = 8;
            return true;
        case 3:
            pThis->bSib &= ~X86_SIB_SCALE_MASK;
            pThis->aOperands[pThis->idxMrmRmOp].uMemScale = 1;
            return false;

        default: AssertReleaseFailedReturn(false);
    }
}


/**
 * Selects the next segment prefix.
 *
 * @returns @c true if done, @c false if the next wheel needs to be moved.
 * @param   pThis               The core state structure.
 */
static bool cidetCoreSetupNextBaseEncoding_SegmentPrefix(PCIDETCORE pThis)
{
    if (   pThis->fHasMemoryOperand
        && (pThis->fTestCfg & CIDET_TESTCFG_SEG_PRF_MASK))
    {
        switch (pThis->uSegPrf)
        {
            case X86_SREG_COUNT:
                pThis->uSegPrf = X86_SREG_ES;
                if (pThis->fTestCfg & CIDET_TESTCFG_SEG_PRF_ES)
                    return true;
                RT_FALL_THRU();
            case X86_SREG_ES:
                pThis->uSegPrf = X86_SREG_CS;
                if (pThis->fTestCfg & CIDET_TESTCFG_SEG_PRF_CS)
                    return true;
                RT_FALL_THRU();
            case X86_SREG_CS:
                pThis->uSegPrf = X86_SREG_SS;
                if (pThis->fTestCfg & CIDET_TESTCFG_SEG_PRF_SS)
                    return true;
                RT_FALL_THRU();
            case X86_SREG_SS:
                pThis->uSegPrf = X86_SREG_DS;
                if (pThis->fTestCfg & CIDET_TESTCFG_SEG_PRF_DS)
                    return true;
                RT_FALL_THRU();
            case X86_SREG_DS:
                pThis->uSegPrf = X86_SREG_FS;
                if (pThis->fTestCfg & CIDET_TESTCFG_SEG_PRF_FS)
                    return true;
                RT_FALL_THRU();
            case X86_SREG_FS:
                pThis->uSegPrf = X86_SREG_GS;
                if (pThis->fTestCfg & CIDET_TESTCFG_SEG_PRF_GS)
                    return true;
                RT_FALL_THRU();
            case X86_SREG_GS:
                break;
            default: AssertReleaseFailedBreak();
        }
        pThis->uSegPrf = X86_SREG_COUNT;
    }
    return false;
}


/**
 * Updates the variable sized operands.
 *
 * @param   pThis               The core state structure.
 */
static void cidetCoreUpdateOperandSizes(PCIDETCORE pThis)
{
    uint8_t iOp = pThis->cOperands;
    while (iOp-- > 0)
        pThis->aOperands[iOp].cb = (uint8_t)CidetCoreGetOperandSize(pThis, iOp);
}


/**
 * Selects the next operand size.
 *
 * @returns @c true if done, @c false if the next wheel needs to be moved.
 * @param   pThis               The core state structure.
 */
static bool cidetCoreSetupNextBaseEncoding_OperandSize(PCIDETCORE pThis)
{
    if (CidetInstrRespondsToOperandSizePrefixes(pThis->pCurInstr))
    {
        if (CIDETMODE_IS_64BIT(pThis->bMode))
        {
            switch (pThis->fOpSizePrf + pThis->fRexW * 2)
            {
                case 0:
                    pThis->fOpSizePrf = true;
                    cidetCoreUpdateOperandSizes(pThis);
                    return true;
                case 1:
                    pThis->fOpSizePrf = false;
                    if (pThis->fNoRexPrefix)
                        break;
                    pThis->fRexW = true;
                    cidetCoreUpdateOperandSizes(pThis);
                    return true;
                case 2:
                    pThis->fOpSizePrf = true; /* check that it's ignored. */
                    cidetCoreUpdateOperandSizes(pThis);
                    return true;
                default: AssertReleaseFailed();
                case 3:
                    break;
            }
        }
        else
        {
            if (!pThis->fOpSizePrf)
            {
                pThis->fOpSizePrf = true;
                cidetCoreUpdateOperandSizes(pThis);
                return true;
            }
        }
        pThis->fRexW = false;
        pThis->fOpSizePrf = false;
        cidetCoreUpdateOperandSizes(pThis);
    }
    return false;
}


bool CidetCoreSetupNextBaseEncoding(PCIDETCORE pThis)
{
    if (pThis->fUsesModRm)
    {
        /*
         * The wheels are lined up as follows:
         *      1. Address size prefix.
         *      2. MODRM.MOD
         *      3. MODRM.REG + REX.R
         *      4. MODRM.R/M + REX.B
         *      5. SIB - MODRM.R/M == 4 && MODRM.MOD != 3:
         *          5a) SIB.BASE + REX.B
         *          5b) SIB.INDEX + REX.X
         *          5c) SIB.SCALE
         *      6. Segment prefix overrides if applicable and supported (memory).
         *      7. Operand size prefix and REX.W if applicable.
         */
        if (cidetCoreSetupNextBaseEncoding_OperandSize(pThis))
            return true;
        if (cidetCoreSetupNextBaseEncoding_SegmentPrefix(pThis))
            return true;

        /* The ModR/M register value for collision detection. */
        uint8_t iReg = ((pThis->bModRm >> X86_MODRM_REG_SHIFT) & X86_MODRM_REG_SMASK) + pThis->fRexR * 8;

        if (pThis->fSib)
        {
            AssertRelease(pThis->fHasMemoryOperand);
            if (cidetCoreSetupNextBaseEncoding_SibScale(pThis, iReg))
                return true;
            if (cidetCoreSetupNextBaseEncoding_SibIndex(pThis, iReg))
                return true;
            if (cidetCoreSetupNextBaseEncoding_SibBase(pThis, iReg))
                return true;
            Assert(pThis->bSib == 0);
            pThis->fSib = false;
        }

        if (cidetCoreSetupNextBaseEncoding_MrmRmMod(pThis, iReg))
            return true;
        if (cidetCoreSetupNextBaseEncoding_MrmReg(pThis, iReg))
            return true;
        if (cidetCoreSetupNextBaseEncoding_AddressSize(pThis))
            return true;
    }
    else
        AssertFailedReturn(false);
    return false;
}


bool CidetCoreSetupFirstBaseEncoding(PCIDETCORE pThis)
{
    /*
     * Reset all the knobs and wheels.
     */
    pThis->fSib         = false;
    pThis->uSegPrf      = X86_SREG_COUNT;
    pThis->fAddrSizePrf = false;
    pThis->fOpSizePrf   = false;
    pThis->fRexW        = false;
    pThis->fRexR        = false;
    pThis->fRexX        = false;
    pThis->fRexB        = false;
    pThis->fRex         = false;
    pThis->bModRm       = 0;
    pThis->bSib         = 0;

    /* Indicators. */
    pThis->cbAddrMode               = CIDETMODE_GET_BYTE_COUNT(pThis->bMode);
    pThis->fHasMemoryOperand        = false;
    pThis->fHasRegCollisionMem      = false;
    pThis->fHasRegCollisionMemBase  = false;
    pThis->fHasRegCollisionMemIndex = false;
    pThis->fHasStackRegInMrmRmBase  = false;

    /*
     * Now, drill down on the instruction encoding.
     */
    if (pThis->pCurInstr->fFlags & CIDET_IF_MODRM)
    {
        Assert(pThis->fUsesModRm == true);
        cidetCoreSetupFirstBaseEncoding_MrmReg(pThis);
        if (pThis->cbAddrMode == 2)
            cidetCoreSetupFirstBaseEncoding_MrmRmMod_16bit(pThis, 0);
        else if (pThis->cbAddrMode == 4)
            cidetCoreSetupFirstBaseEncoding_MrmRmMod_32bit64bit(pThis, 0, false);
        else if (pThis->cbAddrMode == 8)
            cidetCoreSetupFirstBaseEncoding_MrmRmMod_32bit64bit(pThis, 0, true);
        else
            AssertReleaseFailedReturn(false);
    }
    else
        AssertFailedReturn(false);
    return true;
}


/**
 * The next memory operand configuration.
 *
 * @returns true if new one to test, false if we've reached end already.
 * @param   pThis               The core state structure.
 */
bool CidetCoreSetupNextMemoryOperandConfig(PCIDETCORE pThis)
{
    RT_NOREF_PV(pThis);
    return false;
}


/**
 * Sets up the first memory operand configuration and counts memory operands.
 *
 * @returns true on success, false if no data buffers configured or failure.
 * @param   pThis               The core state structure.
 */
bool CidetCoreSetupFirstMemoryOperandConfig(PCIDETCORE pThis)
{
    pThis->cMemoryOperands = 0;
    PCIDETBUF pDataBuf = &pThis->DataBuf;
    uint8_t idxOp = pThis->cOperands;
    while (idxOp-- > 0)
        if (!pThis->aOperands[idxOp].fIsMem)
            pThis->aOperands[idxOp].pDataBuf = NULL;
        else
        {
            if (RT_UNLIKELY(!pThis->cDataBufConfigs))
                return false;

            pDataBuf->idxCfg       = 0;
            pDataBuf->pCfg         = &pThis->paDataBufConfigs[0];
            pDataBuf->off          = 0;
            pDataBuf->cb           = pThis->aOperands[idxOp].cb;
            pDataBuf->cbSegLimit   = UINT16_MAX;
            pDataBuf->offSegBase   = 0;
            pDataBuf->fActive      = false;
            pDataBuf->idxOp        = idxOp;
            pDataBuf->fXcptAfterInstruction = false;
            pDataBuf->enmExpectXcpt = kCidetExpectXcpt_None;
            pThis->aOperands[idxOp].pDataBuf = pDataBuf;
            pThis->cMemoryOperands++;
            pDataBuf++;
        }

    /** @todo implement more than one memory operand. */
    AssertReleaseReturn(pThis->cMemoryOperands <= 1, false);
    return true;
}


/**
 * The next code buffer configuration.
 *
 * @returns true if new one to test, false if we've reached end already.
 * @param   pThis               The core state structure.
 */
bool CidetCoreSetupNextCodeBufferConfig(PCIDETCORE pThis)
{
    RT_NOREF_PV(pThis);
    return false;
}


/**
 * Sets up the first code buffer configuration.
 *
 * @returns true on success, false if no data buffers configured or failure.
 * @param   pThis               The core state structure.
 */
bool CidetCoreSetupFirstCodeBufferConfig(PCIDETCORE pThis)
{
    Assert(pThis->cCodeBufConfigs > 0);
    Assert(CIDETBUF_IS_CODE(pThis->paCodeBufConfigs[0].fFlags));
    pThis->CodeBuf.idxCfg       = 0;
    pThis->CodeBuf.pCfg         = &pThis->paCodeBufConfigs[0];
    pThis->CodeBuf.off          = 0;
    pThis->CodeBuf.cb           = 0x1000;
    pThis->CodeBuf.cbSegLimit   = UINT16_MAX;
    pThis->CodeBuf.offSegBase   = 0;
    pThis->CodeBuf.fActive      = true;
    pThis->CodeBuf.idxOp        = 7;
    pThis->CodeBuf.fXcptAfterInstruction = false;
    pThis->CodeBuf.enmExpectXcpt = kCidetExpectXcpt_None;
    return true;
}


/**
 * Gets the (encoded) size of the given operand in the current context.
 *
 * @returns Size in bytes.
 * @param   pThis               The core state structure (for context).
 * @param   iOp                 The operand index.
 */
uint32_t CidetCoreGetOperandSize(PCIDETCORE pThis, uint8_t iOp)
{
    Assert(iOp < RT_ELEMENTS(pThis->aOperands));
    uint32_t cbOp = g_acbCidetOfSizes[(pThis->aOperands[iOp].fFlags & CIDET_OF_Z_MASK) >> CIDET_OF_Z_SHIFT];
    if (cbOp == UINT16_MAX)
    {
        Assert((pThis->aOperands[iOp].fFlags & CIDET_OF_Z_MASK) == CIDET_OF_Z_VAR_WDQ);
        if (CIDETMODE_IS_64BIT(pThis->bMode))
        {
            if (pThis->fRexW)
                cbOp = 8;
            else if (!pThis->fOpSizePrf)
                cbOp = 4;
            else
                cbOp = 2;
        }
        else if (CIDETMODE_IS_32BIT(pThis->bMode))
            cbOp = !pThis->fOpSizePrf ? 4 : 2;
        else
        {
            Assert(CIDETMODE_IS_16BIT(pThis->bMode));
            cbOp = !pThis->fOpSizePrf ? 2 : 4;
        }
        return cbOp;
    }

    if (cbOp == UINT16_MAX - 1)
    {
        Assert((pThis->aOperands[iOp].fFlags & CIDET_OF_Z_MASK) == CIDET_OF_Z_SPECIAL);
        AssertReleaseFailedReturn(0);
    }

    if (cbOp)
    {
#ifdef VBOX_STRICT
        switch (cbOp)
        {
            case  1: Assert((pThis->aOperands[iOp].fFlags & CIDET_OF_Z_MASK) == CIDET_OF_Z_BYTE); break;
            case  2: Assert((pThis->aOperands[iOp].fFlags & CIDET_OF_Z_MASK) == CIDET_OF_Z_WORD); break;
            case  4: Assert((pThis->aOperands[iOp].fFlags & CIDET_OF_Z_MASK) == CIDET_OF_Z_DWORD); break;
            case  8: Assert((pThis->aOperands[iOp].fFlags & CIDET_OF_Z_MASK) == CIDET_OF_Z_QWORD); break;
            case 10: Assert((pThis->aOperands[iOp].fFlags & CIDET_OF_Z_MASK) == CIDET_OF_Z_TBYTE); break;
            case 16: Assert((pThis->aOperands[iOp].fFlags & CIDET_OF_Z_MASK) == CIDET_OF_Z_OWORD); break;
            case 32: Assert((pThis->aOperands[iOp].fFlags & CIDET_OF_Z_MASK) == CIDET_OF_Z_YWORD); break;
            case 64: Assert((pThis->aOperands[iOp].fFlags & CIDET_OF_Z_MASK) == CIDET_OF_Z_ZWORD); break;
            default: AssertFailed();
        }
#endif
        return cbOp;
    }
    AssertReleaseFailedReturn(0);
}


bool CideCoreSetInstruction(PCIDETCORE pThis, PCCIDETINSTR pInstr)
{
    AssertReleaseMsgReturn(RT_VALID_PTR(pInstr), ("%p\n", pInstr), false);

    pThis->pCurInstr = pInstr;

    /*
     * Extract info from the instruction descriptor.
     */
    pThis->fUsesModRm           = false;
    pThis->fUsesVexIndexRegs    = false;
    pThis->idxMrmRegOp          = 7;
    pThis->idxMrmRmOp           = 7;
    pThis->fMrmRegOp            = 0;
    pThis->fMrmRmOp             = 0;
    pThis->fInstrFlags          = pInstr->fFlags;
    pThis->cOperands            = pInstr->cOperands;
    if (pInstr->fFlags & CIDET_IF_MODRM)
    {
        pThis->fUsesModRm = true;
        for (uint8_t iOp = 0; iOp < pInstr->cOperands; iOp++)
            if (pInstr->afOperands[iOp] & CIDET_OF_M_REG)
            {
                pThis->idxMrmRegOp = iOp;
                pThis->fMrmRegOp   = pInstr->afOperands[iOp];
            }
            else if (pInstr->afOperands[iOp] & CIDET_OF_M_RM)
            {
                pThis->idxMrmRmOp = iOp;
                pThis->fMrmRmOp   = pInstr->afOperands[iOp];
            }
    }
    else
        AssertFailedReturn(false);

    uint8_t iOp;
    for (iOp = 0; iOp < pInstr->cOperands; iOp++)
    {
        pThis->aOperands[iOp].fFlags            = pInstr->afOperands[iOp];
        pThis->aOperands[iOp].iReg              = UINT8_MAX;
        pThis->aOperands[iOp].cb                = (uint8_t)CidetCoreGetOperandSize(pThis, iOp);
        pThis->aOperands[iOp].fIsImmediate      = (pInstr->afOperands[iOp] & CIDET_OF_K_MASK) == CIDET_OF_K_IMM;
        pThis->aOperands[iOp].fIsMem            = (pInstr->afOperands[iOp] & CIDET_OF_K_MASK) == CIDET_OF_K_MEM;
        pThis->aOperands[iOp].fIsRipRelative    = false;
        pThis->aOperands[iOp].cbMemDisp         = 0;
        pThis->aOperands[iOp].iMemBaseReg       = UINT8_MAX;
        pThis->aOperands[iOp].iMemIndexReg      = UINT8_MAX;
        pThis->aOperands[iOp].uMemScale         = 1;
        pThis->aOperands[iOp].iEffSeg           = UINT8_MAX;
        pThis->aOperands[iOp].offSeg            = UINT64_MAX;
        pThis->aOperands[iOp].uEffAddr          = UINT64_MAX;
        pThis->aOperands[iOp].uImmDispValue     = UINT64_MAX;
        pThis->aOperands[iOp].uMemBaseRegValue  = UINT64_MAX;
        pThis->aOperands[iOp].uMemIndexRegValue = UINT64_MAX;
        pThis->aOperands[iOp].In.pv             = NULL;
        pThis->aOperands[iOp].Expected.pv       = NULL;
        pThis->aOperands[iOp].pDataBuf          = NULL;
    }

    for (; iOp < RT_ELEMENTS(pThis->aOperands); iOp++)
    {
        pThis->aOperands[iOp].fFlags            = 0;
        pThis->aOperands[iOp].iReg              = UINT8_MAX;
        pThis->aOperands[iOp].cb                = 0;
        pThis->aOperands[iOp].fIsImmediate      = false;
        pThis->aOperands[iOp].fIsMem            = false;
        pThis->aOperands[iOp].fIsRipRelative    = false;
        pThis->aOperands[iOp].cbMemDisp         = 0;
        pThis->aOperands[iOp].iMemBaseReg       = UINT8_MAX;
        pThis->aOperands[iOp].iMemIndexReg      = UINT8_MAX;
        pThis->aOperands[iOp].uMemScale         = 1;
        pThis->aOperands[iOp].iEffSeg           = UINT8_MAX;
        pThis->aOperands[iOp].offSeg            = UINT64_MAX;
        pThis->aOperands[iOp].uEffAddr          = UINT64_MAX;
        pThis->aOperands[iOp].uImmDispValue     = UINT64_MAX;
        pThis->aOperands[iOp].uMemBaseRegValue  = UINT64_MAX;
        pThis->aOperands[iOp].uMemIndexRegValue = UINT64_MAX;
        pThis->aOperands[iOp].In.pv             = NULL;
        pThis->aOperands[iOp].Expected.pv       = NULL;
        pThis->aOperands[iOp].pDataBuf          = NULL;
    }

    /*
     * Reset various things.
     */
    for (uint32_t i = 0; i < RT_ELEMENTS(pThis->aiInOut); i++)
        pThis->aiInOut[i] = 0;

    return true;
}


bool CidetCoreSetupInOut(PCIDETCORE pThis)
{
    /*
     * Enumerate the operands.
     */
    uint8_t *pbBuf = &pThis->abBuf[0];
    pbBuf = RT_ALIGN_PT(pbBuf, 16, uint8_t *);

    uint8_t idxOp = pThis->cOperands;
    while (idxOp-- > 0)
    {
        if (pThis->aOperands[idxOp].fIsMem)
        {
            /*
             * Memory operand.
             */
            Assert(pThis->aOperands[idxOp].fIsMem);

            /* Set the In & Expected members to point to temporary buffer space. */
            pThis->aOperands[idxOp].Expected.pu8 = pbBuf;
            pbBuf += pThis->aOperands[idxOp].cb;
            pbBuf = RT_ALIGN_PT(pbBuf, 16, uint8_t *);

            pThis->aOperands[idxOp].In.pu8 = pbBuf;
            pbBuf += pThis->aOperands[idxOp].cb;
            pbBuf = RT_ALIGN_PT(pbBuf, 16, uint8_t *);

            /* Initialize the buffer we're gonna use. */
            pThis->aOperands[idxOp].iEffSeg = pThis->uSegPrf != X86_SREG_COUNT
                                            ? pThis->uSegPrf
                                            : !(pThis->aOperands[idxOp].fFlags & CIDET_OF_ALWAYS_SEG_ES) ? X86_SREG_DS
                                            : X86_SREG_ES;

            PCIDETBUF pDataBuf = pThis->aOperands[idxOp].pDataBuf;
            AssertReleaseReturn(pDataBuf, false);
            Assert(pDataBuf->cb == pThis->aOperands[idxOp].cb);
            Assert(pDataBuf->idxOp == idxOp);
            if (!pThis->pfnReInitDataBuf(pThis, pDataBuf))
            {
                pThis->cSkippedReInitDataBuf++;
                return false;
            }
            pDataBuf->fActive = true;

            /* Calc buffer related operand members. */
            pThis->aOperands[idxOp].uEffAddr = pDataBuf->uEffBufAddr + pDataBuf->off;
            uint64_t offSeg = pThis->aOperands[idxOp].uEffAddr - pDataBuf->uSegBase;
            pThis->aOperands[idxOp].offSeg = offSeg;
            AssertRelease(offSeg <= g_au64ByteSizeToMask[pThis->cbAddrMode]);

            /*
             * Select register and displacement values for the buffer addressing (works on offSeg).
             */
            uint8_t const iMemIndexReg = pThis->aOperands[idxOp].iMemIndexReg;
            uint8_t const iMemBaseReg  = pThis->aOperands[idxOp].iMemBaseReg;
            if (pThis->aOperands[idxOp].fIsRipRelative)
            {
                /* rip relative. */
                pThis->aOperands[idxOp].uImmDispValue = offSeg - (pThis->InCtx.rip + pThis->cbInstr);
                Assert(pThis->aOperands[idxOp].cbMemDisp == 4);
                if (   (int64_t)pThis->aOperands[idxOp].uImmDispValue > INT32_MAX
                    || (int64_t)pThis->aOperands[idxOp].uImmDispValue < INT32_MIN)
                {
                    pThis->cSkippedDataBufWrtRip++;
                    return false;
                }
            }
            else if (iMemBaseReg != UINT8_MAX)
            {
                if (   iMemBaseReg != iMemIndexReg
                    || pThis->fUsesVexIndexRegs)
                {
                    /* [base] or [base + disp] or [base + index * scale] or [base + index * scale + disp] */
                    if (pThis->aOperands[idxOp].cbMemDisp > 0)
                    {
                        pThis->aOperands[idxOp].uImmDispValue = CidetCoreGetRandS64(pThis, pThis->aOperands[idxOp].cbMemDisp);
                        offSeg -= (int64_t)pThis->aOperands[idxOp].uImmDispValue;
                    }

                    if (iMemIndexReg != UINT8_MAX)
                    {
                        pThis->aOperands[idxOp].uMemIndexRegValue = CidetCoreGetRandU64(pThis, pThis->cbAddrMode);
                        offSeg -= pThis->aOperands[idxOp].uMemIndexRegValue * pThis->aOperands[idxOp].uMemScale;
                    }

                    pThis->aOperands[idxOp].uMemBaseRegValue = offSeg & g_au64ByteSizeToMask[pThis->cbAddrMode];
                }
                else
                {
                    /* base == index;  [base + index * scale] or [base * (scale + 1)]. */
                    uint8_t const uEffScale = pThis->aOperands[idxOp].uMemScale + 1;
                    if (pThis->aOperands[idxOp].cbMemDisp > 0)
                    {
                        pThis->aOperands[idxOp].uImmDispValue = CidetCoreGetRandS64(pThis, pThis->aOperands[idxOp].cbMemDisp);
                        offSeg -= (int64_t)pThis->aOperands[idxOp].uImmDispValue;
                        offSeg &= g_au64ByteSizeToMask[pThis->cbAddrMode];
                        uint8_t uRemainder = offSeg % uEffScale;
                        if (uRemainder != 0)
                        {
                            Assert(pThis->aOperands[idxOp].cbMemDisp < 8);
                            Assert(   (int64_t)pThis->aOperands[idxOp].uImmDispValue
                                   <= g_ai64ByteSizeToMax[pThis->aOperands[idxOp].cbMemDisp]);
                            pThis->aOperands[idxOp].uImmDispValue = (int64_t)pThis->aOperands[idxOp].uImmDispValue
                                                                  + uRemainder;
                            offSeg -= uRemainder;
                            if (  (int64_t)pThis->aOperands[idxOp].uImmDispValue
                                > g_ai64ByteSizeToMax[pThis->aOperands[idxOp].cbMemDisp])
                            {
                                pThis->aOperands[idxOp].uImmDispValue -= uEffScale;
                                offSeg += uEffScale;
                            }
                            Assert(offSeg % uEffScale == 0);
                        }
                    }
                    else
                    {
                        offSeg &= g_au64ByteSizeToMask[pThis->cbAddrMode];
                        if (offSeg % uEffScale != 0)
                        {
                            pThis->cSkippedSameBaseIndexRemainder++;
                            return false;
                        }
                    }
                    offSeg /= uEffScale;
                    pThis->aOperands[idxOp].uMemBaseRegValue = pThis->aOperands[idxOp].uMemIndexRegValue = offSeg;
                }
            }
            else if (iMemIndexReg != UINT8_MAX)
            {
                /* [index * scale] or  [index * scale + disp] */
                if (pThis->aOperands[idxOp].cbMemDisp > 0)
                {
                    pThis->aOperands[idxOp].uImmDispValue = CidetCoreGetRandS64(pThis, pThis->aOperands[idxOp].cbMemDisp);
                    offSeg -= (int64_t)pThis->aOperands[idxOp].uImmDispValue;
                    pThis->aOperands[idxOp].uImmDispValue += offSeg & (RT_BIT_64(pThis->aOperands[idxOp].uMemScale) - 1);
                    offSeg &= ~(RT_BIT_64(pThis->aOperands[idxOp].uMemScale) - 1);
                }
                else if (offSeg & (RT_BIT_64(pThis->aOperands[idxOp].uMemScale) - 1))
                {
                    pThis->cSkippedOnlyIndexRemainder++;
                    return false;
                }

                pThis->aOperands[idxOp].uMemIndexRegValue = offSeg / pThis->aOperands[idxOp].uMemScale;
                Assert((offSeg % pThis->aOperands[idxOp].uMemScale) == 0);
                AssertRelease(!pThis->fUsesVexIndexRegs); /** @todo implement VEX indexing */
            }
            else
            {
                /* [disp] */
                Assert(   pThis->aOperands[idxOp].cbMemDisp == 8
                       || pThis->aOperands[idxOp].cbMemDisp == 4
                       || pThis->aOperands[idxOp].cbMemDisp == 2
                       || pThis->aOperands[idxOp].cbMemDisp == 1);
                if (  pThis->aOperands[idxOp].cbMemDisp == 4
                    ? (int64_t)offSeg != (int32_t)offSeg
                    : pThis->aOperands[idxOp].cbMemDisp == 2
                    ? (int64_t)offSeg != (int16_t)offSeg
                    : pThis->aOperands[idxOp].cbMemDisp == 1
                    ? (int64_t)offSeg != (int8_t)offSeg
                    : false /* 8 */)
                {
                    pThis->cSkippedDirectAddressingOverflow++;
                    return false;
                }
                pThis->aOperands[idxOp].uImmDispValue = offSeg;
            }

            /*
             * Modify the input and expected output contexts with the base and
             * index register values.  To simplify verification and the work
             * here, we update the uMemBaseRegValue and uMemIndexRegValue
             * members to reflect the whole register.
             */
            if (iMemBaseReg != UINT8_MAX)
            {
                if (pThis->cbAddrMode == 4)
                {
                    pThis->aOperands[idxOp].uMemBaseRegValue &= UINT32_MAX;
                    pThis->aOperands[idxOp].uMemBaseRegValue |= pThis->InCtx.aGRegs[iMemBaseReg] & UINT64_C(0xffffffff00000000);
                }
                else if (pThis->cbAddrMode == 2)
                {
                    pThis->aOperands[idxOp].uMemBaseRegValue &= UINT16_MAX;
                    pThis->aOperands[idxOp].uMemBaseRegValue |= pThis->InCtx.aGRegs[iMemBaseReg] & UINT64_C(0xffffffffffff0000);
                }
                pThis->InCtx.aGRegs[iMemBaseReg]       = pThis->aOperands[idxOp].uMemBaseRegValue;
                pThis->ExpectedCtx.aGRegs[iMemBaseReg] = pThis->aOperands[idxOp].uMemBaseRegValue;
            }

            if (iMemIndexReg != UINT8_MAX)
            {
                if (pThis->cbAddrMode == 4)
                {
                    pThis->aOperands[idxOp].uMemIndexRegValue &= UINT32_MAX;
                    pThis->aOperands[idxOp].uMemIndexRegValue |= pThis->InCtx.aGRegs[iMemIndexReg] & UINT64_C(0xffffffff00000000);
                }
                else if (pThis->cbAddrMode == 2)
                {
                    pThis->aOperands[idxOp].uMemIndexRegValue &= UINT16_MAX;
                    pThis->aOperands[idxOp].uMemIndexRegValue |= pThis->InCtx.aGRegs[iMemIndexReg] & UINT64_C(0xffffffffffff0000);
                }
                pThis->InCtx.aGRegs[iMemIndexReg]       = pThis->aOperands[idxOp].uMemIndexRegValue;
                pThis->ExpectedCtx.aGRegs[iMemIndexReg] = pThis->aOperands[idxOp].uMemIndexRegValue;
            }
        }
        else
        {
            /*
             * Non-memory, so clear the memory related members.
             */
            Assert(!pThis->aOperands[idxOp].fIsMem);
            pThis->aOperands[idxOp].iEffSeg  = UINT8_MAX;
            pThis->aOperands[idxOp].offSeg   = UINT64_MAX;
            pThis->aOperands[idxOp].uEffAddr = UINT64_MAX;
            pThis->aOperands[idxOp].pDataBuf = NULL;

            switch (pThis->aOperands[idxOp].fFlags & CIDET_OF_K_MASK)
            {
                case CIDET_OF_K_GPR:
                    if (!pThis->aOperands[idxOp].fIsHighByteRegister)
                    {
                        pThis->aOperands[idxOp].In.pv = &pThis->InCtx.aGRegs[pThis->aOperands[idxOp].iReg];
                        pThis->aOperands[idxOp].Expected.pv = &pThis->ExpectedCtx.aGRegs[pThis->aOperands[idxOp].iReg];
                    }
                    else
                    {
                        pThis->aOperands[idxOp].In.pv = &pThis->InCtx.aGRegs[pThis->aOperands[idxOp].iReg - 4];
                        pThis->aOperands[idxOp].In.pu8++;
                        pThis->aOperands[idxOp].Expected.pv = &pThis->ExpectedCtx.aGRegs[pThis->aOperands[idxOp].iReg - 4];
                        pThis->aOperands[idxOp].Expected.pu8++;
                    }
                    break;

                case CIDET_OF_K_IMM:
                    pThis->aOperands[idxOp].In.pv = NULL;
                    pThis->aOperands[idxOp].Expected.pv = NULL;
                    break;

                case CIDET_OF_K_SREG:
                    if (pThis->aOperands[idxOp].iReg < RT_ELEMENTS(pThis->InCtx.aSRegs))
                    {
                        pThis->aOperands[idxOp].In.pv = &pThis->InCtx.aSRegs[pThis->aOperands[idxOp].iReg];
                        pThis->aOperands[idxOp].Expected.pv = &pThis->ExpectedCtx.aSRegs[pThis->aOperands[idxOp].iReg];
                    }
                    else
                    {
                        pThis->aOperands[idxOp].In.pv = NULL;
                        pThis->aOperands[idxOp].Expected.pv = NULL;
                    }
                    break;

                case CIDET_OF_K_CR:
                case CIDET_OF_K_SSE:
                case CIDET_OF_K_AVX:
                case CIDET_OF_K_AVX512:
                case CIDET_OF_K_FPU:
                case CIDET_OF_K_MMX:
                case CIDET_OF_K_AVXFUTURE:
                case CIDET_OF_K_SPECIAL:
                case CIDET_OF_K_TEST:
                    /** @todo Implement testing these registers. */
                case CIDET_OF_K_NONE:
                default:
                    AssertReleaseFailedReturn(false);
            }
        }
    }
    AssertRelease((uintptr_t)pbBuf - (uintptr_t)&pThis->abBuf[0] <= sizeof(pThis->abBuf));

    /*
     * Call instruction specific setup function (for operand values and flags).
     */
    int rc = pThis->pCurInstr->pfnSetupInOut(pThis, false /*fInvalid*/);
    if (RT_FAILURE(rc))
    {
        pThis->cSkippedSetupInOut++;
        return false;
    }

    /*
     * Do the 2nd set of the memory operand preparations.
     */
    if (pThis->fHasMemoryOperand)
    {
        idxOp = pThis->cOperands;
        while (idxOp-- > 0)
            if (pThis->aOperands[idxOp].fIsMem)
            {
                Assert(pThis->aOperands[idxOp].pDataBuf);
                if (!pThis->pfnSetupDataBuf(pThis, pThis->aOperands[idxOp].pDataBuf, pThis->aOperands[idxOp].In.pv))
                {
                    pThis->cSkippedSetupDataBuf++;
                    return false;
                }

                Assert(   pThis->aOperands[idxOp].iMemBaseReg == UINT8_MAX
                       || pThis->InCtx.aGRegs[pThis->aOperands[idxOp].iMemBaseReg] == pThis->aOperands[idxOp].uMemBaseRegValue);
                Assert(   pThis->aOperands[idxOp].iMemIndexReg == UINT8_MAX
                       || (  !pThis->fUsesVexIndexRegs
                           ?    pThis->InCtx.aGRegs[pThis->aOperands[idxOp].iMemIndexReg]
                             == pThis->aOperands[idxOp].uMemIndexRegValue
                           : false /** @todo VEX indexing */));
            }
    }

    return true;
}


/**
 * Figures the instruction length.
 *
 * This is a duplicate of CidetCoreAssemble() with the buffer updates removed.
 *
 * @returns true and pThis->cbInstr on success, false on failure.
 * @param   pThis           The core state structure (for context).
 */
bool CidetCoreAssembleLength(PCIDETCORE pThis)
{
    uint8_t off = 0;

    /*
     * Prefixes.
     */
    if (1)
    {
        if (pThis->fAddrSizePrf)
            off++;
        if (pThis->fOpSizePrf)
            off++;
    }
    else
    {
        /** @todo prefix list. */
    }

    /*
     * Prefixes that must come right before the opcode.
     */
    /** @todo VEX and EVEX. */
    if (pThis->fVex)
    {
        /** @todo VEX and EVEX. */
    }
    else if (pThis->fEvex)
    {
        /** @todo VEX and EVEX. */
    }
    else
    {
        if (pThis->fRexB || pThis->fRexX || pThis->fRexR || pThis->fRexW || pThis->fRex)
            off++;
    }

    /*
     * The opcode.
     */
    //uint8_t const *pbOpcode = pThis->pCurInstr->abOpcode;
    switch (pThis->pCurInstr->cbOpcode)
    {
        case 3: off++; RT_FALL_THRU();
        case 2: off++; RT_FALL_THRU();
        case 1: off++;
            break;
        default:
            AssertReleaseFailedReturn(false);
    }

    /*
     * Mod R/M
     */
    if (pThis->fUsesModRm)
    {
        off++;
        if (pThis->fSib)
            off++;
        if (pThis->idxMrmRmOp < RT_ELEMENTS(pThis->aOperands))
        {
            //uint64_t uDispValue = pThis->aOperands[pThis->idxMrmRmOp].uImmDispValue;
            switch (pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp)
            {
                case 0: break;
                case 8:
                case 7:
                case 6:
                case 5:
                case 4:
                case 3:
                case 2:
                case 1:
                    break;
                default: AssertReleaseFailedReturn(false);
            }
            off += pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp;
        }
    }

    /*
     * Immediates.
     */
    uint8_t iOp = pThis->cOperands;
    while (iOp-- > 0)
        if ((pThis->aOperands[iOp].fFlags & CIDET_OF_K_MASK) == CIDET_OF_K_IMM)
        {
            //uint64_t uImmValue = pThis->aOperands[iOp].uImmDispValue;
            switch (pThis->aOperands[iOp].cb)
            {
                case 8:
                case 7:
                case 6:
                case 5:
                case 4:
                case 3:
                case 2:
                case 1:
                    break;
                default: AssertReleaseFailedReturn(false);
            }
            off += pThis->aOperands[iOp].cb;
        }

    pThis->cbInstr = off;
    return true;
}


/**
 * Assembles the instruction.
 *
 * This is a duplicate of CidetCoreAssembleLength() with buffer writes.
 *
 * @returns true and pThis->cbInstr and pThis->abInstr on success, false on
 *          failure.
 * @param   pThis           The core state structure (for context).
 */
bool CidetCoreAssemble(PCIDETCORE pThis)
{
    uint8_t off = 0;

    /*
     * Prefixes.
     */
    if (1)
    {
        if (pThis->fAddrSizePrf)
            pThis->abInstr[off++] = 0x67;
        if (pThis->fOpSizePrf)
            pThis->abInstr[off++] = 0x66;
    }
    else
    {
        /** @todo prefix list. */
    }

    /*
     * Prefixes that must come right before the opcode.
     */
    /** @todo VEX and EVEX. */
    if (pThis->fVex)
    {
        /** @todo VEX and EVEX. */
    }
    else if (pThis->fEvex)
    {
        /** @todo VEX and EVEX. */
    }
    else
    {
        if (pThis->fRexB || pThis->fRexX || pThis->fRexR || pThis->fRexW || pThis->fRex)
            pThis->abInstr[off++] = 0x40 | (pThis->fRexB * 1) | (pThis->fRexX * 2) | (pThis->fRexR * 4) | (pThis->fRexW * 8);
    }

    /*
     * The opcode.
     */
    uint8_t const *pbOpcode = pThis->pCurInstr->abOpcode;
    switch (pThis->pCurInstr->cbOpcode)
    {
        case 3: pThis->abInstr[off++] = *pbOpcode++; RT_FALL_THRU();
        case 2: pThis->abInstr[off++] = *pbOpcode++; RT_FALL_THRU();
        case 1: pThis->abInstr[off++] = *pbOpcode++;
            break;
        default:
            AssertReleaseFailedReturn(false);
    }

    /*
     * Mod R/M
     */
    if (pThis->fUsesModRm)
    {
        pThis->abInstr[off++] = pThis->bModRm;
        if (pThis->fSib)
            pThis->abInstr[off++] = pThis->bSib;
        if (pThis->idxMrmRmOp < RT_ELEMENTS(pThis->aOperands))
        {
            uint64_t uDispValue = pThis->aOperands[pThis->idxMrmRmOp].uImmDispValue;
            switch (pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp)
            {
                case 0: break;
                case 8: pThis->abInstr[off + 3] = (uDispValue >> 56) & UINT8_C(0xff); RT_FALL_THRU();
                case 7: pThis->abInstr[off + 3] = (uDispValue >> 48) & UINT8_C(0xff); RT_FALL_THRU();
                case 6: pThis->abInstr[off + 3] = (uDispValue >> 40) & UINT8_C(0xff); RT_FALL_THRU();
                case 5: pThis->abInstr[off + 3] = (uDispValue >> 32) & UINT8_C(0xff); RT_FALL_THRU();
                case 4: pThis->abInstr[off + 3] = (uDispValue >> 24) & UINT8_C(0xff); RT_FALL_THRU();
                case 3: pThis->abInstr[off + 2] = (uDispValue >> 16) & UINT8_C(0xff); RT_FALL_THRU();
                case 2: pThis->abInstr[off + 1] = (uDispValue >>  8) & UINT8_C(0xff); RT_FALL_THRU();
                case 1: pThis->abInstr[off] = uDispValue & UINT8_C(0xff);
                    break;
                default: AssertReleaseFailedReturn(false);
            }
            off += pThis->aOperands[pThis->idxMrmRmOp].cbMemDisp;
        }
    }

    /*
     * Immediates.
     */
    uint8_t iOp = pThis->cOperands;
    while (iOp-- > 0)
        if ((pThis->aOperands[iOp].fFlags & CIDET_OF_K_MASK) == CIDET_OF_K_IMM)
        {
            uint64_t uImmValue = pThis->aOperands[iOp].uImmDispValue;
            switch (pThis->aOperands[iOp].cb)
            {
                case 8: pThis->abInstr[off + 3] = (uImmValue >> 56) & UINT8_C(0xff); RT_FALL_THRU();
                case 7: pThis->abInstr[off + 3] = (uImmValue >> 48) & UINT8_C(0xff); RT_FALL_THRU();
                case 6: pThis->abInstr[off + 3] = (uImmValue >> 40) & UINT8_C(0xff); RT_FALL_THRU();
                case 5: pThis->abInstr[off + 3] = (uImmValue >> 32) & UINT8_C(0xff); RT_FALL_THRU();
                case 4: pThis->abInstr[off + 3] = (uImmValue >> 24) & UINT8_C(0xff); RT_FALL_THRU();
                case 3: pThis->abInstr[off + 2] = (uImmValue >> 16) & UINT8_C(0xff); RT_FALL_THRU();
                case 2: pThis->abInstr[off + 1] = (uImmValue >>  8) & UINT8_C(0xff); RT_FALL_THRU();
                case 1: pThis->abInstr[off] = uImmValue & UINT8_C(0xff);
                    break;
                default: AssertReleaseFailedReturn(false);
            }
            off += pThis->aOperands[iOp].cb;
        }

    pThis->cbInstr = off;
    return true;
}


bool CidetCoreReInitCodeBuf(PCIDETCORE pThis)
{
    /*
     * Re-initialize the buffer.  Requires instruction length and positioning.
     */
    if (CidetCoreAssembleLength(pThis))
    {
        pThis->CodeBuf.cb  = pThis->cbInstr;
        pThis->CodeBuf.off = CIDET_CODE_BUF_SIZE - PAGE_SIZE - pThis->cbInstr;
        if (pThis->pfnReInitCodeBuf(pThis, &pThis->CodeBuf))
        {
            pThis->CodeBuf.fActive = true;

            /*
             * Update the RIP and CS values in the input and expected contexts.
             */
            pThis->InCtx.rip       = pThis->CodeBuf.uEffBufAddr + pThis->CodeBuf.offActive - pThis->CodeBuf.uSegBase;
            pThis->ExpectedCtx.rip = pThis->InCtx.rip + pThis->cbInstr; /** @todo account for expected traps. */
            if (pThis->CodeBuf.uSeg != UINT32_MAX)
            {
                pThis->InCtx.aSRegs[X86_SREG_CS] = pThis->CodeBuf.uSeg;
                pThis->ExpectedCtx.aSRegs[X86_SREG_CS] = pThis->CodeBuf.uSeg;
            }
            return true;
        }
        else
            pThis->cSkippedReInitCodeBuf++;
    }
    else
        pThis->cSkippedAssemble++;
    return false;
}


#ifdef CIDET_DEBUG_DISAS
/**
 * @callback_method_impl{FNDISREADBYTES}
 */
static DECLCALLBACK(int) cidetCoreDisReadBytes(PDISSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
{
    PCIDETCORE pThis = (PCIDETCORE)pDis->pvUser;
    memcpy(&pDis->abInstr[offInstr], &pThis->abInstr[offInstr], cbMaxRead);
    pDis->cbCachedInstr = offInstr + cbMaxRead;
    return VINF_SUCCESS;
}
#endif


bool CidetCoreSetupCodeBuf(PCIDETCORE pThis, unsigned iSubTest)
{
    if (CidetCoreAssemble(pThis))
    {
        //CIDET_DPRINTF(("%04u: %.*Rhxs\n", i, pThis->cbInstr, pThis->abInstr));
#ifdef CIDET_DEBUG_DISAS
        DISCPUSTATE Dis;
        char        szInstr[80] = {0};
        uint32_t    cbInstr;
        int rcDis = DISInstrToStrEx(pThis->InCtx.rip,
                                    CIDETMODE_IS_64BIT(pThis->bMode)   ? DISCPUMODE_64BIT
                                    : CIDETMODE_IS_32BIT(pThis->bMode) ? DISCPUMODE_32BIT : DISCPUMODE_16BIT,
                                    cidetCoreDisReadBytes,
                                    pThis,
                                    DISOPTYPE_ALL,
                                    &Dis,
                                    &cbInstr,
                                    szInstr, sizeof(szInstr));
        CIDET_DPRINTF(("%04u: %s", iSubTest, szInstr));
        Assert(cbInstr == pThis->cbInstr);
#else
        RT_NOREF_PV(iSubTest);
#endif
        if (pThis->pfnSetupCodeBuf(pThis, &pThis->CodeBuf, pThis->abInstr))
        {
            return true;
        }
        pThis->cSkippedSetupCodeBuf++;
    }
    else
        pThis->cSkippedAssemble++;
    return false;
}


/**
 * Compares the output with the output expectations.
 *
 * @returns true if ok, false if not (calls pfnFailure too).
 * @param   pThis           The core state structure.
 */
bool CidetCoreCheckResults(PCIDETCORE pThis)
{
    if (memcmp(&pThis->ActualCtx, &pThis->ExpectedCtx, CIDETCPUCTX_COMPARE_SIZE) == 0)
        return true;

    unsigned cDiffs = 0;
#define IF_FIELD_DIFFERS_SET_ERROR(a_Field, a_Fmt) \
        if (pThis->ActualCtx.a_Field != pThis->ExpectedCtx.a_Field) \
        { \
            CidetCoreSetError(pThis, #a_Field " differs: got %#llx expected %#llx", \
                              pThis->ActualCtx.a_Field, pThis->ExpectedCtx.a_Field); \
            cDiffs++; \
        } else do { } while (0)

    IF_FIELD_DIFFERS_SET_ERROR(rip,                    "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(rfl,                    "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_xAX],   "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_xBX],   "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_xCX],   "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_xDX],   "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_xSP],   "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_xBP],   "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_xSI],   "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_xDI],   "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_x8],    "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_x9],    "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_x9],    "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_x10],   "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_x11],   "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_x12],   "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_x13],   "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_x14],   "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aGRegs[X86_GREG_x15],   "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(aSRegs[X86_SREG_CS],    "%#06x");
    IF_FIELD_DIFFERS_SET_ERROR(aSRegs[X86_SREG_SS],    "%#06x");
    IF_FIELD_DIFFERS_SET_ERROR(aSRegs[X86_SREG_DS],    "%#06x");
    IF_FIELD_DIFFERS_SET_ERROR(aSRegs[X86_SREG_ES],    "%#06x");
    IF_FIELD_DIFFERS_SET_ERROR(aSRegs[X86_SREG_FS],    "%#06x");
    IF_FIELD_DIFFERS_SET_ERROR(aSRegs[X86_SREG_GS],    "%#06x");
    IF_FIELD_DIFFERS_SET_ERROR(uXcpt,                  "%#04x");
    IF_FIELD_DIFFERS_SET_ERROR(uErr,                   "%#04llx");
    IF_FIELD_DIFFERS_SET_ERROR(cr2,                    "%#010llx");
#ifndef CIDET_REDUCED_CTX
    IF_FIELD_DIFFERS_SET_ERROR(tr,                     "%#06x");
    IF_FIELD_DIFFERS_SET_ERROR(ldtr,                   "%#06x");
    IF_FIELD_DIFFERS_SET_ERROR(cr0,                    "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(cr3,                    "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(cr4,                    "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(cr8,                    "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(dr0,                    "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(dr1,                    "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(dr2,                    "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(dr3,                    "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(dr6,                    "%#010llx");
    IF_FIELD_DIFFERS_SET_ERROR(dr7,                    "%#010llx");
#endif

AssertMsgFailed(("cDiffs=%d\n", cDiffs));
    Assert(cDiffs > 0);
    return cDiffs == 0;
}


bool CidetCoreTest_Basic(PCIDETCORE pThis)
{
    /*
     * Iterate all encodings.
     */
    if (!CidetCoreSetupFirstBaseEncoding(pThis))
        return CidetCoreSetError(pThis, "CidetCoreSetupFirstBaseEncoding failed");
    unsigned cExecuted = 0;
    unsigned cSkipped = 0;
    do
    {
        /*
         * Iterate data buffer configurations (one iteration if none).
         */
        if (CidetCoreSetupFirstMemoryOperandConfig(pThis))
        {
            do
            {
                /*
                 * Iterate code buffer configurations.
                 */
                if (!CidetCoreSetupFirstCodeBufferConfig(pThis))
                    return CidetCoreSetError(pThis, "CidetCoreSetupFirstMemoryOperandConfig failed");
                do
                {
                    /*
                     * Set up inputs and expected outputs, then emit the test code.
                     */
                    pThis->InCtx        = pThis->InTemplateCtx;
                    pThis->InCtx.fTrickyStack = pThis->fHasStackRegInMrmRmBase || pThis->fHasStackRegInMrmReg;
                    pThis->ExpectedCtx  = pThis->InCtx;
                    if (   CidetCoreReInitCodeBuf(pThis)
                        && CidetCoreSetupInOut(pThis)
                        && CidetCoreSetupCodeBuf(pThis, cSkipped + cExecuted)
                       )
                    {
                        if (pThis->pfnExecute(pThis))
                        {
                            cExecuted++;

                            /*
                             * Check the result against our expectations.
                             */
                            CidetCoreCheckResults(pThis);
                            /** @todo check result. */

                        }
                        else
                            cSkipped++;
                    }
                    else
                        cSkipped++;
                } while (CidetCoreSetupNextCodeBufferConfig(pThis));
            } while (CidetCoreSetupNextMemoryOperandConfig(pThis));
        }
        else
            cSkipped++;
    } while (CidetCoreSetupNextBaseEncoding(pThis));

    CIDET_DPRINTF(("CidetCoreTest_Basic: cExecuted=%u cSkipped=%u\n"
                   "  cSkippedSetupInOut               =%u\n"
                   "  cSkippedReInitDataBuf            =%u\n"
                   "  cSkippedSetupDataBuf             =%u\n"
                   "  cSkippedDataBufWrtRip            =%u\n"
                   "  cSkippedAssemble                 =%u\n"
                   "  cSkippedReInitCodeBuf            =%u\n"
                   "  cSkippedSetupCodeBuf             =%u\n"
                   "  cSkippedSameBaseIndexRemainder   =%u\n"
                   "  cSkippedOnlyIndexRemainder       =%u\n"
                   "  cSkippedDirectAddressingOverflow =%u\n"
                   ,
                   cExecuted, cSkipped,
                   pThis->cSkippedSetupInOut,
                   pThis->cSkippedReInitDataBuf,
                   pThis->cSkippedSetupDataBuf,
                   pThis->cSkippedDataBufWrtRip,
                   pThis->cSkippedAssemble,
                   pThis->cSkippedReInitCodeBuf,
                   pThis->cSkippedSetupCodeBuf,
                   pThis->cSkippedSameBaseIndexRemainder,
                   pThis->cSkippedOnlyIndexRemainder,
                   pThis->cSkippedDirectAddressingOverflow
                   ));

    return true;
}


bool CidetCoreTestInstruction(PCIDETCORE pThis, PCCIDETINSTR pInstr)
{
    AssertReleaseMsgReturn(RT_VALID_PTR(pThis), ("%p\n", pThis), false);
    AssertReleaseReturn(pThis->u32Magic == CIDETCORE_MAGIC, false);
    AssertReleaseReturn(pThis->cCodeBufConfigs > 0, false);

    if (!CideCoreSetInstruction(pThis, pInstr))
        return CidetCoreSetError(pThis, "CideCoreSetInstruction failed");

    bool fResult = CidetCoreTest_Basic(pThis);

    return fResult;
}