# Handoff: build `i8086-python`, a pure-Python 8086 core, then its V30 ## Context you are inheriting `/data/emu/i8086-python` holds no core code. It holds the groundwork: documents whose every fact names its source, three hardware-captured corpora already fetched under `tests/` (gitignored, 1.76 GB), a fetch script that reproduces that fetch at pinned commits, and a working recipe for per-instruction V30 traces from MAME. The author's two existing cores set the shape you are to match: - `/data/emu/z80-python` (github alewman/z80-python): the identity is "the code is the spec": readable, oracle-verified, embeddable. Read its `docs/start-here.md`, `docs/validation.md`, `docs/conformance.md`, `src/z80_python/_core.py` and `_dispatch.py` for the dispatch shape, and `validation/vector_utils.py` for the SingleStepTests runner. - `/data/emu/6502-python`: the same discipline in a smaller package; `README.md` for the host contract (`read_byte`/`write_byte` on a host-owned memory object, `CPU.step()`, interrupt lines as levels and edges) and `scripts/fetch_test_vectors.py`. Read these, in this order, before writing any code: 1. `docs/start-here.md`: registers, FLAGS, segmentation and EA, encoding, prefixes, interrupts, strings, multiply/divide, the V20/V30 additions. 2. `docs/timing.md`: what `step()` returns and why cycle exactness is not a first-core goal; what the M72 host needs. 3. `docs/undocumented-behavior.md`: the undefined flags and undocumented forms with their tags. 4. `docs/validation.md`: every oracle, its tier, pin, size and limits; the record shape; what the 8088 corpus does and does not prove for an 8086. 5. `docs/mame-oracle.md`: the trace recipe and a verified sample. 6. `tests/8088_test_vectors/metadata.json` and a decompressed case from `00.json.gz`, so the JSON is real to you before you write the runner. ## Your task Build a readable, dependency-free Python 3.12+ Intel 8086 instruction core in this repository, in the shape of z80-python, and certify it against the oracles in tier order. Then add the NEC V30 as a milestone and certify that against the V20 corpus. The goal at each rung is a stated, reproducible number, not "it runs R-Type". The host contract, fixed now so hosts can be written in parallel: the host subclasses or supplies an object with `read_byte(addr20)`, `write_byte(addr20, value)`, `read_port(port16)`, `write_port(port16, value)`; the core computes every physical address itself (segment × 16 + offset, masked to 20 bits) and performs word accesses as two byte accesses, low byte first; `step()` executes one instruction (prefixes included) or one accepted interrupt and returns its clock count; interrupt requests go through a lifecycle API (`set_intr(level)` with a host callback that supplies the vector byte at acknowledge time, `set_nmi(level)` edge-latched, `set_reset(level)`), never by writing CS:IP. Registers and FLAGS are readable and writable as attributes; a `CPUState` snapshot type lists every processor-owned field and nothing else. ### Milestones, each with its acceptance test, in oracle-tier order 1. **Skeleton and datasheet unit tests** (tier: documentation). `CPUState`, the FLAGS object with the fixed bits, the ModR/M decoder and EA computation with default segments and overrides, the arithmetic and logic flag rules, and the interrupt sequence (push FLAGS, clear IF and TF, push CS, push IP, load from `4n`; IRET; priorities; the MOV/POP segment-register shadow; the STI delay; HLT wakeup; the INTR acknowledge that asks the host for the vector byte). Each test names the manual page it encodes. Acceptance: the suite passes and every rule in `docs/start-here.md` "Interrupts" has a test. 2. **The complete 8086 instruction set against SingleStepTests/8088 v2** (tier: hardware-captured, **the judge**). A native runner over `tests/8088_test_vectors/*.json.gz` that loads `initial`, steps once, and compares `final` registers (flags ANDed with the file's `flags-mask` from `metadata.json`), `final.ram`, and the ordered operand accesses against the `MEMR`/`MEMW`/`IOR`/`IOW` cycles; IN returns 0xFF; the type-0 handler at 0x0400; the pushed flags word masked in divide-exception cases. Do **not** compare `len(cycles)` with `step()`'s return (`docs/timing.md` says why). Acceptance: **323 files, 3,007,000 cases, 0 failed** with masks. Then 2b: drop the masks opcode by opcode until the undefined flags are reproduced, and record which remain masked. Then 2c: the same runner over `tests/8086_test_vectors/` (note its `test_hash`/`test_num` keys): **323 files, 646,000 cases**. 3. **Clock totals** (tier: documentation). `step()` returns Intel table 2-21 + 2-20 figures; unit tests pin a representative row per family and the taken/not-taken splits. The corpus cannot judge this; say so in the README. 4. **The V30 milestone against SingleStepTests/v20 v1_native** (tier: hardware-captured, the judge for the V30). A `V30` subclass or mode: the 60-6F, C0/C1, C8/C9 and 0F instructions, REPC/REPNC, the NEC undefined-flag results, the three-prefix resume rule, NEC clock totals from NEC section 12 cross-checked with MAME's `CLKS` macros, and the V20-vs-8086 semantic differences in `docs/undocumented-behavior.md` (REP IDIV, 63/66/67, invalid-form halts). Acceptance: **360 files, 3,125,000 cases, 0 failed** with masks, then unmasked as far as it goes. 8080 emulation mode is out of scope and must raise a clear error. 5. **MAME lockstep on `rtype`** (tier: emulator-derived, **a detector**). An M72 host (`docs/timing.md` has the map) running the V30 core against a trace produced by `scripts/mame/trace_rtype.sh`; compare the fourteen register fields per instruction; stop at the first divergence; decide every divergence from the corpus or the manual and record it. Acceptance: N instructions with no undecided divergence, N stated. A divergence MAME loses (its `AAM` ignores the immediate; its undefined flags are its own) is recorded as such, not "fixed" in the core. Do not skip a rung or reorder them; each one's failure is cheapest to diagnose before the next. Rung 5 may be run early, up to its first V30-only instruction, as a smoke test of the 8086 core inside a real host. ## Constraints - **Never commit a corpus, a trace, or a ROM.** `tests/*_test_vectors/`, `traces/`, `*.log`, `*.zip` are ignored; keep them so. ROM sets are read in place from the MAME collection with `-rompath`. - **Do not modify the corpora.** If you believe a vector is wrong, stop, show the case and the higher-tier source that contradicts it, and ask. - **Tier discipline.** State the tier of every oracle in every claim. "Verified against hardware" means a SingleStepTests corpus; "agrees with MAME" means exactly that and no more. A claim carries the corpus revision and the pass count or it is not a claim. - **Shape.** Mirror z80-python's layout: a small public facade, instruction families as modules, explicit if-chain or table dispatch that PyPy compiles well, each handler's docstring beginning with its Intel mnemonic, hardware reasons as comments on the lines that encode them, a readability test that enforces the docstring rule. No dependencies. CPython 3.12+ and PyPy. - **Scope boundaries, stated not apologised for**: no bus-cycle timing, no prefetch queue model, no 8087, no 8080 emulation mode, no memory map or devices in the core. - **Commits** in reviewable units with messages that say what the change rests on. Author `alewman `. Trailer on every commit: `Co-Authored-By: Claude Fable 5.1 `. Push and open PRs only when the user asks; the repository is local-only until then. - Tools on this machine: Python 3.14.4 at `/usr/bin/python3` (use a venv for anything extra), `git` 2.53, MAME 0.285 at `/usr/games/mame`, the corpora already under `tests/`. No sudo. ## What done looks like A README that states, with numbers: the 8088 corpus revision and `3,007,000 / 3,007,000`; the 8086 corpus revision and `646,000 / 646,000`; the V20 corpus revision and `3,125,000 / 3,125,000`; which opcodes still run with undefined-flag masks; the MAME version and the length of the clean `rtype` lockstep; and a plain sentence that interrupt sequencing rests on the manuals and MAME because no hardware corpus exercises it. A CI job that runs the unit tests on every push; the corpus gates run locally and are recorded in `docs/validation.md` the way z80-python records ZEX. Before starting, tell the user in a few sentences how you read this brief and what you will do first.