// SPDX-License-Identifier: MIT //! Rung 6: this core's multiplies judged by zaydlang's Booth-array model (the reference's //! rung 4, `tests/multiply_judge.py`, transcribed). //! //! The model was fuzzed against a GBA, so it is hardware-corrected for results and the carry //! flag (the reference's U11). The cases and the model's answers come from //! `scripts/multiply_cases.py` (the reference's generator, the model's own C code); each case //! runs the real instruction through this core, MUL, MLA, UMULL, UMLAL, SMULL, SMLAL with S //! set and Thumb's MUL, and compares the result registers, C, and `m`: the core's internal //! cycles less the fixed extra ones (MLA 1, long 1, long accumulate 2) against the model's //! iteration count. //! //! ```text //! arm7-multiply CASES_DIR //! ``` use std::path::PathBuf; use arm7tdmi::{Bus, CPUState, ARM7TDMI}; const FLAG_C: u32 = 1 << 29; /// (form, opcode with Rd/RdHi = r4, RdLo/Rn = r3, Rs = r2, Rm = r1, S set; extra I cycles; model) const FORMS: [(&str, u32, u32, &str); 7] = [ ("MUL", 0xE0140291, 0, "mul"), ("MLA", 0xE0343291, 1, "mul"), ("UMULL", 0xE0943291, 1, "umull"), ("UMLAL", 0xE0B43291, 2, "umull"), ("SMULL", 0xE0D43291, 1, "smull"), ("SMLAL", 0xE0F43291, 2, "smull"), ("THUMB MUL", 0x4348, 0, "mul"), // MUL r0, r1: r0 = r1 x r0 ]; #[derive(Default)] struct Idles { idles: u32, } impl Bus for Idles { fn read(&mut self, _: u32, _: u32, _: u32) -> u32 { 0 } fn write(&mut self, _: u32, _: u32, _: u32, _: u32) {} fn idle(&mut self, _: u32, _: bool) { self.idles += 1; } } fn main() { let dir = PathBuf::from( std::env::args() .nth(1) .expect("usage: arm7-multiply CASES_DIR"), ); let mut cpu = ARM7TDMI::new(Idles::default()); let mut failed_total = 0u64; for (form, op, extra, kind) in FORMS { let thumb = form == "THUMB MUL"; let accumulate = matches!(form, "MLA" | "UMLAL" | "SMLAL"); let (mut cases, mut failures) = (0u64, 0u64); for part in 0..4 { let path = dir.join(format!("{}-{part}.bin", form.replace(' ', "_"))); let data = std::fs::read(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display())); for record in data.chunks_exact(26) { let word = |at: usize| u32::from_le_bytes(record[at..at + 4].try_into().unwrap()); let (rm, rs, lo, hi) = (word(0), word(4), word(8), word(12)); let want = u64::from_le_bytes(record[16..24].try_into().unwrap()); let (carry, m) = (record[24] as u32, record[25] as u32); let mut registers = [0u32; 16]; if thumb { registers[1] = rm; // MUL r0, r1: Rs-as-multiplier is Rd registers[0] = rs; registers[15] = 0x0800_0004; } else { registers[1] = rm; registers[2] = rs; registers[3] = lo; registers[4] = hi; registers[15] = 0x0800_0008; } cpu.restore_state(&CPUState { registers, cpsr: 0x1F | if thumb { 0x20 } else { 0 }, pipeline: [op, 0], ..CPUState::default() }); cpu.bus.idles = 0; cpu.step(); let got = if kind == "mul" { (if thumb { cpu.r[0] } else { cpu.r[4] }) as u64 } else { (cpu.r[4] as u64) << 32 | cpu.r[3] as u64 }; let got_c = (cpu.cpsr & FLAG_C != 0) as u32; let got_m = cpu.bus.idles.wrapping_sub(extra); cases += 1; if (got, got_c, got_m) != (want, carry, m) { failures += 1; if failures <= 5 { println!( " {form} rm={rm:08x} rs={rs:08x} acc={hi:08x}:{lo:08x} (accumulate {accumulate}): core {got:x} C={got_c} m={got_m}; model {want:x} C={carry} m={m}" ); } } } } println!( "{form:10} {}/{} agree", arm7tdmi::harness::thousands(cases - failures), arm7tdmi::harness::thousands(cases) ); failed_total += failures; } std::process::exit(if failed_total == 0 { 0 } else { 1 }); }