# SPDX-License-Identifier: MIT """The 5200 controllers: analog sticks, keypads, two buttons, through POKEY and GTIA. [AHRM] 13.3 (tier 3), [FSM] Figure 1-6 (tier 2), [BUCH]: - **Stick**: port n drives POT(2n) horizontally and POT(2n+1) vertically, low = left/up. The board gives POKEY each pin's level through pokey-python's RC helper (``pokey_python.rc.PotRC``, the core's B5.8): a stick at count p crosses the threshold after p slow-scan steps (``SCAN_STEP`` cycles each) of charging with the dump transistors off. CONSOL bit 2 powers the pots: with it low no pin ever crosses and every POT reads 228. The stick is not self-centring; defaults centre 114 (C10, a labelled choice). - **Keypad, Start/Pause/Reset**: on POKEY's keyboard scan, for the port that CONSOL bits 0-1 select. K0 and K5 are unconnected ([FSM] Figure 1-6), so a key with code k (KBCODE bits 1-4, [AHRM] 13.3 table) answers at every scan code c with (c >> 1) & 15 == k: four codes per 64-step scan. - **Upper button**: POKEY's /KR2 for the selected port: Shift, Control and Break ([AHRM] 13.3). - **Lower button**: GTIA TRIG(n), 0 when pressed. The 2-port row grounds POT4-POT7 ([FAQ] 4.10 item 2) and has no ports 3-4. Evidence tags (methodology): the wiring above is DOC ([AHRM] 13.3, [FSM] Figure 1-6, [BUCH], [FAQ]); PAM v1.1's Pokey Adjust screen matches [FSM] Figure 3-4 on this model (a manufacturer's test program, read from the screen). The stick's centre and span and every trak-ball constant are HYPOTHESIS (labelled choices, C10, U11). Port 3-4 use: OBSERVED in Super Breakout's own four-player mode only (U4). """ from __future__ import annotations from dataclasses import dataclass, field from pokey_python.rc import PotRC # DOC: KBCODE bits 1-4 for each key ([AHRM] 13.3 table; [FSM] Figure 1-6). KEY_CODES = { "1": 0xF, "2": 0xE, "3": 0xD, "start": 0xC, "4": 0xB, "5": 0xA, "6": 0x9, "pause": 0x8, "7": 0x7, "8": 0x6, "9": 0x5, "reset": 0x4, "*": 0x3, "0": 0x2, "#": 0x1, } # fmt: skip NEVER = 10_000.0 # a pot count that never crosses within a scan (POT reads 228: DOC [AHRM] 13.3) # DOC: one slow pot-scan step per 15 kHz period of 114 machine cycles ([AHRM] 5.2, 5.9). The # core's RC helper keeps charge in cycles, so a stick at count p is a target of p steps. SCAN_STEP = 114 def pot_target(count: float) -> int | None: """PotRC's target (cycles of charging to threshold) for a pot count; None never crosses.""" return None if count >= NEVER else round(count * SCAN_STEP) @dataclass class Pad: """One controller's state, set by the frontend or a script.""" x: float = 114.0 # HYPOTHESIS: a centred stick at 114 (C10; [BUCH] gives the range 1-228) y: float = 114.0 trigger: bool = False # lower button top: bool = False # upper button keys: set[str] = field(default_factory=set) plugged: bool = True # CX53 Trak-Ball instead of a stick (see TrakBall): the frontend sets the # ball's speed in counts per frame; x/y are then derived, not set. trakball: bool = False vx: float = 0.0 vy: float = 0.0 class TrakBall: """The CX53 Trak-Ball as the board sees it: speed in, pot position out (U11). [DANB] (trak-ball page, tier 3): the motion board is two tachometers with digital inputs (slotted encoder wheels between opto-interrupters) and analog outputs; rolling faster is "pushing an analog joystick further in the corresponding direction". [AHRM] 13.3: CONSOL bit 2 "doubles as the calibration control line for the trackball and as a trackball detection mechanism". The numbers here are labelled choices, not measurements: a ball speed of ``full_speed`` encoder counts per frame gives a full deflection of ``span`` pot counts either side of ``centre``, the output follows the speed through a first-order lag of ``lag`` (fraction kept per frame), and with CONSOL bit 2 low the outputs sit at the centre (the calibration state) instead of reading 228 like an unpowered stick, which is what lets software tell the two apart. U11 stays open: the schematic's time constants were not read. Evidence: the speed-to-deflection behaviour is DOC ([DANB], [FSM] section 7); the CONSOL bit 2 behaviour is DOC ([AHRM] 13.3, [FSM] section 7) and is accepted by RealSports Football's and Soccer's own trak-ball tests (the cartridges' code, run on this board); 10 of the 12 [FAQ] titles follow a scripted roll. Every constant below is HYPOTHESIS: reading the motion board's RC values off [DANB]'s schematic and timing a real CX53 would settle them. """ centre = 114.0 # HYPOTHESIS (C10): the stick's labelled centre span = 100.0 # HYPOTHESIS: counts either side, inside [AHRM] 13.3's 160-count minimum full_speed = 8.0 # HYPOTHESIS: encoder counts per frame for a full deflection lag = 0.5 # HYPOTHESIS: fraction of the previous output kept per frame def __init__(self) -> None: self.out = [self.centre, self.centre] def step(self, vx: float, vy: float, calibrating: bool) -> tuple[float, float]: """One frame: the two pot counts for this frame's ball speed.""" for axis, speed in enumerate((vx, vy)): if calibrating: target = self.centre else: deflection = max(-1.0, min(1.0, speed / self.full_speed)) target = self.centre + deflection * self.span self.out[axis] = self.lag * self.out[axis] + (1.0 - self.lag) * target return self.out[0], self.out[1] class Controllers: def __init__(self, board) -> None: self.board = board machine = board.machine self.pads = [Pad(plugged=n < 2) for n in range(machine.ports)] self.wire_pots() board.pokey.key_response = self.key_response self._balls = [TrakBall() for _ in range(4)] self._trak_pins = [False] * 8 self.apply() def wire_pots(self) -> None: """Eight pot capacitors following the chip's dump transistors (core B5.8).""" pokey = self.board.pokey self.rc = PotRC(pokey) self._above = self.rc.level pokey.pot_level = self.pot_level pokey.on_dump = self.rc.dumped def pot_level(self, n: int) -> bool: if not self.board.gtia.consol_out & 4 and not self._trak_pins[n]: return False # DOC: pots unpowered, nothing crosses ([AHRM] 13.3) return self._above(n) def key_response(self, code: int) -> int: # DOC: CONSOL bits 0-1 select the scanned port; the upper button on /KR2 # ([AHRM] 13.3, [FSM] Figure 1-6). Response bit 1 = KR2, bit 0 = KR1. port = self.board.gtia.consol_out & 3 if not self.board.machine.consol_bit1_selects_ports_34: port &= 1 if port >= len(self.pads): return 0 pad = self.pads[port] if not pad.plugged: return 0 response = 2 if pad.top else 0 if pad.keys: k = (code >> 1) & 0x0F for key in pad.keys: if KEY_CODES.get(key) == k: response |= 1 break return response def apply(self) -> None: """Push the pads' analog positions and buttons into the chips (steps each trak-ball). Once per frame, never on a save-state restore: what it derives (pot targets, trak-ball pins, the lag, GTIA's trigger inputs) is saved as it stands. """ for n in range(4): trak = False if n < len(self.pads) and self.pads[n].plugged: pad = self.pads[n] if pad.trakball: trak = True calibrating = not self.board.gtia.consol_out & 4 pad.x, pad.y = self._balls[n].step(pad.vx, pad.vy, calibrating) counts = (max(1.0, pad.x), max(1.0, pad.y)) # DOC: range from 1 ([BUCH]) else: counts = (NEVER, NEVER) # HYPOTHESIS: an empty port reads like an open pot self._trak_pins[2 * n] = self._trak_pins[2 * n + 1] = trak for axis in range(2): pin = 2 * n + axis if pin >= 4 and self.board.machine.pots_upper_grounded: count = NEVER else: count = counts[axis] self.rc.target[pin] = pot_target(count) pressed = n < len(self.pads) and self.pads[n].plugged and self.pads[n].trigger self.board.gtia.set_trigger(n, pressed) def frame(self) -> None: """Called once per frame by the console before it runs.""" self.apply()