alu/
alu.rs

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