// SPDX-License-Identifier: MIT //! The controller models and save states on a synthetic board (a5200-python's //! tests/test_controllers.py and tests/test_state.py, transcribed). mod common; use a5200::cartridge::{load_cartridge, Source}; use a5200::controllers::{pot_level, Controllers, TrakBall}; use a5200::{state, Console, A5200}; use common::{fake_bios, fake_board, picture_program, JMP_F800}; use pokey::Look; #[test] fn trakball_speed_maps_to_deflection_through_the_lag() { let mut ball = TrakBall::new(); let (mut x, mut y) = (0.0, 0.0); for _ in 0..30 { (x, y) = ball.step( TrakBall::FULL_SPEED * 2.0, -TrakBall::FULL_SPEED / 2.0, false, ); } assert!((x - (TrakBall::CENTRE + TrakBall::SPAN)).abs() < 0.01); // clamped full right assert!((y - (TrakBall::CENTRE - TrakBall::SPAN / 2.0)).abs() < 0.01); // half up let (x, _) = ball.step(0.0, 0.0, false); assert!(TrakBall::CENTRE < x && x < TrakBall::CENTRE + TrakBall::SPAN); // decays, not jumps } #[test] fn trakball_calibrates_to_centre_with_consol_bit2_low() { let mut ball = TrakBall::new(); let mut x = 0.0; for _ in 0..30 { (x, _) = ball.step(TrakBall::FULL_SPEED, 0.0, true); } assert!((x - TrakBall::CENTRE).abs() < 0.01); } #[test] fn consol_bit2_low_cuts_a_stick_but_not_a_trakball() { let mut cpu = fake_board(&JMP_F800, None); let mut controllers = Controllers::new(&mut cpu.bus); controllers.pads.borrow_mut()[1].trakball = true; cpu.bus.gtia.consol_out.set(0); // pot power off controllers.apply(&mut cpu.bus); let pins = controllers._trak_pins.get(); assert_eq!(pins[2..4], [true, true]); assert_eq!(pins[0..2], [false, false]); let look = Look { clock: cpu.bus.pokey.clock, dump_active: cpu.bus.pokey.dump_active, }; // the stick on port 1 never crosses assert!(!pot_level( &controllers.rc, cpu.bus.gtia.consol_out.get(), pins, 0, look )); } #[test] fn restore_replays_identically() { let (code, ram) = picture_program(0x22, 0x42); let cart = load_cartridge(Source::Bytes(&vec![0u8; 0x8000]), None, None).unwrap(); let mut console = Console::new(cart, A5200, Some(fake_bios(&code)), None).unwrap(); for (address, value) in ram { console.board_mut().ram[address] = value; } console.run_frames(3); let saved = state::snapshot(&console); console.run_frames(2); let first = ( console.board().frame.clone(), console.board().cycles, console.cpu.pc, ); state::restore(&mut console, &saved).unwrap(); console.run_frames(2); let second = ( console.board().frame.clone(), console.board().cycles, console.cpu.pc, ); assert_eq!(first, second); // Rust: and through bytes, into a fresh console. let bytes = state::to_bytes(&saved); let cart = load_cartridge(Source::Bytes(&vec![0u8; 0x8000]), None, None).unwrap(); let mut fresh = Console::new(cart, A5200, Some(fake_bios(&code)), None).unwrap(); state::restore(&mut fresh, &state::from_bytes(&bytes).unwrap()).unwrap(); fresh.run_frames(2); let third = ( fresh.board().frame.clone(), fresh.board().cycles, fresh.cpu.pc, ); assert_eq!(first, third); }