// SPDX-License-Identifier: MIT //! Decimal-mode ADC and SBC: the worked examples of Bruce Clark's *Decimal Mode* //! (the reference's `tests/test_decimal.py`, transcribed). //! //! DOC: 6502.org's tutorial, sections 3.2.2 and 4 and appendix C, each stated //! there for the 65816. The volume check (valid and invalid BCD, 8 and 16 bits) //! is SingleStepTests' (rung 1). use w65c816::{add, subtract, C, D, N, V, Z}; const FLAGS: u8 = N | V | Z | C; type Operation = fn(u16, u16, u8, bool) -> (u16, u8); #[test] fn clark_examples() { let cases: [(Operation, u16, u16, u8, bool, u16, u8); 9] = [ (add, 0x99, 0x01, 0, false, 0x00, Z | C), // 3.2.2: Z valid on the 65816 (add, 0x90, 0x90, 0, false, 0x80, N | V | C), // 3.2.2: V set as in binary (add, 0x12, 0x34, 1, false, 0x47, 0), (subtract, 0x01, 0x01, 1, false, 0x00, Z | C), // 3.2.2 (subtract, 0x90, 0x0F, 1, false, 0x8B, N | C), // 4: invalid BCD, $8B on the 65816 (subtract, 0x00, 0x01, 1, false, 0x99, N), // a borrow out: C clear (add, 0x9999, 0x0001, 0, true, 0x0000, Z | C), // appendix C: four digits chained (add, 0x0950, 0x0050, 0, true, 0x1000, 0), (subtract, 0x1000, 0x0001, 1, true, 0x0999, C), ]; for (operation, a, value, carry, wide, result, flags) in cases { let (got, p) = operation(a, value, D | carry, wide); assert_eq!( (got, p & FLAGS), (result, flags), "{a:#06x} {value:#06x} C={carry}" ); assert_ne!(p & D, 0); } }