//! A hand-written model of the 51xx coin/credit/joystick MCU //! (`docs/custom-io.md`, "HLE or LLE": the user's decision of 2026-09-11 is //! HLE). The command protocol is the one MAME's pre-2020 model documented; //! the reply pipeline is fitted to what MAME's instruction-level 51xx //! produces in `oracle/reference/game-2400`: //! //! - the MCU samples the cabinet inputs once per frame, at vblank; //! - the reply the main CPU polls in the same vblank carries the *previous* //! frame's sample for level fields (joystick, fire held), and edge events //! (coins, starts, fire pressed) from the sample before that; //! - the "in game" bits (7-6 of the joystick bytes) clear one frame after //! a start deducts its credit; //! - byte 2 in switch mode reads 0xff, not the old model's 0x00; //! - the fire-pressed edge is debounced over two samples (measured against //! MAME's 51xx running the chip's own program, 2026-10-02): a press counts //! only once the button is down on two consecutive samples, and the button //! must be up on two consecutive samples before the next press can count. //! A one-frame tap never fires; the fire-held level bit is not debounced. //! //! What the oracle did not exercise is marked `[unverified]` in place. #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] enum Mode { Switch, Credit, } #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct N51xx { mode: Mode, remap: bool, credits: u32, coins_per_credit: [u8; 2], credits_per_coin: [u8; 2], coin_count: [u8; 2], args_remaining: u8, args: [u8; 4], in_game: bool, in_game_countdown: u8, /// Free play: the poll after a start reads 0x99 instead of 0xa0 (MAME's /// 51xx, measured 2026-09-11); the game watches for that dip. free_play_dip: bool, seq: u8, /// `[in0, in1]` samples, newest first: S[k], S[k-1], S[k-2], S[k-3]. samples: [[u8; 2]; 4], reply: [u8; 3], /// The debounced fire buttons (P1, P2): true while held, as the chip /// sees them. Old snapshots predate it and load with both up. #[serde(default)] fire_down: [bool; 2], } /// Direction code by raw active-low joystick nibble (remapping on). const DIRECTION_CODE: [u8; 16] = [ 0xF, 0xE, 0xD, 0x5, 0xC, 0x9, 0x7, 0x6, 0xB, 0x3, 0xA, 0x4, 0x1, 0x2, 0x0, 0x8, ]; fn pressed(sample: [u8; 2], port: usize, bit: u8) -> bool { sample[port] & (1 << bit) == 0 } fn bcd(value: u32) -> u8 { let value = value.min(99); (((value / 10) << 4) | (value % 10)) as u8 } impl N51xx { pub fn new() -> N51xx { N51xx { mode: Mode::Switch, remap: false, credits: 0, coins_per_credit: [1, 1], credits_per_coin: [1, 1], coin_count: [0, 0], args_remaining: 0, args: [0; 4], in_game: false, in_game_countdown: 0, free_play_dip: false, seq: 0, samples: [[0xff, 0xff]; 4], fire_down: [false; 2], reply: [0xff, 0xff, 0xff], } } /// Power-on and the reset from misc latch bit 3. pub fn reset(&mut self) { *self = N51xx::new(); } pub fn credits(&self) -> u32 { self.credits } /// A command or argument byte from the 06xx (write mode, slot 0). pub fn write(&mut self, value: u8) { if self.args_remaining > 0 { let index = 4 - usize::from(self.args_remaining); self.args[index] = value & 7; self.args_remaining -= 1; if self.args_remaining == 0 { self.coins_per_credit = [self.args[0], self.args[2]]; self.credits_per_coin = [self.args[1], self.args[3]]; } return; } match value & 7 { 1 => { self.args_remaining = 4; self.credits = 0; self.coin_count = [0, 0]; } 2 => { self.mode = Mode::Credit; self.seq = 0; // [unverified] that command 2 ends the game-running state; // the old model did this and no reference run reaches game over. self.in_game = false; self.in_game_countdown = 0; } 3 => self.remap = false, 4 => self.remap = true, 5 => { self.mode = Mode::Switch; self.seq = 0; self.in_game = false; self.in_game_countdown = 0; } _ => {} } } /// A read transfer starts: replies begin at byte 0. pub fn begin_read(&mut self) { self.seq = 0; } /// What decides the chip's O-port writes (`galaga::sched`, measured on /// MAME's 51xx): none in switch mode; in credit mode, whether a game is /// running. pub fn o_write_state(&self) -> Option { match self.mode { Mode::Switch => None, Mode::Credit => Some(self.in_game), } } /// The next reply byte (read mode, slot 0). pub fn read(&mut self) -> u8 { let value = self.reply[usize::from(self.seq)]; self.seq = (self.seq + 1) % 3; value } /// The vblank sample: shift the ring, apply edge events, fix the reply /// bytes the main CPU will poll until the next vblank. pub fn sample(&mut self, in0: u8, in1: u8) { self.samples = [ [in0, in1], self.samples[0], self.samples[1], self.samples[2], ]; let level = self.samples[1]; let edge_now = self.samples[2]; let edge_before = self.samples[3]; let edge = |port: usize, bit: u8| pressed(edge_now, port, bit) && !pressed(edge_before, port, bit); self.free_play_dip = false; if self.in_game_countdown > 0 { self.in_game_countdown -= 1; if self.in_game_countdown == 0 { self.in_game = true; } } if self.mode == Mode::Credit { let free_play = self.coins_per_credit[0] == 0; for slot in 0..2 { if edge(1, 4 + slot as u8) && self.credits < 99 && !free_play { self.coin_count[slot] += 1; if self.coin_count[slot] >= self.coins_per_credit[slot] { self.coin_count[slot] -= self.coins_per_credit[slot]; self.credits += u32::from(self.credits_per_coin[slot]); } } } if edge(1, 6) && self.credits < 99 { self.credits += 1; } if !self.in_game && self.in_game_countdown == 0 { if edge(1, 2) && (self.credits >= 1 || free_play) { self.credits = self.credits.saturating_sub(1); self.in_game_countdown = 1; self.free_play_dip = free_play; } else if edge(1, 3) && (self.credits >= 2 || free_play) { self.credits = self.credits.saturating_sub(2); self.in_game_countdown = 1; self.free_play_dip = free_play; } } } // Fire, debounced: a press shows when the two samples behind the // reply (the ones a plain edge would compare) are both down and the // button was up; the debounced state follows two agreeing samples. let mut fire_edge = [false; 2]; for (p, bit) in [(0usize, 0u8), (1, 1)] { let (older, newer) = (pressed(edge_now, 1, bit), pressed(level, 1, bit)); fire_edge[p] = older && newer && !self.fire_down[p]; if older == newer { self.fire_down[p] = newer; } } let [fire1_edge, fire2_edge] = fire_edge; self.reply = match self.mode { Mode::Switch => [level[0], level[1], 0xff], Mode::Credit => { let free_play = self.coins_per_credit[0] == 0; let byte0 = if pressed(level, 1, 7) { 0xbb } else if free_play { // Free play reads 0xa0, and 0x99 for the one poll after a // start (MAME's 51xx with the free-play DIP, 2026-09-11). if self.free_play_dip { 0x99 } else { 0xa0 } } else { bcd(self.credits) }; let game_bits = if self.in_game { 0x00 } else { 0xc0 }; let stick = |nibble: u8| { if self.remap { DIRECTION_CODE[usize::from(nibble)] } else { nibble } }; let byte1 = stick(level[0] & 0x0f) | if fire1_edge { 0 } else { 0x10 } | if pressed(level, 1, 0) { 0 } else { 0x20 } | game_bits; let byte2 = stick(level[0] >> 4) | if fire2_edge { 0 } else { 0x10 } | if pressed(level, 1, 1) { 0 } else { 0x20 } | game_bits; [byte0, byte1, byte2] } }; } } impl Default for N51xx { fn default() -> Self { N51xx::new() } } #[cfg(test)] mod tests { use super::N51xx; /// The frames on which the P1 fire-pressed bit shows, for a pattern of /// held frames ('#'), one sample per frame, in credit mode. fn pressed_frames(held: &str) -> String { let mut chip = N51xx::new(); chip.write(2); held.chars() .map(|c| { chip.sample(0xff, if c == '#' { 0xfe } else { 0xff }); chip.begin_read(); chip.read(); if chip.read() & 0x10 == 0 { 'E' } else { '.' } }) .collect() } #[test] fn fire_is_debounced_as_mames_51xx_measured() { // Input and reply rows as MAME 0.285's 51xx (the chip's own program) // produced them, 2026-10-02 (docs/custom-io.md). for (held, mame) in [ (".#......", "........"), (".##......", "...E....."), (".##.##......", "...E........"), (".##..##......", "...E...E....."), (".###.###......", "...E.........."), (".#.#.#.#......", ".............."), (".##..##..##......", "...E...E...E....."), (".##########......", "...E............."), ] { assert_eq!(pressed_frames(held), mame, "held {held}"); } } }