# Handoff: build `tlcs900h-python`, the Toshiba TLCS-900/H core ## Context you are inheriting Read the family charter (emulator-lineage `docs/charter.md`) first: x-python is the specification; CPUs always get their own core repository; boards pin cores by commit and never copy their files. This core's first and only known consumer is [ngp-python](../ngp-python) (Neo Geo Pocket / Color, Phase 0 the same night). Other TLCS-900 machines exist (MAME uses the family in several arcade and consumer boards), which is a reason to keep the SoC peripherals out of the core. Read, in order: [start-here.md](start-here.md), [timing.md](timing.md), [validation.md](validation.md), [mame-oracle.md](mame-oracle.md). Then `python -I scripts/fetch_references.py --verify` (11 Toshiba PDFs, the ares and SST subsets) and `nice taskset -c 24-31 python -I scripts/ocr_references.py` if `reference/text/` is missing (the PDFs are scans). Models to copy the shape of: **huc6280-python** (an 8-bit-bus CPU with on-chip peripherals kept apart), **m68000-python** (host protocol, bus-access tracking), **sh2-python** (a CPU whose on-chip modules are separate classes the board wires), **z80-python** (callable embedding contract). ## Decided by Aubrey (standing rules) Name `tlcs900h-python`, package `tlcs900h_python`. Local, no remote until he says. PyPy 3.11 floor, modern forms, classes under about 80 attributes. Correct before fast. Technical choices: made, marked provisional, reported. ## Proposed by this Phase 0 - **Model the 900/H only** (maximum mode, system mode, INTNEST, 900/H state counts). No 900 normal mode, no minimum mode; a `TLCS900` variant is not planned (open decision 3). - **Instruction level with per-access timing**: every instruction is a sequence of bus accesses, each charged its states and waits through the host, so the board sees accesses at the right state; the instruction queue (4 bytes) refills on the bus per a documented, switchable rule (U6). - **Timing as data**: the HB lists become one table row per decode form (states for 8/16/32-bit, extra states for addressing modes and taken branches), read by the executor, never hard-coded in handlers. - **Peripherals out of the core** (open decision 2): the core owns micro-DMA transfer and interrupt acceptance; the board owns the interrupt controller's registers, timers, serial, ports, watchdog, clock gear. ## Package and classes (proposed) ``` tlcs900h_python/ registers.py # Registers: 4 banks x XWA/XBC/XDE/XHL, XIX/XIY/XIZ/XSP, PC, SR (RFP, IFF, F, F') decode.py # the two-level decode table (first byte -> addressing/size, operation byte) timing.py # STATES: per form, 8/16/32-bit; addressing extras; taken/not-taken execute.py # handlers, one per decode row, reading/writing named state, bus via one interface queue.py # InstructionQueue: 4 bytes, refill rule (U6), flush on branch/interrupt interrupts.py # acceptance: level vs IFF, vector fetch from the host, push PC/SR, INTNEST microdma.py # MicroDma: four channels' control registers, transfer modes 0-7 on request cpu.py # Tlcs900h: step(), run(states), request_interrupt(level, vector), request_dma(ch) disasm.py # Toshiba mnemonics (for traces against MAME's) trace.py # the lockstep line format ``` ## The import boundary and the host protocol The host passes callables: `read(address, size) -> int`, `write(address, size, value)`, each returning or accepting the states the access costs (waits by area are the board's); `interrupt_vector(level) -> address` (the SoC's controller supplies it); `on_halt()`. The core never imports the board. Sockets: none (a CPU core). ## Unknowns (U) and contradictions (C) | # | Unknown | Best source now | Status | | --- | --- | --- | --- | | U1 | Undefined flags ("X" in HB; "partly different in 900/H") | none; SST (ares) asserts values | HYPOTHESIS: follow O5 only as a detector, flag each row | | U2 | States per second on the NGP: 6.144 M (MAME, ares) or 3.072 M (one state = two crystal clocks, Toshiba's parts) | MAME's and ares's frame arithmetic | provisional 6.144 M (OBSERVED MAME 0.285, ares `6e67c8b`); settle by a probe ROM | | U3 | The NGP SoC's peripheral set and SFR addresses (TMP95C061-like? TMP95CS64F-like?) | MAME's SFR names; Judge; ares cpu/io.cpp | board's question; the core stays out of it | | U4 | Micro-DMA modes 3, 6, 7 (ares: "unknown behavior") | Toshiba ch. 4 for a 900/H part (TMP95CS64F data sheet, image only, pinned) | read O3 before rung 3 | | U5 | The built-in I/O's automatic wait (CLK phase, H2 CPU900H-31) | O1 text only | board passes waits; default 0, flagged | | U6 | Queue refill order and depth as seen on the bus (ares uses 3 of 4 bytes) | none | two documented rules behind a switch; default: refill whenever a word slot is free | | U7 | `HALT` wake-up and interrupt latency in states | O1 figure 7.x (interrupt receive timing) | read before rung 3 | | # | Contradiction | Sources | Handling | | --- | --- | --- | --- | | C1 | Which CPU the NGP has: MAME "TMP95C061" (a 900 part) with XSSP/XNSP vs Judge's "TLCS-900H, TMP95CS64F package" vs MAME's own states, which are the 900/H's | MAME 0.285 device list and probe; ngpctech.txt; HB | model the 900/H; MAME is a detector | | C2 | `LDIR` with BC = 0: the manual's text says an extra read of the first address, its list says 7n+1 states (as quoted in ares's comment) | O1 HA, HB; ares `instructions.cpp` | read both pages in O1 and record; tag HYPOTHESIS until a probe | | C3 | ares's comment on `SBC`: "the TLCS900/H manual states this is subtract-with-carry, but it isn't" (`instructions.cpp`, before `instructionSubtractBorrow`); ares subtracts the carry as a borrow | O1 HA vs ares | likely wording only (borrow vs carry); confirm on O1's `SBC` page and record | ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M1 | registers, banks, SR, decode table, disassembler | O1 (2) | every first byte and operation byte of appendix C decodes to a form; disassembly of MAME's 66,779-line BIOS trace matches MAME's mnemonics or a named difference per form | | M2 | defined behaviour of all instruction forms | O1 (2); O5 detector | O5's 2,076 forms run (sparse fetch extended in batches); every disagreement triaged: O1 says, or U1 | | M3 | timing table and per-access charging, queue | O1 (2); O7 detector | the states between MAME trace lines equal the core's for the BIOS trace, or each difference named | | M4 | interrupts, `SWI`, `RETI`, INTNEST, `HALT`, micro DMA | O1, O3 (2) | unit tests from the pages; the NGP BIOS's vector use (FFFF00 table, SWI 1) runs in ngp-python's M1 | | M5 | the gate | | seeded faults (validation.md) all caught; lockstep trace format agreed with ngp-python | ## Domains and speed - **Domains**: the core is part of the NGP's main-CPU domain (with the SoC peripherals, the K2GE and the cartridge, all on the 6.144 MHz clock). Micro DMA runs inside it (it steals the CPU's states). Its channels to other domains are the board's (shared RAM and ports to the Z80): ngp-python docs/domains-and-speed.md. - **Instructions per second needed**: MAME's NGPC BIOS trace, 66,779 instructions in 307,454 states over 3 frames = 4.6 states each; at 6.144 M states/s that is 6,144,000 / 4.6 = **1.34 M instructions/s**; the worst case, all 2-state instructions, is 6,144,000 / 2 = **3.07 M/s** (half of each if U2 resolves to 3.072 M states/s). - **Reaching real time without a JIT** (the accepted no-JIT path: Rust -fast, then a cached interpreter, then threads at domain channels): - *Python reference*: comparable family cores run about 33 M instructions/s on PyPy (m68000-python, core alone) and 1.4 M on CPython; inside a board with per-access timing and a line renderer expect several times slower. Real time on PyPy is plausible but not promised (reference boards are correct, not fast). - *Desktop native* (tlcs900h-rust, -fast): family Rust -fast CPUs run 190-390 M instructions/s (m68000-rust-fast, z80-rust-fast): over 100x the need. Yes. - *Phone* (native, no JIT allowed on iOS): a phone core at a third to a half of desktop speed still leaves over 30x headroom. Yes, with `recompile: none`. - *Browser* (wasm, about half native): over 50x headroom. Yes. - A cached (pre-decoded) interpreter is not needed for real time; it may pay for run-ahead on weak targets and belongs in -turbo if ever built. Threads are not needed for this CPU. ## Open decisions for Aubrey 1. **The name and the scope: `tlcs900h-python` for the 900/H only.** Recommendation: yes; a 900 (non-/H) variant only if a second family board needs one. 2. **Peripherals outside the core** (the SoC's timers, interrupt controller registers, serial, watchdog in ngp-python; micro-DMA transfer and interrupt acceptance in the core). Recommendation: yes, because no document of the NGP SoC exists and Toshiba's 900/H parts differ exactly there. 3. **Use SingleStepTests/tlcs900h (no licence, generated by ares) as a detector**, read in place and never committed, extending the sparse fetch in batches. Recommendation: yes, as tier 5 only; and ask its author (raddad772) to add a licence before anything derived from it (a case table) is committed. Note also that v1 carries no timing. 4. **Timing unit U2 (6.144 vs 3.072 M states/s)**: provisional 6.144 M, following MAME and ares, until a probe ROM on a real NGPC; the family owns no NGP (Aubrey's hardware list). Recommendation: accept provisional, and add an NGPC to the probe wish list only if a game's behaviour depends on it.