alu/
alu.rs

1// SPDX-License-Identifier: MIT
2//! Rung 1, the arithmetic tables: ADC's and SBC's result and flags for every
3//! pair of operands, binary and decimal, under both decimal formulas, as one
4//! digest a line. `scripts/reference_alu.py` prints the same from the
5//! reference's `_alu.add` and `_alu.subtract`; the two outputs are compared.
6//!
7//!     alu
8
9use huc6280::{add, subtract, DecimalFormula};
10
11/// P values that cover C and D both ways, with the other flags clear and set.
12const P_VALUES: [u8; 8] = [0x00, 0x01, 0x08, 0x09, 0xC2, 0xC3, 0xEA, 0xEB];
13
14fn main() {
15    type Operation = fn(u8, u8, u8, DecimalFormula) -> (u8, u8);
16    let operations: [(&str, Operation); 2] = [("add", add), ("subtract", subtract)];
17    for (name, operation) in operations {
18        for (formula_name, formula) in [
19            ("mednafen", DecimalFormula::Mednafen),
20            ("mesen", DecimalFormula::Mesen),
21        ] {
22            for p in P_VALUES {
23                // FNV-1a, 64 bits, over (result, P) for every A and operand.
24                let mut digest: u64 = 0xCBF29CE484222325;
25                for a in 0..=255u8 {
26                    for operand in 0..=255u8 {
27                        let (result, flags) = operation(a, operand, p, formula);
28                        for byte in [result, flags] {
29                            digest = (digest ^ byte as u64).wrapping_mul(0x100000001B3);
30                        }
31                    }
32                }
33                println!("{name} {formula_name} p={p:02X} {digest:016x}");
34            }
35        }
36    }
37}