// SPDX-License-Identifier: MIT //! The palettes (the reference's tests/test_frontend.py; the window itself is //! judged headless by scripts/ladder.sh), the quick die traces //! (tests/test_die_traces.py), the public surface (tests/test_api.py, as a //! compile-time check), and this board's own save state. mod common; use a2600::machine::MACHINES; use a2600::palettes::{palette, palettes}; use a2600::state::{load_state, save_state}; use a2600::System; use common::{cart, image_4k, reference_root}; // VSYNC for three lines, COLUBK $5A, 255 more lines, again. pub const FRAME_LOOP: [u8; 28] = [ 0xa9, 0x02, 0x85, 0x00, 0x85, 0x02, 0x85, 0x02, 0x85, 0x02, 0xa9, 0x00, 0x85, 0x00, 0xa9, 0x5a, 0x85, 0x09, 0xa2, 0xff, 0x85, 0x02, 0xca, 0xd0, 0xfb, 0x4c, 0x00, 0xf0, ]; #[test] fn every_palette_has_128_colours_and_says_where_it_comes_from() { for p in palettes() { assert_eq!(p.colours.len(), 128); assert!(!p.origin.is_empty()); } for machine in MACHINES { assert!(palettes().iter().any(|p| p.name == machine.palette)); } } #[test] fn palette_structure_follows_nocash() { let (ntsc, pal, secam) = (palette("ntsc"), palette("pal"), palette("secam")); let (r, g, b) = ntsc.rgb(0x0E); assert!(r == g && g == b); // hue 0 is grey // PAL colours 1, 14 and 15 are "same as color 0". for hue in [1u8, 14, 15] { assert_eq!(pal.rgb(hue << 4 | 0x06), pal.rgb(0x06)); } // SECAM ignores the hue: luminance 1 is blue, 2 red, 7 white. assert!(secam.rgb(0x32) == (0, 0, 255) && secam.rgb(0xF4) == (255, 0, 0)); assert_eq!(secam.rgb(0x0E), (255, 255, 255)); } #[test] fn quick_traces_match_the_die() { let traces = reference_root().join("reference/src/tia-fpga/sim/traces/phi2"); for name in ["vertical.trace", "vdel.trace", "hmove.trace", "audio.trace"] { let path = traces.join(name); if !path.exists() { return; // die traces not fetched } let records = a2600::gates::load_trace(&path).unwrap(); let (scored, mismatches, _, _) = a2600::gates::replay_trace(&records, 2000, true, 0, None, &mut a2600::tia::TIA::default).unwrap(); assert!(scored > 20000); assert_eq!(mismatches.values().sum::(), 0, "{name}: {mismatches:?}"); } } /// Every name the reference's docs/api.md lists, in this crate's spelling /// (docs/api.md here): it compiles, so it exists. #[test] fn every_listed_name_exists() { use a2600::{ audio, board, cartridge, controllers, gates, machine, palettes, riot, savekey, system, television, tia, }; let _ = ( machine::Machine::new, machine::Timing::cpu_hz, machine::PowerOn::default, machine::TIARevision::new, ); let _ = ( machine::REFERENCE_TIA, machine::TIA_REVISIONS, machine::MACHINES, machine::HOSTED_MACHINES, ); let _ = (machine::VCS_NTSC, machine::VCS_PAL, machine::VCS_SECAM); let _ = (board::Board::new, board::Board::read_byte, board::Board::write_byte); let _ = ( system::System::new, system::System::with_board, system::System::from_rom, ); let _ = ( system::System::step, system::System::run_cycles, system::System::run_frames, ); let _ = |s: &mut system::System| s.run_until(|_| true, 0); let _: Option = None; let _ = ( tia::TIA::new, tia::TIA::step, tia::TIA::write, tia::TIA::read, tia::TIA::run, tia::TIA::hclock, ); let _ = ( audio::Audio::take_samples, audio::mix, riot::RIOT::new, riot::RIOT::read, riot::RIOT::write, ); let _ = (riot::RIOT::set_input_a, riot::RIOT::timer_at); let _ = ( controllers::Joystick::new, controllers::Paddles::new, controllers::Keypads::new, ); let _ = (controllers::DrivingController::new, controllers::BoosterGrip::new); let _ = ( savekey::SaveKey::new, savekey::EEPROM24LC256::new, savekey::default_path, ); let _ = ( cartridge::load, cartridge::identify, cartridge::build, cartridge::read_image, ); let _: Option<( cartridge::Loaded, cartridge::UnsupportedCartridge, Box, )> = None; let _ = ( television::Television::new, television::frame_rgb, television::frame_png, ); let _ = (palettes::palettes, palettes::palette); let _ = ( gates::die_traces, gates::replay_trace, gates::load_trace, gates::timer_roms, gates::serial_tables, ); let _ = (gates::frame_hashes, gates::run_hashes, gates::lockstep); // The board's fields the API names. let b = common::blank_board(); let _ = ( b.cycles, b.halted_cycles, b.data_bus, b.bus_observers.len(), &b.tia, &b.riot, b.cartridge.name(), ); let t = &b.tia; let _ = ( t.rdy_low, t.inputs, t.fire, t.pad_source.is_none(), &t.pixels, t.on_line.is_none(), ); let _ = (t.on_vsync.is_none(), t.frame, &t.audio, t.revision); let _ = t.audio.on_sample.is_none(); let r = b.riot.borrow(); let _ = (r.input_b, r.port_a_listener.is_none(), &r.ram); // The port registers a host composes, listed read only (28fb095). let _ = (r.input_a, r.out_a, r.ddr_a, r.out_b, r.ddr_b); let _ = (system::System::with_cpu, system::System::power, cartridge::CLOCKED); let _ = (gates::smoke_names, gates::SMOKE_LIST); } /// The chips keep their boundaries: tia uses only audio and the machine's /// revision record; audio and riot use nothing of the board. #[test] fn the_chips_import_nothing_from_the_board() { let src = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src"); let crate_uses = |file: &str| -> Vec { std::fs::read_to_string(src.join(file)) .unwrap() .lines() .filter(|l| l.starts_with("use crate::")) .map(String::from) .collect() }; assert!(crate_uses("riot.rs").is_empty()); assert!(crate_uses("audio.rs").is_empty()); assert_eq!( crate_uses("tia.rs"), [ "use crate::audio::Audio;", "use crate::machine::{TIARevision, REFERENCE_TIA};" ] ); } /// Save half way, load into a fresh console, go on: the same bus cycles and /// lines as the run that never stopped (no ROM needed: a frame loop). #[test] fn a_state_loads_into_a_fresh_console_and_continues_identically() { use std::cell::RefCell; use std::rc::Rc; let image = image_4k(&FRAME_LOOP); let run = |save_at: Option| -> Vec<(char, u16, u8, u64)> { let log = Rc::new(RefCell::new(Vec::new())); let attach = |system: &mut System| { let sink = log.clone(); system .board_mut() .bus_observers .push(Box::new(move |k, a, v, c| sink.borrow_mut().push((k, a, v, c)))); }; let mut system = System::with_cartridge(cart(&image, "4K")); attach(&mut system); for frame in 0..6 { if save_at == Some(frame) { let state = save_state(&system); let mut fresh = System::with_cartridge(cart(&image, "4K")); attach(&mut fresh); load_state(&mut fresh, &state).unwrap(); system = fresh; } system.run_frames(1); } let out = log.borrow().clone(); out }; let straight = run(None); assert!(straight.len() > 2_000, "{}", straight.len()); assert_eq!(run(Some(3)), straight); }