rsemu 0.0.1

A multiplatform emulator in pure Rust, built bottom-up on a generic framework.
Documentation
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
//! The interpreter: decode, execute, and the flag rules.
//!
//! # The bus is the clock
//!
//! Every guest access goes through [`AddressSpace`] one transfer at a time and
//! charges four clocks, which is what an 8088 bus cycle costs with no wait
//! states. There is no table of instruction lengths: `add [bp+di-64h], cl`
//! reads its operand, writes it back, and pays for both because the accesses
//! happened, and a device watching the bus sees them in the order hardware
//! would.
//!
//! What this does *not* model is the overlap between the bus interface unit
//! and the execution unit. A real 8088 prefetches while the previous
//! instruction is still executing, so its instruction time is roughly the
//! larger of the two rather than their sum. Here the prefetch queue is filled
//! at instruction boundaries and refilled on demand, and every fetch is
//! charged, so a cycle count taken over a long run is an upper bound. That is
//! a deliberate trade: getting the T-state-exact overlap right means modelling
//! the microcode, and the accuracy that actually matters for correctness —
//! *which* accesses happen, in what order, with what values — is checked
//! against hardware in [`conformance`](super).
//!
//! # The undefined flags
//!
//! Intel documents a dozen instructions as leaving some flags undefined. The
//! silicon is not undefined, merely unspecified, and software has been found
//! to depend on it. Each of the following was measured against
//! `SingleStepTests/8088` — hardware output, not anyone's emulator — rather
//! than guessed, and each is implemented where it is described:
//!
//! | Instruction | Officially undefined | What the 8088 actually does | Vectors |
//! | --- | --- | --- | --- |
//! | `AND`, `OR`, `XOR`, `TEST` | `AF` | cleared, always ([`Exec::logic_flags8`]) | exact |
//! | `SHL`/`SAL` | `AF` | bit 4 of the result — the microcode is an `ADD dst,dst` ([`Exec::shift8`]) | exact |
//! | `SHR`, `SAR` | `AF` | cleared | exact |
//! | `MUL` | `SF`, `ZF`, `AF`, `PF` | set from the **high half** of the product: `ZF` if it is zero, `SF` from its top bit, `PF` from its low byte, `AF` cleared ([`Exec::mul_flags`]) | exact |
//! | `DAA`, `DAS` | `OF` | from the *single* correcting add or subtract of `0x00`/`0x06`/`0x60`/`0x66`; and the high correction's threshold moves with `AF` ([`Exec::decimal_adjust`]) | exact |
//! | `AAA`, `AAS` | `OF`, `SF`, `ZF`, `PF` | from an 8-bit `AL ± 6` that happens even when no adjustment is needed, with an operand of zero ([`Exec::ascii_adjust`]) | exact |
//! | `AAM 0` | all | the flags of a zero result, then a divide error ([`Exec::aam`]) | exact |
//! | `AAD` | `OF`, `AF`, `CF` | the adjustment really is an addition and sets them ([`Exec::aad`]) | exact |
//! | `SETMO` (`D0`-`D3` `/6`) | all | the operand becomes all ones and the flags follow it as a logical result | exact |
//! | `IMUL` | `SF`, `ZF`, `AF`, `PF` | the sign-corrected magnitude loop's residue; modelled as `MUL`'s rule | ~66% |
//! | `DIV`, `IDIV` | all | `CF` is the complement of the quotient's top bit (exact); the rest are the last trial subtraction's ([`Exec::cord`]) | ~35% |
//!
//! The last two rows are the whole of `conformance::KNOWN_FAILURES`, and the
//! percentages there are measured rather than estimated.
//!
//! One result that is not a flag belongs in the same list, because it is the
//! same kind of surprise: a shift or rotate **by zero** still writes its
//! operand back. Nothing changes and no flag moves, but the write happens on
//! the bus, where a memory-mapped device would see it ([`Exec::shift`]).
//!
//! # Sources
//!
//! Intel's *iAPX 86/88 User's Manual* for the instruction semantics, the flag
//! definitions and the timing tables; `docs/cpu/x86.md` for the rest. The
//! undefined-flag column above is measurement, and the measurement is
//! reproducible by anyone with the corpus. No copyleft emulator was consulted.

use alloc::vec::Vec;

use crate::core::space::{AddressSpace, MemAttrs};
use crate::core::value::Width;

use super::isa::{self, Arg, Fields, Op, Rep, seg};
use super::{Config, Lines, Model, Regs, flags, linear};

/// Clocks in one bus cycle, with no wait states. T1 through T4.
const BUS_CLOCKS: u32 = 4;

/// Clocks the reset sequence spends before the first fetch.
const RESET_CLOCKS: u32 = 7;

/// Type 0: divide error.
const VEC_DIVIDE: u8 = 0;
/// Type 1: single step, taken after an instruction when `TF` is set.
const VEC_SINGLE_STEP: u8 = 1;
/// Type 2: NMI.
const VEC_NMI: u8 = 2;
/// Type 3: the one-byte breakpoint.
const VEC_BREAKPOINT: u8 = 3;
/// Type 4: `INTO`.
const VEC_OVERFLOW: u8 = 4;

// ---------------------------------------------------------------------------
// The prefetch queue
// ---------------------------------------------------------------------------

/// The bus interface unit's instruction queue.
///
/// Four bytes on an 8088, six on an 8086. It is not an implementation detail:
/// the queue status lines are pins, an interrupted string instruction restarts
/// from it, and self-modifying code that writes within a few bytes of `IP`
/// behaves differently because of it — which is why `docs/cpu/x86.md` calls
/// self-modifying code mandatory rather than optional.
///
/// The queue holds the bytes at `CS:IP` through `CS:IP+len-1`, so the offset
/// the bus interface unit fetches next is always `IP + len`. Keeping that
/// invariant rather than a separate fetch pointer means a snapshot of `IP` and
/// the queue contents is a complete description.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct Queue {
    bytes: [u8; 6],
    len: u8,
    depth: u8,
}

impl Queue {
    /// An empty queue of the depth this part has.
    pub(super) const fn new(model: Model) -> Queue {
        Queue {
            bytes: [0; 6],
            len: 0,
            depth: model.queue_bytes(),
        }
    }

    /// Discard everything, as a control transfer does.
    pub(super) const fn flush(&mut self) {
        self.len = 0;
    }

    /// How many bytes are queued.
    pub(super) const fn len(&self) -> u8 {
        self.len
    }

    /// How many bytes the queue holds when full.
    pub(super) const fn depth(&self) -> u8 {
        self.depth
    }

    fn push(&mut self, byte: u8) {
        if self.len < self.depth {
            self.bytes[self.len as usize] = byte;
            self.len += 1;
        }
    }

    fn pop(&mut self) -> Option<u8> {
        if self.len == 0 {
            return None;
        }
        let byte = self.bytes[0];
        // Shifting four bytes down beats the modular arithmetic a ring buffer
        // would need on every single instruction byte.
        let mut i = 1usize;
        while i < self.len as usize {
            self.bytes[i - 1] = self.bytes[i];
            i += 1;
        }
        self.len -= 1;
        Some(byte)
    }

    /// The queued bytes, oldest first.
    pub(super) fn contents(&self) -> Vec<u8> {
        self.bytes[..self.len as usize].to_vec()
    }

