# SPDX-License-Identifier: MIT """The ALU: sixteen operations on AccA or AccB and that accumulator's six flags. DS p.19 (the operations), DS (14) and the flag table p.25 (the flags), fullsnes "ALU Opcode". Each accumulator has its own flag register; an operation changes only the flags of the accumulator its ASL bit selects (U12), and SBB, ADC and SHL1 read the **other** accumulator's carry (U14). The flag registers are packed as MAME and ares pack them, S1 S0 C Z OV1 OV0 from bit 5 to bit 0. Three rules here are contested between the sources, and each sits behind a class attribute of :class:`~upd7725_python.cpu.UPD7725` so a comparison can select the other behaviour without editing code: the carry (C1), how S1 and OV1 move (C2), and the ALU-NOP when the move overwrites the same accumulator (C3, applied in ``_op.py``). """ from __future__ import annotations FLAG_OV0 = 0x01 # overflow of this operation FLAG_OV1 = 0x02 # an odd number of overflows in the last operations (C2) FLAG_Z = 0x04 # result = 0 FLAG_C = 0x08 # carry or borrow (C1) FLAG_S0 = 0x10 # bit 15 of the result FLAG_S1 = 0x20 # the direction of the last overflow (C2); SGN reads FLAG A's (U4) # ALU codes (DS p.19). NOP, OR, AND, XOR, SUB, ADD, SBB, ADC, DEC, INC, CMP, SHR1, SHL1, SHL2, SHL4, XCHG = range(16) _ARITHMETIC = frozenset((SUB, ADD, SBB, ADC, DEC, INC)) _ADDITIONS = frozenset((ADD, ADC, INC)) S1_OV1_EMULATORS = "emulators" S1_OV1_FULLSNES = "fullsnes" def _signed(value: int) -> int: return value - ((value & 0x8000) << 1) class ALUMixin: """The ALU half of an OP or RT instruction.""" #: C1. False (default): C is the carry or borrow out of bit 15 of the whole operation, #: carry-in included, the DS's definition ("the carry or borrow resulting from the executed #: operation", DS (14)(a)) as ares computes it (``instructions.cpp`` l.105-109, ISC). True: #: MAME's comparison (``upd7725.cpp`` l.433, l.437, BSD-3-Clause): ``r < q`` for an #: addition, ``r > q`` for a subtraction, which loses the carry when P + carry-in is 10000h. CARRY_BY_COMPARISON = False #: C2. ``"emulators"`` (default): the rule ares and MAME share. Every ALU #: operation first copies S0 into S1 when OV1 was 0; an arithmetic operation then sets #: OV1 = (S0 == S1) if this and the previous operation both overflowed, otherwise #: OV0 | OV1; logic operations and shifts clear OV1. ``"fullsnes"``: an arithmetic #: overflow sets S1 = S0 and toggles OV1 and otherwise leaves both; the other operations #: set S1 = S0 and clear OV1. The DS states only the intent (DS (14)(e)(f)). S1_OV1_RULE = S1_OV1_EMULATORS a: int b: int flaga: int flagb: int def _alu(self, operation: int, select_b: int, p: int) -> None: """Run ``operation`` on the selected accumulator with input ``p``; store result and flags. NOP (code 0) is never passed: it leaves both flag sets alone (U12).""" if select_b: q, flags, other_carry = self.b, self.flagb, self.flaga & FLAG_C else: q, flags, other_carry = self.a, self.flaga, self.flagb & FLAG_C carry_in = 1 if other_carry else 0 carry = 0 overflow = 0 if operation == OR: result = q | p elif operation == AND: result = q & p elif operation == XOR: result = q ^ p elif operation in _ARITHMETIC: if operation in (DEC, INC): p = 1 # DEC and INC are SUB and ADD of 1 (DS p.19): P is ignored if operation not in (SBB, ADC): carry_in = 0 # only SBB and ADC take the other accumulator's carry (U14) if operation in _ADDITIONS: total = q + p + carry_in result = total & 0xFFFF if self.CARRY_BY_COMPARISON: carry = 1 if result < q else 0 # C1: MAME's comparison else: carry = total >> 16 # the carry out of bit 15 exact = _signed(q) + _signed(p) + carry_in else: total = q - p - carry_in result = total & 0xFFFF if self.CARRY_BY_COMPARISON: carry = 1 if result > q else 0 # C1: MAME's comparison else: carry = 1 if total < 0 else 0 # the borrow out of bit 15 exact = _signed(q) - _signed(p) - carry_in # OV0: the signed result does not fit 16 bits (DS (14)(d)). overflow = 0 if -0x8000 <= exact <= 0x7FFF else 1 elif operation == CMP: result = q ^ 0xFFFF # one's complement, not a comparison (DS p.19) elif operation == SHR1: result = q >> 1 | (q & 0x8000) # arithmetic: bit 15 kept (fullsnes "SAR1") carry = q & 1 # C is the bit shifted out (DS p.25: SHR1 affects C) elif operation == SHL1: result = (q << 1 | carry_in) & 0xFFFF # the other accumulator's carry in (U14) carry = q >> 15 elif operation == SHL2: result = (q << 2 | 3) & 0xFFFF # shifts ones in (fullsnes "SLL2: Acc*4 + 3") elif operation == SHL4: result = (q << 4 | 0xF) & 0xFFFF # shifts ones in (fullsnes "SLL4: Acc*16 + 15") else: # XCHG result = (q << 8 | q >> 8) & 0xFFFF # swaps the bytes sign = result & 0x8000 new = (FLAG_S0 if sign else 0) | (FLAG_Z if result == 0 else 0) | (FLAG_C if carry else 0) old_ov1 = flags & FLAG_OV1 old_s1 = flags & FLAG_S1 if self.S1_OV1_RULE == S1_OV1_EMULATORS: s1 = old_s1 if old_ov1 else (FLAG_S1 if sign else 0) if operation in _ARITHMETIC: if overflow and old_ov1: ov1 = 1 if bool(s1) == bool(sign) else 0 else: ov1 = 1 if overflow or old_ov1 else 0 else: ov1 = 0 elif operation in _ARITHMETIC: # fullsnes: only an overflow moves S1 and OV1 if overflow: s1 = FLAG_S1 if sign else 0 ov1 = 0 if old_ov1 else 1 else: s1, ov1 = old_s1, 1 if old_ov1 else 0 else: # fullsnes: logic and shifts set S1 = S0 ("Super Air Diver 2") and clear OV1 s1, ov1 = FLAG_S1 if sign else 0, 0 new |= s1 | (FLAG_OV1 if ov1 else 0) | (FLAG_OV0 if overflow else 0) if select_b: self.b, self.flagb = result, new else: self.a, self.flaga = result, new __all__ = [ "FLAG_C", "FLAG_OV0", "FLAG_OV1", "FLAG_S0", "FLAG_S1", "FLAG_Z", "S1_OV1_EMULATORS", "S1_OV1_FULLSNES", "ALUMixin", ]