# Handoff: build `wonderswan-python`, the WonderSwan board ## Context you are inheriting 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 a file between repositories, pin the sibling by commit (charter). The WonderSwan is era 1 in the charter's table: one clock domain, cycle level everywhere, Python runs the tests, Rust plays. Cores it embeds: **v30mz-python** (`../v30mz-python`, Phase 0 done the same day; build it first, to its M2). Boards to model this one on: **gameboy-python** (a handheld SoC, boot ROMs by SHA-1, skip-boot versus real-boot pass lists, a host-side colour curve) and **pcengine-python** (card mappers, save states naming images by hash). Read in order before writing code: 1. [README.md](../README.md), [start-here.md](start-here.md), [memory-and-io.md](memory-and-io.md). 2. [display.md](display.md), [sound.md](sound.md), [eeprom-rtc.md](eeprom-rtc.md), [cartridges.md](cartridges.md), [boot-rom.md](boot-rom.md). 3. [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` (56 WSdev pages by revision, WSMan, STSWS, WStech, MAME's software lists, ws-test-suite, FluBBa's three tests, ares' `ws`; about 5 MB). The WSdev pages land as wikitext in `reference/wiki/`. ## Decided by Aubrey (standing rules that apply here) - Name `wonderswan-python`, package `wonderswan_python` (the x-python naming rule). - Local, no remote, until he says otherwise. Bring your own boot ROMs, EEPROM images and cartridges; nothing of Bandai's in the repository. - Python floor = PyPy's (3.11), PyPy must work; modern over compat; reference boards are correct, not fast; sound may be less exact than pictures; smoke lists before sweeps. - Technical choices are made, marked provisional and reported; policy waits for him. ## Proposed by this Phase 0 - **Machine rows from milestone 1:** `ws` (ASWAN), `wsc` (SPHINX), `sc` (SPHINX2), `pcv2` (ASWAN, no internal EEPROM, its keypad matrix, PCV2 pin-strapped boot), each with its boot ROM SHA-1, RAM size, colour features, LCD kind and CPU row (`soc=` for v30mz-python). - **Mono first, then colour.** ASWAN is the smaller SoC and its boot ROM boots without a cartridge in MAME, which gives the first detector run; SPHINX adds 4bpp, DMA, Hyper Voice and 64 KiB in milestone 4. - **One SoC domain** stepped per master-clock group of four quadrants (CPU, display, sound, palette) in the order WSDEV's frame timing gives; the RTC is a second domain met only at its serial port and IRQ line. - **The picture is always 224 x 144**; the board exports the LCD icon state (orientation, sleep, sound, cartridge) and the host rotates. - **Colour reproduction is the host's**: the board outputs shade numbers or 12-bit colours; curves per machine row live in the host (decision 6). ## Package and classes (proposed) ``` wonderswan_python/ machine.py # machine rows: SoC, RAM size, boot ROM SHA-1, EEPROM size, LCD kind, keypad matrix console.py # build(machine, cartridge=None, boot_rom=..., eeprom=...) -> WonderSwan bus.py # 20-bit memory map, cartridge bus width and waits, boot ROM lockout soc.py # system control ports (A0h, 60h, 62h), the quadrant scheduler interrupts.py # B0h-B7h: latches, enables, acknowledge, vector, NMI control timers.py # HBlank and VBlank timers (WSHW's order of operations) dma.py # general and sound DMA (colour) display.py # screens, sprites (line-144 copy), windows, palettes, LCD and icons sound.py # four channels, noise, sweep, voice, mixer, speaker and headphone outputs hypervoice.py # colour only eeprom.py # M93LCx6 Microwire device + the SoC/2001 controllers keypad.py # matrix scan (WS and PCV2 layouts) uart.py # serial port with a LinkSocket (null device by default) cartridge.py # header decode, CartridgeSlot, mappers: Bandai2001, Bandai2003, Karnak, WonderWitch flash rtc.py # S-3511A behind the 2003's ports savestate.py # whole-machine states naming boot ROM and cartridge by SHA-1 ``` ## What the board needs from v30mz-python 1. A bus with per-access **wait clocks** returned by the host and an access `kind` (fetch, data, stack, I/O), so the board charges cartridge width and wait states and stalls the CPU for DMA. 2. INT level input, NMI, an acknowledge callback the interrupt controller answers with its vector. 3. `soc="aswan" | "sphinx"` for the MULU ZF row. 4. HALT that wakes on INT (the power-off request in port 62h depends on it). 5. A way to stall the CPU for N clocks (general DMA, sound DMA's slot) without breaking instruction timing: the board owns time, the core reports clocks. ## Sockets `CartridgeSlot` (mapper devices), `LinkSocket` (UART), `EepromImage` (internal EEPROM contents supplied by the player or formatted fresh), `BootRom` (by SHA-1). Each has a test-only fake. With the cartridge slot empty, the `ws` row boots its boot ROM to the "no cartridge" state, which is the base gate. ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M1 | **Bus, SoC ports, interrupts, timers, keypad, internal EEPROM, display timing skeleton (line counter, line match, VBlank), the `ws` row with no cartridge** | WSDEV, WSHW (3); MAME trace and I/O log (5) | the ASWAN boot ROM runs its first 3 frames with instruction stream and I/O writes equal to MAME's, or a named reason per difference; B0 unit tests pass | | M2 | **Mono display**: screens, sprites with the line-144 copy and 32-per-line rule, windows, shade LUT and palettes, LCD icons and latches; the 2001 mapper with SRAM | WSDEV (3); WSTS mono display tests (3); MAME snapshots (5) | the boot ROM's splash drawn; ws-test-suite's mono display tests pass; three smoke games reach their titles | | M3 | **Sound** (four channels, noise, sweep, voice, mixer, speaker shift) and the cartridge EEPROM | WSDEV, WSHW sound tests (3) | WSHW's noise and sweep tests pass; audio of two games compared with MAME, differences named | | M4 | **Colour**: SPHINX row, 64 KiB, 2bpp/4bpp/packed modes, colour palette and its quadrant, general and sound DMA, Hyper Voice; the `wsc` boot ROM with the internal EEPROM splash | WSDEV (3); WSTS color tests (3) | the colour boot ROM boots to a cartridge; ws-test-suite's color tests pass; DMA costs as unit tests | | M5 | **Bandai 2003**: wide banks, GPO, self-flash, the S-3511A; WonderWitch flash | WSDEV (3); the RTC's data sheet (2) | an RTC game keeps time across a save state; FreyaOS boots from a WonderWitch image | | M6 | **SwanCrystal and PCV2 rows**, KARNAK (timer IRQ, ADPCM), save states | WSDEV (3) | a PCV2 cartridge reaches its first drill; a save state loads into a fresh machine and continues identically | | M7 | Smoke list grown from failures; full No-Intro sweep only when the board might be ready (Aubrey's rule) | B7 | the smoke list's games reach gameplay | ## The chip rule | Chip | Users in or near the family | Proposal | | --- | --- | --- | | NEC V30MZ | WonderSwan, PCV2 only | its own core, v30mz-python | | Display, sound, Hyper Voice, DMA, interrupt controller, timers (all inside ASWAN/SPHINX) | WonderSwan only | stay in the board | | M93LCx6 Microwire EEPROM | possibly Atari Lynx carts (93C46) | board class with a pin-shaped interface; extract on a second user (decision 5) | | Seiko S-3511A RTC | GBA carts (Pokémon) | same; the likeliest second user is gba-python (decision 5) | | Bandai 2001/2003, KARNAK | WonderSwan only | board | ## Unknowns (U) and conflicts (C) | # | Unknown | Best source now | | --- | --- | --- | | U1 | Mid-line register writes and when they take effect | WSDEV "Frame timing" (partly TODO) | | U2 | No hardware here: no probe ROM can be run on a real unit | none; WSTS/WSHW verdicts are their authors' | | U3 | Speaker main volume levels (9Eh) | WSDEV TODO | | U4 | ASWAN and SPHINX startup tables (only SPHINX2's default-splash start is recorded) | WSDEV "Boot ROM"; the real boot ROM's own run will fill it | | U5 | What mono RAM reads above 3FFFh; open-bus values | STSWS "Open Bus" | | U6 | Timer test mode (A3h) | WSDEV ("exact behaviour still unknown") | | U7 | How SPHINX rewrites EEPROM commands in mono mode | WSDEV TODO | | U8 | ASWAN internal EEPROM on non-word access; the cartridge controller's buggy Done bit | WSDEV errata | | U9 | S-3511A serial timing; rtctest's verdicts | the data sheet (not fetched); the forum's rtctest | | U10 | Whether any boot ROM checks the header checksum | WSDEV marks it unvalidated | | U11 | Telling KARNAK carts from 2001 carts (both mapper 0) | a per-image table | | U12 | The WonderWitch flash command timing | WSDEV, the MBM29DL400TC data sheet | | U13 | Whether the PCV2 has its own boot ROM | MAME marks the WS image baddump in `pockchv2` | | # | Conflict | Sources | For now | | --- | --- | --- | --- | | C1 | Taken-branch cost (CPU) | PUM vs MAME | see v30mz-python C1 | | C2 | Cartridge clock bit: 384 kHz or "6 MHz?" | WSDEV marks its own value uncertain | 384 kHz default, the other tagged HYPOTHESIS | | C3 | Save byte 01h: 64 Kbit (common docs) or 256 Kbit (all known carts) | WSDEV | 32 KiB, as the carts carry | ## Domains and speed Summary of [domains-and-speed.md](domains-and-speed.md): one SoC domain (four 3.072 MHz quadrants of a 12.288 MHz clock), an RTC domain on its own crystal met at a serial port and an IRQ, EEPROMs and the UART as channels, video and audio as pure producers. The CPU needs about **1.36 M instructions/s** typical (3,072,000 / 2.26 clocks per instruction, from MAME's boot-ROM frame), **3.07 M/s** at most; the display **2.43 M pixels/s**; the sound 24,000 samples/s. The no-JIT path (Rust `-fast`) reaches real time on desktop, phone and browser by a wide margin; `recompile: none`; threads only for the producers if a phone asks. Not measured yet. ## Open decisions for Aubrey 1. **Build order.** Recommendation: v30mz-python to its M2 (WSCpuTest Ok), then this board from M1; the CPU's prefetch model (its M5) waits for a board test that needs it. 2. **Machine scope.** Recommendation: WonderSwan, WonderSwan Color, SwanCrystal and the Pocket Challenge V2 as rows of this one board (WSMAN: the PCV2 is the ASWAN with another keypad); no separate `pcv2-python`. 3. **Real boot ROM or skip-boot for pass lists.** Recommendation: real boot ROMs by default (all three are in hand by SHA-1); a skip-boot start only for the SPHINX2 row until the board's own boot runs fill the ASWAN and SPHINX tables. 4. **Internal EEPROM images.** MAME's images are marked baddump and hold owner data. Recommendation: never ship one; format a fresh image (blank owner, splash off) when the player supplies none; the boot ROM then runs its first-time setup as a new console would. 5. **Shared chips.** Recommendation: keep the M93LCx6 EEPROM and the S-3511A RTC in this board behind pin-shaped classes; extract the RTC when gba-python needs it (Pokémon's carts), the EEPROM if lynx-python does. 6. **Colour curves.** Recommendation: the board outputs raw shades/colours; hosts carry per-model curves (FSTN near-linear, CSTN mono-mode table, TFT gamma 2.2) as named host settings, not as board behaviour. 7. **MAME's WonderSwan files are BSD-3-Clause by their headers.** This batch's brief holds them closed. Recommendation: keep them closed (WSdev, WSMan and STSWS cover the board, and MAME's CPU fails WSCpuTest); revisit per file only if a question survives B0-B4. 8. **FluBBa's tests have no licence.** Recommendation: Aubrey asks him for one (or for WSTimingTest's hardware numbers); until then they are run and their README facts restated, never copied or redistributed. 9. **WSdev is on nesdev.org.** Reading is fine (CC0, no AI rule on the wiki); any finding to give back goes through Aubrey in person, as the NESdev forum bans AI-written posts. Recommendation: keep a findings list in this repository for him.