"""Arithmetic, logic, compare and bit test: ADC SBC AND ORA EOR CMP CPX CPY BIT. ``ORA``, ``AND``, ``EOR`` and ``ADC`` honour the T flag (U4): with T set the accumulator is replaced by the zero-page byte at X (logical ``$2000 + X``), the result goes back there, A is untouched and the instruction takes three more cycles (read, internal, write). ``SBC`` does not (C5: SW's entry has no T clause; MAME's yes is a guess), unless ``sbc_honours_t`` is set. Decimal mode (U14, HW HB-4, SW ``ADC``): one more cycle, N, Z and C taken from the decimal result, **V left alone**. How invalid digits adjust is C13: neither manual says. ``decimal_formula`` picks the digit adjust. The default, ``"mednafen"``, gives the results Mednafen's HuC6280 gives ("corrected ... per tests on a HuC6280", its ChangeLog, 2015-04-29: the one author statement of hardware testing); ``"mesen"`` gives Mesen's, the results the 640,000-case SingleStepTests corpus was made with. They differ only with an invalid BCD digit (docs/issues/0004). Both are implemented here from the rules stated in words there and checked against recorded result tables (tests/test_mednafen.py); neither emulator's code is copied. """ from huc6280._core import ZERO_PAGE, C, D, N, V, Z, nz FORMULAS = ("mednafen", "mesen") def _decimal_add(a: int, operand: int, carry: int, formula: str) -> int: """The decimal sum before masking: 256 and up is the carry out. Written from the rule each emulator's result table follows (docs/issues/0004 states both in words), digit by digit; no emulator's source is reproduced here. """ ones = a % 16 + operand % 16 + carry sixteens = a // 16 + operand // 16 if ones >= 10: ones += 6 # a digit past 9 skips the six unused codes if formula == "mednafen": # Everything the ones digit overflowed by is carried, however much that is. total = 16 * sixteens + ones elif formula == "mesen": # One carry at most, and only the low four bits of the ones digit are kept. total = 16 * (sixteens + (1 if ones >= 16 else 0)) + ones % 16 else: raise ValueError(f"unknown decimal_formula {formula!r}") if total >= 0xA0: total += 0x60 return total def _decimal_subtract(a: int, operand: int, carry: int, formula: str) -> int: """The decimal difference: 256 and up means no borrow (carry out). Written as above.""" if formula == "mednafen": # Digit differences with a borrow between them; each digit that borrowed is corrected # (by 6, by $60) on the assembled byte, modulo 256. ones = a % 16 - operand % 16 - (1 - carry) sixteens = a // 16 - operand // 16 - (1 if ones < 0 else 0) byte = 16 * (sixteens % 16) + ones % 16 if ones < 0: byte -= 0x06 if sixteens < 0: byte -= 0x60 return byte % 256 + (0 if sixteens < 0 else 256) if formula == "mesen": # The sum with the operand's complement; a digit that produced no carry is corrected # before the next is formed, and the correction is not reduced modulo 256 here. complement = 255 - operand ones = a % 16 + complement % 16 + carry if ones < 16: ones -= 6 total = 16 * (a // 16 + complement // 16 + (1 if ones >= 16 else 0)) + ones % 16 if total < 256: total -= 0x60 return total raise ValueError(f"unknown decimal_formula {formula!r}") def add(a: int, operand: int, p: int, formula: str = "mednafen") -> tuple[int, int]: """A + operand + C, binary or decimal; return (result, P).""" carry = p & C if p & D: result = _decimal_add(a, operand, carry, formula) p &= ~(N | Z | C) # V is unchanged in decimal mode (U14) else: result = a + operand + carry p &= ~(N | V | Z | C) if ~(a ^ operand) & (a ^ result) & 0x80: p |= V if result > 0xFF: p |= C return result & 0xFF, p | nz(result) def subtract(a: int, operand: int, p: int, formula: str = "mednafen") -> tuple[int, int]: """A - operand - (1 - C), binary or decimal; return (result, P).""" carry = p & C if p & D: result = _decimal_subtract(a, operand, carry, formula) p &= ~(N | Z | C) else: inverted = ~operand & 0xFF result = a + inverted + carry p &= ~(N | V | Z | C) if ~(a ^ inverted) & (a ^ result) & 0x80: p |= V if result > 0xFF: p |= C return result & 0xFF, p | nz(result)