# Handoff: build `pc98-python`, the NEC PC-9801 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 family machine stays in its board and is extracted with history when a second family board needs it; 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 PC-9801 of 1982-86 is an **era-1** machine in the charter's table: a 5-10 MHz CPU, tiles of logic around it, Python slow but usable on short windows, Rust far above real time without recompilation. Boards to model this one on: **ibmpc-python** (Phase 0: the same Intel peripheral trio and the same CPU family; its decisions on the 8259A, the bus-cycle rung and the uPD765 are taken up here), **pcengine-python** (a built board: machine rows, ROMs by SHA-1, save states) and **sms-python** (its `UPD765` and `PPI8255`, the extraction candidates). Cores it embeds: **i8086-python** (Phase 0 only: nothing to pin until its rung 2), **z80-python** (built), and later a shared OPN core and `upd765-python` if those decisions go that way. Read in order before writing code: 1. [README.md](../README.md), [start-here.md](start-here.md), [machines.md](machines.md). 2. The chips: [cpu-cores.md](cpu-cores.md), [gdc-video.md](gdc-video.md), [timers-interrupts-dma.md](timers-interrupts-dma.md), [fdc.md](fdc.md), [sound.md](sound.md), [firmware.md](firmware.md). 3. [domains-and-speed.md](domains-and-speed.md). 4. [sources.md](sources.md), [validation.md](validation.md), [mame-oracle.md](mame-oracle.md), [census.md](census.md), [provenance.md](provenance.md). Then `python3 -I scripts/fetch_references.py --verify` (32 files, ~85 MB; without `--verify` to download). The Japanese texts are Shift-JIS (UND) or UTF-8 OCR (the Technical Data Books): `iconv -f CP932 -t UTF-8` the UND files before grepping. ## Decided by Aubrey (standing rules that apply here) - Name `pc98-python`, package `pc98_python` (the x-python naming rule). Local, no remote, until he says otherwise. - Bring your own ROMs and disks; nothing of NEC's in the repository; firmware by SHA-1. - 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. - Every repository's main line is `main`. ## Proposed by this Phase 0 - **Rows:** `pc9801` (the original) first, `pc9801vm` second, `pc9801vx` (V30 mode) third (open decision 3). The E, F, M, U, UV, VF are rows that follow from these. The 286/386/486 machines are recorded, not built (decision 2). - **One timeline in SCLK ticks** per machine (4.9152 or 9.8304 / 7.9872 MHz), with the 8253's input as an exact ratio; the GDCs at 2.5 MHz (or 5 MHz by the SW2-8 row field) converted at their bus interface. - **The 320 KB drive subsystem is its own domain** behind the 8255 handshake (fdc.md). - **No HLE BIOS:** NEC's BIOS and N88-BASIC(86) run as programs. ## Package and classes (proposed) ``` pc98_python/ machine.py # rows: CPU, SCLK, RAM, GDC parts and clock, banks, GRCG/EGC, FD interfaces, # kanji fitted, ROM SHA-1s per role (ipl, chargen, kanji) console.py # build(row, roms_dir, disks=...) -> Pc98 bus.py # memory map, I/O decode with byte lanes (even/odd ports), wait states, # the bank registers for DMA, A20 on later rows gdc.py # Upd7220(part, clock, memory_port): FIFO, commands, display timing, drawing video.py # text attributes, character generator and kanji access (A1h-A9h, CG window), # line counter (70h-7Ah), mode flip-flops, planes and banks, palette, # GRCG; the picture producer's per-line snapshot pic.py, pit.py, dma.py # 8259A x 2, 8253, 8237A: in-board and pin-shaped (decision 5) ppi.py, usart.py, rtc.py # 8255A x 2, 8251A x 2, uPD1990A/4990A keyboard.py # the keyboard's serial model fdc.py # the interfaces (90h-94h, C8h-CCh, BEh) around upd765 (decision 6) drive2d.py # the 320 KB link; the subsystem itself per decision 7 sound.py # beeper; the -26K card around the OPN core (decision 8) cbus.py # the C-bus slot socket (cards: -26K first) sockets.py # socket Protocols and test-only fakes savestate.py # whole-machine states naming ROMs and disks by hash ``` ## What the board needs from its cores (the embedding contracts) From **i8086-python** (decision 1): the 8086 and the V30 as rows of one core; a per-access bus interface (fetch, data, stack, I/O, INTA) that reports the **lane mask** (BHE#, A0) and takes the host's wait states; INTR with an INTA callback for the cascaded 8259A pair (one vector byte in 8086 mode); NMI (the 50h/52h flip-flop); reset (and on the VX, the CPU swap at I/O F0h). From **z80-python** (the drive subsystem): nothing new. From a shared **OPN core** (decision 8): YM2203 first; /IRQ; the two I/O ports as callbacks (the -26K's interrupt jumper and joystick live there); BUSY; a sample output per 72 clocks. From **upd765-python** (decision 6): DMA mode with DRQ/DACK/TC, two instances in one machine, drive callbacks for 2HD/2DD at 360/300 rpm. ## The gate (defined at milestone 1) The VM row's (and before it the original's) boot from reset to the N88-BASIC(86) prompt, compared with MAME's I/O tap log **as an ordered sequence of (port, direction, data)**, every difference explained or named; the prompt's picture against MAME's snapshot (tier 5, so a difference is triaged, not failed); unit tests of every documented fact the code cites. With an empty C-bus and no disks, later milestones must not move these. ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M0 | **Skeleton:** rows, ROM identification by SHA-1 and interleave, memory map, I/O decode with byte lanes, sockets and fakes | TDB86, UND (2-3) | unit tests: every ROM role of census.md recognised from a MAME zip and from loose files; the port map of machines.md decodes | | M1 | **The original to the BASIC prompt:** 8086 (pinned i8086-python), 8259A pair, 8253, 8237A registers and channel-1 refresh, the 8255s, the text GDC's host interface, status and display timing, the character generator with the ANK font, text VRAM and attributes, VSYNC interrupt | NEC sheets (2); UND (3); MAME (5) as detector | from reset to "How many files(0-15)?": the I/O sequence equals MAME's or each difference is named (MAME's 320 KB polling is inert, so the subsystem's port reads are faked "no drive" and the difference recorded); the picture matches MAME's snapshot or the difference is triaged | | M2 | **Keyboard, calendar, beeper:** the 8251 and a keyboard model, uPD1990A, the beeper level | UND (3) | typed BASIC lines execute (`PRINT 1+1`); the beeper toggles at the documented port bit | | M3 | **The graphics GDC:** drawing (VECTW/VECTE/TEXTE, lines, arcs, rectangles, graphics characters), the three planes, the digital palette | GDCUM, GDCDM (2); MAME pictures (5) | BASIC's LINE, CIRCLE and PSET draw what the GDC manuals say, pixel for pixel; drawing timing constants tagged FITTED or DOC | | M4 | **The VM row:** the V30 row of i8086-python, two banks (A4h/A6h), the GRCG, user-defined characters, the CG window, the kanji ROM (the VM's good dumps) | V20 corpus (1) via the core; UND (3) | the VM boots to the prompt under the M1 gate; kanji text drawn from the VM's own kanji ROM | | M5 | **Floppies:** uPD765A (decision 6), the 1 MB and dual-mode interfaces, DMA channels 2/3 and TC, IR10/IR11, HDM and D88 images | uPD765 sheet (2), UND (3) | a No-Intro HDM title of the smoke list boots to its title screen on the VM row | | M6 | **Sound:** the -26K on the C-bus socket with the OPN core (decision 8), the jumper and joystick ports, the interrupt | YM2203-LLE (1, closed box); YM2203DS (2) | the -26K's ROM is found by the BIOS; a register-write log replayed into the core and into YM2203-LLE gives the same samples except named departures | | M7 | **The 320 KB path** (decision 7): the PC-80S31-class subsystem behind the 8255 link | the drive's own ROM run on z80-python; UND `io_2d` (3) | the original row loads a 2D disk image through the subsystem | | M8 | **The VX in V30 mode**, the 16-colour board on the VM, save states | TDB93H (2) | the VX boots to the prompt in V30 mode; a save state reloads into a fresh machine and continues identically | Full sweeps only when the board might be ready (Aubrey's smoke-sweep rule); the smoke list is picked at M5 from the No-Intro HDM set plus BASIC programs typed or loaded by the harness. ## The chip rule | Chip | Users in or near the family | Proposal | | --- | --- | --- | | 8086, V30 | ibmpc-python (8088), irem-python (V30/V33/V35), pc98 | i8086-python, one core with rows (decision 1) | | 80286 | pc98's VX and later; nothing else in the family | deferred (decision 2) | | Z80 (drive subsystem) | many | z80-python | | uPD7220 x 2 | none in the family (QX-10, APC, VT240 and others in MAME) | in-board, one class used twice (decision 4) | | 8259A x 2 | ibmpc-python, irem-python (uPD71059C) | extract `i8259-python` at the second board's interrupt milestone, with cascade (decision 5) | | 8253 | ibmpc-python, pc88-python (MAME lists a pit8253 on the mkII SR) | extract `i8253-python` when the second needs it (decision 5) | | 8237A | ibmpc-python | extract `i8237-python` when the second needs it (decision 5) | | 8255A x 2 | sms-python (built), msx, cpc, system16, x68000, ibmpc | support extracting `i8255-python` | | uPD765A | sms-python (built), zxspectrum, msx, cpc, x68000, ibmpc, pc88 | support extracting `upd765-python` (decision 6) | | YM2203 / YM2608 | pc88-python | shared `opn-python` (decision 8) | | PC-80S31-class drive | pc88-python | shared device (decision 7) | | 8251A x 2, uPD1990A/4990A, character generator, GRCG, EGC, kanji logic | pc98 only | board | ## Unknowns (U) and conflicts (C) **Unknowns** (none settleable without hardware unless noted): | # | Unknown | Best source now | Status | | --- | --- | --- | --- | | U1 | The crystals (19.6608 and 15.9744 MHz are arithmetic from the SCLK figures) | UND `memsys` 0501h | HYPOTHESIS; a board photo or schematic settles it | | U2 | CPU wait states on VRAM and GDC accesses, and the arbitration with the GDCs' display and drawing cycles | none found | provisional, HYPOTHESIS tags | | U3 | The GDC FIFO's depth and the exact status timing | GDCUM (page images only) | read the scan's pages at M1 | | U4 | uPD7220 vs uPD7220A differences that software can see | GDCUM | open | | U5 | A DOC source for the -26K's 3.9936 MHz | MAME only (tier 5) | open | | U6 | Which early models have the GRCG (the original does not: MAME's run shows no 7Ch/7Eh traffic; UND lists GRCG-equipped models) | UND `io_disp` | open | | U7 | Real kanji dumps for the original (MAME: bad dumps) and the VX (MAME borrows the original's) | census.md | open; the VM's are good | | U8 | Whether any PC-98 software uses the V30's 8080 mode | none | open (the V30 row may defer BRKEM) | | U9 | The master PIC's IR3-IR6 assignments (INT0-INT2, RS-232C) | TDB86's master table did not survive OCR; read its page image | open | | U10 | uPD71054 (8254-class) behaviour on which rows (read-back command) | UND `io_tcu` head | open | | U11 | The refresh cycle's length and bus cost (PIT counter 1 paced, DMA channel 1) | UND `io_tcu`, `io_dma` | HYPOTHESIS | | U12 | The 8237A's real clock | MAME (5 MHz / 15.9744 MHz) | open | | U13 | Whether the PC-9801's external 2D drive ran the PC-80S31's ROM | UND `io_2d` (same family of drives) | open | | U14 | The original's fixed beeper pitch | MAME 2,400 Hz (5) | open | | U15 | The -26K's default interrupt (C3) | TDB86 vs 86UM | open | | U16 | What the -26K's ROM holds and when the BIOS calls it | MAME region `bios` | open | | U17 | A free PC-98 BIOS of clear licence | none found | open | | U18 | The keyboard's controller and its serial timing | UND `io_kb` | open | **Conflicts:** | # | Conflict | Sources | Resolution | | --- | --- | --- | --- | | C1 | Which 8253 clock goes with which series | UND `io_tcu` summary (5/10 MHz -> 1.9968) vs UND `memsys` 0501h and `io_prn` 0042h (5/10 MHz -> 2.4576) | **2.4576 MHz on the 5/10 MHz series**: `io_tcu`'s own baud table agrees (16 x 16 x 9600 = 2.4576 M), and 4 x 2.4576 = 9.8304 | | C2 | CPU clocks | NEC/UND 4.9152 and 9.8304 MHz vs MAME 5.000 and 10.000 MHz | NEC's; MAME rounds (compare sequences, scale times) | | C3 | The -26K's default interrupt | TDB86 "normally INT6" vs 86UM "the built-in FM LSI uses INT5, INT6 is normally the mouse" | open (U15); the row carries a jumper field | | C4 | The VX's CPU | the brief's framing (a V30/8086 machine) vs TDB93H (80286 8/10 MHz plus a V30) and UND `io_cpu` F0h | TDB93H; the VX runs in V30 mode until a 286 core exists | | C5 | The RX's CPU | MAME `pc9801rx` (i386SX) vs TDB93H (V30 + 80286 8/10/12 MHz) | TDB93H; recorded row only | | C6 | The 320 KB drive: inside the machine or an external unit | MAME fits it inside `pc9801` with no ROM vs UND `io_2d` (a separate PC-8031-class subsystem on an 8255 link) | UND; MAME's is a stub | ## Domains and speed (Summary of [domains-and-speed.md](domains-and-speed.md).) **Domains and channels:** one **system** domain in SCLK ticks (CPU and bus, the PIC pair, the PIT, the DMA with its refresh channel, both GDCs with their VRAM cycles, the character generator, the 8255s and 8251s, the OPN's register file and timers); the **320 KB drive subsystem** (Z80, uPD765, 8255) meeting it only at the cross-wired 8255 pair; the **floppy drives** at the FDC's DRQ/TC/IRQ; the **keyboard** at the 8251's serial line; and two pure producers, **picture** (per-line snapshots, 400 x 56.42 = 22,568 lines/s) and **sound** (OPN synthesis from its register log, the beeper level). **Instructions per second needed, with arithmetic:** original: 4,915,200 clocks/s at ~8-12 clocks per instruction = **~0.4-0.6 M instructions/s**, with at most 1.23 M bus cycles/s (4 clocks each); VM: 9,830,400 clocks/s at ~6-10 clocks per instruction = **~1.0-1.6 M instructions/s**, at most 2.46 M bus cycles/s. Around them: 2.4576 M PIT ticks/s (bulk), 1.81 M text cell-lines/s and 0.9 M graphics words/s per plane (both producers from per-line state), ~55 k OPN samples/s, and on the original a 4 MHz Z80 while the drive is busy. (Clocks per instruction are HYPOTHESIS until i8086-python and a MAME instruction count measure them.) **The no-JIT path:** x-python on PyPy is estimated at ~0.1-0.4 x real time for the original and ~0.05-0.2 x for the VM (testing windows, not play); **x-rust reaches real time on desktop, phone (native, no JIT) and browser (wasm)** with margin; x-rust-fast adds run-ahead and rewind, with the drive subsystem's 8255 channel and the producers as the thread splits if a weak device wants them. No cached interpreter or threads are needed for real time. The 286/386/486 rows would need their own arithmetic (era 2). ## Open decisions for Aubrey 1. **One x86 core: i8086-python with the 8086, 8088 and V30 as rows**, plus the bus-cycle interface ibmpc-python asked for, extended with the **lane mask** for the 16-bit bus. *Recommend.* The PC-98 is the third board to ask, and the first needing both an Intel and a NEC part in one machine family; it settles irem-python's decision 1 the same way. The V30MZ stays separate (v30mz-python). Amending i8086-python's brief is your edit. 2. **The 80286: deferred.** The VX runs in its V30 mode (the hardware offers it: I/O F0h); the 286 and every 386/486 row are recorded only. *Recommend;* revisit when a family board needs protected mode. 3. **Row order: the original first, then the VM, then the VX in V30 mode.** *Recommend:* the 8086 lands first in i8086-python, the original is the simplest machine (one bank, no GRCG, no user-defined characters), and its ROM BASIC boots without a disk; the VM follows as soon as the V30 row passes (and has the best-dumped firmware). 4. **The uPD7220 stays in the board**, one pin-shaped class used twice (no family user besides the PC-98; the QX-10, APC, VT240 and Rainbow are outside the family). *Recommend.* 5. **The PIC, PIT and DMA:** the PC-98 is the second family user of the 8253 and 8237A and a second or third of the 8259A. *Recommend* the general trigger: whichever of ibmpc-python and pc98-python writes them first keeps them in-board and pin-shaped; the second extracts `i8259-python` (with cascade from the start), `i8253-python` (8253 and 8254/uPD71054 rows) and `i8237-python` with history. This refines ibmpc-python's decision 3. 6. **`upd765-python`: support the extraction** from sms-python (with zxspectrum, msx, cpc, x68000, ibmpc, and now pc88 and pc98). The PC-98 asks for DMA/TC, two instances per machine and the 2HD drive. *Recommend.* 7. **The PC-80S31-class drive as one shared device for pc88-python and pc98-python** (Z80 + uPD765 + 8255 + ROM socket, pinned by both), built by whichever board reaches its disk milestone first; name `pc80s31-python`. *Recommend;* to settle with pc88-python's Phase 0 (running tonight, no documents committed yet when this was written). 8. **A shared `opn-python` (YM2203, YM2608) for pc88-python and pc98-python**, clean-room from Yamaha's documents with YM2203-LLE/YM2608-LLE as closed-box judges; how its SSG relates to ay8910-python to be settled with that core. *Recommend;* the PC-88 likely starts it (its OPN is on the motherboard). This is the "YM2203 or YM2608 wanted" case ym2610-python's decision 1 waits for. 9. **No HLE BIOS; NEC's ROMs player-supplied by SHA-1; no free BIOS** until one of clear licence is found (NP2's BIOS code sits in a mixed-licence tree). *Recommend.* 10. **Software:** no TOSEC PC-98 set on the mirror; the first workloads are N88-BASIC(86) and the 24 No-Intro HDM images. *Recommend:* if you obtain a TOSEC or other PC-98 floppy set, add an environment variable for it (e.g. `TOSEC_DIR`), and pick the smoke list at M5. 11. **MAME's BSD-3 PC-98 files stay closed** (the licence would allow reading with notice). *Recommend* closed, as ibmpc-python and psx-python chose: NEC's documents and UND suffice, and a closed MAME stays an independent detector. 12. **NP2kai and NP21/W as detectors:** NP2kai (mixed GPL-2.0+ and fmgen's freeware-only terms) only from emu-tools-gpl; NP21/W's BSD-only source download could be built in emu-tools after a per-file licence check. *Recommend:* defer until M3, when pictures need a second detector besides MAME. 13. **The PC-9801-86 is not a first-row card:** NEC's manual states support for hosts with an 80286 at 10 MHz or faster. *Recommend* the -26K for the first rows, the -86 with the later rows. 14. **Seven proposed rows for emulator-lineage `docs/sources.md`** ([sources.md](sources.md), end): UND, NEC's Technical Data Books and manuals, NEC's chip documents and Yamaha's YM2203 sheet, MAME's PC-98 files (BSD-3), NP2kai (mixed), NP21/W, fmgen (excluded for code). *Recommend* adding them; this Phase 0 did not edit sources.md.