# SPDX-License-Identifier: MIT """The 16-bit YA instructions: MOVW ADDW SUBW CMPW INCW DECW, and MUL and DIV. YA is Y:A paired. Direct-page words take their high byte from ``(d + 1) & $FF`` in the same page (U2). """ from __future__ import annotations from spc700_python._core import CoreMixin, pack from spc700_python._flags import FLAG_C, FLAG_H, FLAG_N, FLAG_V, FLAG_Z class WordMixin(CoreMixin): def _read_word_with_idle(self) -> int: """The direct word of MOVW YA,d, ADDW and SUBW: an internal cycle between the bytes.""" offset = self._fetch() low = self._load(offset) self._idle() return pack(self._load(offset + 1), low) def _set_z16(self, value: int) -> None: """Z from all sixteen bits, after two chained 8-bit operations (U8).""" self.psw = self.psw & ~FLAG_Z | (FLAG_Z if value == 0 else 0) def _op_movw_ya_d(self, op: int) -> None: """MOVW YA,d -- load a word; N Z from 16 bits (timing.md MOVW YA,d).""" self.ya = self._read_word_with_idle() self._set_nz16(self.ya) def _op_movw_d_ya(self, op: int) -> None: """MOVW d,YA -- store a word; no flags (timing.md MOVW d,YA).""" offset = self._fetch() self._read_dummy(self._dp(offset)) # U1: "RMW on low byte only" self._store(offset, self.a) self._store(offset + 1, self.y) def _op_addw(self, op: int) -> None: """ADDW YA,d -- two chained ADCs, C cleared first; H from bit 11, Z from 16 bits (U8).""" word = self._read_word_with_idle() ya = self.ya self.psw &= ~FLAG_C low = self._adc(ya & 0xFF, word & 0xFF) high = self._adc(ya >> 8, word >> 8) self.ya = pack(high, low) self._set_z16(self.ya) def _op_subw(self, op: int) -> None: """SUBW YA,d -- two chained SBCs, C set first; H from bit 11, Z from 16 bits (U8).""" word = self._read_word_with_idle() ya = self.ya self.psw |= FLAG_C low = self._sbc(ya & 0xFF, word & 0xFF) high = self._sbc(ya >> 8, word >> 8) self.ya = pack(high, low) self._set_z16(self.ya) def _op_cmpw(self, op: int) -> None: """CMPW YA,d -- N Z C from the 16-bit subtraction, one cycle shorter (U8, timing.md).""" offset = self._fetch() word = self._load_pointer(offset) difference = self.ya - word self.psw = self.psw & ~FLAG_C | (FLAG_C if difference >= 0 else 0) self._set_nz16(difference & 0xFFFF) def _step_word(self, step: int) -> None: offset = self._fetch() value = self._load(offset) + step self._store(offset, value & 0xFF) # the carry or borrow of the low byte reaches the high byte, read only now value = (value + (self._load(offset + 1) << 8)) & 0xFFFF self._store(offset + 1, value >> 8) self._set_nz16(value) def _op_incw(self, op: int) -> None: """INCW d -- N Z from 16 bits; C stays (U8, timing.md INCW/DECW).""" self._step_word(1) def _op_decw(self, op: int) -> None: """DECW d -- N Z from 16 bits; C stays (U8, timing.md INCW/DECW).""" self._step_word(-1) def _op_mul(self, op: int) -> None: """MUL YA -- YA = Y * A; N Z from Y alone, the high byte (U3).""" self._read_dummy(self.pc) for _ in range(7): self._idle() self.ya = self.y * self.a self._set_nz(self.y) def _op_div(self, op: int) -> None: """DIV YA,X -- anomie's 9-step shift-subtract loop, X = 0 included (U5).""" self._read_dummy(self.pc) for _ in range(10): self._idle() divisor = self.x << 9 half = FLAG_H if self.y & 0x0F >= self.x & 0x0F else 0 # U5: the original Y and X value = self.ya for _ in range(9): value = (value << 1 | value >> 16) & 0x1FFFF # a 17-bit rotate if value >= divisor: value ^= 1 if value & 1: value = (value - divisor) & 0x1FFFF self.y = value >> 9 & 0xFF self.a = value & 0xFF overflow = FLAG_V if value & 0x100 else 0 # bit 8: the quotient's ninth bit self.psw = self.psw & ~(FLAG_N | FLAG_V | FLAG_H | FLAG_Z) | overflow | half self._set_nz(self.a)