# z80-python 0.4.0 Version 0.4.0 makes the reference core the shape the rest of the family copies: it takes its bus as callables like m6800-python, runs 2.5 times faster on CPython, cites the manual page behind every instruction, and names the tier of every oracle in the sentence that claims it. It also turns the core into a conformance reference for cores in other languages. ## Breaking changes Two, both deliberate, both with a one-line migration (details in [CHANGELOG.md](../../CHANGELOG.md)): - **The embedding contract is callables.** `Z80CPU(read_byte, write_byte, *, read_port=None, write_port=None)` replaces subclassing with four abstract methods; a flat host is `Z80CPU(memory.__getitem__, memory.__setitem__)`. The core now guarantees 16-bit addresses and 8-bit values, so hosts need not mask, and all 1,604,000 SingleStepTests cases check that guarantee. A class that still defines the bus as methods is refused with a `TypeError` naming the new form. - **CPython 3.11 is no longer tested.** CI covers CPython 3.12-3.14 and PyPy 3.11. The package still installs on 3.11, because the newest PyPy is Python 3.11 and PyPy must keep working. ## Speed Built one measured change at a time, every oracle passing after each, and a change that did not pay reverted (see docs/validation.md, "Speed"): | Workload | CPython 3.14 before | after | PyPy 7.3.20 before | after | | --- | ---: | ---: | ---: | ---: | | base | 1.08 M instr/s | 2.73 M | 34 M | 67 M | | indexed_cb | 0.54 M | 1.42 M | 1.5 M | 33 M | | block_io | 0.93 M | 1.93 M | 42 M | 47 M | One 256-entry table per opcode page, F kept as one int, Q written in place, and the opcode fetch done inside `step()`. The handlers are untouched as code a reader can follow: tables only route opcodes to them. `benchmarks/compare_revisions.py` reproduces any row. ## Readability and sources - Every opcode handler's docstring starts with its Zilog mnemonic and ends with where its rule comes from: a page of Zilog's *Z80 CPU User Manual* UM008011-0816 or a section of Young's *The Undocumented Z80 Documented* v0.91. Handlers that use WZ or read Q, which neither manual covers, also name the SingleStepTests file and, where one exists, the z80test program that pins the rule. `tests/test_readability.py` enforces all of it, and `scripts/fetch_reference_docs.py` fetches both manuals against pinned SHA-256s. - The 26 near-identical IX/IY ALU and load handlers are three. - `docs/start-here.md` explains the embedding contract, and `docs/undocumented-behavior.md` is one page, mechanism first. ## Validation - docs/validation.md ranks every oracle by where its expected values came from: z80test and ZEXDOC/ZEXALL are hardware-captured, SingleStepTests and FUSE emulator-derived, the two manuals documentation. The order of memory accesses is checked only by emulator-derived pin traces; the page says so. - The SingleStepTests gate compares T-states and the sequence of memory accesses with each case's `cycles` array: 1,604,000 of 1,604,000 agree. - FUSE 1.6.0's 1,356 cases join as a fourth oracle: 1,350 agree, and the six others are pinned as expected failures, each decided by a higher-tier source. - One rule departs from a cited manual, decided by hardware: `BIT b,r` takes X/Y from the register, as z80test's `z80full` shows, not from the tested bit as Young 4.1 says. - The full certification against the release commit, with commands and timings, is in docs/validation.md. ## Tests The 178 test files are 30. The 191 hand-written single-instruction test files became nine family files of data rows, each checked in full (every register, every byte of memory, every port write and the T-states) by one shared checker; the rows were recorded from the old tests and reproduce every one of their 910 steps. `pytest -m "not slow"` runs everything but the external corpora in a few seconds. ## Tooling - `DebugSession(track_accesses=True)` records every memory and port access a step makes; watchpoints stop a run after the step that touched a byte; `next_boundary` is public. The console gains `over`, `continue`, `watch`/`unwatch`, `set`, `int`/`nmi`/`reset` and m6800-python's aliases. - `python -m z80_python --load FILE@ADDR --pc ADDR -c ...` steps through a binary without writing a host. - The trace schema gains an optional `accesses` array and stays version 1. ## Conformance kit - `z80_python.conformance`: versioned JSON manifests that fully determine a run (memory, initial state, host profile, lifecycle events, stop rule), one shared `ConformanceHost` with `flat` and `cpm-minimal` profiles, lazy reference traces, lockstep diff against an external trace, and a `python -m z80_python.conformance trace|diff|checkpoints` command line. - `docs/trace-schema.md` documents the trace format as a public contract; `docs/conformance.md` gives the host contract and the certification ladder. - `examples/conformance/` has manifests with committed reference traces, and `examples/conformance/zex/` the ZEXDOC/ZEXALL recipe with the SHA-256 of every checkpoint, so a port can diff ZEXALL in parallel segments. ## Instruction core - Runs of DD/FD prefixes, and DD/FD before ED, execute as on hardware: each stray prefix is a 4-T-state M1 that increments R, the last prefix decides IX or IY, and an ED after a prefix runs unchanged. The rule is Young's (3.7, 6.1, 5.5); no executable oracle checks it beyond FUSE's one `ddfd00` case, and docs/validation.md says so. - `EX (SP),HL` and `EX (SP),IX/IY` write `(SP+1)` before `(SP)`, as SingleStepTests and FUSE agree; memory ends the same either way, so only the bus-order check could see it. ## Compatibility - Apart from the two breaking changes, every public name keeps working: `Flags`, `cpu.f`, `CPUState`, the debugger values, traces and the conformance surface. Everything else new is additive. - No checked instruction semantics changed: every sequence the 1,604,000 vectors, ZEXDOC/ZEXALL and z80test cover behaves exactly as in 0.3.0. - Pre-1.0 minor releases may break the public surface when the changelog says so under **Breaking**; 0.4.0 is the first to use that clause (docs/api-stability.md). ## Verified after release Published 2026-09-21 by the Publish workflow from tag `v0.4.0` (`500608c`, whose package code is byte-identical to the certified `b1720b6`), with these files on PyPI and attached to the GitHub release: ```text a06a7136ff2aa86df9d3843d5cff99df63ba165061c38dd2dcdaa1a506169866 z80_python-0.4.0-py3-none-any.whl 8bdb3052d8f1a409a07f5f92022a5af65f0d5a59b4e30b10f4f83431a43bd59e z80_python-0.4.0.tar.gz ``` `pip install z80-python==0.4.0` from PyPI into a fresh virtual environment, then `python scripts/smoke_installed_package.py` run from outside the source tree, passes under CPython 3.14.4 and PyPy 7.3.20 / Python 3.11.13.