# 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). """ from __future__ import annotations UP, DOWN, LEFT, RIGHT = 0x1, 0x2, 0x4, 0x8 # within a port's nibble; DOC (see above) class ProLine: """Both ports' ProLine sticks on one board (``Board.tia``, ``Board.riot``).""" def __init__(self, tia, riot) -> None: self.tia = tia self.riot = riot self.directions = [0, 0] # UP/DOWN/LEFT/RIGHT bits pressed, per port self.buttons = [[False, False], [False, False]] # [left, right] per port tia.pad_source = self.pads def set(self, port: int, *, up=False, down=False, left=False, right=False, left_button=False, right_button=False) -> None: # fmt: skip self.directions[port] = up * UP | down * DOWN | left * LEFT | right * RIGHT self.buttons[port] = [left_button, right_button] value = 0xFF for p, bits in enumerate(self.directions): nibble = (bits & UP) | (bits & DOWN) | (bits & LEFT) | (bits & RIGHT) shift = 4 if p == 0 else 0 value &= ~(nibble << shift) & 0xFF self.riot.set_input_a(value) def two_button(self, port: int) -> bool: bit = 0x04 if port == 0 else 0x10 # SWCHB D2 / D4 driven low: DOC (interfacing guide) riot = self.riot return bool(riot.ddr_b & bit) and not riot.out_b & bit def pads(self) -> int: """I0-I5 as the TIA sees them now (D0 = I0).""" pads = 0x30 # I4/I5 pulled up; I0-I3 read 0 unpressed (DOC: schematic, a2600 C19) for port in (0, 1): left, right = self.buttons[port] if self.two_button(port): pads |= (right << 0 | left << 1) << (2 * port) elif left or right: pads &= ~(0x10 << port) return pads class LightGun: """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.py). """ # 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. LINES = 4 DELAY = 0 HOLD = 64 BRIGHT = 8 def __init__(self, board, port: int = 0) -> None: self.board = board self.maria = board.maria self.riot = board.riot self.tia = board.tia self.port = port self.aim: tuple[int, int] | None = None # (line, clock); None: pointed off screen self.trigger = False self._chained = self.tia.pad_source self.tia.pad_source = self.pads self.hits = 0 # sensor reads that saw light (instrumentation) def set(self, *, aim: tuple[int, int] | None = None, trigger: bool = False) -> None: self.aim = aim self.trigger = trigger bit = 0x10 if self.port == 0 else 0x01 # trigger SWCHA D4 / D0: DOC (the titles' code) value = self.riot.input_a | bit if trigger: value &= ~bit self.riot.set_input_a(value & 0xFF) def sees_light(self) -> bool: if self.aim is None: return False line, clock = self.aim maria = self.maria if not line <= maria.line < line + self.LINES: return False now = maria.clock_in_line start = clock + self.DELAY if not start <= now < start + self.HOLD: return False maria._render_to(now) return (maria.pixels[clock] & 0x0F) >= self.BRIGHT def pads(self) -> int: pads = self._chained() if self._chained is not None else 0x30 if self.sees_light(): self.hits += 1 pads &= ~(0x10 << self.port) return pads def paddles(board, colour_clock_hz: float): """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).""" from a2600_python.controllers import Paddles return Paddles(board.tia, colour_clock_hz) class TrakBall: """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). """ GRAY = (0b00, 0b01, 0b11, 0b10) # HYPOTHESIS (U24) def __init__(self, riot, port: int = 0, mode: str = "cx22") -> None: if mode not in ("cx22", "st"): raise ValueError("mode is 'cx22' or 'st'") self.riot, self.port, self.mode = riot, port, mode self.x = self.y = 0 # accumulated steps self.dx = self.dy = 1 # last direction self._apply() def move(self, dx: int = 0, dy: int = 0) -> None: """One step per call on each axis that moves (sign = direction).""" if dx: self.dx = 1 if dx > 0 else -1 self.x += self.dx if dy: self.dy = 1 if dy > 0 else -1 self.y += self.dy self._apply() def _apply(self) -> None: if self.mode == "st": bits = self.GRAY[self.x % 4] | self.GRAY[self.y % 4] << 2 else: bits = (self.x & 1) | (self.dx > 0) << 1 | (self.y & 1) << 2 | (self.dy > 0) << 3 shift = 4 if self.port == 0 else 0 value = self.riot.input_a & ~(0x0F << shift) | bits << shift self.riot.set_input_a(value & 0xFF)