// SPDX-License-Identifier: MIT //! The sockets the Atari 7800 board needs (the reference's tests/test_sockets.py). //! //! Existing TIA and RIOT objects handed to the board, with the RIOT's pulse //! count; the CPU/TIA phase as a machine-row parameter; the gates callable from //! outside with `root`. mod common; use std::cell::RefCell; use std::rc::Rc; use a2600::board::Board; use a2600::cartridge::connector::{Cartridge, NoCartridge}; use a2600::gates; use a2600::machine::{Machine, PowerOn, NTSC_7800, VCS_NTSC}; use a2600::riot::RIOT; use a2600::tia::TIA; use a2600::System; use common::{cart, image_4k, reference_root}; // A loop that reads INTIM and RAM and writes the TIA: LDA $284; STA $80; // LDA $80; STA COLUBK; STA WSYNC; JMP start. const LOOP: [u8; 14] = [ 0xad, 0x84, 0x02, 0x85, 0x80, 0xa5, 0x80, 0x85, 0x09, 0x85, 0x02, 0x4c, 0x00, 0xf0, ]; fn cartridge() -> Box { cart(&image_4k(&LOOP), "4K") } #[test] fn the_board_uses_handed_in_chips_as_they_stand() { let mut tia = TIA::new(10, a2600::machine::REFERENCE_TIA); tia.inputs = 0x3F; let riot = Rc::new(RefCell::new(RIOT::new(0x55, 0x40, 64, 64))); riot.borrow_mut().write(0x80, 0xA5, 100); // RAM written by "the BIOS" riot.borrow_mut().input_b = 0x2B; // whatever the host wired let mut board = Board::new(VCS_NTSC, cartridge(), Some(tia), Some(riot.clone()), 500).unwrap(); assert!(Rc::ptr_eq(&board.riot, &riot)); assert_eq!(board.tia.step_in_line, 20); assert!(board.cycles == 500 && board.tia.inputs == 0x3F && riot.borrow().input_b == 0x2B); assert_eq!(riot.borrow().ram[0], 0xA5); let seen = Rc::new(RefCell::new(Vec::new())); let log = seen.clone(); board .bus_observers .push(Box::new(move |_kind, _address, _value, cycle| { log.borrow_mut().push(cycle) })); let mut system = System::with_board(board, VCS_NTSC); system.step(); // the reset sequence: seven cycles from the handed-in count assert!(seen.borrow()[0] == 500 && system.board().cycles == 507); // The RIOT's lazy timer sees the cycle numbers go on from its own count. system.step(); // LDA INTIM let mut expected = RIOT::new(0x55, 0x40, 64, 64); let last = *seen.borrow().last().unwrap(); assert_eq!(riot.borrow().value, expected.timer_at(last) as i64); } #[test] fn a_2600_row_keeps_the_cycle_grid_and_a_hosted_row_does_not() { let off_grid = Machine { power_on: PowerOn { hclock: 0, ..VCS_NTSC.power_on }, ..VCS_NTSC }; assert!(Board::new(off_grid, cartridge(), None, None, 0).is_err()); for (hclock, half) in [(0, 0), (1, 1), (100, 1)] { let row = Machine { power_on: PowerOn { hclock, half_step: half, ..NTSC_7800.power_on }, ..NTSC_7800 }; let board = Board::new(row, cartridge(), None, None, 0).unwrap(); assert_eq!(board.tia.step_in_line, 2 * hclock + half); } } fn handed_over(cart: Box, machine: Machine) -> System { let power = machine.power_on; let mut tia = TIA::new(power.hclock, machine.tia_revision); tia.inputs = 0x30; let mut riot = RIOT::new(power.ram_fill, power.timer_value, power.timer_divider, 1023); riot.input_b = machine.switches; let board = Board::new(machine, cart, Some(tia), Some(Rc::new(RefCell::new(riot))), 0).unwrap(); System::with_board(board, machine) } #[test] fn lockstep_with_a_board_built_from_handed_in_chips() { let (agreed, difference) = gates::lockstep(&image_4k(&LOOP), &handed_over, VCS_NTSC, 20_000).unwrap(); assert!(difference.is_none() && agreed >= 20_000); } fn other_timer(cart: Box, machine: Machine) -> System { let power = PowerOn { timer_value: 0x10, ..machine.power_on }; let row = Machine { power_on: power, ..machine }; System::new(cart, row, None).unwrap() } #[test] fn lockstep_finds_a_board_that_differs() { let (_, difference) = gates::lockstep(&image_4k(&LOOP), &other_timer, VCS_NTSC, 20_000).unwrap(); assert!(difference.is_some()); } #[test] fn the_die_gate_runs_on_a_supplied_tia() { let root = reference_root(); let trace = root.join("reference/src/tia-fpga/sim/traces/phi2/vdel.trace"); if !trace.exists() { return; // die traces not fetched } let mut made = 0; let mut factory = || { made += 1; TIA::default() }; let results = gates::die_traces(&root, &mut factory, Some(vec![trace])).unwrap(); assert_eq!(results.len(), 1); let (scored, bad, passed) = results.values().next().unwrap(); assert!(*passed && bad.is_empty() && *scored > 20_000); assert_eq!(made, 1); } #[test] fn the_timer_rom_gate_runs_from_outside() { let root = reference_root(); if !root .join("tests/test_roms/stella-issues/606-129-TimerTest.zip") .exists() { return; // Stella issue ROMs not fetched } let results = gates::timer_roms(&root, &gates::default_system, VCS_NTSC).unwrap(); assert_eq!(results.len(), 14); assert!(results.values().all(|ok| *ok), "{results:?}"); } /// issue a7800-python 0002: timer_roms(machine=), as the other gates. #[test] fn the_timer_rom_gate_hands_its_row_to_the_factory() { let root = reference_root(); if !root .join("tests/test_roms/stella-issues/606-129-TimerTest.zip") .exists() { return; // Stella issue ROMs not fetched } let rows = RefCell::new(Vec::new()); let factory = |cart: Box, machine: Machine| { rows.borrow_mut().push(machine.name); System::new(cart, machine, None).unwrap() }; let results = gates::timer_roms(&root, &factory, NTSC_7800).unwrap(); assert_eq!(*rows.borrow(), vec![NTSC_7800.name; 14]); assert_eq!(results.len(), 14); assert!(results.values().all(|ok| *ok), "{results:?}"); } /// issue a7800-python 0002: gates.smoke_names(root), in file order, comments dropped. #[test] fn the_smoke_list_is_on_gates() { let root = reference_root(); if !root.join(gates::SMOKE_LIST).exists() { return; // no reference checkout } let names = gates::smoke_names(&root).unwrap(); let unique: std::collections::BTreeSet<&String> = names.iter().collect(); assert!(names.len() > 20 && unique.len() == names.len()); assert!(names.contains(&"Alpha Beam with Ernie (Japan, USA) (En)".to_string())); assert!(!names.iter().any(|n| n.starts_with('#') || n != n.trim())); // This repository's lockstep runs every name of it (scripts/cases.tsv, group smoke). let table = std::fs::read_to_string(std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("scripts/cases.tsv")).unwrap(); let cased: Vec<&str> = table .lines() .filter(|l| l.starts_with("smoke/")) .map(|l| { let rom = l.split('\t').nth(2).unwrap(); rom.strip_prefix("$NOINTRO/").unwrap().strip_suffix(".zip").unwrap() }) .collect(); assert_eq!(cased, names.iter().map(String::as_str).collect::>()); } /// issue a7800-python 0001: System(board=, cpu=) takes a CPU mid-run, no power-on. #[test] fn a_running_cpu_is_adopted_as_it_stands() { let mut host = System::with_cartridge(cartridge()); for _ in 0..40 { host.step(); } let System { mut cpu, .. } = host; let state = (cpu.pc, cpu.a, cpu.x, cpu.y, cpu.s, cpu.p); let old = &mut cpu.bus; let tia = std::mem::take(&mut old.tia); let cart = std::mem::replace(&mut old.cartridge, Box::new(NoCartridge)); let mut board = Board::new(VCS_NTSC, cart, Some(tia), Some(old.riot.clone()), old.cycles).unwrap(); let seen = Rc::new(RefCell::new(Vec::new())); let log = seen.clone(); board .bus_observers .push(Box::new(move |_kind, address, _value, _cycle| { log.borrow_mut().push(address) })); let (mut system, host_board) = System::with_cpu(board, VCS_NTSC, cpu); let c = &system.cpu; assert_eq!((c.pc, c.a, c.x, c.y, c.s, c.p), state); system.step(); // the next instruction of the loop, not a reset sequence assert!(!seen.borrow().is_empty() && seen.borrow()[0] == state.0 & 0x1FFF); assert!(host_board.cycles < system.board().cycles); // the old board no longer sees the bus } type Log = Rc>>; fn logging(log: &Log) -> a2600::board::BusObserver { let log = log.clone(); Box::new(move |k, a, v, c| log.borrow_mut().push((k, a, v, c))) } #[test] fn a_hand_over_mid_run_continues_every_bus_cycle() { let run = |hand_over: bool| -> Vec<(char, u16, u8, u64)> { let mut system = System::with_cartridge(cartridge()); let log: Log = Rc::new(RefCell::new(Vec::new())); system.board_mut().bus_observers.push(logging(&log)); for _ in 0..300 { system.step(); } if hand_over { let System { mut cpu, .. } = system; let old = &mut cpu.bus; let tia = std::mem::take(&mut old.tia); let cart = std::mem::replace(&mut old.cartridge, Box::new(NoCartridge)); let mut board = Board::new(VCS_NTSC, cart, Some(tia), Some(old.riot.clone()), old.cycles).unwrap(); board.bus_observers.push(logging(&log)); board.data_bus = old.data_bus; system = System::with_cpu(board, VCS_NTSC, cpu).0; } for _ in 0..3000 { system.step(); } let out = log.borrow().clone(); out }; assert_eq!(run(true), run(false)); } /// issue a7800-python 0003: a per-sample socket instead of replacing Audio.tick_b. #[test] fn on_sample_sees_every_sample_as_it_is_made() { let mut system = System::with_cartridge(cartridge()); let tia = &mut system.board_mut().tia; tia.audio.write(0x15, 0x04); // AUDC0: a pure tone tia.audio.write(0x19, 0x0F); // AUDV0 let heard = Rc::new(RefCell::new(Vec::new())); tia.audio.take_samples(); let ear = heard.clone(); tia.audio.on_sample = Some(Box::new(move |sample| ear.borrow_mut().push(sample))); let lines_before = system.board().tia.line; system.run_cycles(76 * 50); let lines = system.board().tia.line as i64 - lines_before as i64; let samples = system.board_mut().tia.audio.take_samples(); assert!(samples.len() >= 98 && *heard.borrow() == samples); // Two ticks a line, at two fixed clocks (the reference reads tia.hclock in // the callback; here the closure cannot borrow the TIA, so the count per // finished line stands in for it). assert!(lines >= 49 && (samples.len() as i64 - 2 * lines).abs() <= 2); let distinct: std::collections::BTreeSet = samples.iter().map(|s| s.to_bits()).collect(); assert_eq!(distinct.len(), 2); } /// issue a7800-python 0003: input_a, and port B's direction and output (read only). #[test] fn the_riot_ports_a_host_composes_are_readable() { let mut riot = RIOT::new(0, 0, 1024, 1024); riot.set_input_a(0x5F); riot.write(0x283, 0x14, 0); // SWBCNT: D2 and D4 outputs riot.write(0x282, 0x10, 1); // SWCHB: D4 high, D2 low assert_eq!((riot.input_a, riot.ddr_b, riot.out_b), (0x5F, 0x14, 0x10)); } /// issue a7800-python 0003: load(cpu_hz=) reaches DPC's oscillator only. #[test] fn a_clocked_scheme_takes_the_row_clock_and_the_rest_ignore_it() { use a2600::cartridge::dpc::{DPC, MUSIC_HZ, NTSC_CPU_HZ}; use a2600::cartridge::{build, load, mame_hash, Options, Rom}; use a2600::machine::PAL_7800; let clocked = |hz: Option| Options { cpu_hz: hz, ..Options::default() }; let ticks = |c: &dyn Cartridge| c.as_any().downcast_ref::().unwrap().ticks_per_cycle; let dpc_image = vec![0u8; 0x2800 + 255]; let pal = build(&dpc_image, "DPC", false, clocked(Some(PAL_7800.timing.cpu_hz()))).unwrap(); assert_eq!(ticks(&*pal), MUSIC_HZ / PAL_7800.timing.cpu_hz()); // The NTSC row's clock is the scheme's default to the last bit, so passing // it changes nothing on the 2600's reference row. assert_eq!(VCS_NTSC.timing.cpu_hz(), NTSC_CPU_HZ); assert_eq!( ticks(&*build(&dpc_image, "DPC", false, Options::default()).unwrap()), MUSIC_HZ / NTSC_CPU_HZ ); let plain = load( Rom::Bytes(&image_4k(&LOOP)), None, None, &mame_hash(), clocked(Some(PAL_7800.timing.cpu_hz())), ) .unwrap() .cartridge; assert!(["2K", "4K"].contains(&plain.name())); let banked = build(&[0u8; 0x2000], "F8", false, clocked(Some(PAL_7800.timing.cpu_hz()))).unwrap(); assert!(banked.name().starts_with("F8")); }