# Sound: the standard section, the QSound D-board, Kabuki Two different sound systems share the board's 68000 side. Tags as in [memory-map.md](memory-map.md). The chips are sibling cores being built in parallel; this document says what the **board** owns and what it needs from them. ## Standard sound (149 sets) On the A-board: Z80 at 3.579545 MHz, 64 KiB program ROM (32 KiB fixed + two 16 KiB banks at `8000-BFFF`, bit 0 of `F004`), 2 KiB RAM, **YM2151** at the same 3.579545 MHz, **MSM6295** at 1 MHz ([memory-map.md](memory-map.md), "Z80"). Mono output; MAME mixes the YM2151's two channels at 0.35 each and the OKI at 0.30 `[mame]` (mixing levels are board data with no hardware source: U19). - **Latches**: the 68000 writes the command to `800181` (latch 1, low byte; MAME also accepts the high byte) and a second byte to `800189` (latch 2, "timer fade" in MAME); the Z80 reads them at `F008` and `F00A`. They are plain 8-bit latches: no interrupt to the Z80, no flag back to the 68000 `[mame: generic_latch_8]`; the Z80 polls. The decode is IOA1 "sound 0B/1B" `[pal]`. - **YM2151**: `F000` address, `F001` data, read = status; `/IRQ` to the Z80's `/INT`. The core is `ym2151-python` (in Phase 0). - **MSM6295**: `F002` command/status; 256 KiB sample ROM, the whole of the chip's 18-bit address space, **no bank switching** on CPS-1 (none in MAME's map; Pang! 3 has none either). Clock 1 MHz (the CPS-A's 4 MHz output divided by 4). **Pin 7 (SS) is driven by the Z80 through `F006` bit 0**: the sample-rate divider, power-up high in MAME. pnickj's first Z80 write is `F006 = 01` (measured here, [mame-oracle.md](mame-oracle.md)). The OKI data sheet's rule is clock/132 with SS (pin 7) high and clock/165 with it low: **7,575.8 Hz or 6,060.6 Hz** at 1 MHz (`../msm6295-python/docs/bus.md`, tier 2). A "/164" divisor (6,097.5 Hz) seen in jtcps's notes comes only from a comment in jt6295 and is not used. The rate is `msm6295-python`'s business; the board drives the pin. That repo's `docs/census.md` lists the 40 distinct CPS-1 sample ROMs with SHA-1s. The core is `msm6295-python` (in Phase 0). The board owns: the Z80 memory map, the bank latch, the two latches, the pin-7 latch, routing the YM2151 IRQ, mixing, and the clock ratios. It needs from the chip cores: a register write/read interface with the bus timing they model, a clock-step call producing samples at the chip's own rate, an IRQ output (YM2151), a pin-7 input (MSM6295), and a ROM read callback (MSM6295). ## QSound (19 sets: wof, dino, punisher, slammast, mbombrd and clones, plus wofch) "CPS-1.5": a QSound A-board revision whose PRG2 PAL replaces PRG1, and a **D-board** (sound) with its own Z80 at 8 MHz, two shared RAMs, the **QSound DSP** (a DSP16A running Capcom's program `dl-1425.bin`), a 93C46 EEPROM and stereo output `[mame: qsound machine]`. - **68000 to Z80**: no latches. The 68000 writes the low bytes of `F18000-F19FFF` and `F1E000-F1FFFF`; the Z80 sees them as `C000-CFFF` and `F000-FFFF` ([memory-map.md](memory-map.md)). How the two CPUs arbitrate the shared RAM (wait states, contention) is not described in anything read; the D-board PALs (D7L1, D8L1, D9K1/D9K2, D10F1) are in the ROM sets and were not decoded (U3). - **Z80 interrupt**: 250 Hz periodic (MAME, "measured" on CPS-2). - **Z80 banks**: `D003` low 4 bits select a 16 KiB bank of the 128 KiB Z80 ROM into `8000-BFFF` (MAME configures 6 banks from offset `0x10000`; a write past the end logs a warning and selects bank 0). - **The DSP interface** (Z80 side): `D000` data high, `D001` data low, `D002` the DSP register address (this write raises the DSP's interrupt), `D007` read bit 7 = ready `[mame: qsound.cpp]`, as also written up in the dsp16 Phase 0 (`../dsp16-python/docs/phase0-status.md`). - **Sample ROM**: 2 MiB (wof, dino, punisher) or 4 MiB (slammast, mbombrd); 8-bit samples. The 68000 can read its first 64 KiB at `F00000-F0FFFF` (one byte per word, high byte `FF`): Slam Masters reads it as a protection check `[mame: qsound_rom_r]`. - **The DSP program** `dl-1425.bin`: 24,576 bytes, SHA-1 `555f50fe5cdf127619da7d854c03f4a244a0c501`, present locally in `MAME/ROMs (bios-devices)/qsound.zip` (census). It is internal ROM of the DL-1425 (a DSP16A with mask ROM) and is the same file for CPS-1.5 and CPS-2. **Aubrey decided: QSound is the real DSP program run on `dsp16-python`, low-level.** So the QSound games need: the DSP16A core, the DL-1425 program, and a QSound "device" that wires the DSP's parallel port to the Z80 (`D000-D007`) and its external memory interface to the sample ROM, and turns its serial output into stereo samples. Where that device lives is the dsp16 Phase 0's open question; this board needs only that it is **one importable class used identically by cps1-python and cps2-python**. Recommendation shared with the cps2 brief: a `qsound` module in a small `qsound-python` repository that pins `dsp16-python` (two boards consume it; it is not a CPU, so it would otherwise live in a board, and the chip rule says break it out when a second emulator uses it, which CPS-2 does from day one). Open decision 4. **Speed**: the DSP runs one output sample per about 1,248 machine cycles (dsp16 Phase 0: MAME's low-level core loops in 1,245; 1,248 is the documented figure: their U), i.e. about 30 million DSP instructions a second. In pure Python that is two to three orders of magnitude below real time (no measurement: neither core exists). This makes a QSound game the slowest machine in the family; the reference board accepts it ("correct, not fast"). An HLE is at most a later speed option that must be proven sample-equal to the low-level path, not a milestone goal. ## Kabuki (QSound sets only) The QSound Z80 is a **Kabuki**: a custom Z80 module with on-chip decryption keyed by battery-backed RAM (a "suicide" part; with the battery removed and pin 28 pulled low it behaves as a plain Z80). From MAME `kabuki.cpp` (copyright Nicola Salmoria; tier 5, but the algorithm produces decrypted code that runs, which makes it a specification): - Every byte of `0000-7FFF` is decrypted by a chain of conditional swaps of adjacent bit pairs, rotates and an XOR with a key byte, the swaps chosen by an address-derived value; **opcode fetches use `select = address + addr_key`, every other read uses `select = ((address ^ 0x1FC0) + 1) + addr_key`**. `8000-FFFF` (the banked ROM, RAM) is not encrypted on CPS-1.5. - Keys (`swap_key1, swap_key2, addr_key, xor_key`): wof `01234567 54163072 5151 51`, dino `76543210 24601357 4343 43`, punisher `67452103 75316024 2222 22`, slammast and mbombrd `54321076 65432107 3131 19`; wofch uses the wof key `[mame: init_*]`. - MAME decrypts the opcode image at load into a separate 32 KiB region mapped as the Z80's opcode space, and decrypts the data image in place. **Where it lives**: Kabuki is a decode function plus the knowledge of which reads are opcode fetches. Mitchell boards (Pang, Super Pang, Block Block, ...) use the same chip with their own keys (`mitchell_decode` in the same file). Recommendation: a pure function `kabuki.decode(rom, keys) -> (opcodes, data)` inside this board's package now (one consumer), moved to its own small repository when a Mitchell board starts (second consumer) (open decision 5). **What z80-python must offer** (checked in `../z80-python`, pin `b62891b`): the core has a single `read_byte` for all memory reads. Its `_fetch_byte` (`_core.py` l.70) is exactly the M1 read (it bumps R) and `_read_operand_byte` is a separate method, so the split exists inside the core but is **not exposed**: there is no `read_opcode` hook. Kabuki needs one: an optional `read_opcode(address)` callable used by `_fetch_byte` only (prefix bytes included, the `op` byte of `DD CB d op` not, since the real CPU reads it outside M1 and z80-python already treats it as an operand), defaulting to `read_byte`. The hot loop in `cpu.py` l.190 inlines the fetch and would call it too. This is a small core change (raise it as a PR on z80-python: "boards exist to find core corners"); the alternative of the board guessing fetches from PC is wrong for operands. Whether MAME's Z80 reads operands through its opcode or its program space was not checked (the Z80 source is not in the pinned sparse tree): the Kabuki algorithm itself says "if the byte is an opcode ... if the byte is data", and decrypted operands that work in MAME use the data key (U21).