# Handoff: build `nds-python`, the Nintendo DS board ## Context you are inheriting Chips are cores, boards compose cores; the board pins its cores by commit and raises the cores' corners on the cores (charter). This board composes two CPU cores: - `../arm946es-python` (Phase 0 done tonight): the ARM9. Its brief proposes the AHB-side bus contract this board implements, and decides (Open decision 1 there) that it is a new core, not an axis on arm7tdmi-python. - `../arm7tdmi-python` (built, rungs 0-4 pass): the ARM7, used unchanged with the GBA's per-cycle contract. The DS sits between eras 2 and 3 (charter table): two CPUs, one at 67 MHz with caches, a 3D engine. Python will run at roughly 1/20 to 1/100 of real time (domains.md), so cases are short and targeted; the Rust tiers carry the playing. Nearest models in the family: - `../gba-python`: the 2D engines' ancestor (its PPU, line and dot levels), DMA, timers, IRQs, keys, the cartridge-backup and RTC patterns, the test-ROM-on-screen judging, the Nintendo test-program rule (run in place, never read). - `../psx-python` and `../saturn-python` (Phase 0s): the domains-and-speed document and the brief's shape; `../sega32x-python` docs/domains.md for catch-up at channels. - `../segacd-python`: a board that runs a second CPU domain inside the first's order of record. Rules: Python >= 3.11, CPython and PyPy; MIT; nothing read from GPL/LGPL/MPL emulators (melonDS, DeSmuME, NooDS, DualSOUP); MAME's BSD-3 files closed (Open decision 11); no BIOS, firmware, cartridge, save or Nintendo artwork in the repository (MAME and test-program screenshots stay in git-ignored `tests/mame_traces/`); `NO_INTRO_DIR`, `MAME_ROMPATH`; local git only; no sweeps over the library. ### Read in this order README.md; docs/start-here.md; docs/domains.md; docs/memory-map.md; docs/cpus-and-ipc.md; docs/video-2d.md; docs/video-3d.md; docs/system.md; docs/sound.md; docs/peripherals.md; docs/cartridge.md; docs/boot.md; docs/census.md; docs/validation.md; docs/mame-oracle.md; docs/provenance.md; docs/dsi.md; then arm946es-python's handoff-brief.md (the bus contract) and GBATEK's DS chapters for the milestone at hand. ## Scope In: everything on the DS's buses except the two CPU cores: memory and its control (maps, WRAMCNT, VRAMCNT, EXMEMCNT, BIOSPROT, timing), IPC, both interrupt controllers, DMA, timers, the maths unit, keys and hinge, POWCNT and HALTCNT, the two 2D engines with capture and master brightness, the 3D geometry engine and rasteriser, sound with capture, the SPI bus with the firmware flash, touchscreen and power management devices, the RTC, the microphone input, the DS slot (protocol, KEY1/KEY2, the save chips, infrared and NAND cartridges), the GBA slot and its accessories (late), BIOS and firmware loading, direct boot, and the wireless controller's registers as a console with no partner answers them. Model rows: `ntr` (DS) and `ntr-lite` (DS Lite: TSC, power management, wireless chip and LCD differences) from the start; `twl` (DSi) later (dsi.md). Out: the CPU cores; GBA mode (a gba-python model row, Open decision 6); working wireless (late); hosts, windows, sites. ## Reuse first (the family's existing cores and boards, chip by chip) | DS chip or block | Existing in the family | What it needs | | --- | --- | --- | | ARM946E-S | arm946es-python (Phase 0 tonight) | to be built from its brief | | ARM7TDMI | arm7tdmi-python (built) | nothing new: the board supplies DS wait states through its per-cycle contract | | 2D engines | gba-python's PPU (inside gba-python, single machine) | knowledge carries, code does not (video-2d.md); extraction of a shared engine is Open decision 5 | | DMA, timers, IRQ, keys | gba-python (inside the board) | re-specified here from GBATEK's DS chapters with gba-python's judged behaviours as reading | | RTC (Seiko S-35180) | gba-python has the cartridge RTC S-3511A | a different part and interface: nothing to share | | GBA mode (DS as a GBA) | gba-python with gb-apu-python's AGB row | a DS model row there (16.76 MHz clock, the DS's GBA BIOS, no link port, the screen border) | | sound unit, 3D engine, TSC, firmware flash, power management, wireless | none | new, inside this board (single-machine chips; chip rule) | | DSi XpertTeak DSP | none | a new CPU core (`teak-python`) when the DSi row starts | No other family core (z80, m68000, mcs48, tms9918, ay8910, ym2151, ym2413, sn76489, sh2, i8086, huc6280, ...) has a part in the DS. ## Proposed names - Package **`nds_python`**; `nds_python.NDS` (the board), constructed with `model` (`"ntr"`, `"ntr-lite"`; later `"twl"`), `bios9`, `bios7`, `firmware` (paths or bytes the player supplies, or `None` for direct boot with synthesised firmware data), and a cartridge. - Modules: `bus.py` (both CPUs' maps, EXMEMCNT, WRAMCNT, BIOSPROT, timing, arbitration), `vram.py` (banks and mappings), `ipc.py`, `irq.py`, `dma.py`, `timers.py`, `maths.py`, `keys.py`, `power.py`, `gpu2d/` (`engine.py` x2, `capture.py`, `timing.py`: dot and line counters, VRAM slots), `gpu3d/` (`geometry.py`, `fifo.py`, `rasterizer.py`), `sound/` (`channel.py`, `adpcm.py`, `mixer.py`, `capture.py`), `spi.py` with `firmware.py`, `touch.py`, `powerman.py`; `rtc.py`; `cartridge/` (`slot.py`: ROMCTRL and transfers; `key1.py`, `key2.py`; `rom.py`: image, secure-area re-encryption; `backup.py`: EEPROM, FLASH, FRAM; `infrared.py`, `nand.py` later); `gbaslot.py`; `wifi.py` (register stubs); `boot.py` (real and direct boot); `domains.py` (the channels as code); `machine.py`. - Sockets (pin-shaped interfaces, so later extraction is a move): the two CPUs; the DS slot (`Cartridge` protocol: commands in, bytes out, plus a save-chip SPI device); the GBA slot (`Accessory` protocol); SPI devices; the microphone source; the touch input. ## Import boundary The board imports `arm946es_python` and `arm7tdmi_python` and nothing else of the family's. The cores never import the board. The ARM9 core calls the board for AHB transactions and receives their cost in bus cycles; the ARM7 core calls the board once per cycle. ## Unknowns (U) and contradictions (C) Collected from the documents (each item is detailed where it lives): - memory-map.md U1-U5, C1: overlapping VRAM banks; main-RAM arbitration; the nonsequential penalty; ARM7 VRAM timing "1?"; empty ARM9 WRAM reads; ARM9 BIOS size. - cpus-and-ipc.md U1-U3: arbitration per cycle; IPCFIFO latency; IPCSYNC IRQ latency. - video-2d.md U1-U6: mid-line latching; OBJ cycle budget; 3D/2D blending; brightness rounding and LCD colours; capture timing; engine B's reserved modes. - video-3d.md U1-U6, C1: rasteriser arithmetic (no judge); per-line budget; NORMAL's timing; PIPE stalls; geometry precision; model differences; GBATEK vs ndsdoc. - sound.md U1-U5, C1: ADPCM clamping; mixer rounding; capture timing; noise; sound fetches' contention; the output rate (1,024 bus cycles?). - system.md U1-U4: DMA priorities; DMA start latency; maths timings unchecked; disabled engines. - peripherals.md U1-U4: /PENIRQ IRQ; TSC conversion timing; flash busy times; wireless register answers. - cartridge.md U1-U5, C1: transfer timing per word; chip IDs; secure-area reads after boot; save types; NAND commands. - boot.md U1-U3: post-boot state; firmware differences; free BIOS SWI differences. - dsi.md U1-U3. ## Milestones M1 bus and boot skeleton; M2 2D engines (line level); M3 DMA, cartridge, KEY1/KEY2, saves; M4 geometry engine; M5 rasteriser; M6 sound and capture; M7 peripherals; M8 timing pass; M9 seeded faults; later wireless, the DSi row, GBA mode in gba-python. Exit tests in validation.md. ## Domains and speed (Full arithmetic in domains.md.) **Domains and channels.** One **CPU-and-bus domain** holds both CPUs, all memory, DMA, timers, IRQs, IPC, the maths unit, SPI and the RTC, the slot engines, the 2D engines' front ends (registers, counters, VRAM slots), the geometry engine (its FIFO and timing are CPU-visible) and the sound registers with their busy bits. **Producers** beside it: the 2D back ends, the 3D rasteriser, the sound mixer (pure producers from register logs, VRAM and polygon RAM), and later the wireless domain. **Joins**: CPU or DMA reads of VRAM banks the capture unit writes, CPU reads of sound-capture buffers. Everything else is a log the producers drain. **Instructions per second.** ARM9: ceiling 67 M/s, planning 40 M/s (MAME's flat model 22.3 M/s, OBSERVED); ARM7: ceiling 33.5 M/s, planning 10 M/s; **planning 50 M/s, ceiling 100 M/s** for the CPU domain (HYPOTHESIS until measured on games). **The no-JIT path.** arm7tdmi-rust measured 34-40 M instructions/s native, 41.5 M as wasm (2026-11-25); arm946es-rust estimated 25-40 M/s; `-fast` adds x1.4-1.9. At the planning mix one `-fast` CPU domain needs 0.66-1.36 cores on this desktop: **marginal**; the turbo tier's **cached interpreter** (exact for I-cache-fetched ARM9 code; ITCM, uncached and ARM7 code invalidated on writes, all of which the core or bus sees) brings it to 0.22-0.9 cores. Producers (3D 6-36% of a core, 2D and sound under 12%) go to other threads. Verdict: **desktop and iPhone app reach real time without JIT** (the iPhone with the cached interpreter); **browser on desktop marginal; browser on iPhone and low-cost ARM handhelds need recompilation** (`recompile: wasm | native`, off by default, allowed where the target permits). All estimates until nds-rust measures them. ## Open decisions for Aubrey 1. **One CPU-and-bus domain for both CPUs**, with the 2D back ends, the 3D rasteriser and the mixer as producers. Recommended (domains.md): the CPUs share polled memory and edge-triggered IPC; splitting them would put a sync point on every shared access. 2. **Boot paths: real boot (player's BIOS and firmware) as the reference, direct boot as the convenience path judged against it.** Recommended: the real boot exercises KEY1, the firmware and the BIOS as they are; direct boot's post-boot state is a list from GBATEK that the real boot can check value by value (boot.md U1). 3. **The free BIOS** (DraStic's replacement, BSD-2-Clause, Gilead Kutnick 2013, kept in melonDS's GPL tree with its own notice): recommended: adopt it only for direct boot without the player's BIOS, fetching **only** its BSD-2 files by commit after checking each file's header (or have emu-tools fetch and build it), and say in provenance.md that it came through a GPL repository. Alternative: no free BIOS; the player supplies one. 4. **Save types: detect from the game's commands, with an override table we build from our own runs; no third-party database** until one is found with a licence we can honour. Recommended: the header and DAT carry nothing (census.md), and detection is the hardware-faithful path (the cartridge answers what it is). 5. **The 2D engines stay in nds-python; revisit a shared "AGB/NTR 2D engine" core after M2.** Recommended: same lineage, different chip (video-2d.md); extract with history only if most of the code turns out the same. 6. **GBA mode belongs to gba-python as a DS model row**, not to nds-python. Recommended: in GBA mode the DS is a GBA (ARM7 at 16.76 MHz, the GBA's video and sound, the DS's GBA BIOS differing in one byte, no link port, a border from VRAM A/B). 7. **Decided by Aubrey, 2026-11-26: a contributions policy, not an objection to use.** "He isn't banning AI usage, but he clearly does not want us to commit." We honour it by never submitting anything to Jakly's projects; polyrastertest (MIT) and ndsframereplay (CC0) are usable under their licences, and polyrastertest is the 3D rasteriser's tier-3 judge (emulator-lineage sources.md, "no AI contributions (honoured)"). The original note follows. **Jakly's work set aside, pending your word** (provenance.md): DualSOUP's CONTRIBUTING "AI Policy" says "any AI usage will be frowned upon and rejected". Its scope is contributions to DualSOUP. Recommended: treat it as an objection to AI use of Jakly's work and leave all of it out (DualSOUP, polyrastertest, ndsframereplay), accepting that the 3D rasteriser then has **no hardware-recorded judge** until another is found or made. If you read it as contributions-only, polyrastertest (MIT) becomes the rasteriser's tier-3 judge. 8. **The 3D rasteriser at functional per-line level with a per-line budget**; the geometry engine at command level with cycle costs. Recommended (video-3d.md): what the CPU can see (GXSTAT, IRQ, RDLINES, underflow) is modelled in time; the pixels are a producer. 9. **Wireless out of scope until late**, its registers answered as on a console with the radio idle. Recommended: Download Play and Wi-Fi need a second console or a network model, a project of their own. 10. **The DSi as a later row of this board, with three switches built in from the start** (ARM9 clock ratio, memory sizes, SCFG revisions); the XpertTeak DSP becomes its own core repository then. Recommended (dsi.md). 11. **MAME's BSD-3 nds and ARM files stay closed.** Recommended: MAME's nds has no video or media and a flat CPU timing model; nothing it holds is needed. 12. **Speed answer, to accept as the plan:** no JIT needed on desktop or as an iPhone app (cached interpreter in turbo); recompilation only for browsers on phones and low-cost handhelds, off by default. All estimates until nds-rust measures them. 13. **Default provisional choices** (the 2026-11-12 rule), each HYPOTHESIS in code until judged: overlapping VRAM banks read as the OR of the banks; an IPCFIFO word visible to the receiver one bus cycle after the send; the sound output period 1,024 bus cycles; main-RAM arbitration by EXMEMCNT bit 15 then DMA channel order, with GBATEK's 3-cycle nonsequential penalty charged by the board. 14. **Probe queue for a real DS** (if one is ever in reach): asiekierka's cache test, a VRAM-overlap read, IPCFIFO latency, the sound output period, GXSTAT timing after a command burst. Recommended: queue; nothing blocks on them.