# Handoff: build `astrocade-python`, the Bally Astrocade board ## Context you are inheriting The family's shape: chips are cores, boards compose cores; CPUs always get their own core; a non-CPU chip used by one machine stays in its board; never copy a file between repositories, pin the sibling by commit. Read the charter first (emulator-lineage `docs/charter.md`), then [start-here.md](start-here.md). The Astrocade is era 1 in the charter's table: one clock domain, cycle level everywhere; Python runs the tests, Rust plays. Boards to model this one on: **colecovision-python** and **sms-python** (a z80-python board, BIOS by SHA-1), **a2600-python** (a board whose video is dot-exact). Core it embeds: **z80-python** (pin its `main`; read [cpu-z80.md](cpu-z80.md)). Read in order: [README.md](../README.md), [start-here.md](start-here.md), [memory-map.md](memory-map.md), [video.md](video.md), [io-chip.md](io-chip.md), [cpu-z80.md](cpu-z80.md), [cartridges-bios-basic.md](cartridges-bios-basic.md), [arcade-boards.md](arcade-boards.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` (9 files, ~35 MB; `--verify` to check). Read the drawings in SYS 83-84, 124-134 (memory maps, bus and video timing diagrams): they are figures without text in the retype; the scans are on BallyAlley. ## Decided by Aubrey (standing rules that apply here) - Name `astrocade-python`, package `astrocade_python`. Local, no remote, until he says. - Bring your own BIOS, cartridges and tapes; nothing of Bally's in the repository. - Python floor = PyPy's (3.11), PyPy must work; 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. ## Package and classes (proposed) ``` astrocade_python/ machine.py # machine rows: astrocade, astrocade-white, astrocade-hlc (BIOS SHA-1s); # later commercial rows (seawolf2, spacezap, ebases) console.py # build(machine, cartridge=None, bios_dir=...) -> Astrocade bus.py # the memory map, magic-write routing, the port decoder (16-bit port addresses) microcycle.py # the WAIT model: one per memory access, one more on a scan collision data_chip.py # DataChip: colour registers, boundary, background, magic function # generator (expand, shift, rotate, flop, OR/XOR), intercept, pixel output address_chip.py# AddressChip: scan address generator, vertical blank, interrupt generator # (screen line, light pen), feedback registers io_chip.py # IoChip: switch matrix, keypad, pot A/D, music processor cartridge.py # CartridgeSlot: plain ROM; later rom_256k/512k, rom_cass sockets.py # controller, light pen, expansion RAM, cassette: Protocols and fakes savestate.py # whole-machine states naming the BIOS and cartridge by hash ``` ## Embedding contract with z80-python The callable bus and `request_maskable_interrupt(vector)` cover the bus and IM 2. Two requests may go upstream (a fix starts upstream, with a repro): per-access timing so the board can place the collision WAIT (SYS 111-112), and nothing else. See [cpu-z80.md](cpu-z80.md). ## The chip rule | Chip | Users in or near the family | Proposal | | --- | --- | --- | | Z80 | many | z80-python (pin) | | Data chip, Address chip | Astrocade; Bally/Midway arcade (same chips, commercial mode) | stay in this board | | I/O chip (music processor) | Astrocade; the same arcade boards (one or two per board) | stays in this board | | Votrax SC-01 | Gorf, Wizard of Wor; other arcades | only with the arcade board (decision 2) | | Pattern board (blitter) | Robby Roto, Professor Pac-Man, Demons & Dragons, Ten Pin Deluxe | only with the arcade board | ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M1 | bus, magic-write routing, ports, colour registers, boundary, vertical blank, the screen interrupt, frame timing (455 x 262, the Phi stall), the fixed one-per-access WAIT; sockets and fakes | SYS (2); MAME (5) | the BIOS runs from reset to the "SELECT GAME" menu; the first 3 frames' I/O writes equal MAME's write list (port, value, order) with each cycle difference named (the WAIT model will move cycles: C3) | | M2 | the full magic system and intercept, unit-tested rule by rule from SYS 94-100 | SYS, P503 (2) | each magic function has a test written from the document; the menu and Scribbling draw as in MAME, pixel differences triaged | | M3 | inputs: hand controls, keypad (with ghosting), pots | SYS 101 (2) | Gunfight and Calculator playable from scripted input | | M4 | the music processor | SYS 103-104, 119-120 (2) | the BIOS's sounds; audio compared with MAME; U8-U11 FITTED in one table | | M5 | cartridges: plain ROM; smoke list from No-Intro (problem titles plus well-behaved ones) | the programs themselves | 10 smoke cartridges reach play; save states load into a fresh machine and continue identically | | M6 | BASIC (Bally BASIC, Astrovision BASIC with `rom_cass`), tape (a `.wav` loader) | Astro BASIC manual (2) | a BASIC program typed through the keypad socket runs; one tape loads (needs decision 5) | | M7 | the collision WAIT at the right T-state (needs the z80-python request), the light pen | SYS 124-127 (2) | timing tests from the diagrams; a light-pen position read back | | M8 | commercial rows: Seawolf II, Space Zap, Extra Bases (320 x 204, rotate) | SYS, MIL (2-3); MAME (5) | each reaches attract mode; the rotator tested by a real program | ## Unknowns (U) and conflicts (C) | # | Unknown | Best source now | | --- | --- | --- | | U1 | magic writes to 1000-3FFF on a 4 KB home unit: which RAM they reach | SYS 83-84 figures (scan) | | U2 | `OUT` to ports 0B and 18 outside `OTIR` | high-byte decoding OBSERVED in MAME | | U3 | the 256-entry palette as RGB | none measured; MAME's palette as a detector | | U4 | the rotator in consumer mode ("commercial mode only", SYS 98) | none | | U5 | shift carry across the end of a line | none | | U6 | whether an OR/XOR magic write costs a RAM read cycle | SYS 115 (the latch); timing diagrams | | U7 | pot conversion rate | none | | U8 | the noise generator's construction | SYS 119 (behaviour only) | | U9 | vibrato rates in Hz | SYS 103 (00 fastest only) | | U10 | DAC levels | none | | U11 | whether a frequency write restarts a tone | none | | U12 | odd-sized cartridge images' unmapped bytes | none; propose open bus | | U13 | large-cartridge bank registers (`rom_256k/512k`) | MAME device names only | | U14 | Bally BASIC's program storage and tape interface | the Astro BASIC manual (not pinned) | | U15 | Votrax SC-01 sources and licences | not checked | | U16 | the cycle of the screen interrupt within its line | SYS 93 ("completes scanning the line") | | U17 | which T-state each WAIT occupies | SYS 124-127 drawings | | # | Conflict | Sides | Proposal | | --- | --- | --- | --- | | C1 | Phi cycles per line | SYS 131 retype: "133 3/4"; 455 / 4 = 113 3/4 | 113 3/4 (a typo in the retype); check the scan | | C2 | lines per field | MAME: 262 progressive at 60.054 Hz; MIL: "each NTSC field is 262 1/2 lines" | SYS 133-134 drawings decide; until then 262 progressive, provisional | | C3 | WAIT states | SYS 111-112: one per memory access plus collisions; MAME: none | follow SYS; MAME lockstep compares instruction and write order, not cycles | ## Domains and speed **Domains and channels.** One domain: the Z80, the Data chip and the Address chip share the microcycle bus and the WAIT line every memory cycle, so they cannot run apart. The I/O chip's music processor is a **pure producer**: it needs only its register writes, each stamped with the CPU cycle it happened on (a channel: the port-write log), and can synthesise beside the CPU on another thread from `-fast` on. Input (switches, pots) is a channel read at port-read time. The picture is a second producer when the renderer draws from a log of register and RAM writes per line. **Instructions a second.** Z80 at 1,789,773 Hz. MAME's BIOS trace: 8,263 instructions in 89,400 cycles, 10.8 cycles an instruction, so 1,789,773 / 10.8 = **~166,000 instructions a second** (WAIT states make it fewer). Video: 455 x 262 x 60 = 7.15 M 7M ticks a second; pixels change at most at PX = 3.58 MHz, and in low resolution every 4 PX (a 160-pixel line), so a per-pixel renderer handles 160 x 204 x 60 = **~2.0 M pixels a second**, or **12,240 line jobs** (204 lines x 60) when drawn per line with mid-line write events. **The no-JIT path.** - *Python (PyPy)*: z80-python runs ~2.5 M instructions a second on PyPy (its validation.md; i9-13900K): 15 x the need. The pixel work at 2 M a second is the cost that matters; per-line rendering with a write log keeps it at ~12 k jobs a second. Real time on desktop PyPy is likely; CPython is not a target (reference boards are correct, not fast). - *Rust -fast*: z80-rust-fast runs ~350 M instructions a second on desktop: over 2,000 x the need. Phone and browser (WASM, no JIT: `recompile: none`) reach real time with a wide margin; no cached interpreter and no threads are needed. Threads at the sound channel are available but would not pay (prune what does not pay). - Stated capability: `recompile: none` on every target. ## Open decisions for Aubrey (each with a recommendation) 1. **Scope and first machine.** Recommend the home unit with the Professional Arcade BIOS first; the white and HLC BIOS as rows from M1 (one SHA-1 each). 2. **The arcade boards.** Recommend Seawolf II, Space Zap and Extra Bases as rows here (M8), and a separate board `astrocade-arcade-python` pinning this one for Wizard of Wor, Gorf and the pattern-board games (Votrax SC-01, blitter); not started now. 3. **WAIT model before the core can place it.** Recommend M1 charges the documented one wait per memory access (reachable now by counting accesses in the bus callbacks), logs the collision wait as a named gap, and raises the per-access timing request to z80-python with a repro; M7 closes it. The alternative (no waits, as MAME) would bake in a known-wrong timing. 4. **The palette (U3).** Recommend a palette table marked FITTED from MAME's numbers until a measured one is found, and asking BallyAlley's community whether anyone has measured a console's video output. 5. **BASIC.** The BASIC cartridges are not in the No-Intro set and the source for the tape interface is not pinned. Recommend: Aubrey supplies the Bally BASIC and Astrovision BASIC images (MAME's software list `bbasic`, `astrobas`) when M6 comes; the builder pins the Astro BASIC manual then. 6. **The Nutting manual.** Its first part (system routines, custom-chip specifications) is useful, but the same file carries the BIOS source listing. Recommend not pinning it; the System Description (pinned) covers the chips. If Aubrey wants the rest, pin it with a note that only pages before "Continues with ROM Source" may be opened. 7. **sources.md rows** (emulator-lineage; Aubrey's file): see the proposed rows in this repository's Phase 0 report (BallyAlley documents, Tony Miller, the patents, MikeJ's HDL, MAME's Astrocade files as BSD-3, the excluded BIOS listings).