    /// Replace the contents, as if the bus interface unit had fetched them.
    ///
    /// # Errors
    ///
    /// If more bytes are given than the queue holds.
    pub(super) fn install(&mut self, bytes: &[u8]) -> Result<(), ()> {
        if bytes.len() > self.depth as usize {
            return Err(());
        }
        self.len = bytes.len() as u8;
        self.bytes[..bytes.len()].copy_from_slice(bytes);
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Execution state
// ---------------------------------------------------------------------------

/// The architectural state one core owns.
///
/// Split from [`super::I8086`] because the interrupt *lines* live outside the
/// lock: a device asserting `INTR` from inside a CPU-initiated MMIO write
/// would otherwise re-enter the CPU's own critical section and deadlock (the
/// re-entrancy contract, `ROADMAP.md` §4.7).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct State {
    /// The register file.
    pub regs: Regs,
    /// Clock cycles executed since power-on.
    pub cycles: u64,
    /// Set by `HLT`; cleared by any interrupt or reset.
    pub halted: bool,
    /// A reset was requested and its sequence has not run yet.
    pub reset_pending: bool,
    /// Interrupts are inhibited for the next instruction.
    ///
    /// Set by `MOV SS,x` and `POP SS` so that the `SS:SP` pair can be reloaded
    /// atomically. On an 8086 the shadow covers NMI too.
    pub int_shadow: bool,
    /// The bus interface unit's queue.
    pub queue: Queue,
    /// The last value driven on the data bus.
    ///
    /// An 8086 has no bus-error input: an access nothing answers leaves the
    /// previous value on the bus, and that is what the CPU reads.
    pub open_bus: u8,
    /// How many accesses an address space refused.
    pub faults: u64,
    /// Physical address of the most recent refused access.
    pub last_fault: u32,
}

impl State {
    /// Power-on state, before the reset sequence has run.
    pub(super) const fn new(model: Model) -> State {
        State {
            regs: Regs::new(),
            cycles: 0,
            halted: false,
            reset_pending: true,
            int_shadow: false,
            queue: Queue::new(model),
            open_bus: 0,
            faults: 0,
            last_fault: 0,
        }
    }
}

/// One step's worth of execution, borrowing everything it needs.
///
/// Created per step rather than stored: it holds the per-instruction
/// bookkeeping — the effective address, the restart point — that is
/// meaningless between instructions, and dropping it makes that explicit.
pub(super) struct Exec<'a> {
    state: &'a mut State,
    mem: &'a AddressSpace,
    io: Option<&'a AddressSpace>,
    cfg: &'a Config,
    lines: &'a Lines,
    attrs: MemAttrs,
    /// The memory operand's `(segment register, offset)`, computed once.
    ea: Option<(u8, u16)>,
    /// Where the current instruction started, prefixes included, so a string
    /// operation interrupted between iterations can be restarted.
    start_ip: u16,
    /// Clocks this step has charged.
    used: u64,
}

