// SPDX-License-Identifier: MIT // Cites MAME 0.285 a7800.cpp (BSD-3-Clause, Dan Boris, Fabio Priuli, Mike Saarna, Robert // Tuccitto) as a detector's statement of the wiring; no code taken. NOTICE, section 2. //! Controllers in 7800 mode: the ProLine joystick with its two buttons. //! //! Wiring (docs/start-here.md "TIA and RIOT in 7800 mode"; DOC: the wiki's "Hardware //! interfacing guide"; MAME 0.285 `a7800.cpp` read as a detector's statement, OBSERVED): //! //! - directions on the 6532's port A, active low: left port D7-D4 (right, left, //! down, up), right port D3-D0, as on a 2600 (DOC); //! - **two-button mode**: when port B bit 2 (left port) or bit 4 (right port) is //! an output driven low, the RIOT holds controller pin 6 so that the buttons //! read on the TIA's pot inputs, 1 = pressed: INPT1 the left port's left //! button, INPT0 its right button (INPT3 / INPT2 for the right port), and //! INPT4 / INPT5 read released (DOC: the interfacing guide); //! - **one-button mode** otherwise: either button pulls INPT4 (INPT5) low, as a //! 2600 stick's fire button (DOC). //! //! The pads are computed when the TIA reads them (`tia.pad_source`), so the //! mode follows port B as the program sets it. The power-on state of port B //! (inputs) gives one-button mode, which One on One and Worm! rely on (DOC: the //! wiki's "Common emulator development issues"; the titles' behaviour agrees). //! //! Rust-forced differences (a7800-python `controllers.py` at d6b220d): the pad sources //! are boxed closures on the TIA (a2600-rust's `PadSource`), holding the controller's //! state in shared cells and the RIOT's `Rc`; `ProLine.set`'s keyword arguments are a //! [`Stick`]. The light gun's sensor needs MARIA's picture, which the TIA's closure //! cannot reach (MARIA is the board's): the board calls [`LightGun::look`] just before //! each TIA read that asks the pads, and the gun's closure uses what it saw; the look is //! the reference's `sees_light` unchanged (the same render, the same pixel), so the //! pads and `hits` are the reference's. use std::cell::{Cell, RefCell}; use std::rc::Rc; use a2600::controllers::Paddles; use a2600::riot::RIOT; use a2600::tia::TIA; use crate::board::Board; use crate::maria::Maria; pub const UP: u8 = 0x1; // within a port's nibble; DOC (see above) pub const DOWN: u8 = 0x2; pub const LEFT: u8 = 0x4; pub const RIGHT: u8 = 0x8; /// `ProLine.set`'s keyword arguments. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct Stick { pub up: bool, pub down: bool, pub left: bool, pub right: bool, pub left_button: bool, pub right_button: bool, } #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct ProLineState { pub directions: [u8; 2], // UP/DOWN/LEFT/RIGHT bits pressed, per port pub buttons: [[bool; 2]; 2], // [left, right] per port } /// Both ports' ProLine sticks on one board (`Board.tia`, `Board.riot`). pub struct ProLine { pub riot: Rc>, pub state: Rc>, } impl ProLine { pub fn new(tia: &mut TIA, riot: Rc>) -> ProLine { let state = Rc::new(RefCell::new(ProLineState::default())); let (s, r) = (state.clone(), riot.clone()); tia.pad_source = Some(Box::new(move |_tia: &TIA| pads(&s.borrow(), &r.borrow()))); ProLine { riot, state } } pub fn set(&self, port: usize, stick: Stick) { let mut state = self.state.borrow_mut(); state.directions[port] = (stick.up as u8) * UP | (stick.down as u8) * DOWN | (stick.left as u8) * LEFT | (stick.right as u8) * RIGHT; state.buttons[port] = [stick.left_button, stick.right_button]; let mut value: u8 = 0xFF; for (p, bits) in state.directions.iter().enumerate() { let nibble = (bits & UP) | (bits & DOWN) | (bits & LEFT) | (bits & RIGHT); let shift = if p == 0 { 4 } else { 0 }; value &= !(nibble << shift); } drop(state); self.riot.borrow_mut().set_input_a(value); } pub fn two_button(&self, port: usize) -> bool { two_button(&self.riot.borrow(), port) } /// I0-I5 as the TIA sees them now (D0 = I0). pub fn pads(&self) -> u32 { pads(&self.state.borrow(), &self.riot.borrow()) } } fn two_button(riot: &RIOT, port: usize) -> bool { let bit = if port == 0 { 0x04 } else { 0x10 }; // SWCHB D2 / D4 driven low: DOC (interfacing guide) riot.ddr_b & bit != 0 && riot.out_b & bit == 0 } fn pads(state: &ProLineState, riot: &RIOT) -> u32 { let mut pads: u32 = 0x30; // I4/I5 pulled up; I0-I3 read 0 unpressed (DOC: schematic, a2600 C19) for port in [0usize, 1] { let [left, right] = state.buttons[port]; if two_button(riot, port) { pads |= ((right as u32) << 0 | (left as u32) << 1) << (2 * port); } else if left || right { pads &= !(0x10 << port); } } pads } /// The XG-1 light gun on `port`, aimed at a beam position (U22: FITTED). /// /// Wiring, **confirmed from the dumps' own code** (DOC: read from Barnyard Blaster (USA), /// Crossbow (USA): `LDA SWCHA / AND #$10` reads the trigger, and an /// unrolled `BIT INPT4 / BPL` loop, one poll every 22 MARIA clocks, times /// the sensor): the trigger pulls port A's pin-1 line low (SWCHA D4 for the /// left port, D0 for the right), the light sensor pulls INPT4 (INPT5) low /// while it sees light. The start-here.md description (the trigger on a /// joystick direction line, the sensor on INPT4/5) agrees. /// /// **The sensor is a fit** (FITTED, docs/undocumented-behavior.md U22: the XG-1 /// schematic is not pinned, and would settle it): it sees light when the beam is on the lines /// `aim_line .. aim_line + LINES - 1` and between `aim_clock + DELAY` /// and `aim_clock + DELAY + HOLD` MARIA clocks of the line, and the colour /// shown at the aim point on that line has luminance >= `BRIGHT`. Positions /// are the board's (MARIA line and clock in line, docs/start-here.md; the /// visible picture is clocks 107-426, television.rs). pub struct LightGun { pub riot: Rc>, pub port: usize, pub aim: Option<(u32, u32)>, // (line, clock); None: pointed off screen pub trigger: bool, pub hits: Rc>, // sensor reads that saw light (instrumentation) pub seen: Rc>, // what the last look saw (see the module header) } impl LightGun { // FITTED (U22): Barnyard Blaster (an aimed shot starts the game, a miss does not) and // Crossbow (the window figure scores 500, sky 0) pass; other values tried: not recorded. pub const LINES: u32 = 4; pub const DELAY: u32 = 0; pub const HOLD: u32 = 64; pub const BRIGHT: u8 = 8; /// `LightGun(board, port)`: chained after the TIA's pad source, held by the board. pub fn attach(board: &mut Board, port: usize) { let hits = Rc::new(Cell::new(0)); let seen = Rc::new(Cell::new(false)); let mut chained = board.tia.pad_source.take(); let (h, s) = (hits.clone(), seen.clone()); board.tia.pad_source = Some(Box::new(move |tia: &TIA| { let mut pads = match chained.as_mut() { Some(source) => source(tia), None => 0x30, }; if s.get() { h.set(h.get() + 1); pads &= !(0x10 << port); } pads })); board.light_gun = Some(LightGun { riot: board.riot.clone(), port, aim: None, trigger: false, hits, seen, }); } pub fn set(&mut self, aim: Option<(u32, u32)>, trigger: bool) { self.aim = aim; self.trigger = trigger; let bit: u8 = if self.port == 0 { 0x10 } else { 0x01 }; // trigger SWCHA D4 / D0: DOC (the titles' code) let mut riot = self.riot.borrow_mut(); let mut value = riot.input_a | bit; if trigger { value &= !bit; } riot.set_input_a(value); } pub fn sees_light(&self, maria: &mut Maria) -> bool { let Some((line, clock)) = self.aim else { return false; }; if !(line <= maria.line && maria.line < line + Self::LINES) { return false; } let now = maria.clock_in_line; let start = clock + Self::DELAY; if !(start <= now && now < start + Self::HOLD) { return false; } maria.render_to(now); (maria.pixels[clock as usize] & 0x0F) >= Self::BRIGHT } /// The sensor, just before the TIA asks the pads (the module header). pub fn look(&mut self, maria: &mut Maria) { self.seen.set(self.sees_light(maria)); } } /// Four paddles on I0-I3: a2600-python's `controllers.Paddles` on this board's TIA. /// /// The pads' RC (1,800 ohm, 0.068 uF) is the 2600 console board's (DOC: 2600 /// schematic) and the pot's range and trip point are a2600-python's fit (FITTED /// there, to its own judges; not judged here); the 7800 console board's values /// are not in the pinned schematics (HYPOTHESIS that they equal the 2600's, U25: /// the 7800 schematic's pot section would settle it). The colour clock is MARIA's /// clock / 2 (DOC: [MT84] 2.1). pub fn paddles(board: &mut Board, colour_clock_hz: f64) -> Paddles { Paddles::new(&mut board.tia, colour_clock_hz) } /// The CX22 Trak-Ball in trak-ball mode, or an ST mouse: motion on port A (a fit). /// /// **Wiring not sourced here** (docs/undocumented-behavior.md U24): the CX22 /// in trak-ball mode is commonly described as giving, per axis, a direction /// level and a motion clock on the four direction pins (pin 1 horizontal /// motion, pin 2 horizontal direction, pin 3 vertical motion, pin 4 /// vertical direction); an Atari ST mouse gives a two-bit Gray code per axis /// on the same pins. `mode` picks one; neither is confirmed by a document /// pinned here or by a title's code (HYPOTHESIS, U24: the CX22 service manual or /// a title that reads one would settle it; no test judges it, so not FITTED). pub struct TrakBall { pub riot: Rc>, pub port: usize, pub mode: &'static str, pub x: i64, // accumulated steps pub y: i64, pub dx: i64, // last direction pub dy: i64, } impl TrakBall { pub const GRAY: [u8; 4] = [0b00, 0b01, 0b11, 0b10]; // HYPOTHESIS (U24) pub fn new(riot: Rc>, port: usize, mode: &'static str) -> Result { if mode != "cx22" && mode != "st" { return Err("mode is 'cx22' or 'st'".to_string()); } let mut ball = TrakBall { riot, port, mode, x: 0, y: 0, dx: 1, dy: 1, }; ball.apply(); Ok(ball) } /// One step per call on each axis that moves (sign = direction). pub fn move_by(&mut self, dx: i64, dy: i64) { if dx != 0 { self.dx = if dx > 0 { 1 } else { -1 }; self.x += self.dx; } if dy != 0 { self.dy = if dy > 0 { 1 } else { -1 }; self.y += self.dy; } self.apply(); } fn apply(&mut self) { let bits: u8 = if self.mode == "st" { Self::GRAY[self.x.rem_euclid(4) as usize] | Self::GRAY[self.y.rem_euclid(4) as usize] << 2 } else { (self.x & 1) as u8 | ((self.dx > 0) as u8) << 1 | ((self.y & 1) as u8) << 2 | ((self.dy > 0) as u8) << 3 }; let shift = if self.port == 0 { 4 } else { 0 }; let mut riot = self.riot.borrow_mut(); let value = (riot.input_a & !(0x0F << shift)) | bits << shift; riot.set_input_a(value); } }