// SPDX-License-Identifier: MIT //! Milestone 6: the Driving controller, the Booster Grip, joysticks and the SaveKey //! (the reference's tests/test_controllers.py). //! //! 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. use std::path::{Path, PathBuf}; use a2600::controllers::{BoosterGrip, DrivingController, Joystick, Paddles, Stick, GRAY}; use a2600::riot::RIOT; use a2600::savekey::{round_half_even, SaveKey, SIZE}; use a2600::tia::{TIA, VBLANK}; use a2600::{System, VCS_NTSC}; const INPT0: u16 = 0x08; const INPT2: u16 = 0x0A; const INPT4: u16 = 0x0C; const INPT5: u16 = 0x0D; const SWCHA: u16 = 0x280; const SWACNT: u16 = 0x281; const NTSC_CLOCK: f64 = 3_579_545.0; const CPU_HZ: f64 = NTSC_CLOCK / 3.0; fn swcha(riot: &mut RIOT) -> u8 { riot.read(SWCHA, 0) } // -- joystick, Driving controller, Booster Grip ------------------------------ #[test] fn a_joystick_closes_its_own_ports_pins_only() { let (mut tia, mut riot) = (TIA::default(), RIOT::new(0, 0, 1024, 1024)); let (left, right) = (Joystick::new(0), Joystick::new(1)); left.set( &mut tia, &mut riot, Stick { up: true, right: true, fire: true, ..Stick::default() }, ); assert_eq!(swcha(&mut riot), 0b0110_1111); // D4 up and D7 right closed right.set( &mut tia, &mut riot, Stick { down: true, ..Stick::default() }, ); assert_eq!(swcha(&mut riot), 0b0110_1101); assert!(tia.read(INPT4) & 0x80 == 0 && tia.read(INPT5) & 0x80 != 0); } #[test] fn 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_eq!(GRAY, [0b11, 0b01, 0b00, 0b10]); let (mut tia, mut riot) = (TIA::default(), RIOT::new(0, 0, 1024, 1024)); let mut wheel = DrivingController::new(&mut tia, &mut riot, 0); let mut seen = Vec::new(); for _ in 0..8 { seen.push(swcha(&mut riot) >> 4); wheel.turn(&mut tia, &mut riot, 1); } // Pins 3 and 4 are not connected and read 1; one bit changes per click. assert_eq!(seen, [0xF, 0xD, 0xC, 0xE, 0xF, 0xD, 0xC, 0xE]); assert_eq!(swcha(&mut riot) & 0x0F, 0x0F); // the right port is untouched wheel.turn(&mut tia, &mut riot, -1); assert_eq!(swcha(&mut riot) >> 4, 0xE); wheel.press(&mut tia, &mut riot, true); assert_eq!(tia.read(INPT4) & 0x80, 0); } #[test] fn a_booster_grip_button_charges_its_pad_after_the_dump() { let (mut tia, mut riot) = (TIA::default(), RIOT::new(0, 0, 1024, 1024)); tia.inputs = 0x30; // as the board sets it: nothing charges I0-I3 let paddles = Paddles::new(&mut tia, NTSC_CLOCK); let grip = BoosterGrip::new(paddles, 1); grip.set( &mut tia, &mut riot, true, false, Stick { up: true, ..Stick::default() }, ); tia.write(VBLANK as u8, 0x80); tia.run(10); assert_eq!(tia.read(INPT2) & 0x80, 0); // dumped tia.write(VBLANK as u8, 0x00); tia.run(228 * 3); // three lines: past 1,800 ohm x 0.068 uF at the trip point assert_ne!(tia.read(INPT2) & 0x80, 0); // pin 5 pressed: charged assert_eq!(tia.read(INPT0 + 3) & 0x80, 0); // pin 9 released: no charge (C19) assert_eq!(swcha(&mut riot) & 0x01, 0); // the joystick part: right port up grip.set(&mut tia, &mut riot, false, false, Stick::default()); assert_eq!(tia.read(INPT2) & 0x80, 0); } // -- the SaveKey --------------------------------------------------------------- /// An I2C master on the RIOT's right port, as a SaveKey game drives it. struct Master { riot: RIOT, cycle: u64, scl: bool, sda: bool, } impl Master { fn new(riot: RIOT) -> Master { let mut m = Master { riot, cycle: 0, scl: true, sda: true, }; m.put(); m } fn put(&mut self) { self.cycle += 10; // SWCHA D3 = SCL level, D2 = 0; SWACNT D3 always out, D2 out only to pull SDA low. self.riot.write(SWCHA, if self.scl { 0x08 } else { 0x00 }, self.cycle); self.riot .write(SWACNT, 0x08 | if self.sda { 0 } else { 0x04 }, self.cycle); } fn lines(&mut self, scl: bool, sda: bool) { self.scl = scl; self.sda = sda; self.put(); } fn start(&mut self) { self.lines(true, true); self.lines(true, false); self.lines(false, false); } fn stop(&mut self) { self.lines(false, false); self.lines(true, false); self.lines(true, true); } fn write_byte(&mut self, value: u8) -> bool { for i in (0..8).rev() { let bit = (value >> i) & 1 != 0; 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); let ack = swcha(&mut self.riot) & 0x04 == 0; self.lines(false, true); ack } fn read_byte(&mut self, ack: bool) -> u8 { let mut value = 0u8; for _ in 0..8 { self.lines(false, true); self.lines(true, true); value = value << 1 | (swcha(&mut self.riot) & 0x04 != 0) as u8; self.lines(false, true); } self.lines(false, !ack); self.lines(true, !ack); self.lines(false, !ack); self.lines(false, true); value } fn wait(&mut self, cycles: u64) { self.cycle += cycles; } } fn key_and_master(path: Option<&Path>) -> (SaveKey, Master) { let mut riot = RIOT::new(0, 0, 1024, 1024); let key = SaveKey::new(&mut riot, CPU_HZ, 1, path); (key, Master::new(riot)) } fn write_bytes(m: &mut Master, address: u16, data: &[u8]) { m.start(); assert!(m.write_byte(0xA0)); assert!(m.write_byte((address >> 8) as u8) && m.write_byte((address & 0xFF) as u8)); for &value in data { assert!(m.write_byte(value)); } m.stop(); } fn read_bytes(m: &mut Master, address: u16, count: usize) -> Vec { m.start(); assert!(m.write_byte(0xA0)); assert!(m.write_byte((address >> 8) as u8) && m.write_byte((address & 0xFF) as u8)); m.start(); // a random read: a repeated Start, then the read control byte assert!(m.write_byte(0xA1)); let out = (0..count).map(|i| m.read_byte(i < count - 1)).collect(); m.stop(); out } #[test] fn a_fresh_savekey_reads_ff_and_keeps_what_is_written() { let (key, mut m) = key_and_master(None); assert_eq!(read_bytes(&mut m, 0x0700, 4), [0xFF; 4]); write_bytes(&mut m, 0x0700, &[0x12, 0x34, 0x56]); m.wait(6000); // past the 5 ms write cycle assert_eq!(read_bytes(&mut m, 0x0700, 4), [0x12, 0x34, 0x56, 0xFF]); assert_eq!(key.chip.borrow().memory[0x0700..0x0703], [0x12, 0x34, 0x56]); } #[test] fn a_page_write_rolls_over_inside_its_64_byte_page() { let (key, mut m) = key_and_master(None); write_bytes(&mut m, 0x013E, &[0x01, 0x02, 0x03, 0x04]); // two bytes fit before the page end let chip = key.chip.borrow(); assert_eq!(chip.memory[0x013E..0x0140], [0x01, 0x02]); assert_eq!(chip.memory[0x0100..0x0102], [0x03, 0x04]); // wrapped to the page's start assert_eq!(chip.memory[0x0140], 0xFF); } #[test] fn the_chip_does_not_acknowledge_during_its_write_cycle() { let (_key, mut m) = key_and_master(None); write_bytes(&mut m, 0x0000, &[0x55]); m.start(); assert!(!m.write_byte(0xA0)); // busy: no acknowledge (data sheet 7.0) m.stop(); m.wait(round_half_even(0.005 * CPU_HZ) as u64); m.start(); assert!(m.write_byte(0xA0)); m.stop(); } #[test] fn another_control_code_or_chip_select_is_ignored() { let (_key, mut m) = key_and_master(None); m.start(); assert!(!m.write_byte(0xA2)); // chip select 001 m.stop(); m.start(); assert!(!m.write_byte(0x50)); m.stop(); } #[test] fn a_sequential_read_wraps_from_the_last_byte_to_the_first() { let (key, mut m) = key_and_master(None); key.chip.borrow_mut().memory[SIZE - 1] = 0x77; key.chip.borrow_mut().memory[0] = 0x88; assert_eq!(read_bytes(&mut m, (SIZE - 1) as u16, 2), [0x77, 0x88]); } fn scratch(name: &str) -> PathBuf { let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join(name); let _ = std::fs::create_dir_all(&dir); dir } #[test] fn the_savekey_keeps_its_bytes_in_its_file() { let path = scratch("savekey-file").join("a2600-rust").join("savekey.bin"); let _ = std::fs::remove_file(&path); let (_key, mut m) = key_and_master(Some(&path)); write_bytes(&mut m, 0x0040, &[0xA5]); assert_eq!(std::fs::metadata(&path).unwrap().len(), SIZE as u64); let (key, _m) = key_and_master(Some(&path)); assert_eq!(key.chip.borrow().memory[0x0040], 0xA5); } // (The reference's test of the save directory sets XDG_DATA_HOME in-process; // Rust's test runner shares one environment between threads, so it runs as one // test that restores the variable.) #[test] fn saves_go_under_the_user_data_directory() { let before = std::env::var("XDG_DATA_HOME").ok(); let tmp = scratch("xdg"); std::env::set_var("XDG_DATA_HOME", &tmp); assert_eq!( a2600::savekey::default_path(), tmp.join("a2600-rust").join("savekey.bin") ); std::env::remove_var("XDG_DATA_HOME"); let home = PathBuf::from(std::env::var("HOME").unwrap()); assert_eq!( a2600::savekey::default_path(), home.join(".local/share/a2600-rust/savekey.bin") ); if let Some(v) = before { std::env::set_var("XDG_DATA_HOME", v); } } // -- a SaveKey game's own code as the judge ------------------------------------- const GO_FISH: &str = "Atari - Atari 2600 (Aftermarket)/Go Fish! (World) (NTSC) (Aftermarket) (Unl).zip"; #[test] fn go_fish_reads_its_block_back_through_the_savekey() { let Ok(base) = std::env::var("NO_INTRO_DIR") else { return; }; let path = Path::new(&base).join(GO_FISH); if !path.is_file() { return; // NO_INTRO_DIR does not hold Go Fish! } let mut system = System::from_path(&path, VCS_NTSC).unwrap(); let riot = system.board().riot.clone(); let key = SaveKey::new(&mut riot.borrow_mut(), system.machine.timing.cpu_hz(), 1, None); let pattern = [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77]; key.chip.borrow_mut().memory[0xC0..0xC7].copy_from_slice(&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. let wanted: Vec = pattern[1..].iter().rev().copied().collect(); let ram = riot.borrow().ram.clone(); assert!(ram.windows(wanted.len()).any(|w| w == wanted.as_slice())); }