# SPDX-License-Identifier: MIT """Milestone 6: the Driving controller, the Booster Grip, joysticks and the SaveKey. Device rules come from nocash's 2k6specs (the port pins, the Driving controller's switch, the 3-button adapter) and Microchip's 24LC256 data sheet (the I2C protocol); the SaveKey's wiring from a SaveKey game's own code. The I2C master here bit-banges the RIOT the way that game does: SCL driven on SWCHA D3, SDA pulled low by making D2 an output that holds 0, released by making it an input. """ from __future__ import annotations import os from pathlib import Path import pytest from a2600_python import System from a2600_python.controllers import ( GRAY, BoosterGrip, DrivingController, Joystick, Paddles, ) from a2600_python.riot import RIOT from a2600_python.savekey import SIZE, SaveKey, default_path from a2600_python.tia import TIA, VBLANK INPT0, INPT1, INPT2, INPT4, INPT5 = 0x08, 0x09, 0x0A, 0x0C, 0x0D SWCHA, SWACNT = 0x280, 0x281 NTSC_CLOCK = 3_579_545.0 CPU_HZ = NTSC_CLOCK / 3 def swcha(riot: RIOT) -> int: return riot.read(SWCHA, 0) # -- joystick, Driving controller, Booster Grip ------------------------------ def test_a_joystick_closes_its_own_ports_pins_only(): tia, riot = TIA(), RIOT(0, 0, 1024, 1024) left, right = Joystick(tia, riot, 0), Joystick(tia, riot, 1) left.set(up=True, right=True, fire=True) assert swcha(riot) == 0b0110_1111 # D4 up and D7 right closed right.set(down=True) assert swcha(riot) == 0b0110_1101 assert not tia.read(INPT4) & 0x80 and tia.read(INPT5) & 0x80 def test_the_driving_controller_reads_nocash_s_gray_code(): # Pin 1 = NOT bit 1, pin 2 = NOT (bit 1 XOR bit 0); pin 1 in bit 0. assert GRAY == (0b11, 0b01, 0b00, 0b10) tia, riot = TIA(), RIOT(0, 0, 1024, 1024) wheel = DrivingController(tia, riot, 0) seen = [] for _ in range(8): seen.append(swcha(riot) >> 4) wheel.turn(1) # Pins 3 and 4 are not connected and read 1; one bit changes per click. assert seen == [0xF, 0xD, 0xC, 0xE] * 2 assert swcha(riot) & 0x0F == 0x0F # the right port is untouched wheel.turn(-1) assert swcha(riot) >> 4 == 0xE wheel.press(True) assert not tia.read(INPT4) & 0x80 def test_a_booster_grip_button_charges_its_pad_after_the_dump(): tia, riot = TIA(), RIOT(0, 0, 1024, 1024) tia.inputs = 0x30 # as the board sets it: nothing charges I0-I3 paddles = Paddles(tia, NTSC_CLOCK) grip = BoosterGrip(tia, riot, paddles, port=1) grip.set(pin5=True, up=True) tia.write(VBLANK, 0x80) tia.run(10) assert not tia.read(INPT2) & 0x80 # dumped tia.write(VBLANK, 0x00) tia.run(228 * 3) # three lines: past 1,800 ohm x 0.068 uF at the trip point assert tia.read(INPT2) & 0x80 # pin 5 pressed: charged assert not tia.read(INPT0 + 3) & 0x80 # pin 9 released: no charge (C19) assert swcha(riot) & 0x01 == 0 # the joystick part: right port up grip.set() assert not tia.read(INPT2) & 0x80 # -- the SaveKey --------------------------------------------------------------- class Master: """An I2C master on the RIOT's right port, as a SaveKey game drives it.""" def __init__(self, riot: RIOT) -> None: self.riot = riot self.cycle = 0 self.scl = True self.sda = True self._put() def _put(self) -> None: self.cycle += 10 # SWCHA D3 = SCL level, D2 = 0; SWACNT D3 always out, D2 out only to pull SDA low. self.riot.write(SWCHA, 0x08 if self.scl else 0x00, self.cycle) self.riot.write(SWACNT, 0x08 | (0 if self.sda else 0x04), self.cycle) def lines(self, scl: bool, sda: bool) -> None: self.scl, self.sda = scl, sda self._put() def start(self) -> None: self.lines(True, True) self.lines(True, False) self.lines(False, False) def stop(self) -> None: self.lines(False, False) self.lines(True, False) self.lines(True, True) def write_byte(self, value: int) -> bool: for i in range(7, -1, -1): bit = bool(value >> i & 1) self.lines(False, bit) self.lines(True, bit) self.lines(False, bit) self.lines(False, True) # release SDA for the acknowledge self.lines(True, True) ack = not swcha(self.riot) & 0x04 self.lines(False, True) return ack def read_byte(self, ack: bool) -> int: value = 0 for _ in range(8): self.lines(False, True) self.lines(True, True) value = value << 1 | bool(swcha(self.riot) & 0x04) self.lines(False, True) self.lines(False, not ack) self.lines(True, not ack) self.lines(False, not ack) self.lines(False, True) return value def wait(self, cycles: int) -> None: self.cycle += cycles def key_and_master(path: Path | None = None) -> tuple[SaveKey, Master]: riot = RIOT(0, 0, 1024, 1024) return SaveKey(riot, CPU_HZ, path=path), Master(riot) def write_bytes(m: Master, address: int, data: bytes) -> None: m.start() assert m.write_byte(0xA0) assert m.write_byte(address >> 8) and m.write_byte(address & 0xFF) for value in data: assert m.write_byte(value) m.stop() def read_bytes(m: Master, address: int, count: int) -> bytes: m.start() assert m.write_byte(0xA0) assert m.write_byte(address >> 8) and m.write_byte(address & 0xFF) m.start() # a random read: a repeated Start, then the read control byte assert m.write_byte(0xA1) out = bytes(m.read_byte(ack=i < count - 1) for i in range(count)) m.stop() return out def test_a_fresh_savekey_reads_ff_and_keeps_what_is_written(): key, m = key_and_master() assert read_bytes(m, 0x0700, 4) == b"\xff" * 4 write_bytes(m, 0x0700, b"\x12\x34\x56") m.wait(6000) # past the 5 ms write cycle assert read_bytes(m, 0x0700, 4) == b"\x12\x34\x56\xff" assert key.chip.memory[0x0700:0x0703] == b"\x12\x34\x56" def test_a_page_write_rolls_over_inside_its_64_byte_page(): key, m = key_and_master() write_bytes(m, 0x013E, b"\x01\x02\x03\x04") # two bytes fit before the page end assert key.chip.memory[0x013E:0x0140] == b"\x01\x02" assert key.chip.memory[0x0100:0x0102] == b"\x03\x04" # wrapped to the page's start assert key.chip.memory[0x0140] == 0xFF def test_the_chip_does_not_acknowledge_during_its_write_cycle(): _, m = key_and_master() write_bytes(m, 0x0000, b"\x55") m.start() assert not m.write_byte(0xA0) # busy: no acknowledge (data sheet 7.0) m.stop() m.wait(round(0.005 * CPU_HZ)) m.start() assert m.write_byte(0xA0) m.stop() def test_another_control_code_or_chip_select_is_ignored(): _, m = key_and_master() m.start() assert not m.write_byte(0xA2) # chip select 001 m.stop() m.start() assert not m.write_byte(0x50) m.stop() def test_a_sequential_read_wraps_from_the_last_byte_to_the_first(): key, m = key_and_master() key.chip.memory[SIZE - 1] = 0x77 key.chip.memory[0] = 0x88 assert read_bytes(m, SIZE - 1, 2) == b"\x77\x88" def test_the_savekey_keeps_its_bytes_in_its_file(tmp_path): path = tmp_path / "a2600-python" / "savekey.bin" _, m = key_and_master(path) write_bytes(m, 0x0040, b"\xa5") assert path.stat().st_size == SIZE key, m = key_and_master(path) assert key.chip.memory[0x0040] == 0xA5 def test_saves_go_under_the_user_data_directory(monkeypatch, tmp_path): monkeypatch.setenv("XDG_DATA_HOME", str(tmp_path)) assert default_path() == tmp_path / "a2600-python" / "savekey.bin" monkeypatch.delenv("XDG_DATA_HOME") assert default_path() == Path.home() / ".local/share/a2600-python/savekey.bin" # -- a SaveKey game's own code as the judge ------------------------------------- GO_FISH = "Atari - Atari 2600 (Aftermarket)/Go Fish! (World) (NTSC) (Aftermarket) (Unl).zip" @pytest.mark.slow def test_go_fish_reads_its_block_back_through_the_savekey(): base = os.environ.get("NO_INTRO_DIR") if not base or not (Path(base) / GO_FISH).is_file(): pytest.skip("NO_INTRO_DIR does not hold Go Fish!") system = System.from_rom(Path(base) / GO_FISH) key = SaveKey(system.board.riot, system.machine.timing.cpu_hz) pattern = bytes([0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77]) key.chip.memory[0xC0:0xC7] = pattern system.run_frames(10) # The game reads $00C0-$00C6 into RAM (stored last byte first): every # bit of every byte crossed the I2C lines as the game's code clocks them. assert bytes(reversed(pattern[1:])) in bytes(system.board.riot.ram)