# SPDX-License-Identifier: MIT """Controllers on the two joystick ports: joysticks, paddles, keypads, the Driving controller and the Booster Grip (the SaveKey is in savekey.py). The port's nine pins (DOC: nocash 2k6specs "Controllers: External Port Pin-Outs"): pins 1-4 are SWCHA D4-D7 on the left port and D0-D3 on the right, pin 6 is INPT4/INPT5, pins 5 and 9 are INPT0/INPT1 (left) and INPT2/INPT3 (right), pin 7 is +5 V and pin 8 ground. A switch closed to ground reads 0. Joysticks and the console switches are plain levels (``RIOT.set_input_a``, ``RIOT.input_b``, ``TIA.fire``); :class:`Joystick` sets them for one port. A paddle is a timing measurement and a keypad a matrix scanned through a RIOT port: each installs itself as ``TIA.pad_source``, which the TIA asks for the pads' levels I0-I5 when the 6507 reads one (one pad source at a time). **Paddles** (docs/timing.md "Inputs"). Each of I0-I3 has a 1,800 Ω resistor and a 0.068 µF capacitor to ground on the console board (DOC: 2600 schematic, sheet 1, ``reference/schematics/Schematic_Atari2600_2000.png``), charged from +5 V through the paddle's pot. VBLANK D7 grounds the pads, emptying the capacitors; from its release the voltage rises as 1 - exp(-t/RC) and the pad reads 1 once it passes the input's trip point. The pot's range and the trip point are nocash's (DOC, below); the die traces have no paddle (tia-fpga left them off the board). The time is the TIA's own, in half colour clocks. **The Driving controller** (DOC: nocash "Controllers: Steering"): a 16-position rotary binary switch, of which bit 1 drives pin 1 and bit 1 XOR bit 0 drives pin 2, each closing to ground; the button is pin 6. So pins 1 and 2 read a two-bit Gray code that repeats every four positions: 11, 10, 00, 01 for positions 0-3 (pin 1 first, 1 = open). Pins 3 and 4 are not connected and read 1. Which way round is clockwise is not in the document: HYPOTHESIS that turning clockwise counts the switch up; Indy 500's car turning the same way as the knob would settle it (milestone 6 records the experiment). **The Booster Grip** (DOC: nocash "3-Button Joystick Adapter", used by Omega Race and Thrust): a joystick with two more buttons that connect pins 5 and 9 to +5 V, with no resistor of their own, so a pressed button charges its pad through the console's 1,800 Ω like a paddle at its 0 Ω end, and a released one leaves the pad uncharged (it reads 0, as with nothing plugged in, C19). Programs dump the pads with VBLANK D7 and read them later, as for paddles. nocash calls them the 2nd button (pin 5: INPT0/INPT2) and the 3rd (pin 9: INPT1/INPT3); which of CBS's printed names ("trigger", "booster") goes with which pin no permitted source says. **Keypads** (docs/timing.md "Inputs"). Twelve keys in four rows and three columns. The rows are driven from SWCHA (left port D4-D7, right D0-D3, top row first), so a program makes a row's bit an output and drives it low; a pressed key in that row pulls its column low. The columns are read on INPT0, INPT1, INPT4 (left) and INPT2, INPT3, INPT5 (right). The columns settle instantly here; a console's column lines have an RC delay programs wait out, so a program that waits reads the same. """ from __future__ import annotations import math R_SERIES = 1800.0 # ohms; DOC: 2600 schematic, sheet 1 CAPACITANCE = 0.068e-6 # farads; DOC: 2600 schematic, sheet 1 POT_MAX = 1.0e6 # ohms; DOC: nocash 2k6specs "POT Timing Table" (1M at -135 degrees) # The trip point, as a fraction of the supply. DOC: nocash's table gives 380 # scanlines for the 1 MOhm end and 190 for 500 kOhm; under the schematic's RC # that is a trip at 1 - exp(-t/RC) of the supply, about 0.30 (a TTL-like # threshold; milestone 6). The table is "counted in scanline units" and says # a console varies by at least one line and drifts as it warms: so the value # is the document's typical console, not a measurement here. Milestone 3 had # taken half the supply as a fit (742 lines at 1 MOhm, twice the document's). NOCASH_LINES_AT_POT_MAX = 380 _NTSC_LINE_S = 228 / (315e6 / 88) TRIP = 1 - math.exp(-NOCASH_LINES_AT_POT_MAX * _NTSC_LINE_S / ((R_SERIES + POT_MAX) * CAPACITANCE)) class Paddles: """Four paddles on I0-I3; ``position[i]`` from 0.0 (no resistance) to 1.0 (POT_MAX). ``None`` leaves a pad to ``TIA.inputs``: 0 on the board, as a pad with no pot never charges (docs/undocumented-behavior.md C19).""" def __init__(self, tia, colour_clock_hz: float) -> None: self.tia = tia self.position: list[float | None] = [None, None, None, None] self._seconds_per_step = 1 / (2 * colour_clock_hz) self._trip = -math.log(1 - TRIP) tia.pad_source = self.levels def trip_steps(self, i: int) -> float: """Half colour clocks from the dump's release until pad ``i`` reads 1.""" resistance = R_SERIES + POT_MAX * self.position[i] return resistance * CAPACITANCE * self._trip / self._seconds_per_step def levels(self) -> int: tia = self.tia levels = tia.inputs if tia.vblank & 0x80: return levels elapsed = tia.now - tia.dump_released for i in range(4): if self.position[i] is not None: if elapsed >= self.trip_steps(i): levels |= 1 << i else: levels &= ~(1 << i) return levels class Keypads: """Two 12-key keypads; ``pressed[port]`` is a set of (row, column), 0-3 x 0-2.""" COLUMN_PADS = ((0, 1, 4), (2, 3, 5)) def __init__(self, tia, riot) -> None: self.tia = tia self.riot = riot self.pressed: tuple[set, set] = (set(), set()) tia.pad_source = self.levels def levels(self) -> int: riot = self.riot driven_low = riot.ddr_a & ~riot.out_a & 0xFF levels = self.tia.inputs for port in (0, 1): shift = 4 if port == 0 else 0 for row, column in self.pressed[port]: if driven_low >> (shift + row) & 1: levels &= ~(1 << self.COLUMN_PADS[port][column]) return levels class Joystick: """A joystick on ``port`` (0 left, 1 right): four switches and a button. DOC: nocash "Controllers: Joysticks": up, down, left, right on pins 1-4 (SWCHA D4-D7 left, D0-D3 right), the button on pin 6 (INPT4/INPT5), each closing to ground.""" def __init__(self, tia, riot, port: int = 0) -> None: self.tia, self.riot, self.port = tia, riot, port self.shift = 4 if port == 0 else 0 def set(self, *, up=False, down=False, left=False, right=False, fire=False) -> None: closed = up | down << 1 | left << 2 | right << 3 riot, mask = self.riot, 0x0F << self.shift riot.set_input_a((riot.input_a & ~mask) | (~closed & 0x0F) << self.shift) self.tia.fire[self.port] = bool(fire) # The Driving controller's pins 1 and 2 for switch positions 0-3, 1 = open # (DOC: nocash "Controllers: Steering"; pin 1 = NOT bit 1, pin 2 = NOT (bit 1 XOR bit 0)). GRAY = tuple((1 - (n >> 1 & 1)) | (1 - ((n >> 1 ^ n) & 1)) << 1 for n in range(4)) class DrivingController: """A Driving controller on ``port``: ``turn(+1)`` is one click clockwise (HYPOTHESIS for the sense, see the module notes); ``fire`` the button.""" def __init__(self, tia, riot, port: int = 0) -> None: self.tia, self.riot, self.port = tia, riot, port self.shift = 4 if port == 0 else 0 self.position = 0 # the 16-position switch self.fire = False self._apply() def turn(self, clicks: int) -> None: self.position = (self.position + clicks) % 16 self._apply() def press(self, fire: bool) -> None: self.fire = fire self._apply() def _apply(self) -> None: riot, mask = self.riot, 0x0F << self.shift levels = GRAY[self.position & 3] | 0b1100 # pins 3 and 4 not connected riot.set_input_a((riot.input_a & ~mask) | levels << self.shift) self.tia.fire[self.port] = self.fire class BoosterGrip: """A Booster Grip on ``port``: a :class:`Joystick` plus the buttons on pins 5 and 9. The two buttons are pads charged through the console's RC (module notes), so they share a :class:`Paddles` with anything else on I0-I3.""" def __init__(self, tia, riot, paddles: Paddles, port: int = 0) -> None: self.joystick = Joystick(tia, riot, port) self.paddles = paddles self.port = port def set(self, *, pin5=False, pin9=False, **joystick) -> None: self.joystick.set(**joystick) first = 2 * self.port self.paddles.position[first] = 0.0 if pin5 else None self.paddles.position[first + 1] = 0.0 if pin9 else None