dormann/
dormann.rs

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