impl<'a> Exec<'a> {
    /// Borrow a core for one step.
    pub(super) fn new(
        state: &'a mut State,
        mem: &'a AddressSpace,
        io: Option<&'a AddressSpace>,
        cfg: &'a Config,
        lines: &'a Lines,
    ) -> Exec<'a> {
        let attrs = MemAttrs::DEFAULT.with_requester(cfg.requester);
        Exec {
            state,
            mem,
            io,
            cfg,
            lines,
            attrs,
            ea: None,
            start_ip: 0,
            used: 0,
        }
    }

    /// Run one reset sequence, interrupt sequence, or instruction.
    ///
    /// Returns the clocks charged — zero only when the CPU is halted with
    /// nothing pending, which the caller must notice rather than spin on.
    pub(super) fn step(&mut self) -> u64 {
        if self.state.reset_pending {
            self.reset_sequence();
            return self.used;
        }

        // The shadow suppresses the interrupt check for exactly one
        // instruction. It is read here and cleared below, so an instruction
        // that sets it again extends it by one more.
        let shadow = self.state.int_shadow;
        if !shadow {
            if self.lines.take_nmi_pending() {
                self.state.halted = false;
                self.charge(Op::INT.clocks());
                self.service(VEC_NMI);
                return self.used;
            }
            if self.flag(flags::IF) && self.lines.intr_pending() {
                self.state.halted = false;
                // Two INTA bus cycles; a PC's 8259A drives the vector onto the
                // data bus during the second.
                self.charge(2 * BUS_CLOCKS + Op::INT.clocks());
                let vector = self.lines.intr_vector();
                self.service(vector);
                return self.used;
            }
        }

        if self.state.halted {
            return 0;
        }

        // `TF` is sampled before the instruction runs, which is why a `POPF`
        // that sets it does not trap on itself.
        let trap = self.flag(flags::TF) && !shadow;
        self.state.int_shadow = false;
        self.instruction();
        if trap && !self.state.int_shadow {
            self.charge(Op::INT.clocks());
            self.service(VEC_SINGLE_STEP);
        }
        self.used
    }

    // -----------------------------------------------------------------
    // The clock and the bus
    // -----------------------------------------------------------------

    fn charge(&mut self, clocks: u32) {
        let clocks = u64::from(clocks);
        self.used += clocks;
        self.state.cycles = self.state.cycles.wrapping_add(clocks);
    }

    /// One byte-wide bus read.
    fn bus_read8(&mut self, addr: u32) -> u8 {
        self.charge(BUS_CLOCKS);
        match self.mem.read(u64::from(addr), Width::U8, self.attrs) {
            Ok(value) => {
                let byte = value as u8;
                self.state.open_bus = byte;
                byte
            }
            Err(_) => {
                // No bus-error input exists on this chip, so the honest model
                // is open bus — and the fault counter is how anyone finds out.
                self.state.faults = self.state.faults.wrapping_add(1);
                self.state.last_fault = addr;
                self.state.open_bus
            }
        }
    }

    /// One byte-wide bus write.
    fn bus_write8(&mut self, addr: u32, value: u8) {
        self.charge(BUS_CLOCKS);
        self.state.open_bus = value;
        if self
            .mem
            .write(u64::from(addr), Width::U8, u64::from(value), self.attrs)
            .is_err()
        {
            self.state.faults = self.state.faults.wrapping_add(1);
            self.state.last_fault = addr;
        }
    }

    /// Whether a word at this segment and offset is one bus cycle on this part.
    ///
    /// Only on an 8086, and only when the transfer is aligned and does not
    /// straddle the end of the segment — an 8088 has eight data pins and
    /// always takes two.
    fn word_is_one_cycle(&self, base: u32, offset: u16) -> bool {
        self.cfg.model.bus_bytes() == 2 && base.is_multiple_of(2) && offset != 0xffff
    }

    /// Read a word at an explicit segment value.
    ///
    /// The *offset* wraps, not the physical address: a word at offset `0xffff`
    /// takes its high byte from offset `0x0000` of the same segment. Both
    /// halves then go through the same 20-bit adder, so a segment near the top
    /// of memory wraps at 1 MiB exactly as a byte access would.
    fn read16_seg(&mut self, segment: u16, offset: u16) -> u16 {
        let base = linear(segment, offset);
        if self.word_is_one_cycle(base, offset) {
            self.charge(BUS_CLOCKS);
            return match self.mem.read(u64::from(base), Width::U16, self.attrs) {
                Ok(value) => {
                    self.state.open_bus = (value >> 8) as u8;
                    value as u16
                }
                Err(_) => {
                    self.state.faults = self.state.faults.wrapping_add(1);
                    self.state.last_fault = base;
                    let byte = u16::from(self.state.open_bus);
                    byte | (byte << 8)
                }
            };
        }
        let lo = self.bus_read8(base);
        let hi = self.bus_read8(linear(segment, offset.wrapping_add(1)));
        u16::from(lo) | (u16::from(hi) << 8)
    }

    /// Write a word at an explicit segment value, low byte first.
    fn write16_seg(&mut self, segment: u16, offset: u16, value: u16) {
        let base = linear(segment, offset);
        if self.word_is_one_cycle(base, offset) {
            self.charge(BUS_CLOCKS);
            self.state.open_bus = (value >> 8) as u8;
            if self
                .mem
                .write(u64::from(base), Width::U16, u64::from(value), self.attrs)
                .is_err()
            {
                self.state.faults = self.state.faults.wrapping_add(1);
                self.state.last_fault = base;
            }
            return;
        }
        self.bus_write8(base, value as u8);
        self.bus_write8(linear(segment, offset.wrapping_add(1)), (value >> 8) as u8);
    }

    fn read8(&mut self, sr: u8, offset: u16) -> u8 {
        let segment = self.state.regs.segment(sr);
        self.bus_read8(linear(segment, offset))
    }

    fn write8(&mut self, sr: u8, offset: u16, value: u8) {
        let segment = self.state.regs.segment(sr);
        self.bus_write8(linear(segment, offset), value);
    }

    fn read16(&mut self, sr: u8, offset: u16) -> u16 {
        let segment = self.state.regs.segment(sr);
        self.read16_seg(segment, offset)
    }

    fn write16(&mut self, sr: u8, offset: u16, value: u16) {
        let segment = self.state.regs.segment(sr);
        self.write16_seg(segment, offset, value);
    }

    /// One I/O read. A core with no I/O space sees an unterminated bus, which
    /// reads as ones — the same answer the corpus expects from a bare 8088.
    fn io_read8(&mut self, port: u16) -> u8 {
        self.charge(BUS_CLOCKS);
        let Some(io) = self.io else {
            return 0xff;
        };
        match io.read(u64::from(port), Width::U8, self.attrs) {
            Ok(value) => value as u8,
            Err(_) => {
                self.state.faults = self.state.faults.wrapping_add(1);
                self.state.last_fault = u32::from(port);
                0xff
            }
        }
    }

    fn io_write8(&mut self, port: u16, value: u8) {
        self.charge(BUS_CLOCKS);
        let Some(io) = self.io else {
            return;
        };
        if io
            .write(u64::from(port), Width::U8, u64::from(value), self.attrs)
            .is_err()
        {
            self.state.faults = self.state.faults.wrapping_add(1);
            self.state.last_fault = u32::from(port);
        }
    }

    // -----------------------------------------------------------------
    // Instruction fetch
    // -----------------------------------------------------------------

    /// Top the prefetch queue up, as the bus interface unit does whenever the
    /// execution unit leaves the bus free.
    fn fill_queue(&mut self) {
        while self.state.queue.len() < self.state.queue.depth() {
            let offset = self
                .state
                .regs
                .ip
                .wrapping_add(u16::from(self.state.queue.len()));
            let byte = self.read8(seg::CS, offset);
            self.state.queue.push(byte);
        }
    }

    /// Take the next instruction byte, refilling the queue if it is empty.
    fn fetch_byte(&mut self) -> u8 {
        if self.state.queue.len() == 0 {
            let offset = self.state.regs.ip;
            let byte = self.read8(seg::CS, offset);
            self.state.queue.push(byte);
        }
        let byte = self.state.queue.pop().unwrap_or(self.state.open_bus);
        // Guest arithmetic wraps: IP is 16 bits and `ffff` is followed by
        // `0000` in the same code segment.
        self.state.regs.ip = self.state.regs.ip.wrapping_add(1);
        byte
    }

    // -----------------------------------------------------------------
    // Flags
    // -----------------------------------------------------------------

    fn flag(&self, mask: u16) -> bool {
        self.state.regs.flags & mask != 0
    }

    fn set_flag(&mut self, mask: u16, on: bool) {
        if on {
            self.state.regs.flags |= mask;
        } else {
            self.state.regs.flags &= !mask;
        }
    }

    /// Even parity of the low eight bits — the only parity an 8086 computes.
    const fn parity(value: u8) -> bool {
        (value.count_ones() & 1) == 0
    }

    fn set_szp8(&mut self, value: u8) {
        self.set_flag(flags::ZF, value == 0);
        self.set_flag(flags::SF, value & 0x80 != 0);
        self.set_flag(flags::PF, Self::parity(value));
    }

    fn set_szp16(&mut self, value: u16) {
        self.set_flag(flags::ZF, value == 0);
        self.set_flag(flags::SF, value & 0x8000 != 0);
        self.set_flag(flags::PF, Self::parity(value as u8));
    }

    // -----------------------------------------------------------------
    // The ALU
    // -----------------------------------------------------------------

    fn add8(&mut self, a: u8, b: u8, carry: bool) -> u8 {
        let sum = u16::from(a) + u16::from(b) + u16::from(carry);
        let r = sum as u8;
        self.set_flag(flags::CF, sum > 0xff);
        self.set_flag(flags::AF, (a ^ b ^ r) & 0x10 != 0);
        self.set_flag(flags::OF, (!(a ^ b)) & (a ^ r) & 0x80 != 0);
        self.set_szp8(r);
        r
    }

    fn add16(&mut self, a: u16, b: u16, carry: bool) -> u16 {
        let sum = u32::from(a) + u32::from(b) + u32::from(carry);
        let r = sum as u16;
        self.set_flag(flags::CF, sum > 0xffff);
        self.set_flag(flags::AF, (a ^ b ^ r) & 0x10 != 0);
        self.set_flag(flags::OF, (!(a ^ b)) & (a ^ r) & 0x8000 != 0);
        self.set_szp16(r);
        r
    }

    fn sub8(&mut self, a: u8, b: u8, borrow: bool) -> u8 {
        let rhs = u16::from(b) + u16::from(borrow);
        let diff = u16::from(a).wrapping_sub(rhs);
        let r = diff as u8;
        self.set_flag(flags::CF, u16::from(a) < rhs);
        self.set_flag(flags::AF, (a ^ b ^ r) & 0x10 != 0);
        self.set_flag(flags::OF, (a ^ b) & (a ^ r) & 0x80 != 0);
        self.set_szp8(r);
        r
    }

    fn sub16(&mut self, a: u16, b: u16, borrow: bool) -> u16 {
        let rhs = u32::from(b) + u32::from(borrow);
        let diff = u32::from(a).wrapping_sub(rhs);
        let r = diff as u16;
        self.set_flag(flags::CF, u32::from(a) < rhs);
        self.set_flag(flags::AF, (a ^ b ^ r) & 0x10 != 0);
        self.set_flag(flags::OF, (a ^ b) & (a ^ r) & 0x8000 != 0);
        self.set_szp16(r);
        r
    }

    /// Flags after `AND`, `OR`, `XOR` and `TEST`.
    ///
    /// Carry and overflow are documented as cleared. `AF` is documented as
    /// undefined; on this part it is cleared too, on every one of the tens of
    /// thousands of corpus vectors that exercise it, so it is modelled as
    /// cleared rather than left alone.
    fn logic_flags8(&mut self, r: u8) {
        self.set_flag(flags::CF | flags::OF | flags::AF, false);
        self.set_szp8(r);
    }

    fn logic_flags16(&mut self, r: u16) {
        self.set_flag(flags::CF | flags::OF | flags::AF, false);
        self.set_szp16(r);
    }

    // -----------------------------------------------------------------
    // Sequences
    // -----------------------------------------------------------------

    /// The RESET sequence.
    ///
    /// `CS:IP` becomes `ffff:0000`, every other segment register zero, and the
    /// flags only their hard-wired bits. The general registers are not
    /// specified by Intel and are left alone, which is what hardware does — a
    /// cold [`Device::reset`](crate::core::device::Device::reset) zeroes them
    /// separately, because determinism is a first-class mode.
    fn reset_sequence(&mut self) {
        self.state.reset_pending = false;
        self.state.halted = false;
        self.state.int_shadow = false;
        let regs = &mut self.state.regs;
        regs.cs = 0xffff;
        regs.ip = 0;
        regs.ds = 0;
        regs.es = 0;
        regs.ss = 0;
        regs.flags = flags::RESERVED_SET;
        self.state.queue.flush();
        self.charge(RESET_CLOCKS);
    }

    /// Take an interrupt of the given type.
    ///
    /// The order is the one the hardware traces show, and it is not the order
    /// the manual's prose suggests: **the vector is read first**, before
    /// anything is pushed. Then flags, then `CS`, then the return `IP` — and
    /// `IF` and `TF` are cleared between the flags push and the `CS` push, so
    /// the saved flags still have them.
    fn service(&mut self, vector: u8) {
        let base = u16::from(vector) << 2;
        let target_ip = self.read16_seg(0, base);
        let target_cs = self.read16_seg(0, base.wrapping_add(2));

        let saved = self.state.regs.flags;
        self.push_word(saved);
        self.set_flag(flags::IF | flags::TF, false);
        self.push_word(self.state.regs.cs);
        self.push_word(self.state.regs.ip);

        self.state.regs.cs = target_cs;
        self.state.regs.ip = target_ip;
        self.state.queue.flush();
        self.state.halted = false;
    }

    /// Push a word: the stack pointer moves first, then the write happens.
    ///
    /// That order is why `PUSH SP` stores `SP - 2` on an 8086 and the value
    /// before the decrement on a 286.
    fn push_word(&mut self, value: u16) {
        let sp = self.state.regs.sp.wrapping_sub(2);
        self.state.regs.sp = sp;
        self.write16(seg::SS, sp, value);
    }

    fn pop_word(&mut self) -> u16 {
        let sp = self.state.regs.sp;
        let value = self.read16(seg::SS, sp);
        self.state.regs.sp = sp.wrapping_add(2);
        value
    }

    // -----------------------------------------------------------------
    // Decode
    // -----------------------------------------------------------------

    fn instruction(&mut self) {
        self.start_ip = self.state.regs.ip;
        self.fill_queue();
        let fields = isa::decode_stream(&mut || Some(self.fetch_byte()));
        self.prepare_ea(&fields);
        self.charge(fields.insn.op.clocks());
        self.execute(&fields);
    }

    /// Compute the memory operand's address once, before execution.
    ///
    /// The 8086 computes it in microcode and the cost depends on how many
    /// terms are summed, which is why the charge happens here rather than at
    /// the access.
    fn prepare_ea(&mut self, f: &Fields) {
        self.ea = None;
        let insn = f.insn;
        if let Some(m) = f.modrm
            && !m.is_register()
            && [insn.dst, insn.src]
                .iter()
                .any(|a| matches!(a, Arg::Eb | Arg::Ev | Arg::M | Arg::Mp))
        {
            let regs = &self.state.regs;
            let terms = match m.rm {
                0 => regs.bx.wrapping_add(regs.si),
                1 => regs.bx.wrapping_add(regs.di),
                2 => regs.bp.wrapping_add(regs.si),
                3 => regs.bp.wrapping_add(regs.di),
                4 => regs.si,
                5 => regs.di,
                6 if m.md == 0 => 0,
                6 => regs.bp,
                _ => regs.bx,
            };
            let offset = if m.md == 0 && m.rm == 6 {
                f.disp
            } else {
                terms.wrapping_add(f.disp)
            };
            self.ea = Some((f.segment(m.default_segment()), offset));
            self.charge(isa::ea_clocks(m.md, m.rm, f.seg_override.is_some()));
        } else if [insn.dst, insn.src]
            .iter()
            .any(|a| matches!(a, Arg::Ob | Arg::Ov))
        {
            // The direct-offset moves carry their address in the immediate
            // field and have no ModRM byte at all.
            self.ea = Some((f.segment(seg::DS), f.imm16()));
        }
    }

    fn ea(&self) -> (u8, u16) {
        self.ea.unwrap_or((seg::DS, 0))
    }

    // -----------------------------------------------------------------
    // Operands
    // -----------------------------------------------------------------

    fn read_arg8(&mut self, f: &Fields, arg: Arg) -> u8 {
        match arg {
            Arg::Eb => match f.modrm {
                Some(m) if m.is_register() => self.state.regs.byte(m.rm),
                _ => {
                    let (sr, off) = self.ea();
                    self.read8(sr, off)
                }
            },
            Arg::Gb => self.state.regs.byte(f.modrm.map_or(0, |m| m.reg)),
            Arg::Ib => f.imm as u8,
            Arg::Rb => self.state.regs.byte(f.opcode & 7),
            Arg::Al => self.state.regs.ax as u8,
            Arg::Cl => self.state.regs.cx as u8,
            Arg::One => 1,
            Arg::Ob => {
                let (sr, off) = self.ea();
                self.read8(sr, off)
            }
            // String operands are driven by `string_step`, which knows which
            // pointer moves; nothing else should ask for one.
            _ => 0,
        }
    }

    fn write_arg8(&mut self, f: &Fields, arg: Arg, value: u8) {
        match arg {
            Arg::Eb => match f.modrm {
                Some(m) if m.is_register() => self.state.regs.set_byte(m.rm, value),
                _ => {
                    let (sr, off) = self.ea();
                    self.write8(sr, off, value);
                }
            },
            Arg::Gb => self
                .state
                .regs
                .set_byte(f.modrm.map_or(0, |m| m.reg), value),
            Arg::Rb => self.state.regs.set_byte(f.opcode & 7, value),
            Arg::Al => self.state.regs.set_byte(0, value),
            Arg::Cl => self.state.regs.set_byte(1, value),
            Arg::Ob => {
                let (sr, off) = self.ea();
                self.write8(sr, off, value);
            }
            _ => {}
        }
    }

    fn read_arg16(&mut self, f: &Fields, arg: Arg) -> u16 {
        match arg {
            Arg::Ev => match f.modrm {
                Some(m) if m.is_register() => self.state.regs.word(m.rm),
                _ => {
                    let (sr, off) = self.ea();
                    self.read16(sr, off)
                }
            },
            Arg::Gv => self.state.regs.word(f.modrm.map_or(0, |m| m.reg)),
            // Only the low two bits of `reg` are decoded, which is why `8C`
            // and `8E` accept a `reg` of 4-7 and alias down onto ES-DS.
            Arg::Sw => self.state.regs.segment(f.modrm.map_or(0, |m| m.reg) & 3),
            Arg::Sr => self.state.regs.segment((f.opcode >> 3) & 3),
            Arg::Iv | Arg::Ibs => f.imm16(),
            Arg::Rv => self.state.regs.word(f.opcode & 7),
            Arg::Ax => self.state.regs.ax,
            Arg::Dx => self.state.regs.dx,
            Arg::M => self.ea().1,
            Arg::Ov => {
                let (sr, off) = self.ea();
                self.read16(sr, off)
            }
            _ => 0,
        }
    }

    fn write_arg16(&mut self, f: &Fields, arg: Arg, value: u16) {
        match arg {
            Arg::Ev => match f.modrm {
                Some(m) if m.is_register() => self.state.regs.set_word(m.rm, value),
                _ => {
                    let (sr, off) = self.ea();
                    self.write16(sr, off, value);
                }
            },
            Arg::Gv => self
                .state
                .regs
                .set_word(f.modrm.map_or(0, |m| m.reg), value),
            Arg::Sw => {
                let sr = f.modrm.map_or(0, |m| m.reg) & 3;
                self.load_segment(sr, value);
            }
            Arg::Sr => {
                let sr = (f.opcode >> 3) & 3;
                self.load_segment(sr, value);
            }
            Arg::Rv => self.state.regs.set_word(f.opcode & 7, value),
            Arg::Ax => self.state.regs.ax = value,
            Arg::Dx => self.state.regs.dx = value,
            Arg::Ov => {
                let (sr, off) = self.ea();
                self.write16(sr, off, value);
            }
            _ => {}
        }
    }

    /// Load a segment register, opening the interrupt shadow for `SS`.
    ///
    /// Every write to `SS` inhibits interrupts for one instruction, whichever
    /// encoding did it, so that `MOV SS,ax` / `MOV SP,bx` cannot be split by
    /// an interrupt landing on a half-changed stack. Nothing else on an 8086
    /// makes a two-instruction sequence atomic.
    fn load_segment(&mut self, sr: u8, value: u16) {
        self.state.regs.set_segment(sr, value);
        if sr == seg::SS {
            self.state.int_shadow = true;
        }
    }

    /// The operand width this encoding fixes, in bytes.
    fn width(f: &Fields) -> u8 {
        f.insn.width_bytes().unwrap_or(2)
    }

    // -----------------------------------------------------------------
    // Execution
    // -----------------------------------------------------------------

    #[allow(clippy::too_many_lines)]
    fn execute(&mut self, f: &Fields) {
        let insn = f.insn;
        match insn.op {
            Op::ADD | Op::ADC | Op::SUB | Op::SBB | Op::CMP | Op::AND | Op::OR | Op::XOR => {
                self.arith(f);
            }
            Op::TEST => self.test(f),
            Op::INC | Op::DEC => self.inc_dec(f),
            Op::NOT => {
                if Self::width(f) == 1 {
                    let a = self.read_arg8(f, insn.dst);
                    self.write_arg8(f, insn.dst, !a);
                } else {
                    let a = self.read_arg16(f, insn.dst);
                    self.write_arg16(f, insn.dst, !a);
                }
            }
            Op::NEG => {
                if Self::width(f) == 1 {
                    let a = self.read_arg8(f, insn.dst);
                    let r = self.sub8(0, a, false);
                    self.write_arg8(f, insn.dst, r);
                } else {
                    let a = self.read_arg16(f, insn.dst);
                    let r = self.sub16(0, a, false);
                    self.write_arg16(f, insn.dst, r);
                }
            }
            Op::MOV => {
                if Self::width(f) == 1 {
                    let v = self.read_arg8(f, insn.src);
                    self.write_arg8(f, insn.dst, v);
                } else {
                    let v = self.read_arg16(f, insn.src);
                    self.write_arg16(f, insn.dst, v);
                }
            }
            Op::XCHG => self.xchg(f),
            Op::LEA => {
                let offset = self.ea().1;
                self.write_arg16(f, insn.dst, offset);
            }
            Op::LES | Op::LDS => {
                let (sr, off) = self.ea();
                let value = self.read16(sr, off);
                let segment = self.read16(sr, off.wrapping_add(2));
                self.write_arg16(f, insn.dst, value);
                let target = if insn.op == Op::LES { seg::ES } else { seg::DS };
                self.load_segment(target, segment);
            }
            Op::PUSH => {
                // The pointer moves before the operand is read, which is what
                // makes `PUSH SP` push the decremented value.
                let sp = self.state.regs.sp.wrapping_sub(2);
                self.state.regs.sp = sp;
                let value = self.read_arg16(f, insn.dst);
                self.write16(seg::SS, sp, value);
            }
            Op::POP => {
                let value = self.pop_word();
                self.write_arg16(f, insn.dst, value);
            }
            Op::PUSHF => {
                let value = self.state.regs.flags;
                self.push_word(value);
            }
            Op::POPF => {
                let value = self.pop_word();
                self.state.regs.flags = Regs::normalise_flags(value);
            }
            Op::SAHF => {
                let ah = (self.state.regs.ax >> 8) & 0xff;
                let kept = self.state.regs.flags & !flags::LOW_BYTE;
                self.state.regs.flags = Regs::normalise_flags(kept | (ah & flags::LOW_BYTE));
            }
            Op::LAHF => {
                let low = (self.state.regs.flags & 0xff) as u8;
                self.state.regs.set_byte(4, low);
            }
            Op::CBW => {
                let al = self.state.regs.ax as u8;
                self.state.regs.ax = i16::from(al as i8) as u16;
            }
            Op::CWD => {
                self.state.regs.dx = if self.state.regs.ax & 0x8000 != 0 {
                    0xffff
                } else {
                    0
                };
            }
            Op::ROL | Op::ROR | Op::RCL | Op::RCR | Op::SHL | Op::SHR | Op::SAR | Op::SETMO => {
                self.shift(f);
            }
            Op::MUL | Op::IMUL => self.multiply(f),
            Op::DIV | Op::IDIV => self.divide(f),
            Op::AAM => self.aam(f),
            Op::AAD => self.aad(f),
            Op::DAA => self.daa(),
            Op::DAS => self.das(),
            Op::AAA => self.aaa(),
            Op::AAS => self.aas(),
            Op::CLC => self.set_flag(flags::CF, false),
            Op::STC => self.set_flag(flags::CF, true),
            Op::CMC => {
                let cf = self.flag(flags::CF);
                self.set_flag(flags::CF, !cf);
            }
            Op::CLD => self.set_flag(flags::DF, false),
            Op::STD => self.set_flag(flags::DF, true),
            Op::CLI => self.set_flag(flags::IF, false),
            Op::STI => {
                self.set_flag(flags::IF, true);
                // `STI` shares the one-instruction shadow with `MOV SS`: the
                // instruction after it runs before any interrupt can, which is
                // what makes `sti` / `hlt` race-free.
                self.state.int_shadow = true;
            }
            Op::NOP | Op::WAIT | Op::LOCK | Op::REP | Op::REPNE | Op::SEG => {}
            Op::HLT => self.state.halted = true,
            Op::ESC => {
                // The 8088 computes the address and performs the read so the
                // coprocessor can latch the operand off the bus; the value is
                // of no use to the CPU itself.
                if matches!(f.modrm, Some(m) if !m.is_register()) {
                    let (sr, off) = self.ea();
                    let _ = self.read16(sr, off);
                }
            }
            Op::SALC => {
                let value = if self.flag(flags::CF) { 0xff } else { 0x00 };
                self.state.regs.set_byte(0, value);
            }
            Op::XLAT => {
                let sr = f.segment(seg::DS);
                let al = self.state.regs.ax as u8;
                let offset = self.state.regs.bx.wrapping_add(u16::from(al));
                let value = self.read8(sr, offset);
                self.state.regs.set_byte(0, value);
            }
            Op::IN => {
                let port = self.port(f, insn.src);
                if insn.dst == Arg::Al {
                    let value = self.io_read8(port);
                    self.state.regs.set_byte(0, value);
                } else {
                    let lo = self.io_read8(port);
                    let hi = self.io_read8(port.wrapping_add(1));
                    self.state.regs.ax = u16::from(lo) | (u16::from(hi) << 8);
                }
            }
            Op::OUT => {
                let port = self.port(f, insn.dst);
                if insn.src == Arg::Al {
                    let value = self.state.regs.ax as u8;
                    self.io_write8(port, value);
                } else {
                    let value = self.state.regs.ax;
                    self.io_write8(port, value as u8);
                    self.io_write8(port.wrapping_add(1), (value >> 8) as u8);
                }
            }
            Op::CALL => {
                let target = match insn.dst {
                    Arg::Jv | Arg::Jb => self.state.regs.ip.wrapping_add(f.imm16()),
                    _ => self.read_arg16(f, insn.dst),
                };
                let ret = self.state.regs.ip;
                self.push_word(ret);
                self.state.regs.ip = target;
                self.state.queue.flush();
            }
            Op::CALLF => {
                let (offset, segment) = self.far_target(f);
                let cs = self.state.regs.cs;
                let ip = self.state.regs.ip;
                self.push_word(cs);
                self.push_word(ip);
                self.state.regs.cs = segment;
                self.state.regs.ip = offset;
                self.state.queue.flush();
            }
            Op::JMP => {
                let target = match insn.dst {
                    Arg::Jv | Arg::Jb => self.state.regs.ip.wrapping_add(f.imm16()),
                    _ => self.read_arg16(f, insn.dst),
                };
                self.state.regs.ip = target;
                self.state.queue.flush();
            }
            Op::JMPF => {
                let (offset, segment) = self.far_target(f);
                self.state.regs.cs = segment;
                self.state.regs.ip = offset;
                self.state.queue.flush();
            }
            Op::RET => {
                let ip = self.pop_word();
                self.state.regs.ip = ip;
                if insn.dst == Arg::Iv {
                    self.state.regs.sp = self.state.regs.sp.wrapping_add(f.imm16());
                }
                self.state.queue.flush();
            }
            Op::RETF => {
                let ip = self.pop_word();
                let cs = self.pop_word();
                self.state.regs.ip = ip;
                self.state.regs.cs = cs;
                if insn.dst == Arg::Iv {
                    self.state.regs.sp = self.state.regs.sp.wrapping_add(f.imm16());
                }
                self.state.queue.flush();
            }
            Op::IRET => {
                let ip = self.pop_word();
                let cs = self.pop_word();
                let fl = self.pop_word();
                self.state.regs.ip = ip;
                self.state.regs.cs = cs;
                self.state.regs.flags = Regs::normalise_flags(fl);
                self.state.queue.flush();
            }
            Op::INT => {
                let vector = f.imm as u8;
                self.service(vector);
            }
            Op::INT3 => self.service(VEC_BREAKPOINT),
            Op::INTO => {
                if self.flag(flags::OF) {
                    self.service(VEC_OVERFLOW);
                }
            }
            Op::LOOP | Op::LOOPE | Op::LOOPNE => {
                let cx = self.state.regs.cx.wrapping_sub(1);
                self.state.regs.cx = cx;
                let zf = self.flag(flags::ZF);
                let take = cx != 0
                    && match insn.op {
                        Op::LOOPE => zf,
                        Op::LOOPNE => !zf,
                        _ => true,
                    };
                if take {
                    self.state.regs.ip = self.state.regs.ip.wrapping_add(f.imm16());
                    self.state.queue.flush();
                }
            }
            Op::JCXZ => {
                if self.state.regs.cx == 0 {
                    self.state.regs.ip = self.state.regs.ip.wrapping_add(f.imm16());
                    self.state.queue.flush();
                }
            }
            op if op.is_conditional_jump() => {
                if self.condition(op) {
                    self.state.regs.ip = self.state.regs.ip.wrapping_add(f.imm16());
                    self.state.queue.flush();
                }
            }
            op if op.is_string() => self.string(f),
            // Every operation the table can produce is handled above; this arm
            // exists because `Op` is `#[non_exhaustive]` to the compiler.
            _ => {}
        }
    }

    /// The I/O port an `IN` or `OUT` names.
    fn port(&self, f: &Fields, arg: Arg) -> u16 {
        match arg {
            Arg::Dx => self.state.regs.dx,
            _ => u16::from(f.imm as u8),
        }
    }

    /// The `offset:segment` pair a far transfer jumps to.
    fn far_target(&mut self, f: &Fields) -> (u16, u16) {
        if f.insn.dst == Arg::Ap {
            (f.imm16(), f.imm_seg())
        } else {
            let (sr, off) = self.ea();
            let offset = self.read16(sr, off);
            let segment = self.read16(sr, off.wrapping_add(2));
            (offset, segment)
        }
    }

    fn condition(&self, op: Op) -> bool {
        let cf = self.flag(flags::CF);
        let zf = self.flag(flags::ZF);
        let sf = self.flag(flags::SF);
        let of = self.flag(flags::OF);
        let pf = self.flag(flags::PF);
        match op {
            Op::JO => of,
            Op::JNO => !of,
            Op::JB => cf,
            Op::JNB => !cf,
            Op::JZ => zf,
            Op::JNZ => !zf,
            Op::JBE => cf || zf,
            Op::JA => !cf && !zf,
            Op::JS => sf,
            Op::JNS => !sf,
            Op::JP => pf,
            Op::JNP => !pf,
            Op::JL => sf != of,
            Op::JGE => sf == of,
            Op::JLE => zf || (sf != of),
            _ => !zf && (sf == of),
        }
    }

    fn arith(&mut self, f: &Fields) {
        let insn = f.insn;
        let carry = self.flag(flags::CF);
        if Self::width(f) == 1 {
            let a = self.read_arg8(f, insn.dst);
            let b = self.read_arg8(f, insn.src);
            let r = match insn.op {
                Op::ADD => self.add8(a, b, false),
                Op::ADC => self.add8(a, b, carry),
                Op::SUB | Op::CMP => self.sub8(a, b, false),
                Op::SBB => self.sub8(a, b, carry),
                Op::AND => {
                    let r = a & b;
                    self.logic_flags8(r);
                    r
                }
                Op::OR => {
                    let r = a | b;
                    self.logic_flags8(r);
                    r
                }
                _ => {
                    let r = a ^ b;
                    self.logic_flags8(r);
                    r
                }
            };
            if insn.op != Op::CMP {
                self.write_arg8(f, insn.dst, r);
            }
        } else {
            let a = self.read_arg16(f, insn.dst);
            let b = self.read_arg16(f, insn.src);
            let r = match insn.op {
                Op::ADD => self.add16(a, b, false),
                Op::ADC => self.add16(a, b, carry),
                Op::SUB | Op::CMP => self.sub16(a, b, false),
                Op::SBB => self.sub16(a, b, carry),
                Op::AND => {
                    let r = a & b;
                    self.logic_flags16(r);
                    r
                }
                Op::OR => {
                    let r = a | b;
                    self.logic_flags16(r);
                    r
                }
                _ => {
                    let r = a ^ b;
                    self.logic_flags16(r);
                    r
                }
            };
            if insn.op != Op::CMP {
                self.write_arg16(f, insn.dst, r);
            }
        }
    }

    fn test(&mut self, f: &Fields) {
        let insn = f.insn;
        if Self::width(f) == 1 {
            let a = self.read_arg8(f, insn.dst);
            let b = self.read_arg8(f, insn.src);
            self.logic_flags8(a & b);
        } else {
            let a = self.read_arg16(f, insn.dst);
            let b = self.read_arg16(f, insn.src);
            self.logic_flags16(a & b);
        }
    }

    /// `INC` and `DEC` are an add and a subtract that leave carry alone.
    fn inc_dec(&mut self, f: &Fields) {
        let insn = f.insn;
        let carry = self.flag(flags::CF);
        if Self::width(f) == 1 {
            let a = self.read_arg8(f, insn.dst);
            let r = if insn.op == Op::INC {
                self.add8(a, 1, false)
            } else {
                self.sub8(a, 1, false)
            };
            self.write_arg8(f, insn.dst, r);
        } else {
            let a = self.read_arg16(f, insn.dst);
            let r = if insn.op == Op::INC {
                self.add16(a, 1, false)
            } else {
                self.sub16(a, 1, false)
            };
            self.write_arg16(f, insn.dst, r);
        }
        self.set_flag(flags::CF, carry);
    }

    fn xchg(&mut self, f: &Fields) {
        let insn = f.insn;
        if Self::width(f) == 1 {
            let a = self.read_arg8(f, insn.dst);
            let b = self.read_arg8(f, insn.src);
            self.write_arg8(f, insn.dst, b);
            self.write_arg8(f, insn.src, a);
        } else {
            let a = self.read_arg16(f, insn.dst);
            let b = self.read_arg16(f, insn.src);
            self.write_arg16(f, insn.dst, b);
            self.write_arg16(f, insn.src, a);
        }
    }

    // -----------------------------------------------------------------
    // Shifts and rotates
    // -----------------------------------------------------------------

    fn shift(&mut self, f: &Fields) {
        let insn = f.insn;
        // The 8086 uses the whole of `CL`: masking the count to five bits is
        // 80186 behaviour, and the corpus masks `CL` to six bits precisely to
        // catch a core that made that mistake.
        let count = if insn.src == Arg::One {
            1
        } else {
            self.state.regs.cx as u8
        };
        // The write-back happens even when the count is zero and nothing
        // changed: the 8088 still drives the operand back onto the bus, which
        // a memory-mapped device would see. The flags, by contrast, really are
        // left alone.
        if Self::width(f) == 1 {
            let a = self.read_arg8(f, insn.dst);
            let r = self.shift8(insn.op, a, count);
            self.write_arg8(f, insn.dst, r);
        } else {
            let a = self.read_arg16(f, insn.dst);
            let r = self.shift16(insn.op, a, count);
            self.write_arg16(f, insn.dst, r);
        }
    }

    fn shift8(&mut self, op: Op, value: u8, count: u8) -> u8 {
        if count == 0 {
            return value;
        }
        if op == Op::SETMO {
            // Undocumented, and the corpus says every flag is affected: the
            // result is all ones and the flags follow it as a logical result
            // would, carry and overflow cleared.
            self.logic_flags8(0xff);
            return 0xff;
        }
        let mut v = value;
        let mut cf = self.flag(flags::CF);
        let mut of = self.flag(flags::OF);
        for _ in 0..count {
            match op {
                Op::ROL => {
                    cf = v & 0x80 != 0;
                    v = (v << 1) | u8::from(cf);
                    of = (v & 0x80 != 0) != cf;
                }
                Op::ROR => {
                    cf = v & 1 != 0;
                    v = (v >> 1) | (u8::from(cf) << 7);
                    of = ((v >> 7) ^ (v >> 6)) & 1 != 0;
                }
                Op::RCL => {
                    let carry_in = cf;
                    cf = v & 0x80 != 0;
                    v = (v << 1) | u8::from(carry_in);
                    of = (v & 0x80 != 0) != cf;
                }
                Op::RCR => {
                    let carry_in = cf;
                    of = (v & 0x80 != 0) != carry_in;
                    cf = v & 1 != 0;
                    v = (v >> 1) | (u8::from(carry_in) << 7);
                }
                Op::SHL => {
                    cf = v & 0x80 != 0;
                    v <<= 1;
                    of = (v & 0x80 != 0) != cf;
                }
                Op::SHR => {
                    of = v & 0x80 != 0;
                    cf = v & 1 != 0;
                    v >>= 1;
                }
                _ => {
                    of = false;
                    cf = v & 1 != 0;
                    v = ((v as i8) >> 1) as u8;
                }
            }
        }
        self.set_flag(flags::CF, cf);
        self.set_flag(flags::OF, of);
        if matches!(op, Op::SHL | Op::SHR | Op::SAR) {
            self.set_szp8(v);
            // `AF` is documented as undefined for the shifts. On this part a
            // left shift leaves bit 4 of the result there — the microcode is
            // an `ADD dst,dst`, so the auxiliary carry is the real one — and
            // the right shifts clear it.
            self.set_flag(flags::AF, op == Op::SHL && v & 0x10 != 0);
        }
        v
    }

    fn shift16(&mut self, op: Op, value: u16, count: u8) -> u16 {
        if count == 0 {
            return value;
        }
        if op == Op::SETMO {
            self.logic_flags16(0xffff);
            return 0xffff;
        }
        let mut v = value;
        let mut cf = self.flag(flags::CF);
        let mut of = self.flag(flags::OF);
        for _ in 0..count {
            match op {
                Op::ROL => {
                    cf = v & 0x8000 != 0;
                    v = (v << 1) | u16::from(cf);
                    of = (v & 0x8000 != 0) != cf;
                }
                Op::ROR => {
                    cf = v & 1 != 0;
                    v = (v >> 1) | (u16::from(cf) << 15);
                    of = ((v >> 15) ^ (v >> 14)) & 1 != 0;
                }
                Op::RCL => {
                    let carry_in = cf;
                    cf = v & 0x8000 != 0;
                    v = (v << 1) | u16::from(carry_in);
                    of = (v & 0x8000 != 0) != cf;
                }
                Op::RCR => {
                    let carry_in = cf;
                    of = (v & 0x8000 != 0) != carry_in;
                    cf = v & 1 != 0;
                    v = (v >> 1) | (u16::from(carry_in) << 15);
                }
                Op::SHL => {
                    cf = v & 0x8000 != 0;
                    v <<= 1;
                    of = (v & 0x8000 != 0) != cf;
                }
                Op::SHR => {
                    of = v & 0x8000 != 0;
                    cf = v & 1 != 0;
                    v >>= 1;
                }
                _ => {
                    of = false;
                    cf = v & 1 != 0;
                    v = ((v as i16) >> 1) as u16;
                }
            }
        }
        self.set_flag(flags::CF, cf);
        self.set_flag(flags::OF, of);
        if matches!(op, Op::SHL | Op::SHR | Op::SAR) {
            self.set_szp16(v);
            self.set_flag(flags::AF, op == Op::SHL && v & 0x10 != 0);
        }
        v
    }

    // -----------------------------------------------------------------
    // Multiply and divide
    // -----------------------------------------------------------------

    /// The flags a multiply leaves.
    ///
    /// Intel documents `SF`, `ZF`, `AF` and `PF` as undefined here. The
    /// hardware sets them from the **high half** of the product, which is what
    /// the microcode's last step operates on: `ZF` if that half is zero, `SF`
    /// from its top bit, `PF` from its low byte, and `AF` cleared. `CF` and
    /// `OF` are the documented ones, and the caller supplies them because
    /// `MUL` and `IMUL` disagree about what "the result does not fit" means.
    fn mul_flags(&mut self, high: u16, overflow: bool) {
        self.set_flag(flags::ZF, high == 0);
        self.set_flag(flags::SF, high & 0x8000 != 0);
        self.set_flag(flags::PF, Self::parity(high as u8));
        self.set_flag(flags::AF, false);
        self.set_flag(flags::CF | flags::OF, overflow);
    }

    fn multiply(&mut self, f: &Fields) {
        let insn = f.insn;
        let signed = insn.op == Op::IMUL;
        if Self::width(f) == 1 {
            let src = self.read_arg8(f, insn.dst);
            let al = self.state.regs.ax as u8;
            let product = if signed {
                (i16::from(al as i8) * i16::from(src as i8)) as u16
            } else {
                u16::from(al) * u16::from(src)
            };
            self.state.regs.ax = product;
            let high = (product >> 8) as u8;
            let overflow = if signed {
                // Signed overflow means the product does not fit back in AL
                // with its sign intact.
                high != if product & 0x80 != 0 { 0xff } else { 0x00 }
            } else {
                high != 0
            };
            // The high half of a byte multiply is `AH`, so the same rule
            // reads its sign out of bit 7 rather than bit 15.
            self.mul_flags(u16::from(high) << 8, overflow);
            self.set_flag(flags::PF, Self::parity(high));
        } else {
            let src = self.read_arg16(f, insn.dst);
            let ax = self.state.regs.ax;
            let product = if signed {
                (i32::from(ax as i16) * i32::from(src as i16)) as u32
            } else {
                u32::from(ax) * u32::from(src)
            };
            self.state.regs.ax = product as u16;
            self.state.regs.dx = (product >> 16) as u16;
            let high = (product >> 16) as u16;
            let overflow = if signed {
                high != if product & 0x8000 != 0 {
                    0xffff
                } else {
                    0x0000
                }
            } else {
                high != 0
            };
            self.mul_flags(high, overflow);
        }
    }

    fn divide(&mut self, f: &Fields) {
        let insn = f.insn;
        let signed = insn.op == Op::IDIV;
        // A `REP` prefix in front of `IDIV` inverts the sign of the quotient
        // on this part. It is not a documented feature and not useful, but it
        // is deterministic, the corpus exercises it deliberately, and software
        // that stumbles on it deserves to be emulated correctly.
        let negate = signed && f.rep.is_some();
        let bits = if Self::width(f) == 1 { 8 } else { 16 };

        let (source, dividend) = if bits == 8 {
            (
                u32::from(self.read_arg8(f, insn.dst)),
                u32::from(self.state.regs.ax),
            )
        } else {
            let src = u32::from(self.read_arg16(f, insn.dst));
            let dividend = (u32::from(self.state.regs.dx) << 16) | u32::from(self.state.regs.ax);
            (src, dividend)
        };

        // The hardware divides magnitudes and applies the signs afterwards;
        // for an unsigned divide the magnitudes are the operands.
        let (magnitude, divisor_magnitude) = if signed {
            (
                Self::sign_extend(dividend, bits * 2).unsigned_abs(),
                Self::sign_extend(source, bits).unsigned_abs(),
            )
        } else {
            (u64::from(dividend), u64::from(source))
        };

        // Run the loop even when the result will not fit: the flags it leaves
        // are visible either way, because a divide error pushes them.
        let (quotient_magnitude, remainder_magnitude) =
            self.cord(magnitude, divisor_magnitude, bits);

        // Whether the result is representable is decided separately: the loop
        // produces exactly `bits` bits of quotient whatever the true one would
        // have been, so it cannot report an overflow itself.
        let mask = (1u64 << bits) - 1;
        let (quotient, remainder, fault) = if divisor_magnitude == 0 {
            (0, 0, true)
        } else if signed {
            let n = Self::sign_extend(dividend, bits * 2);
            let d = Self::sign_extend(source, bits);
            let mut q = n / d;
            if negate {
                q = q.wrapping_neg();
            }
            let limit = 1i64 << (bits - 1);
            (
                (q as u64) & mask,
                ((n % d) as u64) & mask,
                !(-limit..limit).contains(&q),
            )
        } else {
            let q = u64::from(dividend) / divisor_magnitude;
            // The loop and the host's divide have to agree whenever the result
            // fits; if they ever do not, the loop is the bug.
            debug_assert!(q > mask || q == quotient_magnitude);
            (
                quotient_magnitude & mask,
                remainder_magnitude & mask,
                q > mask,
            )
        };
        if fault {
            self.divide_error();
            return;
        }

        // Measured, and exact on every corpus vector that completes: the carry
        // comes out as the complement of the quotient's top bit.
        self.set_flag(flags::CF, quotient & (1 << (bits - 1)) == 0);

        if bits == 8 {
            self.state.regs.ax = ((quotient as u16) & 0xff) | (((remainder as u16) & 0xff) << 8);
        } else {
            self.state.regs.ax = quotient as u16;
            self.state.regs.dx = remainder as u16;
        }
    }

    /// Sign-extend the low `bits` of `value`.
    const fn sign_extend(value: u32, bits: u32) -> i64 {
        let shift = 64 - bits;
        ((value as i64) << shift) >> shift
    }

    /// The restoring-division loop an 8086 runs in microcode, and the flag
    /// residue it leaves.
    ///
    /// Intel describes `DIV` as a sequence of shifts and conditional
    /// subtractions, and that is what this is: the running remainder is
    /// shifted up one bit at a time, the divisor is subtracted on trial, and
    /// the quotient bit is the complement of the borrow. Running the loop
    /// rather than calling the host's divide is what brings the officially
    /// undefined flag results near the hardware's — what an 8088 leaves behind
    /// is the last trial subtraction's flags. "Near", not "equal":
    /// `conformance::KNOWN_FAILURES` records what is still missing.
    fn cord(&mut self, dividend: u64, divisor: u64, bits: u32) -> (u64, u64) {
        let mask = (1u64 << bits) - 1;
        let top = 1u64 << (bits - 1);
        let mut remainder = (dividend >> bits) & mask;
        let mut quotient = dividend & mask;
        for _ in 0..bits {
            let carried = (quotient >> (bits - 1)) & 1;
            quotient = (quotient << 1) & mask;
            let overflowed = remainder & top != 0;
            let shifted = ((remainder << 1) | carried) & mask;
            let difference = shifted.wrapping_sub(divisor) & mask;
            let borrow = shifted < divisor;
            self.set_flag(flags::CF, borrow);
            self.set_flag(flags::AF, (shifted ^ divisor ^ difference) & 0x10 != 0);
            self.set_flag(
                flags::OF,
                (shifted ^ divisor) & (shifted ^ difference) & top != 0,
            );
            self.set_flag(flags::SF, difference & top != 0);
            self.set_flag(flags::ZF, difference == 0);
            self.set_flag(flags::PF, Self::parity(difference as u8));
            if overflowed || !borrow {
                remainder = difference;
                quotient |= 1;
            } else {
                remainder = shifted;
            }
        }
        (quotient, remainder)
    }

    /// Take a type-0 interrupt.
    ///
    /// On an 8088 the return address pushed is the address of the *next*
    /// instruction, not of the faulting one — a detail later parts changed and
    /// generic x86 emulators habitually get wrong. `IP` has already been
    /// advanced past the instruction by the time this runs, so pushing it is
    /// exactly right.
    fn divide_error(&mut self) {
        self.service(VEC_DIVIDE);
    }

    // -----------------------------------------------------------------
    // The decimal adjustments
    // -----------------------------------------------------------------

    fn aam(&mut self, f: &Fields) {
        let base = f.imm as u8;
        if base == 0 {
            // The microcode enters the divide, produces nothing, and faults.
            // What it leaves behind is the flag set of a zero result, on all
            // 47 corpus vectors that reach it.
            self.set_flag(flags::CF | flags::OF | flags::AF, false);
            self.set_szp8(0);
            self.divide_error();
            return;
        }
        let al = self.state.regs.ax as u8;
        let quotient = al / base;
        let remainder = al % base;
        self.state.regs.ax = u16::from(remainder) | (u16::from(quotient) << 8);
        self.set_szp8(remainder);
        self.set_flag(flags::CF | flags::OF | flags::AF, false);
    }

    fn aad(&mut self, f: &Fields) {
        let base = f.imm as u8;
        let al = self.state.regs.ax as u8;
        let ah = (self.state.regs.ax >> 8) as u8;
        let product = ah.wrapping_mul(base);
        // The adjustment really is an addition, so it sets carry and the
        // auxiliary carry even though Intel calls them undefined.
        let r = self.add8(product, al, false);
        self.state.regs.ax = u16::from(r);
    }

    fn daa(&mut self) {
        self.decimal_adjust(false);
    }

    fn das(&mut self) {
        self.decimal_adjust(true);
    }

    /// `DAA` and `DAS`, which differ only in the sign of the correction.
    ///
    /// Two things here are not what Intel's later pseudocode says, and both
    /// are measured on all 20 000 corpus vectors:
    ///
    /// - The two corrections are **one** arithmetic operation, not two. The
    ///   value added or subtracted is `0x00`, `0x06`, `0x60` or `0x66`, and
    ///   the officially undefined overflow flag is that single operation's.
    /// - The high correction's threshold **moves with the auxiliary carry**:
    ///   `AL > 0x9f` when `AF` is set, `AL > 0x99` when it is not. So `daa` on
    ///   `AL = 0x9a` with `AF` set corrects only the low digit, where the
    ///   published algorithm would correct both. Sixty-odd vectors per opcode
    ///   turn on it.
    fn decimal_adjust(&mut self, subtract: bool) {
        let al = self.state.regs.ax as u8;
        let auxiliary = self.flag(flags::AF);
        let low = (al & 0x0f) > 9 || auxiliary;
        let threshold = if auxiliary { 0x9f } else { 0x99 };
        let high = self.flag(flags::CF) || al > threshold;
        let correction = if low { 0x06 } else { 0x00 } + if high { 0x60 } else { 0x00 };
        let adjusted = if subtract {
            self.sub8(al, correction, false)
        } else {
            self.add8(al, correction, false)
        };
        self.state.regs.set_byte(0, adjusted);
        // The carry and the auxiliary carry are the *conditions*, not the
        // arithmetic's — a correction that happens not to carry still sets
        // them.
        self.set_flag(flags::CF, high);
        self.set_flag(flags::AF, low);
    }

    fn aaa(&mut self) {
        self.ascii_adjust(false);
    }

    fn aas(&mut self) {
        self.ascii_adjust(true);
    }

    /// `AAA` and `AAS`, which likewise differ only in sign.
    ///
    /// Intel calls the sign, zero, parity and overflow results undefined here.
    /// They are not: they are the flags of the 8-bit `AL ± 6`, and that
    /// operation happens **whether or not the adjustment is needed** — with an
    /// operand of zero when it is not, which is why an unadjusted `AAA` leaves
    /// the sign, zero and parity of the original `AL`. Measured on all 20 000
    /// corpus vectors.
    fn ascii_adjust(&mut self, subtract: bool) {
        let al = self.state.regs.ax as u8;
        let adjust = (al & 0x0f) > 9 || self.flag(flags::AF);
        let operand = if adjust { 6 } else { 0 };
        let adjusted = if subtract {
            self.sub8(al, operand, false)
        } else {
            self.add8(al, operand, false)
        };
        let ah = (self.state.regs.ax >> 8) as u8;
        let ah = match (adjust, subtract) {
            (true, false) => ah.wrapping_add(1),
            (true, true) => ah.wrapping_sub(1),
            (false, _) => ah,
        };
        // Only the low digit survives; the carry and auxiliary carry report
        // whether a digit was carried out of it.
        self.state.regs.ax = (u16::from(ah) << 8) | u16::from(adjusted & 0x0f);
        self.set_flag(flags::CF | flags::AF, adjust);
    }

    // -----------------------------------------------------------------
    // String operations
    // -----------------------------------------------------------------

    fn string(&mut self, f: &Fields) {
        let op = f.insn.op;
        let width = u16::from(Self::width(f));
        let delta = if self.flag(flags::DF) {
            width.wrapping_neg()
        } else {
            width
        };
        let Some(rep) = f.rep else {
            self.string_step(f, delta);
            return;
        };
        // `REP` with `CX == 0` does nothing at all — not even one iteration.
        while self.state.regs.cx != 0 {
            self.string_step(f, delta);
            self.state.regs.cx = self.state.regs.cx.wrapping_sub(1);
            if matches!(op, Op::CMPSB | Op::CMPSW | Op::SCASB | Op::SCASW) {
                let zf = self.flag(flags::ZF);
                let stop = match rep {
                    Rep::While => !zf,
                    Rep::WhileNot => zf,
                };
                if stop {
                    break;
                }
            }
            if self.state.regs.cx == 0 {
                break;
            }
            self.charge(op.clocks());
            // A repeat is interruptible between iterations: the 8086 backs the
            // instruction pointer up to the prefix and re-enters. Modelled as
            // a clean restart; the erratum where an 8086 forgets all but the
            // last prefix on resume is not.
            if self.lines.nmi_pending() || (self.flag(flags::IF) && self.lines.intr_pending()) {
                self.state.regs.ip = self.start_ip;
                self.state.queue.flush();
                return;
            }
        }
    }

    fn string_step(&mut self, f: &Fields, delta: u16) {
        let op = f.insn.op;
        // The source of a string move is overridable; its destination is
        // always `ES:DI`, and no prefix can change that.
        let src_seg = f.segment(seg::DS);
        let regs = self.state.regs;
        match op {
            Op::MOVSB => {
                let value = self.read8(src_seg, regs.si);
                self.write8(seg::ES, regs.di, value);
                self.advance_si_di(delta, true, true);
            }
            Op::MOVSW => {
                let value = self.read16(src_seg, regs.si);
                self.write16(seg::ES, regs.di, value);
                self.advance_si_di(delta, true, true);
            }
            Op::CMPSB => {
                let a = self.read8(src_seg, regs.si);
                let b = self.read8(seg::ES, regs.di);
                self.sub8(a, b, false);
                self.advance_si_di(delta, true, true);
            }
            Op::CMPSW => {
                let a = self.read16(src_seg, regs.si);
                let b = self.read16(seg::ES, regs.di);
                self.sub16(a, b, false);
                self.advance_si_di(delta, true, true);
            }
            Op::STOSB => {
                let value = regs.ax as u8;
                self.write8(seg::ES, regs.di, value);
                self.advance_si_di(delta, false, true);
            }
            Op::STOSW => {
                let value = regs.ax;
                self.write16(seg::ES, regs.di, value);
                self.advance_si_di(delta, false, true);
            }
            Op::LODSB => {
                let value = self.read8(src_seg, regs.si);
                self.state.regs.set_byte(0, value);
                self.advance_si_di(delta, true, false);
            }
            Op::LODSW => {
                let value = self.read16(src_seg, regs.si);
                self.state.regs.ax = value;
                self.advance_si_di(delta, true, false);
            }
            Op::SCASB => {
                let b = self.read8(seg::ES, regs.di);
                let a = self.state.regs.ax as u8;
                self.sub8(a, b, false);
                self.advance_si_di(delta, false, true);
            }
            _ => {
                let b = self.read16(seg::ES, regs.di);
                let a = self.state.regs.ax;
                self.sub16(a, b, false);
                self.advance_si_di(delta, false, true);
            }
        }
    }

    fn advance_si_di(&mut self, delta: u16, si: bool, di: bool) {
        if si {
            self.state.regs.si = self.state.regs.si.wrapping_add(delta);
        }
        if di {
            self.state.regs.di = self.state.regs.di.wrapping_add(delta);
        }
    }
}