# Handoff: build `mcs48-python`, a pure-Python MCS-48 core ## Context you are inheriting `/data/emu/mcs48-python` holds no core code. It holds the groundwork: documents whose every fact names its source and states how well that source is established, a pinned and hash-verified fetch of the MAME sources they rest on, a mechanical check that re-derives the opcode table and diffs it against the documents, and a working, verified recipe for per-instruction traces of a real arcade MCS-48 program. The author's 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`, `src/z80_python/_core.py` and `_dispatch.py` for the dispatch shape. - `/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). Read these, in this order, before writing any code: 1. `README.md` — what the project is, the MCS-48 / MCS-51 scope decision, and the verified table of arcade boards this core is aimed at. 2. `docs/start-here.md` — the programming model: A, the two register banks and BS, the 12-bit PC with its 2 KB pages and the DBF flip-flop, the PSW, the internal RAM with its eight-level stack, `MOVX` and the ports, T0/T1/INT, the timer with its ÷32 prescaler, the interrupt model, and the complete instruction table with opcodes, bytes, cycles and flag effects. 3. `docs/timing.md` — the 15-clock machine cycle, what `step()` returns, the ALE/PSEN/RD/WR/PROG rules a host needs, the clocks the arcade boards run at, and how to schedule an MCU against a main CPU. 4. `docs/undocumented-behavior.md` — twelve corners with their sources and their confidence tags. §6 in particular: the only two hardware measurements that exist, and why agreeing with MAME there is a bug. 5. `docs/validation.md` — every oracle with its tier, and a list of the things that were searched for and **do not exist**, so you do not re-search them. 6. `docs/mame-oracle.md` — the trace recipe, the register names MAME exposes, four gotchas re-tested (one of them corrected), and verified samples. Then run the three commands in `docs/validation.md` § "Reproducing what is recorded here" so the numbers on those pages are real to you before you write a line. ## What makes this project different from its siblings **There is no hardware-captured test corpus for the MCS-48, and there is no prospect of one.** No SingleStepTests set, no capture rig, no Intel verification program — all three established by looking, and recorded as confident negatives in `docs/validation.md`. The entire hardware-tier evidence available is two short programs measured on a real 8048 and quoted in MAME's own source header. So the discipline is different, not weaker: - The **judge** is Intel's manual, cited by section and page. - The strongest *executable* oracle is the **T48 verification suite** — 102 self-checking assembly programs written by a third party from the same manual, with a twenty-line pass/fail protocol. It is GPL-2.0: fetch and run it, never commit it or a translation of it. - The strongest *hardware* oracle is those two experiments. Passing them is the only "verified against hardware" claim this project may ever make. - MAME is a **detector**, and in three named regions it is known to be wrong. Say all of that in the README when the core exists. Do not quietly borrow the vocabulary of the Z80 and 6502 repositories, where "hardware-verified" means something much stronger. ## Your task Build a readable, dependency-free Python 3.12+ MCS-48 instruction core in this repository, in the shape of z80-python, and certify it against the oracles in tier order. The host contract, fixed now so hosts can be written in parallel. The host supplies an object with: ```python read_program(addr12) -> int # every fetch, MOVP, MOVP3 read_ext(addr8) -> int # MOVX A,@Rr (RD pulses) write_ext(addr8, value) # MOVX @Rr,A (WR pulses) read_port(port) -> int # port in (1, 2) or BUS write_port(port, value) # port in (1, 2) or BUS read_expander(p) -> int # p in 4..7, low nibble write_expander(p, nibble, op) # op: WRITE / AND / OR read_t0() -> bool read_t1() -> bool ``` and the core exposes `set_int(level)` — a **level**, not an edge, because the manual is explicit that `INT` is level-triggered and active low and that a request left asserted re-enters the service routine. `set_ea(level)` selects internal or external program memory. `reset()` performs exactly the eleven documented effects and **nothing else** — it does not clear A, the timer/counter, CY, AC or internal RAM. `step()` executes one instruction, or accepts one interrupt and executes the implied `CALL`, and returns its machine cycle count. Internal RAM size, the presence and size of internal ROM, and the MCS-48 / UPI-41 opcode table are constructor parameters, so one class covers 8035, 8039, 8048, 8049, 8749, MB8884 and the 8041/8042. Out of scope, stated rather than apologised for: the 8021 and 8022 (reduced instruction set, 30-clock cycle), CMOS `IDL`, state-time or bus-cycle timing, and any memory map or device model inside the core. ### Milestones, each with its acceptance test, in oracle-tier order 1. **Skeleton and datasheet unit tests** (tier: documentation). `CPUState`, the PSW object with bit 3 reading 1, the register-bank base pointer, the 12-bit PC with its three different update rules, the eight-level wrapping stack, the timer and its 5-bit prescaler, and the interrupt sequence. Each test names the UM section it encodes. Acceptance: every rule in `docs/start-here.md` §"Interrupts" and §"Reset" has a test, and every entry in `docs/undocumented-behavior.md` that is tagged **documented** has a test. 2. **The complete instruction set against UM §4.1** (tier: documentation). All 230 defined opcodes; the 26 undefined ones behave as MAME's `illegal` (one cycle, no operand consumed, no state change) and call a host hook. Acceptance: `python scripts/check_instruction_table.py` still reports 0 disagreements, and a test asserts the core's own per-opcode byte and cycle table is identical to the one that script derives. This is the rung that makes the documents executable. 3. **The T48 verification suite** (tier: documentation-derived, independent, **the judge in practice**). Fetch `devsaurus/t48` at a pinned commit (do not vendor it: GPL-2.0), install Alfred Arnold's `asl`/`p2hex` (not present on this machine), assemble each of the 102 `sw/verif/**/test.asm` in the `t48`, `t49` and `t3x` ROM layouts, run each under the core, and detect the `MOV A,#AAH / MOV A,#55H / MOV A,#01H` pass marker. Write the runner from the documented protocol; do not translate the test sources. Acceptance: **102/102 PASS**, with the eight `white_box/` tests named individually in the report — `pc_wrap_bit11`, `int_on_call`, `int_on_jmp`, `int_on_mb1`, `p1_port_reg_conflict`, `p2_port_reg_conflict`, `p2_io_exp`, `psen_rd_wr_timing`. Record the T48 commit. Any failure is a disagreement between two readings of the manual: stop, show both readings and the section, and decide it from UM — not from T48 and not from MAME. 4. **The two hardware measurements** (tier: **hardware-captured**). Encode the two programs quoted in `docs/undocumented-behavior.md` §6 as tests. Acceptance: after the interrupt sequence `A == 0x22`; and `A == 0xFF` after 31 `NOP`s and `0x00` after 32. If the core can only satisfy these by choosing between "STRT T has a one-cycle delay" and "MOV A,T reads the pre-increment value", **say which you chose and that the measurement does not distinguish them**. This is the only hardware-tier claim this project gets; do not spend it on a guess left unlabelled. 5. **MAME lockstep on `dkong`** (tier: emulator-derived, a **detector**). A Donkey Kong sound-board host — the map is in `docs/mame-oracle.md` §"Using the trace in lockstep" — running the core against a trace from `scripts/mame/trace_dkong.sh`; compare the seventeen fields per instruction; stop at the first divergence; decide every divergence from the manual, from T48, or from milestone 4. Acceptance: N instructions with no undecided divergence, N stated, the three known-bad MAME regions excluded by name. The 460-frame reference run is 2,406,109 instructions; a clean lockstep over the whole of it is the target. Note before you start: that trace contains **no interrupt at all**, so this rung does not test the interrupt path. Say so in the report. 6. **The UPI-41/42 as a second opcode table** (tier: mixed). The fourteen changed opcodes, the DBB/STS/IBF/OBF host interface, and the IBF interrupt. Acceptance: T48's `black_box/upi41/` group (`basic_echo`, `basic_echo_int`, `dma`, `master_int`, `status41_test`, `status41a_test`) all PASS, and a lockstep against MAME on a DECO Cassette set or on `gsword` (three 8041As) with no undecided divergence. Do not skip a rung or reorder them. Rung 3's failures are cheapest to diagnose before rung 5 produces a million-line diff, and rung 4 is what tells you when to stop trusting rung 5. ### Optional, if the rungs are clean and you want more evidence The Philips Videopac's main CPU is a real 8048 with internal ROM, and there is a factory **Service Test Cartridge** for it (`testcart` in MAME's `hash/videopac.xml`). Its software-list ROM is not in the local collection, so this was never run here. A diagnostic cartridge that exercises a whole 8048 is the closest thing to the Intel self-test that was never published. ## Constraints - **Never commit a ROM, a trace, or a third-party test program.** `traces/`, `vendor/`, `tests/roms/`, `tests/programs/`, `*.log`, `*.zip`, `*.bin` are gitignored; keep them so. Arcade ROMs are read in place from the MAME collection with `-rompath`. T48 is GPL-2.0 and this repository is MIT: fetch it, run it, do not vendor it, do not translate its sources into this tree. - **Do not modify the documents to match the code.** If you believe `docs/start-here.md` is wrong, show the manual section and fix the document in its own commit, with `scripts/check_instruction_table.py` still green. - **Tier discipline.** State the tier of every oracle in every claim. "Agrees with MAME" means exactly that and no more. "Verified against hardware" means milestone 4 and nothing else. A claim carries the source 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 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. - **Commits** in reviewable units with messages that say what the change rests on. Author `alewman `. Trailers on every commit: ```text Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01USzp3rFUxfVkjibErYHzCm ``` 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`. **Not** installed: `asl`/`p2hex` (needed for milestone 3), `ghdl`, `poppler-utils` (so PDFs need `pypdf` in a venv to read). No sudo. ## What done looks like A README that states, with numbers: the MAME version and `check_instruction_table.py` clean; the T48 commit and `102 / 102`; the two hardware measurements passing, with the ambiguity in the second one named; the MAME version and the length of the clean `dkong` lockstep; and a plain sentence saying that no hardware-captured corpus exists for this processor and that the interrupt model therefore rests on the manual and on T48's `int/` and `white_box/int_on_*` tests, because no arcade trace made here contained a single interrupt. A CI job that runs the unit tests and `check_instruction_table.py` on every push; the T48 and lockstep gates run locally and recorded in `docs/validation.md`. Before starting, tell the user in a few sentences how you read this brief and what you will do first.