"""Shifts, rotates, increments and decrements of memory and of the accumulator. Memory forms are read-modify-writes (row 1d and its indexed and direct-page variants): MLB throughout, 16-bit data written high byte first, and in emulation mode the modify cycle writes the old value (note 17, C3). The accumulator forms are row 8: the opcode, then an IO cycle at PC. """ from w65c816._core import C, M, N, Z, nz8, nz16 class ShiftMixin: """Private shift, rotate and increment/decrement implementation.""" def _width(self) -> tuple[bool, int, int]: wide = not self.p & M return wide, (0xFFFF if wide else 0xFF), (0x8000 if wide else 0x80) def _flags(self, result: int, wide: bool, carry: int | None = None) -> None: p = (self.p & ~(N | Z)) | (nz16(result) if wide else nz8(result)) if carry is not None: p = (p & ~C) | carry self.p = p def _asl(self, value: int) -> int: wide, mask, top = self._width() result = (value << 1) & mask self._flags(result, wide, 1 if value & top else 0) return result def _lsr(self, value: int) -> int: wide, _, _ = self._width() result = value >> 1 self._flags(result, wide, value & 1) return result def _rol(self, value: int) -> int: wide, mask, top = self._width() result = ((value << 1) | (self.p & C)) & mask self._flags(result, wide, 1 if value & top else 0) return result def _ror(self, value: int) -> int: wide, _, top = self._width() result = (value >> 1) | (top if self.p & C else 0) self._flags(result, wide, value & 1) return result def _inc(self, value: int) -> int: wide, mask, _ = self._width() result = (value + 1) & mask self._flags(result, wide) return result def _dec(self, value: int) -> int: wide, mask, _ = self._width() result = (value - 1) & mask self._flags(result, wide) return result # -- memory -------------------------------------------------------------- def _op_asl(self, mode) -> None: """ASL -- C <- M <- 0 (DS Table 5-5 p. 32).""" self._modify(mode(self), not self.p & M, self._asl) def _op_lsr(self, mode) -> None: """LSR -- 0 -> M -> C (DS Table 5-5 p. 33).""" self._modify(mode(self), not self.p & M, self._lsr) def _op_rol(self, mode) -> None: """ROL -- C <- M <- C (DS Table 5-5 p. 33).""" self._modify(mode(self), not self.p & M, self._rol) def _op_ror(self, mode) -> None: """ROR -- C -> M -> C (DS Table 5-5 p. 33).""" self._modify(mode(self), not self.p & M, self._ror) def _op_inc(self, mode) -> None: """INC -- M + 1 -> M (DS Table 5-5 p. 32).""" self._modify(mode(self), not self.p & M, self._inc) def _op_dec(self, mode) -> None: """DEC -- M - 1 -> M (DS Table 5-5 p. 32).""" self._modify(mode(self), not self.p & M, self._dec) # -- the accumulator --------------------------------------------------- def _accumulator(self, operation) -> None: self._io_pc() if self.p & M: self.a = (self.a & 0xFF00) | operation(self.a & 0xFF) else: self.a = operation(self.a) def _op_asl_a(self) -> None: """ASL A -- C <- A <- 0 (DS Table 5-5 p. 32).""" self._accumulator(self._asl) def _op_lsr_a(self) -> None: """LSR A -- 0 -> A -> C (DS Table 5-5 p. 33).""" self._accumulator(self._lsr) def _op_rol_a(self) -> None: """ROL A -- C <- A <- C (DS Table 5-5 p. 33).""" self._accumulator(self._rol) def _op_ror_a(self) -> None: """ROR A -- C -> A -> C (DS Table 5-5 p. 33).""" self._accumulator(self._ror) def _op_inc_a(self) -> None: """INC A -- A + 1 -> A (DS Table 5-5 p. 32).""" self._accumulator(self._inc) def _op_dec_a(self) -> None: """DEC A -- A - 1 -> A (DS Table 5-5 p. 32).""" self._accumulator(self._dec)