# Handoff: build `dkong-python`, the Nintendo Donkey Kong board ## Context you are inheriting The family's shape (emulator-lineage `docs/charter.md`): chips are cores, boards compose cores, CPUs always get their own core, a non-CPU chip used by one board stays in it; siblings are pinned by commit, never copied from. Era 1 in the charter's table, with two CPUs on separate clocks. Read [start-here.md](start-here.md) first. Reading order: [start-here.md](start-here.md), [video.md](video.md), [dma.md](dma.md), [sound.md](sound.md), [rows.md](rows.md), [cpu-cores.md](cpu-cores.md), [sources.md](sources.md), [validation.md](validation.md), [mame-oracle.md](mame-oracle.md), [census.md](census.md), [provenance.md](provenance.md). Then `python -I scripts/fetch_references.py` (13 files, about 55 MB; `--verify` to check). Read schematic sheets as images (`pdftoppm -r 150`); the parts lists' OCR is good. Dependencies: z80-python (built); **mcs48-python (Phase 0: its milestones 1-5 must land before this board's sound milestone)**; 6502-python's `RP2A03` and an APU (DK3 only); s2650-python (Phase 0; conversion rows only). ## Decided by Aubrey (standing rules) Name `dkong-python`, package `dkong_python`; local, no remote; romsets through `MAME_ROMPATH` by SHA-1, nothing of Nintendo's in the repository; PyPy floor and support; modern over compat; reference boards correct, not fast; sound may be less exact than pictures; smoke lists before sweeps; technical choices made, marked provisional and reported. ## Scope TKG-4 Donkey Kong (`dkong`) at cycle level: Z80, the 8257 and its bus takeover, the latches, NMI, inputs and DIPs, tiles and the sprite engine per line, the MB8884 sound CPU with its DAC and the discrete circuits; then Donkey Kong Junior and Radar Scope (TRS02) as rows, Radar Scope TRS01's speech, the conversions, and Donkey Kong 3's DKC1 board. ## Package and classes (proposed) ``` dkong_python/ machine.py # rows: CPU board (TKG-4, DKC1), ROM layouts and SHA-1s, latches, sound board board.py # Board: the main domain (Z80, 8257, video) and the sound domain; run_frame() bus.py # main map, the 74LS259s, inputs; bus takeover for DMA i8257.py # I8257: registers, F/L flip-flop, mode set, status, cycles (dma.md) video.py # Video: tiles; the sprite engine's per-line search and line buffer sound.py # SoundBoard: MB8884 (mcs48-python) host, DAC, external decay, discrete circuits dk3/ # DKC1: Z80 at 4 MHz, Z80DMA, two RP2A03 + APU, their latches romset.py savestate.py ``` ## What the board needs from its cores - **z80-python**: NMI exists; BUSREQ does not: the board takes the bus between instructions for the DMA and charges the time ([dma.md](dma.md)). Request a machine-cycle grant only if traces need it. - **mcs48-python**: exactly the host contract and milestones of its own handoff brief ([cpu-cores.md](cpu-cores.md)): an 8035-type core (romless, 64 B RAM, EA high), `read_program`/`read_ext`/ports/BUS/T0/T1, `set_int(level)`, `step()` in machine cycles, the timer. Nothing more. - **6502-python** `RP2A03` (exists) and an APU (open decision 4) for DK3. ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M1 | **Main domain skeleton**: romset loader, map, latches, NMI, inputs, the 8257 with bus takeover; no picture or sound | DKS, DKM, I8257 (2); MAME trace (5) | lockstep with MAME's `dkong` main-CPU trace from reset and at frame 600 (instructions, DMA/latch/control writes, NMI lines): `0 unexplained`; 8257 unit tests; seeded faults caught | | M2 | **Picture**: tiles, palette banks, the sprite engine per line from the video sheets (U3-U5), flip | DKS (2); MAME snapshots (5) | attract screens and the "how high can you get" screen pixel-identical to MAME at fixed frames, or each difference explained from the sheets | | M3 | **Rows and smoke**: `dkongj`, `dkongjr`, `radarscp`; save states | DJS, RSS (2) | each boots to attract; states reload identically | | M4 | **Sound CPU** (after mcs48-python's M1-M5): the MB8884 host, DAC with cycle times, ROM banking, T0/T1/P2.5/BUS/INT | DKS sound sheet (2); MAME sound-CPU trace (5) | sound-CPU lockstep with MAME over mcs48-python's 460-frame window, including a played game: `0 unexplained`; DAC stream equal | | M5 | **Discrete sounds and the analog stage** (walk, jump, stomp, external decay) as our own circuit models | DKS values (2); MAME audio (5) | each trigger sounds; spectra compared, FITTED constants in one table | | M6 | **Radar Scope TRS01** speech (M58817 + M58819) | RSS (2); a TMS5100-family document (to find) | speech plays | | M7 | **Donkey Kong 3** (DKC1): Z80 at 4 MHz, Z80 DMA, two RP2A03s with the APU | DK3M (2), ZDMA (2); MAME (5) | `dkong3` lockstep for the main CPU and both 2A03s; APU output compared | | M8 | **Conversions** (2650, SN76496, YM2151 rows) when their cores exist | MAME (5) | rows boot | ## The chip rule | Chip | Other users in the family | Proposal | | --- | --- | --- | | Z80A | many | z80-python | | MB8884 (8035) | other MCS-48 boards (mcs48-python's list) | mcs48-python (Phase 0) | | Intel 8257 | none today | in the board (`i8257.py`) | | Zilog Z80 DMA | none today (other Z80 arcade and computer boards could use it later) | in the board (DK3), pin-shaped | | M58817 | none | in the board (TRS01 row) | | RP2A03 CPU | famicom-python | 6502-python's `RP2A03` | | 2A03 APU | **famicom-python (in its board)** | the second user: extract (open decision 4) | | discrete sound | per board | in the board | ## Domains and speed **Domains:** two for Donkey Kong. (1) The **main domain** on the 61.44 MHz master: the Z80 (/20), the 8257 (sharing the Z80's bus), the video (/10) and the sprite engine. (2) The **sound domain** on its own 6.000 MHz crystal: the MB8884, its ROM, the DAC and the analog stage. **Channels** between them: the tune latch (7C00 → BUS), the 74LS259 bits read by the MB8884 (T0, T1, P2.5) and those that fire discrete circuits, the interrupt (7D80 → INT) and possibly one status line back to the main CPU (claimed only by Computer Archeology's "(AI)" page: to confirm from DKS, U10). Every channel is a latch the main CPU writes and the sound CPU samples, and the return line is a single bit, so the sound domain can run ahead to the next main-CPU write of a channel. In the order of record the board steps the Z80 one instruction, runs any DMA it has requested, advances the beam, then steps the MB8884 until its time catches up (6.000 MHz against 3.072 MHz, converted at each channel event). DK3 has three CPU domains (the Z80 and two 2A03s on the 21.47727 MHz crystal) meeting at latches. **Instructions per second:** Z80: 3,072,000 T-states a second, at most 768,000 and typically about 400,000 instructions. MB8884: 6,000,000 / 15 = 400,000 machine cycles a second; at one or two cycles an instruction, about 270,000-400,000 instructions. DMA: about 385 transfers a frame, about 23,000 a second. Video: 264 x 60.606 = 16,000 lines a second, each with a sprite search. Total: under a million instructions a second across both CPUs. DK3: Z80 at 4 MHz (about 500,000 a second) plus two 2A03s at 1.79 MHz (about 600,000 together) plus two APUs stepped at 1.79 MHz. **No-JIT path:** Python on PyPy runs z80-python at about 2.5 M instructions a second (its target, its validation.md); the board as a whole will be slower than real time in Python and that is accepted (reference boards are correct, not fast). A Rust transcription needs about 1 M instructions a second across two cores; z80-rust-fast runs about 350 M Z80 instructions a second on this machine: **real time on desktop, phone and browser with x-rust alone; `-fast` only if the discrete-sound models are expensive; threads (sound domain on its own) optional.** recompile: none. ## Unknowns (U) and conflicts (C) | # | Unknown | Best source now | Status | | --- | --- | --- | --- | | U1 | the clock divider chain (61.44 MHz to 3.072 and 6.144 MHz) | DKS | MAME's values agree with /20 and /10 | | U2 | the main memory map in gates, mirrors, open bus, any CPU waits | DKS CPU sheet | MAME-derived map until traced | | U3 | the sprite engine (ECL line buffer, MB93419) | DKS video sheets | trace at M2 | | U4 | the tile colour rule (PROM addressing, palette bank) | DKS video sheets | trace at M2 | | U5 | sprites per line, priority, edges | DKS; MAME detector | trace at M2 | | U6 | Radar Scope's grid/star background | RSS | at M3 | | U7 | the 8257 terminal-count type bits (scan of I8257's table) | I8257 scan | read at M1 | | U8 | how TKG-4 pairs two 8257 channels for memory-to-memory and holds the byte | DKS CPU sheet | trace at M1 | | U9 | when the Z80 grants the bus within an instruction | Zilog's Z80 manual; DKS | between instructions until a trace needs more | | U10 | the sound CPU's port wiring (from MAME via mcs48-python) | DKS sound sheet | confirm at M4 | | U11 | which 74LS259 bit fires which discrete sound | DKS sound sheet | at M5 | | U12 | an M58817 / TMS5100-family document we may use | none found tonight | before M6 | | U13 | DK3's "PC2A03" revision and its APU differences (the letterless RP2A03 has no tonal noise: famicom-python) | DK3M; famicom-python's revision table | before M7 | | # | Conflict | Sides | Proposal | | --- | --- | --- | --- | | C1 | the sound CPU's identity | common description: "an Intel 8035"; DKM's parts list and MAME: Fujitsu **MB8884** | MB8884, modelled as an 8035 (romless, 64 B RAM) per mcs48-python | | C2 | Computer Archeology's DK page vs evidence | its own site labels it "(AI)" and it cites MAME | never used as a source | | C3 | DK3's Z80 DMA clock | MAME: Z80 at 4.00 MHz, `z80dma` at 3.07 MHz; DK3M: 8 MHz and 21.47727 MHz crystals on the CPU board, 61.44 MHz on the video board | read DK3M's sheets before M7 | ## Open decisions for Aubrey (each with a recommendation) 1. **First machine.** Recommend `dkong` (US set 1, TKG-4, whose manual and schematics we hold), with `dkongjr` and `radarscp` as rows at M3. 2. **Order against mcs48-python.** Recommend building M1-M3 (picture, no sound) now and starting mcs48-python's core in parallel; this board's M4 waits for mcs48-python's milestones 1-5. No board code goes into the core's gate. 3. **Sound strategy.** Recommend the MB8884 path exact (core + DAC) and the discrete sounds as our own circuit models from DKS at M5 under "sound good enough"; no samples. 4. **Extract the 2A03 APU from famicom-python** for Donkey Kong 3. Recommend extracting it with history into `rp2a03-apu-python` (or `nes-apu-python`), pinned by both famicom-python and this board, when the DK3 row starts (M7), not before; the APU's revision differences (tonal noise) become a constructor parameter. MAME's APU source is GPL-2.0+ and stays closed. 5. **Radar Scope TRS01's speech chip.** Recommend leaving TRS01 until a usable M58817/TMS5100-family document is found; TRS02 needs no speech. 6. **BUSREQ in z80-python.** Recommend the between-instructions takeover for M1 and a request upstream only if a trace shows the difference. 7. **mcs48-python's brief** says "Python 3.12+" and names an old co-author line; recommend updating it to the family's PyPy 3.11 floor before its core is built (its owner's call, reported here). 8. **sources.md rows** (emulator-lineage): recommend adding the rows proposed in [sources.md](sources.md), including donkeykongforum.net set aside and the `nes_apu.cpp` GPL-2.0+ finding.