dormann/
dormann.rs

1// SPDX-License-Identifier: MIT
2//! Rung 3: Klaus Dormann's 65C02 extended opcodes test, relocated, natively.
3//!
4//!     dormann TEST.bin START SUCCESS [--limit N]
5//!
6//! `TEST.bin` is the 64 KB image `scripts/reference_dormann.py` builds as the
7//! reference's `scripts/run_dormann.py` builds it; `START` and `SUCCESS` are
8//! its labels `start` and `success_address` (hex). The program is
9//! self-checking: it traps (a jump or branch to itself) at the first wrong
10//! result and reaches the success address when everything passed.
11//!
12//! The memory is the 65C02's flat 64 KB through the mapper as the reference's
13//! `tests/dormann.py` maps it: MPR0 and MPR1 are one bank, so the absolute
14//! `$00xx`/`$01xx` the program uses and the zero page and stack at
15//! `$20xx`/`$21xx` the core uses are the same bytes; the code is at `$4000`
16//! and up.
17
18use huc6280::{Bus, HuC6280, Pins};
19
20const BANK: [u8; 8] = [0x10, 0x10, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17];
21
22struct Ram(Vec<u8>);
23
24impl Bus for Ram {
25    fn read(&mut self, address: u32, _: &mut Pins) -> u8 {
26        self.0[address as usize]
27    }
28    fn write(&mut self, address: u32, value: u8, _: &mut Pins) {
29        self.0[address as usize] = value;
30    }
31}
32
33fn main() {
34    let args: Vec<String> = std::env::args().skip(1).collect();
35    if args.len() < 3 {
36        eprintln!("usage: dormann TEST.bin START SUCCESS [--limit N]");
37        std::process::exit(2);
38    }
39    let image = std::fs::read(&args[0]).expect("read the image");
40    let start = u16::from_str_radix(&args[1], 16).expect("START");
41    let done = u16::from_str_radix(&args[2], 16).expect("SUCCESS");
42    let limit: u64 = match args.get(3).map(String::as_str) {
43        Some("--limit") => args[4].parse().expect("N"),
44        _ => 100_000_000,
45    };
46    let mut memory = vec![0u8; 0x200000];
47    for page in [0usize, 2, 3, 4, 5, 6, 7] {
48        // page 1 is page 0 again (MPR1 = MPR0)
49        let base = (BANK[page] as usize) << 13;
50        memory[base..base + 0x2000].copy_from_slice(&image[page * 0x2000..(page + 1) * 0x2000]);
51    }
52    let mut cpu = HuC6280::new(Ram(memory));
53    cpu.mpr = BANK;
54    cpu.pc = start;
55    cpu.s = 0xFF;
56    cpu.p = 0x04;
57    let test_case = |cpu: &HuC6280<Ram>| cpu.bus.0[((BANK[0] as usize) << 13) | 0x202];
58    let began = std::time::Instant::now();
59    let mut instructions = 0u64;
60    let mut reason = "limit";
61    while instructions < limit {
62        let before = cpu.pc;
63        cpu.step();
64        instructions += 1;
65        if cpu.pc == before {
66            // a jump or branch to itself: a trap, or success
67            reason = if before == done { "success" } else { "trap" };
68            break;
69        }
70    }
71    println!(
72        "{reason} at ${:04X} after {instructions} instructions (test case {})",
73        cpu.pc,
74        test_case(&cpu)
75    );
76    eprintln!("{:.3} s", began.elapsed().as_secs_f64());
77    std::process::exit(if reason == "success" { 0 } else { 1 });
78}