// SPDX-License-Identifier: MIT //! Rung 3: Dormann's 65C02 extended-opcodes test (the image built for the row), //! his NMOS functional test and Clark's decimal test, natively on every row, //! run as the reference's `scripts/run_programs.py` runs them (transcribed, //! with its `w65c02_python.harness.Harness` and `tests/programs.py`). //! //! programs DORMANN_BUILT FUNCTIONAL_BIN [--row NAME ...] [--program NAME ...] //! //! Each program is loaded at `$0000`, the CPU is reset (its seven cycles are //! counted) and PC set to `$0400`, and it runs until an instruction leaves PC //! where it was (the program's trap), the CPU halts, or 200 million //! instructions. Every outcome ends in a jump to itself, and the address says //! which. Prints one line per run in the reference's format (so the lines can //! be compared with its rungs 2 and 3), and the speed of each run on stderr. use std::path::{Path, PathBuf}; use std::time::Instant; use w65c02::{Bus, Cmos65c02, Frame, Row, Signals, G65SC02, LYNX2_CELL, R65C02, W65C02S}; /// The functional test's success trap (its listing, label `success`). const FUNCTIONAL_SUCCESS: u16 = 0x3469; /// The decimal test's ERROR byte: 1 while running and on failure, 0 on success. const DECIMAL_ERROR: usize = 0x000B; /// row -> (image, the address of its success trap, from the build's own listing). fn extended(row: &Row) -> (&'static str, u16) { match row.name { "W65C02S" => ("wdc", 0x24F1), "R65C02" => ("rockwell", 0x2569), "G65SC02" => ("sc", 0x2434), "Lynx2Cell" => ("rockwell", 0x2569), other => panic!("no extended image for {other}"), } } /// The reference's flat-memory `Harness`: 64 KB of RAM; `cycles` counts callbacks. struct Harness { memory: Vec, cycles: u64, } impl Bus for Harness { fn read(&mut self, address: u16, _: &mut Frame, _: Signals) -> u8 { self.cycles += 1; self.memory[address as usize] } fn write(&mut self, address: u16, value: u8, _: &mut Frame, _: Signals) { self.cycles += 1; self.memory[address as usize] = value; } } /// Run `image` from $0400 to its trap, counting instructions. fn run(row: Row, image: &[u8]) -> (Cmos65c02, u64) { let mut memory = vec![0u8; 0x10000]; memory[..image.len()].copy_from_slice(image); let mut cpu = Cmos65c02::new(row, Harness { memory, cycles: 0 }); // Harness.start(0x0400): the reset sequence, then PC. cpu.frame.request_reset(); cpu.step(); cpu.pc = 0x0400; let mut instructions = 0u64; loop { let before = cpu.pc; cpu.step(); instructions += 1; if cpu.pc == before || cpu.halted() || instructions > 200_000_000 { return (cpu, instructions); } } } /// 1234567 -> "1,234,567", as Python's `{:,}`. fn commas(value: u64) -> String { let text = value.to_string(); let mut out = String::new(); for (index, digit) in text.chars().enumerate() { if index > 0 && (text.len() - index) % 3 == 0 { out.push(','); } out.push(digit); } out } fn read(path: &Path) -> Vec { std::fs::read(path).unwrap_or_else(|e| panic!("{}: {e}", path.display())) } fn main() { let mut args = std::env::args().skip(1); let mut paths: Vec = Vec::new(); let mut rows: Vec = Vec::new(); let mut wanted: Vec = Vec::new(); while let Some(arg) = args.next() { match arg.as_str() { "--row" => rows.push(args.next().expect("--row NAME").to_lowercase()), "--program" => wanted.push(args.next().expect("--program NAME")), _ => paths.push(PathBuf::from(arg)), } } let [built, functional] = paths.as_slice() else { eprintln!("usage: programs DORMANN_BUILT FUNCTIONAL_BIN [--row NAME] [--program extended|functional|decimal]"); std::process::exit(2); }; if wanted.is_empty() { wanted = vec!["extended".into(), "functional".into(), "decimal".into()]; } let functional_image = read(functional); let mut decimal_image = read(&built.join("65C02_decimal_test.bin")); decimal_image[DECIMAL_ERROR] = 0xA5; // the test must overwrite it: 1 while running let mut failed = false; // The reference prints the rows sorted by their harness names. let mut all = [G65SC02, LYNX2_CELL, R65C02, W65C02S]; all.sort_by_key(|row| row.name.to_lowercase()); for row in all { if !rows.is_empty() && !rows.contains(&row.name.to_lowercase()) { continue; } let (built_as, success) = extended(&row); let extended_image = read(&built.join(format!("65C02_extended_opcodes_test_{built_as}.bin"))); let runs: [(String, &[u8], u8); 3] = [ (format!("65C02 extended ({built_as})"), &extended_image, 0), ("NMOS functional".into(), &functional_image, 1), ("Clark decimal".into(), &decimal_image, 2), ]; for (title, image, which) in runs { if !wanted .iter() .any(|word| title.to_lowercase().contains(word.as_str())) { continue; } let started = Instant::now(); let (cpu, instructions) = run(row, image); let seconds = started.elapsed().as_secs_f64(); let ok = match which { 0 => cpu.pc == success, 1 => cpu.pc == FUNCTIONAL_SUCCESS, _ => cpu.bus.memory[DECIMAL_ERROR] == 0, }; failed |= !ok; println!( "{:<9} {:<25} ended at ${:04X} after {} instructions, {} cycles: {}", row.name, title, cpu.pc, commas(instructions), commas(cpu.bus.cycles), if ok { "success" } else { "FAILED" } ); eprintln!( " {:.2} s: {:.1} million cycles a second", seconds, cpu.bus.cycles as f64 / seconds / 1e6 ); } } std::process::exit(if failed { 1 } else { 0 }); }