# SPDX-License-Identifier: MIT """Shifts, rotates, and memory increment and decrement: ASL LSR ROL ROR INC DEC. On memory each is a read-modify-write: the 65C02 reads the operand twice and writes once (DS Table 7-1; ``_core._modify``). ``INC A`` and ``DEC A`` are the 65C02's own (CL section 3). The absolute,X forms of the four shifts take 6 cycles within a page and 7 across (DS Table 7-1 "Read/Modify/Write instructions absolute indexed in same page: six cycles"; CAP: `1E 3E 5E 7E` 6); ``INC`` and ``DEC abs,X`` always take 7 (CL section 7; CAP: `DE FE` 7; C11). The dispatch table picks the addressing method accordingly. """ from collections.abc import Callable from w65c02_python._core import NZ, C, N, Z Mode = Callable[..., int] # Evidence tags (emulator-lineage docs/methodology.md): results and flags are # DOC (DS Table 6-4) and MEASURED (CAP: Dormann's tests on both parts); the # read-modify-write cycles and the abs,X counts within a page are MEASURED # (CAP) and DOC (DS Table 7-1; CL section 7 for INC/DEC abs,X). The shifts' # page-crossing cycle is DOC only (DS Table 7-1): no capture crosses a page # there (timing.md). MAME's 7-cycle shifts within a page are OBSERVED and # overruled (C19). class ShiftMixin: """ASL LSR ROL ROR INC DEC, on memory and on A.""" # -- the operations: value in, value out, flags set ------------------------ def _asl(self, value: int) -> int: result = (value << 1) & 0xFF self.p = (self.p & ~(N | Z | C)) | NZ[result] | (value >> 7) # C = old bit 7 return result def _lsr(self, value: int) -> int: result = value >> 1 self.p = (self.p & ~(N | Z | C)) | NZ[result] | (value & C) # C = old bit 0 return result def _rol(self, value: int) -> int: result = ((value << 1) | (self.p & C)) & 0xFF self.p = (self.p & ~(N | Z | C)) | NZ[result] | (value >> 7) return result def _ror(self, value: int) -> int: result = (value >> 1) | ((self.p & C) << 7) self.p = (self.p & ~(N | Z | C)) | NZ[result] | (value & C) return result def _inc(self, value: int) -> int: return self._set_nz((value + 1) & 0xFF) def _dec(self, value: int) -> int: return self._set_nz((value - 1) & 0xFF) # -- on memory --------------------------------------------------------------- def _op_asl(self, mode: Mode) -> None: """ASL -- shift memory left; C takes bit 7 (DS Table 6-4 note 1).""" self._modify(mode(self), self._asl) def _op_lsr(self, mode: Mode) -> None: """LSR -- shift memory right; C takes bit 0 (DS Table 6-4 note 3).""" self._modify(mode(self), self._lsr) def _op_rol(self, mode: Mode) -> None: """ROL -- rotate memory left through C (DS Table 6-4 note 4).""" self._modify(mode(self), self._rol) def _op_ror(self, mode: Mode) -> None: """ROR -- rotate memory right through C (DS Table 6-4 note 5).""" self._modify(mode(self), self._ror) def _op_inc(self, mode: Mode) -> None: """INC -- increment memory (DS Table 6-4).""" self._modify(mode(self), self._inc) def _op_dec(self, mode: Mode) -> None: """DEC -- decrement memory (DS Table 6-4).""" self._modify(mode(self), self._dec) # -- on the accumulator: two cycles, the second a dropped read of the next byte # MEASURED (CAP: 2 cycles, both reads). def _op_asl_a(self) -> None: """ASL A -- shift A left (DS Table 6-4 note 1).""" self._fetch_discard() self.a = self._asl(self.a) def _op_lsr_a(self) -> None: """LSR A -- shift A right (DS Table 6-4 note 3).""" self._fetch_discard() self.a = self._lsr(self.a) def _op_rol_a(self) -> None: """ROL A -- rotate A left through C (DS Table 6-4 note 4).""" self._fetch_discard() self.a = self._rol(self.a) def _op_ror_a(self) -> None: """ROR A -- rotate A right through C (DS Table 6-4 note 5).""" self._fetch_discard() self.a = self._ror(self.a) def _op_inc_a(self) -> None: """INC A -- increment A (DS Table 6-4; CL section 3).""" self._fetch_discard() self.a = self._inc(self.a) def _op_dec_a(self) -> None: """DEC A -- decrement A (DS Table 6-4; CL section 3).""" self._fetch_discard() self.a = self._dec(self.a)