# Handoff: build `ngp-python`, the Neo Geo Pocket and Pocket Color board ## Context you are inheriting Read the family charter (emulator-lineage `docs/charter.md`) first. Chips are cores, boards compose cores; CPUs always get their own core; a non-CPU chip used by one machine stays in its board until a second family project needs it; never copy files between repositories, pin siblings by commit. Then read, in order: [start-here.md](start-here.md), [k1ge-k2ge.md](k1ge-k2ge.md), [sound.md](sound.md), [cartridge-bios-rtc.md](cartridge-bios-rtc.md), [domains-and-speed.md](domains-and-speed.md), [sources.md](sources.md), [validation.md](validation.md), [mame-oracle.md](mame-oracle.md), [census.md](census.md). Then `python -I scripts/fetch_references.py --verify`. **Author respect is part of this brief**: the NGP shares the Neo Geo name, and the Neo Geo Development Wiki and furrtek's work are set aside. Check the lineage of any new source (FPGA cores especially) before reading it. Boards to model this one on: **gameboy-python** (a handheld with a boot ROM, an LCD, a cartridge, save data), **sms-python** (a Z80 and an SN76489 relative), **pcengine-python** (a CPU core with on-chip peripherals kept in the board). Cores it embeds: [tlcs900h-python](../tlcs900h-python) (Phase 0 tonight; build its M1-M4 first) and [z80-python](../z80-python) (v0.4.0). ## Decided by Aubrey (standing rules) Name `ngp-python`, package `ngp_python`. Local, no remote until he says. Bring your own BIOS and cartridges, identified by SHA-1; nothing of SNK's in the repository. PyPy 3.11 floor; reference boards are correct, not fast; sound may be less exact than pictures; smoke lists before sweeps. Technical choices: made, marked provisional, reported. ## Proposed by this Phase 0 - **One board, two machine rows**: `ngp` (K1GE, NGP BIOS) and `ngpc` (K2GE, NGPC BIOS), plus a row per extra NGP BIOS image only if one behaves differently. - **The real BIOS by default**, from power-on with the power button as an input; an HLE BIOS only by decision 4. - **Two domains** (main, sound) with the channels of [domains-and-speed.md](domains-and-speed.md) as first-class structure from milestone 1. - **The T6W28 in the board** (decision 3), as a pin-shaped class built from sn76489-python's documented counter rules (not its code), lifted out on a second consumer. - **The SoC peripherals in the board** (tlcs900h-python decision 2): a `Soc` class owning 000000-0000FF. ## Package and classes (proposed) ``` ngp_python/ machine.py # rows: ngp / ngpc, BIOS SHA-1s, video chip, colour support console.py # build(machine, cartridge=None, bios=...) -> Ngp bus.py # the main CPU's map, waits, open bus; the Z80's map soc.py # Soc: SFR window: timers 0-3, interrupt controller, micro-DMA triggers, # serial, ports, watchdog, clock gear, ADC (battery), Z80/PSG control rtc.py # Rtc: BCD registers 0x90-0x97 kge.py # Kge: K1GE/K2GE registers, VRAM, line renderer, LED, interrupts sound.py # SoundDomain: Z80 (z80-python) + shared RAM + comm byte + T6W28 t6w28.py # T6W28: two ports, per-side attenuation, noise period register dac.py # the two 8-bit DACs flash.py # Flash: one chip (ID, program, erase, protect); Cartridge: one or two chips cartridge.py # header reader (licence string, entry, catalog, colour byte, title) controls.py # pad, Option, power button savestate.py # whole machine, naming BIOS and cartridge by SHA-1, flash deltas sockets.py # LinkPort (the link cable) and its test fake ``` ## The import boundary From **tlcs900h-python**: `read/write(address, size)` callables returning states, `interrupt_vector(level)`, `request_interrupt(level, vector)`, `request_dma(channel)`, `run(states)`, the trace line. From **z80-python** (v0.4.0, exists): bus callables, `request_non_maskable_interrupt()`, `request_maskable_interrupt(vector_byte)`, `request_reset()`; it is instruction-level, which is enough unless U5 needs cycle-level stalls (then a request upstream, with a repro). The board owns everything else. **Sockets**: the link cable (`LinkPort`), empty by default; the gate runs with it empty. ## Unknowns (U) | # | Unknown | Best source now | Status | | --- | --- | --- | --- | | U1 | SNK's development manual (cited by ares's comments) | not found | look for it; until then ares is the only complete account (tier 5) | | U2 | The CPU's state rate (tlcs900h-python U2); the K2GE's sprite line buffer and "character over" timing | MAME, ares | provisional 6.144 M states/s; character-over HYPOTHESIS | | U3 | The SoC's peripheral set and SFR addresses (TMP95C061-like per MAME, TMP95CS64F-like per Judge) | MAME's SFR names, ares cpu/io.cpp, Toshiba data sheets | follow ares's map (OBSERVED), check each register against a Toshiba part's chapter | | U4 | Wait states for flash, work RAM, VRAM, SFRs | none | 0, flagged HYPOTHESIS | | U5 | Shared-RAM arbitration between the CPUs (ares stalls the Z80 while the main CPU's MAR is in 7000-7FFF) | ares only | follow ares, OBSERVED; switchable | | U6 | T6W28 noise LFSR length, taps, reset value, white/periodic | ares only (15 bits, taps 0 and 2, 0x4000) | OBSERVED; a recording would settle it | | U7 | Monochrome shade curve and K2GE colour output (LCD response) | ares (linear 8 steps, inverted) | output register values; curves in the host | | U8 | Flash chips and IDs real cartridges carry; what the BIOS accepts | ares (Toshiba 0x98 for all) | OBSERVED | | U9 | Which licence strings the BIOS accepts | census: only the two SNK strings occur | run the BIOS on a patched header when the board exists | | U10 | Power-down, resume, the BIOS's shutdown path, battery ADC | ares partial | B4 | | U11 | RTC alarm or interrupt, registers above 0x97 | none | omitted until found | | U12 | Which NGP BIOS image shipped where ("(Monochrome)" vs plain vs beta) | No-Intro names | compare the images' bytes when the board runs them | | U13 | Why MAME 0.285's NGPC stays white under our recipe while the NGP shows its setup screen | our runs | try a longer power press or a saved NVRAM; not needed to start | ## Contradictions (C) | # | Contradiction | Sources | Handling | | --- | --- | --- | --- | | C1 | The CPU part: TMP95C061 (MAME) vs a TMP95CS64F-like 900/H (Judge) | see tlcs900h-python C1 | 900/H core, SoC in the board | | C2 | Z80 PSG ports: Mackintosh calls 4000 the noise register and 4001 the tone register; ares has 4000 = right and 4001 = left, each taking every command | M vs ares | consistent if tone periods only take effect from the left port and the noise period from the right (ares): Mackintosh describes the usual driver use, not the decode. Follow ares, tagged OBSERVED | | C3 | ares's header comment puts the first flash window at 0x020000-0x03ffff; its code, MAME, Judge and Gollum put it at 0x200000-0x3FFFFF | ares cpu/memory.cpp comment | a dropped digit in the comment | | C4 | Gollum: scroll maps "32 tiles x 20 tiles"; 0x800 bytes of 2-byte entries is 32 x 32 | T11 vs arithmetic, ares | 32 x 32 | | C5 | Sprite attribute byte: Gollum's `HVBPPPPT` vs ares's H, V, mono palette, priority (2), H chain, V chain, tile bit 8 | T11 vs ares | ares (a complete renderer); confirm with a game that chains sprites | | C6 | Judge's word write of AAAA/5555 to 0xB8 "disables/enables the Z80" vs ares's separate bytes B8 (PSG/DAC) and B9 (Z80) | JT vs ares | consistent: a word write covers both bytes | ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M1 | machine rows, bus and maps, the SoC register file (timers, interrupt controller), BIOS loading by SHA-1, the power button, tlcs900h-python pinned | O9; MAME trace (5) | the NGPC BIOS runs from reset with its instruction stream equal to MAME's 3-frame trace (66,779 lines) or a named reason per difference | | M2 | K1GE: registers, VRAM, line renderer, window, mono palettes, vblank interrupt | O3, O5 (3-5) | the NGP BIOS's language screen drawn as MAME draws it (pixel diff triaged) | | M3 | K2GE colour and compatibility mode; the NGPC setup and boot screens | O5 | NGPC BIOS screens drawn; a monochrome cartridge shown in compatibility colours | | M4 | the sound domain: z80-python pinned, shared RAM, comm byte, NMI/IRQ/INT5, T6W28, DACs | O4, O5 | the BIOS's boot sound: the T6W28 write sequence equal to MAME's (`Z` lines with ports) and audio compared with ares by ear and spectrum | | M5 | cartridges: one and two flash chips, the header, flash commands (saves), the RTC | O2, O5 | 5 smoke games (problem and well-behaved, including a 4 MiB one and Cool Cool Jam's mono proto) reach title screens; a save made in one survives a reload | | M6 | save states, the gate, seeded faults | | a state saved half way loads into a fresh machine and continues identically; every seeded fault in validation.md caught | | M7 | the link cable socket (two boards) | O5 | a two-player game links two boards in one process | Full No-Intro sweeps only when the board might be ready (smoke-sweep rule). ## The chip rule | Chip | Other users in or near the family | Proposal | | --- | --- | --- | | TLCS-900/H | none in the family (MAME uses the 900 family in other machines) | its own core, tlcs900h-python (CPU rule) | | Z80 | many | z80-python, pinned | | T6W28 | none | in this board (decision 3) | | K1GE/K2GE | none | in this board | | SoC peripherals, RTC, flash | none | in this board | ## Domains and speed Domains: **main** (TLCS-900/H, SoC, K1GE/K2GE, memory, DACs) and **sound** (Z80, T6W28), meeting only at the shared RAM, the communication byte, NMI, IRQ (timer 3), INT5, and the run/select bytes ([domains-and-speed.md](domains-and-speed.md) has the table, the sync points and the memory kinds). Instructions per second: main CPU 307,454 states / 66,779 instructions = 4.6 states each; 6,144,000 / 4.6 = **1.34 M/s** (3.07 M/s worst case at 2 states each; half if U2 says 3.072 M states/s). Z80 3,072,000 / ~6 = **~0.5 M/s**. K2GE 160 x 152 x 59.95 = 1.46 M pixels/s. The no-JIT path: **Rust -fast reaches real time on desktop, phone and browser with `recompile: none`** (family Rust cores run 190-390 M instructions/s native, over 100x the need; phones and wasm at a third to a half of that still leave tens of times headroom). A cached interpreter and threads at the channels are not needed; the domain split is declared so a thread can take the sound domain if run-ahead on a weak device ever needs it. The Python reference on PyPy may approach real time but is not promised. ## Open decisions for Aubrey 1. **Board name and scope: `ngp-python` covering both the NGP and the NGPC** as machine rows of one board. Recommendation: yes (one design, the K2GE a superset with a compatibility mode). 2. **Sources with no hardware tier**: the board's video, sound and cartridge behaviour will rest on 2000-era emulator authors' notes (tier 3 at best) and ares (tier 5, ISC, readable) until SNK's development manual (cited in ares's comments) or hardware captures appear. Recommendation: accept, tag every such fact OBSERVED (ares and revision) or DOC (the note), and look for the SNK manual's provenance before reading it if it turns up. 3. **The T6W28: in this board, or a variant in sn76489-python?** It needs two latches, per-side attenuation and a separate noise period, which the frozen `PSGVariant` record does not express. Recommendation: in-board now (chip rule: one user), lift it into sn76489-python as a sibling class if a second family board needs it. 4. **HLE BIOS**: the real BIOS by default; an HLE BIOS (system calls, boot) only if players without a BIOS are a goal. Recommendation: not now; revisit for play-ngp. 5. **No NGP hardware in the family**: the timing unknowns (U2, U5) and the T6W28 noise (U6) can only be settled by a probe ROM on a real NGPC. Recommendation: proceed provisional; add an NGPC to the probe wish list only if a game's behaviour turns on one of them.