# SPDX-License-Identifier: MIT """Rung 1: the sixteen ALU operations and both flag sets. Expected values from the DS (manufacturer): the operations DS p.19, the flag table DS p.25 ("EFFECT OF ALU OPERATION ON FLAGS"), the flag definitions DS (14). Where the DS is silent or the sources disagree the test names the entry: U12, U14, C1, C2. """ from __future__ import annotations import random import pytest from words import core, op from upd7725_python import ( FLAG_C, FLAG_OV0, FLAG_OV1, FLAG_S0, FLAG_S1, FLAG_Z, S1_OV1_FULLSNES, UPD7725, ) NOP, OR, AND, XOR, SUB, ADD, SBB, ADC, DEC, INC, CMP, SHR1, SHL1, SHL2, SHL4, XCHG = range(16) P_IDB = 1 SRC_TR = 3 def run_alu(operation, q, p, *, asl=0, other_carry=0, flags=0, cls=UPD7725): """Run ``operation`` on AccA (asl 0) or AccB with P on the bus from TR.""" other = FLAG_C if other_carry else 0 registers = {"tr": p} if asl: registers.update(b=q, flagb=flags, flaga=other) else: registers.update(a=q, flaga=flags, flagb=other) dsp = core(op(alu=operation, p=P_IDB, asl=asl, src=SRC_TR), cls=cls, **registers) dsp.step() return (dsp.b, dsp.flagb, dsp.flaga) if asl else (dsp.a, dsp.flaga, dsp.flagb) # DS p.19 results, with both accumulators (ASL, DS p.19). CASES = [ (OR, 0x1234, 0x00F0, 0x12F4), (AND, 0x1234, 0x00F0, 0x0030), (XOR, 0x1234, 0xFFFF, 0xEDCB), (SUB, 0x0005, 0x0007, 0xFFFE), (ADD, 0x7000, 0x1000, 0x8000), (DEC, 0x0000, 0x5555, 0xFFFF), # DEC ignores P (INC, 0xFFFF, 0x5555, 0x0000), (CMP, 0x1234, 0x5555, 0xEDCB), # one's complement, P ignored (SHR1, 0x8003, 0, 0xC001), # arithmetic (SHL2, 0x4001, 0, 0x0007), # ones shifted in (SHL4, 0x1234, 0, 0x234F), (XCHG, 0x1234, 0, 0x3412), ] @pytest.mark.parametrize("asl", [0, 1]) @pytest.mark.parametrize(("operation", "q", "p", "expected"), CASES) def test_results(operation, q, p, expected, asl): result, _, _ = run_alu(operation, q, p, asl=asl) assert result == expected @pytest.mark.parametrize("asl", [0, 1]) def test_other_accumulators_carry(asl): """U14: SBB, ADC and SHL1 read the carry of the *other* accumulator (DS p.25 "Nonselected FLAG ... C: Affects the result").""" assert run_alu(ADC, 0x0001, 0x0001, asl=asl, other_carry=1)[0] == 0x0003 assert run_alu(ADC, 0x0001, 0x0001, asl=asl, other_carry=0)[0] == 0x0002 assert run_alu(SBB, 0x0005, 0x0001, asl=asl, other_carry=1)[0] == 0x0003 assert run_alu(SBB, 0x0005, 0x0001, asl=asl, other_carry=0)[0] == 0x0004 assert run_alu(SHL1, 0x4000, 0, asl=asl, other_carry=1)[0] == 0x8001 assert run_alu(SHL1, 0x4000, 0, asl=asl, other_carry=0)[0] == 0x8000 # The selected accumulator's own carry does not feed in. assert run_alu(ADC, 1, 1, asl=asl, flags=FLAG_C)[0] == 2 # SUB and ADD ignore the other carry. assert run_alu(ADD, 1, 1, asl=asl, other_carry=1)[0] == 2 assert run_alu(SUB, 5, 1, asl=asl, other_carry=1)[0] == 4 @pytest.mark.parametrize("operation", [OR, AND, XOR, CMP, SHL2, SHL4, XCHG]) def test_logic_flags(operation): """DS p.25: C, OV1, OV0 cleared; S0 and Z from the result.""" for q in (0x0000, 0x8000, 0xFFFF, 0x00FF): result, flags, _ = run_alu(operation, q, 0x00FF, flags=FLAG_C | FLAG_OV0 | FLAG_OV1) assert not flags & (FLAG_C | FLAG_OV0 | FLAG_OV1) assert bool(flags & FLAG_S0) == bool(result & 0x8000) assert bool(flags & FLAG_Z) == (result == 0) def test_shift_carries(): """DS p.25: SHR1 and SHL1 set C to the bit shifted out; OV0/OV1 cleared.""" assert run_alu(SHR1, 0x0001, 0)[1] & FLAG_C assert not run_alu(SHR1, 0x0002, 0)[1] & FLAG_C assert run_alu(SHL1, 0x8000, 0)[1] & FLAG_C assert run_alu(SHL1, 0x8000, 0)[1] & FLAG_Z assert not run_alu(SHL1, 0x4000, 0)[1] & FLAG_C def test_arithmetic_flags(): """DS (14)(a)-(d): carry/borrow, zero, sign, overflow.""" _, flags, _ = run_alu(ADD, 0xFFFF, 0x0001) assert flags & FLAG_C and flags & FLAG_Z and not flags & FLAG_OV0 _, flags, _ = run_alu(ADD, 0x7FFF, 0x0001) assert flags & FLAG_OV0 and flags & FLAG_S0 and not flags & FLAG_C _, flags, _ = run_alu(SUB, 0x0000, 0x0001) assert flags & FLAG_C and flags & FLAG_S0 and not flags & FLAG_OV0 # borrow _, flags, _ = run_alu(SUB, 0x8000, 0x0001) assert flags & FLAG_OV0 and not flags & FLAG_C _, flags, _ = run_alu(DEC, 0x8000, 0) assert flags & FLAG_OV0 _, flags, _ = run_alu(INC, 0x7FFF, 0) assert flags & FLAG_OV0 def test_overflow_matches_the_sign_formula(): """OV0 equals the textbook sign test on (q, p, r) for every arithmetic op, with and without the other carry (the formula every emulator uses).""" rng = random.Random(7725) values = [0, 1, 0x7FFF, 0x8000, 0x8001, 0xFFFF] + [rng.randrange(0x10000) for _ in range(40)] for operation in (SUB, ADD, SBB, ADC): for q in values: for p in values: for carry in (0, 1): r, flags, _ = run_alu(operation, q, p, other_carry=carry) if operation in (ADD, ADC): formula = (q ^ r) & ~(q ^ p) & 0x8000 else: formula = (q ^ r) & (q ^ p) & 0x8000 assert bool(flags & FLAG_OV0) == bool(formula), (operation, q, p, carry) class Comparison(UPD7725): CARRY_BY_COMPARISON = True def test_c1_carry_chain_default(): """C1: with P = FFFFh and the other carry 1, ADC's true carry out is 1 (DS (14)(a)); the MAME comparison gives 0. SBB likewise borrows.""" _, flags, _ = run_alu(ADC, 0x1234, 0xFFFF, other_carry=1) assert flags & FLAG_C _, flags, _ = run_alu(SBB, 0x1234, 0xFFFF, other_carry=1) assert flags & FLAG_C _, flags, _ = run_alu(ADC, 0x1234, 0xFFFF, other_carry=1, cls=Comparison) assert not flags & FLAG_C _, flags, _ = run_alu(SBB, 0x1234, 0xFFFF, other_carry=1, cls=Comparison) assert not flags & FLAG_C def test_c1_both_rules_agree_elsewhere(): rng = random.Random(1) for _ in range(2000): operation = rng.choice((SUB, ADD, SBB, ADC, DEC, INC)) q, p, carry = rng.randrange(0x10000), rng.randrange(0xFFFF), rng.randrange(2) assert run_alu(operation, q, p, other_carry=carry) == run_alu( operation, q, p, other_carry=carry, cls=Comparison ) def test_nop_keeps_both_flag_sets(): """U12; DS p.25: NOP "Retains the previous state".""" result, flags, other = run_alu(NOP, 0x1234, 0x5678, flags=0x2A, other_carry=1) assert (result, flags, other) == (0x1234, 0x2A, FLAG_C) def test_only_selected_flags_change(): """DS p.25: the nonselected FLAG retains its state (only C feeds SBB/ADC/SHL1).""" dsp = core(op(alu=ADD, p=P_IDB, asl=1, src=SRC_TR), tr=0x7FFF, b=1, flaga=0x3F) dsp.step() assert dsp.flaga == 0x3F class FullSnes(UPD7725): S1_OV1_RULE = S1_OV1_FULLSNES def test_c2_emulators_rule(): """C2, the emulators' formula: S1 follows S0 while OV1 = 0; two overflows in the same direction leave OV1 set (fullsnes: "doesn't work if two overflows occur in same direction").""" # No overflow, OV1 = 0: S1 follows S0. _, flags, _ = run_alu(ADD, 0x8000, 0x0001) assert flags & FLAG_S1 and not flags & FLAG_OV1 # Positive overflow: OV1 set, S1 = S0 = 1. _, flags, _ = run_alu(ADD, 0x7FFF, 0x0001) assert flags & FLAG_OV1 and flags & FLAG_S1 # Second overflow in the same direction: OV1 = (S0 == S1) = 1. _, flags2, _ = run_alu(ADD, 0x7FFF, 0x0001, flags=flags) assert flags2 & FLAG_OV1 # Overflow back in the other direction (S0 = 0, S1 = 1): OV1 = 0. _, flags3, _ = run_alu(ADD, 0x8000, 0x8000, flags=flags) assert not flags3 & FLAG_OV1 and flags3 & FLAG_S1 # No overflow while OV1 = 1: S1 kept, OV1 kept. _, flags4, _ = run_alu(ADD, 0x0001, 0x0001, flags=FLAG_OV1 | FLAG_S1) assert flags4 & FLAG_OV1 and flags4 & FLAG_S1 # A logic op clears OV1; with OV1 = 1 before it, S1 is kept. _, flags5, _ = run_alu(OR, 0x0001, 0, flags=FLAG_OV1 | FLAG_S1) assert not flags5 & FLAG_OV1 and flags5 & FLAG_S1 def test_c2_fullsnes_rule(): """C2, fullsnes: an overflow sets S1 = S0 and toggles OV1; otherwise both stay; logic ops set S1 = S0 and clear OV1.""" _, flags, _ = run_alu(ADD, 0x8000, 0x0001, cls=FullSnes) assert not flags & FLAG_S1 # no overflow: S1 unchanged (was 0) _, flags, _ = run_alu(ADD, 0x7FFF, 0x0001, cls=FullSnes) assert flags & FLAG_OV1 and flags & FLAG_S1 _, flags2, _ = run_alu(ADD, 0x7FFF, 0x0001, flags=flags, cls=FullSnes) assert not flags2 & FLAG_OV1 # toggled back: two overflows _, flags3, _ = run_alu(OR, 0x0001, 0, flags=FLAG_OV1 | FLAG_S1, cls=FullSnes) assert not flags3 & FLAG_OV1 and not flags3 & FLAG_S1