# The two Z80s and the disk subsystem ## The main CPU A Z80 at 4 MHz (MAME: 3.99 MHz, OBSERVED); from the FH on, NEC's **uPD70008C-8** at 8 MHz with a 4 MHz compatibility mode (FHPG, "本機の特長"; MAPG the same), the setting readable at port 6Eh bit 7 (YK-IO). Embedded from `../z80-python` (main `cc2f794`). What the board needs: - **Z1, wait states per access.** The text DMA (uPD8257 channel 2 feeding the uPD3301) takes the bus from the CPU during display, and GVRAM accesses are slower than main RAM in some modes (GAMEPROG: the text screen is fed by DMA, so stopping the DMA speeds the program up; YK-IO: the V1S "high-speed mode" bit). The board must stall the CPU per access and for DMA bursts. segasys1-python's Phase 0 asked z80-python for per-access waits too (its cpu-cores.md, request 2); the PC-88 is a second asker. - **Z2, IM 2 vectors from the 8214.** The main CPU runs in mode 2 with the vector formed from the interrupt level (interrupts-and-peripherals.md); z80-python's `request_maskable_interrupt(vector_byte)` covers it as long as the board computes the vector at acknowledge time (the level can change between request and acknowledge: same concern as the X1's Z2, smaller here because the 8214 is the only source). - **Z3, the 8 MHz uPD70008C.** A Z80-compatible NEC part; any timing difference from Zilog's Z80 (and the CMOS `OUT (C),0` question, z80-python-cmos) is unknown (U3). Run it as the Z80 at 8 MHz until a difference is shown. ## The disk unit: a second computer The PC-80S31 (external on the PC-8801, built in from the mkII's internal-drive models on) is a **separate Z80 system** with its own ROM, RAM, a **uPD765A** and an 8255, talking to the main machine only through a pair of **cross-wired 8255s** (YK-DISK; MAME `-listdevices`: `pc80s31` with `fdc_cpu` Z80 @ 4.00 MHz, `fdc` uPD765A @ 8.00 MHz, `ppi_fdc`, `ppi_host`, OBSERVED). - Firmware: `disk.rom` / `mk2fr_disk.rom` (2 KB, PC-80S31) or `ma_disk.rom` / `m2mr_disk.rom` (8 KB, PC-80S31K: the MR and later, 2HD-capable) (census.md). The board runs it: the disk ROM is a program like any other, so the disk unit is a **real second Z80**, never high-level. - The link (YK-DISK): main side FCh (port A, data from the unit), FDh (port B, data to the unit), FEh (port C: ATN, DAC, RFD, DAV out on the high nibble, the unit's DAC, RFD, DAV in on the low nibble), FFh control. The unit has the mirror image at its own FCh-FFh. Both sides program the 8255 with **91h** (mode 0; A in, C upper out, B out, C lower in) (MAME run, OBSERVED: main at 0.312 s, unit at 0.12 ms). The OBSERVED cross-wiring: the unit wrote `FDh <- 0Fh` at 2.017375 s and the main read `FCh = 0Fh` 25 µs later. - Protocols (YK-DISK): a command is sent with ATN (sender ATN = 1, receiver RFD = 1, sender ATN = 0, data, DAV, DAC...); parameters and data with the normal four-step handshake; a fast handshake that moves two bytes per cycle. The main ROM's routines: PUTCOM 37CCh, PUTDAT 37D2h, GETDAT 3847h in N88 (017Ch, 0183h, 01E9h in N-BASIC) (YK-DISK; firmware facts, cited, nothing copied). - Commands (YK-DISK): 00h initialise, 01h write data, 02h read data, 03h send data, 04h copy, 05h format, 06h send result status (bit 0 error, bit 6 data in buffer, bit 7 I/O done), 07h send drive status (bits 7-4: drives connected), 08h memory test (80h OK), 09h send memory, 0Ah out to the unit's port F7h, 0Bh send memory 2, 0Ch receive memory, 0Dh execute at an address, and more. Commands 0Ch and 0Dh let games load their own code into the disk unit: copy protection and fast loaders run there, which is why the unit must be a real Z80. - The unit's own ports (MAME run, OBSERVED): `F7h <- 08h` first, `FFh <- 91h`, the uPD765A at **FAh (main status) and FBh (data)**: status 80h (RQM), then `FBh <- 03h` (SPECIFY) at 2.0168 s; `F8h` written twice (05h first; motor or TC: U4). ## The uPD765A NEC's floppy controller (NEC82 p. 459; UPD765, UPD765AN), at 8 MHz, polled by the unit's Z80 (no DMA in the PC-80S31; the main machine's 8-inch DMA interface is a separate, rare option: KAISEKI's "DMA タイプ" routines, out of scope). Interrupt from the uPD765A to the unit's Z80: U5. Drives: 2D (SR, FR), 2HD/2D (MR and later), as the disk ROM and DIP switches set. ## The chip rule - **Two Z80s** are two instances of `z80-python`, each its own domain (domains-and-speed.md). - **8255 x 2**: the X1, MSX, CPC, IBM PC, System 16, X68000 and sms-python already make the case; this board needs mode 0 and the port C bit set/reset (the handshake lines). **Support extracting `i8255-python`** (decision 5). - **uPD765A**: sms-python holds one in board (SF-7000); zxspectrum (its decision 3), msx, cpc (its decision 6), ibmpc and x68000 (uPD72065) want it. **Support extracting `upd765-python` from sms-python with history** (decision 6); the PC-88 needs the polled (non-DMA) mode, two drives per unit, 2D and 2HD.