// SPDX-License-Identifier: MIT //! The board: one bus callback per cycle, 13-bit decode, the floating bus, WSYNC //! (the reference's tests/test_board.py). mod common; use std::cell::RefCell; use std::rc::Rc; use a2600::board::Board; use a2600::cartridge::connector::{Cartridge, Restore}; use a2600::machine::{PowerOn, VCS_NTSC}; use a2600::System; use common::{blank_board, cart, image_4k, system_running}; // Hand-assembled. const NOP: &[u8] = &[0xEA]; const LDA_IMM: u8 = 0xA9; #[test] fn reset_lands_on_the_vector_with_the_power_on_registers() { let system = system_running(NOP); assert_eq!(system.cpu.pc, 0xF000); assert_eq!(system.cpu.s, 0xFD); assert_eq!(system.cycles(), 7); assert_eq!(system.cpu.x, VCS_NTSC.power_on.x); } #[test] fn addresses_are_masked_to_13_bits() { // LDA $F080 reads RIOT RAM $80 through its mirror (A12 = 1 would be the cart: // $F080 masks to $1080, the cartridge; $E080 masks to $0080, RAM). let program = [LDA_IMM, 0x42, 0x85, 0x80, 0xAD, 0x80, 0xE0]; // LDA #$42; STA $80; LDA $E080 let mut system = system_running(&program); for _ in 0..3 { system.step(); } assert_eq!(system.cpu.a, 0x42); } #[test] fn tia_reads_drive_only_d7_and_d6() { // LDA $0C (INPT4, fire released: D7 = 1): D5-D0 float, and the last byte // on the bus was the operand $0C. let mut system = system_running(&[0xA5, 0x0C]); system.step(); assert_eq!(system.cpu.a, 0x80 | 0x0C); } #[test] fn fire_button_reads_low_when_pressed() { let mut system = system_running(&[0xA5, 0x0C]); system.board_mut().tia.fire[0] = true; system.step(); assert_eq!(system.cpu.a & 0x80, 0); } /// A board whose next CPU cycle's access lands on line step `step`. fn board_at(step: u32) -> Board { let mut board = blank_board(); while (board.tia.step_in_line + 6) % 456 != step { board.read_byte(0x1000); } board } /// (halted cycles, step of the next read's access) for a WSYNC landing on `step`. fn halted_after_wsync_at(step: u32) -> (u64, u32) { let mut board = board_at(step); board.write_byte(0x02, 0); board.read_byte(0x1000); (board.halted_cycles, board.tia.step_in_line) } #[test] fn wsync_halts_reads_until_the_release_at_step_454() { // A write at step 16: halted until the cycle ending at 454 releases RDY. let (halted, landed) = halted_after_wsync_at(16); assert_eq!(landed, 4); // the read is the first cycle of the next line (steps 455-4) assert_eq!(halted, (454 - 16) / 6); } #[test] fn wsync_one_cycle_before_the_release_halts_one_cycle() { assert_eq!(halted_after_wsync_at(448), (1, 4)); } /// tia-fpga "The bus": a strobe entirely inside the release pulse is lost. #[test] fn wsync_inside_the_release_pulse_is_lost() { assert_eq!(halted_after_wsync_at(454), (0, 4)); } #[test] fn wsync_overlapping_the_pulse_takes_once_it_ends() { let (halted, landed) = halted_after_wsync_at(4); assert_eq!(landed, 4); assert_eq!(halted, 75); // a whole line less the write's cycle } #[derive(serde::Serialize, serde::Deserialize)] struct Spy { #[serde(skip)] inner: Option>, #[serde(skip)] seen: Option>>>, } impl Restore for Spy {} impl Cartridge for Spy { fn name(&self) -> &str { "spy" } fn access(&mut self, address: u16, value: u8, t: u64, write: bool) -> Option { self.seen.as_ref().unwrap().borrow_mut().push((address, t)); self.inner.as_mut().unwrap().access(address, value, t, write) } a2600::cartridge_state!(); } #[test] fn every_cycle_reaches_the_cartridge() { let seen = Rc::new(RefCell::new(Vec::new())); let spy = Spy { inner: Some(cart(&image_4k(&[0x85, 0x80, 0xEA]), "4K")), seen: Some(seen.clone()), }; let mut system = System::with_cartridge(Box::new(spy)); system.step(); system.step(); // STA $80: the RIOT write is seen by the cartridge too let seen = seen.borrow(); assert!(seen.contains(&(0x0080, system.cycles() - 1))); let times: Vec = seen.iter().map(|(_, t)| *t).collect(); assert_eq!(times, (0..seen.len() as u64).collect::>()); } #[test] fn power_on_phase_must_be_a_multiple_of_three() { let machine = a2600::Machine { power_on: PowerOn { hclock: 1, ..VCS_NTSC.power_on }, ..VCS_NTSC }; assert!(Board::new(machine, cart(&[0u8; 0x1000], "4K"), None, None, 0).is_err()); }