// SPDX-License-Identifier: MIT // Relies on the Acid800 suite's label names and verdict codes (MIT, Copyright (C) // 2010-2022 Avery Lee); its notice is in NOTICE. No suite file is committed. //! Acid5200 harness: run one standalone test ROM headless, read its verdict. //! //! Acid800 1.2 (MIT, Avery Lee) builds each 5200 test as an 8K cartridge at //! $A000 (`src/Acid5200/source/sacartridge.s`). The cartridge boots through //! the BIOS logo, runs the test, and ends in `_jam` (an endless `JMP *`) //! with Y = $00 pass, $80 fail, $40 skipped (`src/Acid800/source/library.s`, //! `_testPassed`, `_testFailed`, `_testSkipped`). Its text goes to a //! 40 x 24 screen whose row addresses are at $0380 (low) and $0398 (high) //! (`display5200.s`). The harness takes `_jam`'s address from the ROM's //! label file and reads the verdict and the screen from memory. //! //! Tier 3: the suite's author states the tests pass on real hardware; no //! recorded 5200 run exists (U7). The harness's addresses and verdict codes are //! DOC (the suite's MIT sources, files named above); its verdicts are TEST ROM. //! //! A transcription of a5200-python's `acid.py` (at cf82d44); its `main` is the //! `a5200-acid` binary. Rust-forced differences: the suite's directory has no default inside //! this repository (the ROMs are the reference's fetched copies): `default_dir()` reads //! `$ACID5200_DIR`, else the reference checkout beside this one; `console_hook` is an //! `FnMut(&mut Console)`; errors are `Err(String)`. use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::time::Instant; use crate::board::Board; use crate::machine::A5200; use crate::system::Console; /// Where the Acid5200 ROMs are: `$ACID5200_DIR`, else a5200-python's fetched copies. pub fn default_dir() -> PathBuf { if let Some(dir) = std::env::var_os("ACID5200_DIR").filter(|v| !v.is_empty()) { return PathBuf::from(dir); } PathBuf::from(env!("CARGO_MANIFEST_DIR")) .join("../a5200-python/tests/test_roms/acid800/out/Release/Acid5200") } pub fn verdict_name(y: u8) -> String { match y { 0x00 => "pass".to_string(), 0x80 => "fail".to_string(), 0x40 => "skipped".to_string(), _ => format!("y={y:02x}"), } } #[derive(Clone, Debug)] pub struct AcidResult { pub name: String, pub verdict: String, // pass | fail | skipped | timeout | jam pub text: String, pub frames: u64, pub cycles: u64, pub seconds: f64, } impl std::fmt::Display for AcidResult { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let lines: Vec<&str> = self .text .lines() .filter(|ln| !ln.trim().is_empty()) .collect(); let tail = lines[lines.len().saturating_sub(3)..].join(" | "); write!( f, "{}: {} ({} frames) {}", self.name, self.verdict, self.frames, tail ) } } pub fn labels(path: &Path) -> Result, String> { let data = std::fs::read(path).map_err(|e| format!("{}: {e}", path.display()))?; let text: String = data.iter().map(|&b| b as char).collect(); // latin-1 let mut table = HashMap::new(); for line in text.lines() { let parts: Vec<&str> = line.split_whitespace().collect(); if parts.len() >= 3 { if let Ok(value) = u32::from_str_radix(parts[1], 16) { table.insert(parts[2].to_string(), value); } } } Ok(table) } pub fn screen_text(board: &Board) -> String { let ram = &board.ram; let mut rows = Vec::new(); for row in 0..24 { let address = ram[0x380 + row] as u16 | (ram[0x398 + row] as u16) << 8; let mut chars = String::new(); for col in 0..40u16 { let mut code = board.peek(address.wrapping_add(col)) & 0x7F; if code < 0x40 { code += 0x20; } else if code < 0x60 { code -= 0x40; } chars.push(if (32..127).contains(&code) { code as char } else { '.' }); } rows.push(chars.trim_end().to_string()); } rows.join("\n") } pub fn run_test( name: &str, directory: &Path, max_frames: u64, console_hook: Option<&mut dyn FnMut(&mut Console)>, ) -> Result { let rom = directory.join("standalone").join(format!("{name}.rom")); let jam = labels(&rom.with_extension("lab"))? .get("_jam") .copied() .ok_or("no _jam label")? as u16; let mut console = Console::open(&rom, A5200, Some("8k"))?; if let Some(hook) = console_hook { hook(&mut console); } let limit = console.machine.cycles_per_frame() * max_frames; let start = Instant::now(); let cpu = &mut console.cpu; let mut verdict = "timeout".to_string(); while cpu.bus.cycles < limit { cpu.step(); if cpu.pc == jam { verdict = verdict_name(cpu.y); break; } if cpu.halted() { verdict = "jam".to_string(); break; } } // Let the screen settle for the text (the result was printed before _jam). let board = console.board(); Ok(AcidResult { name: name.to_string(), verdict, text: screen_text(board), frames: board.frames, cycles: board.cycles, seconds: start.elapsed().as_secs_f64(), }) } /// Run the whole suite (`acid5200.rom`, 32K) until its summary line; (screen, frames). pub fn run_full(directory: &Path, max_frames: u64) -> Result<(String, u64), String> { let mut console = Console::open(&directory.join("acid5200.rom"), A5200, None)?; let mut text = String::new(); while console.frames() < max_frames { console.run_frames(30); text = screen_text(console.board()); if text.contains("All tests complete") { break; } } Ok((text, console.frames())) } pub fn all_tests(directory: &Path) -> Vec { let mut names: Vec = std::fs::read_dir(directory.join("standalone")) .map(|entries| { entries .filter_map(|e| e.ok()) .map(|e| e.path()) .filter(|p| p.extension().is_some_and(|x| x == "rom")) .filter_map(|p| p.file_stem().map(|s| s.to_string_lossy().into_owned())) .collect() }) .unwrap_or_default(); names.sort(); names }