# Custom I/O: 06xx, 51xx, and the 54xx bus side Three Namco customs sit between the main Z80 and the cabinet. The **06xx** is a bus controller: the CPU talks to it through two registers and it forwards bytes to up to four downstream chips, raising an NMI on the main CPU to pace each byte. Galaga populates two slots: the **51xx** (MB8843 MCU programmed as a coin/credit/joystick manager) and the **54xx** (MB8844 noise generator; its sound side is in [sound-hardware.md](sound-hardware.md), only its command path is covered here). Sources: MAME `src/mame/namco/namco06.cpp`, `namco51.cpp`, `namco54.cpp`, `galaga.cpp` and `src/devices/cpu/mb88xx/mb88xx.cpp` at master (2026-09); the pre-2020 high-level 51xx model from the `mame0200` tag (`src/mame/machine/namco51.cpp`). Facts are restated, never quoted. ## 06xx bus controller ### Registers (`galaga_map`) | Address | Access | Meaning | |---|---|---| | `0x7000`-`0x70ff` | R/W | Data window; every address in the range is the same port | | `0x7100` | R/W | Control register; reads return the last value written | The driver's schematic-derived table decodes `01110--0--------` (data) and `01110--1--------` (control), i.e. only A15-A11 and A8, which would mirror both across `0x7000`-`0x77ff`. MAME maps only the ranges above and the game uses `0x7000`/`0x7100`; wider mirroring is `[unverified]`. ### Control register (`namco06.cpp`, `ctrl_w_sync`, `nmi_generate`) | Bits | Meaning | |---|---| | 3-0 | Chip selects /IO1-/IO4, one per slot, active high here; several may be set | | 4 | Direction: 1 = read (downstream chip drives the bus), 0 = write | | 7-5 | Clock-divider exponent *n*: transfer clock = `06xx clock / 2^n`; 0 stops it | There is no separate NMI-enable bit: NMIs run whenever the divider is non-zero. MAME through 0.219 ignored bits 7-5, treated any non-zero select nibble as "enabled" and pulsed NMI every 200 µs; the divider model dates from commit 6589 (2020-04-23, first shipped in 0.220 `[unverified]`). ### Clock and timing Galaga clocks the 06xx at `18.432 MHz / 6 / 64 = 48 kHz` (`galaga.cpp`, `NAMCO_06XX(config, "06xx", MASTER_CLOCK/6/64)`; older MAME called it 64H). MAME ticks at twice the divided clock, alternating two phases: in the *active* phase every slot's R/W line is driven with control bit 4, NMI is asserted, and the chip select of each selected slot is asserted; in the *idle* phase NMI and all chip selects are cleared. One byte moves per transfer-clock period: the CPU's NMI handler reads or writes the data window while the selected chip sees its /IRQ. Values Galaga writes, per MAME's trace of the game: | Control | Divider | Byte period | Use | |---|---|---|---| | `0x10` | 0 | stopped | Idle (read mode, nothing selected); written after every transfer | | `0x71` | 2^3 | 48 kHz/8 = 6 kHz, 166.7 µs | Read 3 bytes from slot 0 (51xx) | | `0xA1` | 2^5 | 1.5 kHz, 666.7 µs | Write 4 bytes to slot 0 (51xx) | | `0xA8` | 2^5 | 1.5 kHz, 666.7 µs | Write 12 bytes to slot 3 (54xx) | At power-up the game writes control `0x10` then data `0xFF` (a write in read mode, dropped). Other rules (`ctrl_w_sync`, `data_r`, `write_sync`): - Divider 0 stops the timer and deasserts NMI and all chip selects; R/W lines keep their last state. Reset sets the register to 0. - A non-zero divider starts the timer on the next 48 kHz edge, then every half period. If bit 4 = 1, NMI is cleared at once and the *first* active-phase NMI is skipped, giving the chip one period to present its first byte. - Data read: read mode only; result = bitwise AND of all selected slots (unpopulated slots give `0xff`). In write mode a read returns `0x00`. - Data write: write mode only; delivered to every selected slot. In read mode the byte is dropped. - Control and data writes take effect at a scheduler sync point, not instantly (MAME's `synchronize`) — an emulator ordering device, not hardware. - NMI is withheld while the main CPU is in reset or halted. 06xx pin 1 is wired to K3 of the 51xx on Galaga: the R/W line the 51xx samples to decide whether to output or input a byte. ### Slot population (`galaga_state::galaga`) | Slot (bit) | Chip | Lines bound | |---|---|---| | 0 | 51xx | chip select, R/W, read, write | | 1 | none | reads give `0xff`, writes vanish | | 2 | none | (Bosconian puts a 50xx here) | | 3 | 54xx | chip select, write only; reads give `0xff` | ## 51xx I/O MCU Fujitsu MB8843 running `51xx.bin`, 0x400 bytes, CRC `c2f57ef8`, SHA1 `50de79e0d6a76bda95ffb02fcce369a79e6abfec` (`namco51.cpp` ROM_START). Clock `18.432 MHz / 6 / 2 = 1.536 MHz`, divided by 6 internally → 256 kHz, 3.9 µs per cycle. Since commit 6589 (2020-04-23) MAME runs that program on its MB88xx core; before that a hand-written model stood in. ### Pin/port wiring (`namco51.cpp` `device_add_mconfig`, `galaga.cpp`) | MCU port | Dir | Galaga connection | |---|---|---| | K0-K2 | in | Bits 2-0 of the byte latched by the 06xx (the command) | | K3 | in | 06xx R/W line (1 = CPU wants to read) | | O0-O7 | out | 8-bit response byte, returned through the 06xx data window | | R0, R1 | in | `IN0` bits 3-0, bits 7-4 | | R2, R3 | in | `IN1` bits 3-0, bits 7-4 | | P0-P3 | out | P0 start-2 lamp, P1 start-1 lamp, P2 coin counter 2, P3 coin counter 1 (counters active low; `galaga_state::out`) | | /IRQ | in | 06xx slot-0 chip select | | /TC | in | Screen vblank (timer counts falling edges) | | /RESET | in | Misc latch bit 3 (`0x6823`), active low, shared with sub CPUs and 54xx | The device declares a coin-lockout output but nothing in the current LLE drives it; the pre-2020 model asserted it at 99 credits. Its hardware pin is `[unverified]`. Because the command reaches the MCU on K0-K2 only, commands and their arguments are 3-bit values. ### Cabinet inputs (`INPUT_PORTS_START( galaga )`; all active low) | Port | Bit | Signal | 51xx pin | |---|---|---|---| | `IN0` | 0 | unused (P1 up on 4/8-way boards) | R0.0 | | `IN0` | 1 | P1 joystick right | R0.1 | | `IN0` | 2 | unused (P1 down) | R0.2 | | `IN0` | 3 | P1 joystick left | R0.3 | | `IN0` | 4-7 | P2 (cocktail) joystick, same order up/right/down/left | R1.0-3 | | `IN1` | 0 | P1 fire | R2.0 | | `IN1` | 1 | P2 (cocktail) fire | R2.1 | | `IN1` | 2 | Start 1 | R2.2 | | `IN1` | 3 | Start 2 | R2.3 | | `IN1` | 4 | Coin 1 | R3.0 | | `IN1` | 5 | Coin 2 | R3.1 | | `IN1` | 6 | Service credit button | R3.2 | | `IN1` | 7 | Test (service mode) switch | R3.3 | DIP switches bypass the 51xx: the CPU reads them at `0x6800`-`0x6807`, one switch per address, bit 0 from `DSWB`, bit 1 from `DSWA` (`bosco_dsw_r`). Coinage is `DSWB` bits 2-0 (SWA:1-3): `7` 1C/1C, `6` 2C/1C, `2` 3C/1C, `4` 4C/1C, `1` 2C/3C, `3` 1C/2C, `5` 1C/3C, `0` free play. The game ROM turns that into a 51xx coinage command; the bytes it sends are `[unverified]` (MAME's trace only records a 4-byte write). The cabinet-type "switch" (DSWA bit 7) is an edge-connector signal. Old MAME (0.200) named the ports `IN0L`/`IN0H` (buttons, starts, coins) and `IN1L`/`IN1H` (joysticks) and fed the model in that order; the wiring above is what the real program expects. ### Command protocol (0.200 model, `namco51.cpp` `write`) Commands are the low 3 bits of the written byte. | Cmd | Effect | |---|---| | 0 | No operation | | 1 | Set coinage; credits reset to 0. Next four bytes: coins-per-credit slot 1, credits-per-coin slot 1, coins-per-credit slot 2, credits-per-coin slot 2 (each 0-7). Coins-per-credit 0 on slot 1 = free play | | 2 | Enter *credit* mode; start buttons active; response sequence restarts | | 3 | Disable joystick remapping (raw switch nibble returned) | | 4 | Enable joystick remapping (direction code returned) | | 5 | Enter *switch* mode; response sequence restarts | | 6, 7 | No operation | Power-on state of the model: switch mode, remapping off, credits 0, both slots 1 coin/1 credit. (Xevious got a six-argument variant of command 1 that MAME called a possible game bug; not applicable to Galaga.) ### Responses Reads return a repeating three-byte sequence; commands 2 and 5 reset the position. | Mode | Byte | Bits | Contents | |---|---|---|---| | switch | 0 | 7-0 | `IN0` as wired (R0 low nibble, R1 high) | | switch | 1 | 7-0 | `IN1` as wired (R2 low nibble, R3 high) | | switch | 2 | 7-0 | `0x00` | | credit | 0 | 7-0 | Credits in BCD, tens high; `0xBB` whenever the test switch (`IN1` bit 7) is closed; free play reports 100 (`0xA0`) | | credit | 1 | 3-0 | P1 joystick: raw active-low nibble (bit 0 up, 1 right, 2 down, 3 left) or, with remapping, the direction code below | | credit | 1 | 4 | P1 fire edge: 0 if pressed since the previous byte-1 read, else 1 | | credit | 1 | 5 | P1 fire level: 0 while held, 1 released | | credit | 1 | 7-6 | 0 | | credit | 2 | 5-0 | Same for P2 from `IN0` bits 7-4 and `IN1` bit 1 | Coin and start handling happens while byte 0 is produced: - Coin 1, coin 2 and service are edge-detected against the previous byte-0 read (no timed debounce in the model). A coin edge bumps that slot's coin count and pulses its counter on P (`0x04` or `0x08`, then `0x0c`); when the count reaches coins-per-credit it drops by that amount and credits grow by credits-per-coin. Service adds one credit, no counter pulse. - At 99+ credits lockout is asserted and coins are ignored (credits are not clamped, so a multi-credit coin can carry them past 99). - Start 1 with 1+ credits deducts 1, else Start 2 with 2+ credits deducts 2; either enters a "game running" sub-state where starts are ignored and lamps are off until command 2 arrives again. - Lamps: 2+ credits both blink, 1 credit only start-1 blinks, else off. The model blinked on frame-counter bit 4 and called that rate a guess `[unverified]`. Direction code, indexed by the raw active-low joystick nibble: | Raw | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| | Code | F | E | D | 5 | C | 9 | 7 | 6 | B | 3 | A | 4 | 1 | 2 | 0 | 8 | Codes 0-7 run clockwise from up (0) through right (2), down (4), left (6); 8 is centre. Galaga's 2-way stick yields only raw `F`→8, `D`→2, `7`→6, `5`→9. MAME notes the impossible-combination codes came from bootlegs, not a Namco chip. ### What MAME's instruction-level 51xx actually does (oracle, 2026-09-11) Fitted from `oracle/reference/game-2400` and implemented in `src/galaga/n51xx.rs`; these override the 0.200 model where they differ: - Switch-mode byte 2 reads `0xff`, not `0x00`. - Credit-mode bytes 1 and 2 read `0xff` until a start has been accepted; the game-running state clears bits 7-6 (`0x3f` idle) one frame after the start deducts its credit. - The chip samples the cabinet once per frame at vblank. The reply the main CPU polls in a given vblank carries the *previous* frame's sample for level fields (joystick nibble, fire held in bit 5) and edge events (coins, starts, fire pressed in bit 4) computed from the sample before that: a coin held from frame N shows as a credit in the poll of frame N+2, a joystick held from frame N shows in the poll of frame N+1, and the fire-pressed bit is a one-frame pulse one frame behind the level. - The fire-pressed bit is debounced over two samples (measured 2026-10-02 with `oracle/movie_on_mame.sh`, every reply byte compared per frame): a press shows only once the button has been down on two consecutive samples, and the button must be up on two consecutive samples before another press can show. A one-frame tap never fires, tapping on alternate frames fires nothing at all, and hold 2 / release 1 / hold 2 is one press; when a press does show, it shows on the same frame as an undebounced edge would. The fire-held level bit is not debounced. So the fastest fire the chip passes is a press every 4 frames. The model had no debounce until then, and the TAS player and challenge planner fired on alternate frames, twice as fast as a cabinet can: that is how a run reached "NUMBER OF HITS 41" (two rockets 2 frames apart meeting one enemy a frame apart). `n51xx::tests` holds the measured rows. Coins and starts were not checked with short presses. - Coins are ignored outside credit mode (the self-test and the switch-mode window before the game's first command 2). - Galaga's boot sends command 5 four times, then the 54xx's twelve parameter bytes, then `1,1,1,1,1` (coinage 1/1 on both slots), `2`, `3`, `0`. ## 54xx bus side (`namco54.cpp`) Fujitsu MB8844 running `54xx.bin`, 0x400 bytes, CRC `ee7357e0`, SHA1 `01bdf984a49e8d0cc8761b2cc162fd6434d5afbe`, also at 1.536 MHz (256 kHz instruction rate). Only writes reach it: - The 06xx slot-3 write latches the whole byte; the MCU sees the high nibble on K3-K0 and the low nibble on port R0 (R3-R0). - The slot-3 chip select drives the MCU /IRQ (pin R10//IRQ), so each byte is an external interrupt whose handler reads K and R0. - Under control `0xA8` a byte lands every 666.7 µs; a 12-byte burst takes about 8 ms of the main CPU's NMI attention. - Reset from misc latch bit 3, shared with the 51xx. - Command format, for orientation: high nibble is the operation (`0` nop, `1`/`2`/`5` trigger sound A/B/C, `3`/`4` load 4 parameter bytes for A/B, `6` loads 5 for C, `7` sets C's volume from the low nibble, `8`-`F` nop). O0-O7 and R4-R7 feed the discrete network — see sound-hardware.md. Pre-2020 MAME asserted /IRQ for a fixed 21 µs per byte (one 48 kHz clock); the current model uses the 06xx select pulse, half the byte period. ## MB88xx family: sizing summary (`mb88xx.cpp`, `mb88xx.h`) | Item | Value | |---|---| | Data width | 4-bit A, X, Y; RAM addressed as `(X<<4)+Y` | | Program counter | 6-bit PC in a 64-byte page + 4-bit page PA; byte-wide instruction ROM | | MB8843 | 10-bit program space (1024 bytes), 6-bit data space (64 nibbles), 42 pins | | MB8844 | Same 1024 bytes / 64 nibbles, 28 pins (fewer ports) | | Stack | 4 × 10-bit return addresses; interrupts also stash C, Z, ST in the spare bits | | Flags | ST (status: gates `jmp`/`call`/`jpl`, set by most instructions, cleared by failed tests), Z, C, V (timer overflow), S (serial), I (IRQ pin) | | Instructions | 256 opcodes fully decoded, about 68 mnemonics: 4-bit ALU with carry, DAA/DAS, rotates, RAM load/store/exchange, bit set/reset/test on RAM and R0, port in/out (K, O, P, R), immediate compare/add, 2-byte `call`/`jpl` across pages, 1-byte in-page `jmp`, `en`/`dis` interrupt masks | | Cycles | 1 for everything except `jpa`, `en`, `dis`, `call`, `jpl` (2); interrupt entry charged 3 (MAME marks it a guess) | | Clock | External clock ÷ 6 per cycle: 1.536 MHz → 256 kHz → 3.9 µs | | Timer | 8-bit TH:TL; +1 every 32 cycles on the internal clock (prescale a guess) or on TC falling edges; overflow sets V and raises the timer interrupt | | Interrupts | External /IRQ (vector `0x002`), timer (`0x004`), serial (`0x006`), each with an enable bit; entry pushes PC + C/Z/ST, page → 0, ST → 1. Vectors inferred from another game's MCU, not a datasheet | | Serial | 4-bit shift register on SI/SO, prescale 6 (a guess); neither Galaga chip binds SI/SO in MAME | | Output PLA | O writes pass a mask option (carry picks the nibble); neither Galaga chip supplies PLA data | What the two programs exercise in MAME: the 51xx uses the external interrupt, the TC-clocked timer (vblank) and ports K/O/P/R0-R3; the 54xx the external interrupt and ports K/O/R0/R1. Whether either uses the internal-clock timer or serial port is `[unverified]` short of disassembling the ROMs. ## HLE or LLE Two ways to provide the 51xx and 54xx, stated without a recommendation. **(a) LLE — run `51xx.bin`/`54xx.bin` on an MB88xx core.** - Needs a new 4-bit CPU core covering the table above: 256 opcodes, the ST/skip model, the 4-deep stack, three-source interrupts, the dual-clock timer, port callbacks. Both chips together cost ~0.5 M cycles/s, negligible beside three Z80s at 3.072 MHz. - Both 1 KB ROMs (hashes above) are needed at run time; they ship in the MAME `galaga` set. - The 06xx must be modelled at line level (select pulses, R/W, first-NMI suppression) so the programs see the edges MAME gives them. - Verification: MAME's `mb88xx` is the only public oracle, written from the datasheet plus observation, with its own guesses (timer and serial prescales, interrupt cost, vectors). Matching it trace-for-trace proves equivalence to MAME, not to silicon; no MB88xx conformance suite exists. Credit/coinage/lamp/start behaviour comes from the real program, with no hand-written rules. **(b) HLE — hand-written 51xx per the protocol above, plus a 54xx that only latches bytes for the sound model.** - No new CPU core; the 06xx can stay a byte-level state machine (an NMI per byte period, a byte counter, no select edges). - The credit and coinage logic must be rebuilt by hand: per-slot coins-per-credit/credits-per-coin, service credits, free play, the 99-credit lockout, start deduction and the "game running" state, lamp blink, the `0xBB` test response, the 3-byte cycle, fire edge/level bits, and the direction table. MAME shipped exactly this for ~20 years (through 0.219) and Galaga was playable on it; it also needed an Xevious-specific patch and a guessed blink rate. - Verification: response bytes can be compared with MAME's LLE under the same input sequence, and the rules against the 0.200 source. Anything the real program does that the old model did not (the fire debounce, measured since; behaviour outside the documented commands, coin-counter pulse timing) stays `[unverified]` until `51xx.bin` is disassembled. Either route needs the same 06xx register semantics and NMI rate; the game's NMI handler and transfer bookkeeping depend on them.