// SPDX-License-Identifier: MIT //! Run a program on the core in the minimal harness: the reference's `python -m arm7tdmi_python` //! (`arm7tdmi_python/__main__.py`) without its debugger. //! //! ```text //! arm7tdmi jsmolka-gba-tests/arm/arm.gba //! arm7tdmi rom.gba --max-cycles 1000000 --trace run.jsonl //! arm7tdmi multiboot.bin --at 0x02000000 //! ``` //! //! The host is not a GBA: memory only, with DISPSTAT's VBlank bit toggling and no BIOS //! (`arm7tdmi::harness`). A raw image loads at `--at` (the cartridge, `0x08000000`, by default) //! and starts there; an ELF file loads its segments and starts at its entry point (Thumb if bit //! 0 is set). The program runs until it branches to itself and R12 is printed: jsmolka's and //! alyosha-tas's tests leave 0 when every test passed, so the exit status is 0 then and 1 //! otherwise. `--trace` writes every bus cycle as JSON Lines (`arm7tdmi::trace`), byte for byte //! the reference's. Output and exit status are the reference's, so the two can be compared. use std::cell::RefCell; use std::fs::File; use std::io::BufWriter; use std::path::PathBuf; use std::process::ExitCode; use std::rc::Rc; use arm7tdmi::harness::{elf_image, thousands, Harness, Observer, RunError}; use arm7tdmi::trace::TraceWriter; fn parse_number(text: &str) -> Option { let text = text.trim(); if let Some(hex) = text .strip_prefix("0x") .or_else(|| text.strip_prefix("0X")) .or_else(|| text.strip_prefix('$')) { return u32::from_str_radix(&hex.replace('_', ""), 16).ok(); } text.replace('_', "").parse().ok() } fn usage() -> ExitCode { eprintln!("usage: arm7tdmi ROM [--at ADDRESS] [--thumb] [--max-cycles N] [--trace FILE]"); ExitCode::from(2) } fn main() -> ExitCode { let mut args = std::env::args().skip(1); let mut rom: Option = None; let mut at = 0x0800_0000u32; let mut thumb = false; let mut max_cycles = 50_000_000u64; let mut trace: Option = None; while let Some(arg) = args.next() { match arg.as_str() { "--at" => match args.next().as_deref().and_then(parse_number) { Some(value) => at = value, None => return usage(), }, "--thumb" => thumb = true, "--max-cycles" => match args.next().and_then(|v| v.parse().ok()) { Some(value) => max_cycles = value, None => return usage(), }, "--trace" => trace = args.next().map(PathBuf::from), _ if rom.is_none() && !arg.starts_with("--") => rom = Some(PathBuf::from(arg)), _ => return usage(), } } let Some(rom) = rom else { return usage() }; let name = rom .file_name() .map(|n| n.to_string_lossy().into_owned()) .unwrap_or_default(); let data = match std::fs::read(&rom) { Ok(data) => data, Err(error) => { eprintln!("{}: {error}", rom.display()); return ExitCode::from(2); } }; let writer = trace.map(|path| { Rc::new(RefCell::new(TraceWriter::new(BufWriter::new( File::create(path).expect("cannot create the trace file"), )))) }); let observer = || -> Option { writer.as_ref().map(|w| { let w = Rc::clone(w); Box::new(move |kind, address, width, access, value| { w.borrow_mut().record(kind, address, width, access, value) }) as Observer }) }; // The harness with the program loaded and started; `cycles` and the trace start with the // program's own two refill fetches. let elf = data.len() >= 4 && &data[..4] == b"\x7fELF"; let mut harness; let mut start_error = None; if !elf && at == 0x0800_0000 && !thumb { harness = Harness::new(&data, observer()); // the cartridge, as the BIOS would leave it } else { harness = Harness::new(b"", None); if elf { match elf_image(&data) { Ok((segments, entry)) => { for (address, chunk) in segments { if let Err(error) = harness.load(address, &chunk) { eprintln!("{error}"); return ExitCode::from(2); } } at = entry & !1; thumb = entry & 1 != 0; } Err(error) => { eprintln!("{error}"); return ExitCode::from(2); } } } else if let Err(error) = harness.load(at, &data) { eprintln!("{error}"); return ExitCode::from(2); } harness.cpu.bus.observer = observer(); harness.cpu.bus.cycles = 0; if let Err(message) = harness.start(at, thumb) { start_error = Some(message); } } let result = match start_error { Some(message) => Err(RunError::EnteredBIOS(arm7tdmi::harness::EnteredBIOS { message, r12: harness.cpu.r[12], })), None => harness.run(max_cycles), }; let cycles = harness.cycles(); drop(harness); if let Some(w) = writer { use std::io::Write; w.borrow_mut().stream.flush().expect("trace write failed"); } match result { Ok(r12) => { println!("{name}: r12 = {r12} after {} cycles", thousands(cycles)); if r12 == 0 { ExitCode::SUCCESS } else { ExitCode::from(1) } } Err(RunError::EnteredBIOS(error)) => { eprintln!("{name}: r12 = {}; then {}", error.r12, error.message); ExitCode::from(1) } Err(RunError::Timeout(message)) => { eprintln!("{name}: {message}"); ExitCode::from(2) } } }