# Handoff: build `sm8521-python`, the SM85CPU core of the Sharp SM8521 ## Context you are inheriting A new CPU for the family (the chip rule: CPUs always get their own core). No family core is related: the SM85CPU is Sharp's own 8-bit design with registers in RAM (a register pointer, like the TMS9900's workspace or the Z8's register file, not like the Z80 or 6502), big-endian words, 23 addressing modes. Its first and only planned board is `gamecom-python` (`../gamecom-python`, Phase 0 done alongside this one). Other SM85CPU machines (the SM8500 series was sold for pagers, ID cards and appliances, DB96 p. 64) have no family board. Nearest family models in shape: `r3000a-python` (a CPU Phase 0 with a manufacturer manual and MAME as the detector), `huc6280-python` (a core whose chip carries peripherals the board owns), `sh2-python` (timing as data, handlers in a restricted form: the JIT proposal's pattern, useful even though this core will not need a JIT). Rules: Python >= 3.11, CPython and PyPy; MIT; MAME's BSD-3 CPU files closed unless decision 2 changes; no firmware in the repository; local git only. ### Read in this order README.md; docs/start-here.md; cpu.md; decode-and-timing.md; validation.md; mame-oracle.md; provenance.md; then DS pages 8-29 and 51-54 (`reference/docs/`, after `python -I scripts/fetch_references.py`) and DB96 book pages 74-80; then gamecom-python's handoff brief for the bus contract from the other side. ## Scope In: the SM85CPU (R0-R15 through RP, PS0, PS1, SYS, SP, PC), all 67 instructions in all their addressing modes, the illegal-instruction trap, interrupt acceptance (IM, I, priorities, vectors, push and IRET), HALT and STOP as states, the instruction prefetch where software can see it, 1 KB internal RAM, the 4 KB internal ROM's place in the map. Out (gamecom-python's, by the chip rule): IE0/IE1/IR0/IR1, ports, MMU, LCDC/DMA, the sound generator, timers, clock timer, watchdog, UART, CKC's effect on time. ## Proposed names - Package **`sm8521_python`**; class `SM85CPU` (the core) and `SM8521` (a thin subclass fixing the SM8521's internal ROM and RAM sizes and its vector table; the SM8502-8506 would be other subclasses if ever wanted). - Modules: `registers.py` (RP mapping, PS0/PS1/SYS/SP), `decode.py` (generated from the decode table, rung 0), `handlers/` (one small method per instruction, DS page in the docstring), `timing.py` (cycles per row as data), `interrupts.py`, `tooling/` (disassembler, trace, debugger hooks like the 6502 family's). - The bus contract: `read(address) -> int`, `write(address, value)` for 0000-FFFF (the board maps 0010-007F's peripheral registers and everything outside internal RAM and ROM); `irq_lines()` from the board (a bitmask of requested and enabled sources, or the core reads IE/IR through the bus: decision 3); `step() -> cycles`; `halted`, `stopped` and `wake()`. The core counts cycles per instruction from its timing table; the board scales them by CKC's divider. ## Import boundary The core imports nothing of the family's. The board imports `sm8521_python`. ## Unknowns (U) and contradictions (C) - U1 word access to an odd register-file address ("unreliable" in DS). - U2 interrupt push order and stack layout; U3 whether IR bits clear on acceptance; U4 whether I clears on entry. - U5 which encodings are illegal. - U6 a cartridge started headless in MAME (needs a stylus input). - U7 per-instruction cycles (only MAME's model, and DS's one example). - U8 per-instruction flags where DS's flag definitions leave room (V on INC/DEC, H on word operations, B's update rules, D after logic operations). - C1 DS p. 11's 12-level IM table (copied from the sibling series) vs DS p. 24's 8 levels. - C2 "9 CPU control instructions" vs eight listed; RL missing from p. 53's summary. - C3 RET's description reads "(Logical Exclusive OR Word)". - C4 CKKC vs CKC. - C5 MAME runs at fCK/2 from reset; DS starts the CPU at fCK/32 until a STOP with FCPUEN. - C6 HDK's `DM` mnemonic (2D, 3D) is not in Sharp's instruction list; HDK's A8/A9 rows. ## Milestones (exit tests in validation.md) R0 decode map (MAME `dasm` sweep as closed-box measurement, HDK, DS); R1 semantics and flags; R2 register file; R3 interrupts; R4 standby and prefetch; R5 timing table; R6 MAME lockstep on the firmware; R7 seeded faults; then sm8521-rust. ## Domains and speed - **Domains and channels:** the core is one domain with its board (gamecom-python's domains.md: one main-clock domain; the 32.768 kHz clock timer meets it only at its interrupt and CLKT). The core's channels are its bus callbacks and interrupt lines. - **Instructions per second:** ceiling = 5,529,600 cycles / 2 (NOP, the shortest: MAME 2 cycles, DS's 2-cycle memory access) = **2.76 M/s**; MAME's firmware run 0.71 M/s (7.86 cycles per instruction, OBSERVED). Planning figure: 2.76 M/s. - **No-JIT path (estimates, `recompile: none`):** the family's plain Rust transcriptions of 8-bit cores run 50 M (z80-rust) to 190 M (w65c02-rust) instructions/s on desktop; an SM85CPU transcription with RP-mapped registers and many addressing modes is put at 30-60 M/s native (HYPOTHESIS): 10-20x the ceiling on desktop, ~8-16x on a phone, ~5-10x in iPhone Safari's wasm (~0.5x native), ~2-4x on a Cortex-A55 handheld. **Real time everywhere without `-fast`, a cached interpreter, threads or recompilation.** Python (PyPy, ~0.3-1 M/s estimated) is a specification at roughly 1/3-1x of real time at the ceiling, closer to real time on firmware-like code. ## Open decisions for Aubrey 1. **Decode map by closed-box measurement**: a MAME debugger `dasm` sweep over every opcode pattern, recorded as OBSERVED, cross-checked against HDK and DS. Recommended: it fills DS's gap without reading anyone's source. 2. **MAME's `cpu/sm8500` files (BSD-3-Clause) stay closed.** Recommended, as the brief asked for this Phase 0; the alternative (read them as "code OK with notice" for the opcode map and cycle table, crediting Wilbert Pol in NOTICE.md) would save the sweep and is legal. If Aubrey prefers it, the core's files would carry `MIT AND BSD-3-Clause` where they hold anything taken. 3. **The line between core and board:** the core owns R0-R15, PS0, PS1, SYS, SP, PC and interrupt acceptance; IE/IR and all peripheral registers at 0010-007F are the board's, reached through the bus. Recommended: it keeps the core a CPU (the chip rule) and lets the board model the SoC's peripherals as the Game.com uses them. 4. **Modelling level: instruction level with cycle counts as data**, the prefetch modelled only where DS says software sees it (the MMU0 switch). Recommended: no source gives the bus-cycle order inside instructions. 5. **Timing table seeded from MAME's traces (OBSERVED), checked against DS's bus rules.** Recommended, provisional: the only alternative is no timing at all. 6. **Interrupt stack layout (U2) from IRET's description (PS1 at the lower address, then PC), checked in lockstep.** Recommended, provisional. 7. **One Rust tier (`sm8521-rust`), no `-fast`.** Recommended (estimates above).