# Handoff: build `pcfx-python`, the PC-FX board ## Context you are inheriting The family's shape: chips are cores, boards compose cores, CPUs always get their own core, and a non-CPU chip moves into its own core when a second board needs it (charter). This board's CPU is `v810-python` (`../v810-python`, Phase 0 tonight, `Variant.UPD70732`). It is **the second board for two PC Engine chips**, the HuC6270 VDC and the PSG ([chip-rule.md](chip-rule.md)): until Aubrey decides, read pcengine-python's `vdc/` and `psg/` as the models and do not copy them. Era 3: a 32-bit CPU beside a stack of custom video chips. The hard parts are not the CPU but the KING, the HuC6261 and above all the Rainbow, whose knowledge is thin. Models to read: `pcengine-python` (the VDC, PSG, sockets, save states), `supergrafx-python` (two VDCs on one screen with a priority chip, the nearest relative of the PC-FX's mixing), `pcenginecd-python` (a SCSI drive model, disc images), `psx-python` docs/domains.md (the domains-and-speed arithmetic). Rules: Python >= 3.11, CPython and PyPy; local git only; MIT; nothing read from GPL or unlicensed emulators (Mednafen closed; MAME's BSD files closed until decision 5); no BIOS or disc bytes committed; Python that reads `reference/` runs with `-I`. ### Read in this order README.md; docs/start-here.md; docs/memory-map.md; docs/video.md; docs/king.md; docs/sound.md; docs/cdrom.md; docs/chip-rule.md; docs/census.md; docs/domains-and-speed.md; docs/validation.md; docs/mame-oracle.md; then every page in `reference/docs/trap15-pcfx/`, LIB's headers (`reference/src/liberis/include/eris/`), PFI's controller and FX-BMP notes, and `../v810-python/docs/handoff-brief.md`. ## Scope In: the memory and I/O maps with the A/B windows, main RAM, BIOS ROM, backup memory (internal and FX-BMP) with its control port, the interrupt controller, the timer, the pads (pad, mouse, multitap), two HuC6270s (through the shared core once extracted), the HuC6261, the KING (KRAM, backgrounds with the microprogram and rotation, SCSI with DMA, ADPCM and Rainbow feeds), the HuC6230 (PSG through the shared core, ADPCM, CD-DA mix), the SCSI CD drive, the Rainbow (after decision 3), the EXTIO boot hook (as a socket), save states, frame and audio output. Out: the PC-FXGA's HuC6273 3D chip and its PC-side interfaces (decision 7), the FX-SCSI adapter (a socket later), presentation. ## Proposed names - Repository `pcfx-python`, package **`pcfx_python`**. - `pcfx_python.PcFx` (machine), `board.Board` (decode, A/B windows), `intc.InterruptController`, `timer.Timer`, `pads.PadPorts`, `backup.BackupMemory`, `huc6261.Mixer`, `king.King` (with `king.kram`, `king.bg`, `king.scsi`, `king.feeds`), `rainbow.Rainbow`, `soundbox.SoundBox`, `cd.ScsiCdDrive`, `state`. - Sockets: `ExpansionBus` (EXTIO ROM, FX-SCSI), `BackupCard` (FX-BMP), `PadDevice`. - Machine rows: `pcfx` (BIOS v1.00 or v1.01 by SHA-1); `pcfxga` only if decision 7 says so. ## Unknowns (U) and contradictions (C) - U1 The V810's bus mode on the PC-FX (v810-python U4). - U2 Wait states per region and chip (RAM, ROM, each port, the A/B windows, KRAM while busy). - U3 KRAM size (1 MB inferred), access arbitration, and the microprogram's slot timing against the dot clock. - U4 KING rotation: which BG, arithmetic, rounding; the block modes' BAT entry format. - U6 The Rainbow's codec (entropy coding, quantisation, colour format, block layout). - U7 The Rainbow's registers 200h-214h. - U8 The HuC6261's dot clocks, line length, visible area; its palette size. - U9 Cellophane's integer arithmetic. - U10 The SoundBox PSG's clock divider and any difference from the HuC6280's PSG. - U11 The ADPCM step table and adaptation. - U12 The ADPCM rates' exact values (C1). - U13 SCSI and drive timing (selection, commands, seek, read rate, DMA into KRAM). - U14 The drive's command set. - U15 The interrupt controller's priority semantics and INTV mapping. - U17 Pad scan timing (to read from PFI's traces). - U18 The backup memory control port C80h. - U19 Port 700h. - U20 The drive's speed. - U21 The HuC6270s on the PC-FX: 16-bit port semantics (the `8/16` pin), VRAM size, how two VDCs give 256 colours. - U22 Ports C00h-C43h ("HuC6270 #0 & #1", PGN only). - U23 Port CC0h (gate array version, PGN only). - C1 ADPCM rates: 32/16/8/4 kHz (T15 HuC6230) vs 31.47/15.73/7.87/3.93 kHz (T15 KING). - C2 KRAM data port 0Eh: T15 says writes use the read address (0Ch) and reads the write address (0Dh). - C3 KING rotation: "BG0 rotation enable" (priority register bit 12) vs "BG0 cannot be rotated" (microprogram limits). - C4 Mouse bits: T15 vs PFI (PFI's logic traces win). - C5 HuC6230 port 126h labelled "Vol1 right" in T15's table, left in its text. - C6 80000000h: T15 "Ports Write 80000000-80FFFFFF" vs PGN "alternate to ports" for the whole 80000000-8FFFFFFF. - C7 Noise channels "5 and 6" (T15, counting from 1) vs channel select 0-5. - C9 HuC6261 palette ports: register table 02h = read, 03h = write; the text the reverse. - C10 pcengine-python's start page plans "nothing for the PSG" in a PC-FX extraction; the HuC6230 contains a PSG (T15; MAME's device tree). (U5, U16 and C8 were merged away while writing; the numbers are kept stable.) ## Milestones (exit tests in validation.md) 0. Board skeleton on v810-python: maps, RAM, BIOS by SHA-1, interrupt controller, timer; the BIOS reaches its first VDC and KING writes (MAME's 443,321-instruction window as a lockstep detector). 1. The HuC6270 core extraction (decision 1) and two instances on the HuC6261's timing; the HuC6261's palettes, priorities, chroma key and cellophane. 2. KING backgrounds; the BIOS menu matches MAME's picture as a detector. 3. Pads (PFI's traces as the judge), backup memory with the FX-BMP socket. 4. SCSI, the drive, disc images; the BIOS finds the signature track in all three census cases and starts a game. 5. SoundBox: the PSG core (decision 2), ADPCM via the KING, CD-DA. 6. The Rainbow (decision 3). 7. Save states; seeded faults, each caught. ## Domains and speed Summary of [domains-and-speed.md](domains-and-speed.md): - **Domains** (one locked crystal family, scheduled apart): CPU (21.48 MHz, with RAM, ROM, backup, interrupts, timer, pads); video (HuC6261 timing, two VDCs, KING BGs, Rainbow output); KING DMA; sound (PSG 3.58 MHz, ADPCM, CD-DA); the CD drive (its own clock). - **Channels:** chip ports and the A/B windows; KRAM and each VRAM; pixel streams into the HuC6261; the SCSI bus; CD-DA; interrupt lines. Code runs only from CPU-written memory, so a cached interpreter never watches DMA. - **Need, with the arithmetic:** CPU at most 21.5 M instructions/s (one per clock), ~13 M/s typical (MAME's model: 443,321 instructions in 0.03343 s of BIOS boot); video 320 x 240 x 60 = 4.6 M dots/s x 7-8 sources = 32-37 M source-dot evaluations/s at worst (HYPOTHESIS, profile first); Rainbow ~1.8 M pixels/s of IDCT. - **No-JIT verdict:** real time on desktop at x-rust with video on a second thread; on a native phone app at -fast with a video thread; in a desktop browser at -fast with a video worker; **in a phone browser at the edge** (-fast, a video worker, turbo's whole-line paths and a cached interpreter; run-ahead doubtful). `recompile: none` is enough except perhaps for run-ahead in a phone browser: measure there first. ## Open decisions for Aubrey 1. **Extract the HuC6270 into `huc6270-python` now that a second board needs it.** Recommended: yes, when this board reaches milestone 1 (not before), as a move with history from pcengine-python (whose import boundaries were built for it), adding three parameters with the PC Engine's values as defaults: the line timing (today imported from `pcengine_python.machine`), a 16-bit port mode (the chip's `8/16` pin), and the raw pixel output (VD0-7 + SP8G). Exit test: pcengine-python's and supergrafx-python's gates unchanged. It touches public repositories, hence your call. 2. **Extract the PSG with it** (into its own core, e.g. `huc6280-psg-python`, or a `psg` package beside the VDC in one "huc62xx" core). Recommended: its own small core, as sn76489-python is; the HuC6230 wraps it with ADPCM and CD-DA here. 3. **The Rainbow has no public description.** Options: (a) reverse-engineer the codec ourselves from the streams on the discs (a research task with its own repository of notes, honest and slow); (b) read MAME's `video/huc6271.cpp` (BSD-3-Clause line) as a permitted source, after checking whether it decodes at all; (c) leave the Rainbow a stub that shows nothing. Recommended: (c) for the first milestones, then (a) with (b) as a cross-check if MAME's file turns out to implement the decode; never Mednafen's source. 4. **No shared CD core; reuse `pcenginecd-python`'s `disc.py` by pin if its interface fits.** Recommended: the PC-FX drive is a different unit (its own MCU firmware, faster); the disc reader is a utility both boards need, extracted only when a third board wants it. 5. **MAME's BSD-3 PC-FX files stay closed tonight.** They are the most complete public description of the KING and HuC6261 after T15. Recommended: allow reading them as facts sources with notice (as the PCE line does for `huc6260.cpp`, `c6280.cpp`), file by file after a `license:` check, because the alternative for some questions is nothing; keep Mednafen closed. 6. **Ask emu-tools-gpl2 for a Mednafen `pcfx` build** as a detector (shared with v810-python decision 5). Recommended: yes; Mednafen is the most complete PC-FX emulator, and run as a closed box it is the best detector for the Rainbow and the KING. 7. **The PC-FXGA is out of scope**, except as a later machine row if wanted: its 3D chip (HuC6273) is on few boards and no emulator supports it (PGN). Recommended: out. 8. **Default provisional choices** (HYPOTHESIS in code): U1 32-bit bus; U3 1 MB KRAM; U8 dot clocks 21.47727/4 and /3 MHz; U10 PSG at 21.47727/6 MHz; U12 the KING's line-locked rates; C2 the register names win (writes use 0Dh, reads 0Ch); C4 PFI's bits; U21 16-bit port writes act as a low-then-high byte pair.