# SPDX-License-Identifier: MIT """The bit instructions: TSB TRB on every part; RMB SMB BBR BBS where the row has them. ``TSB`` and ``TRB`` are on every 65C02 (CL section 3). ``RMB0-7``, ``SMB0-7``, ``BBR0-7`` and ``BBS0-7`` are Rockwell's, also on the W65C02S (CL section 4; DS Table 5-2; RDS) and in the Lynx II's cell (C12); a part without them runs a one-byte, one-cycle NOP at each of the 32 opcodes (``_dispatch.opcodes_for``). The bit number is bits 4 to 6 of the opcode. """ from collections.abc import Callable from w65c02_python._core import Z Mode = Callable[..., int] # Evidence tags (emulator-lineage docs/methodology.md): TSB and TRB are DOC # (DS Table 6-4, CL section 3) and MEASURED (CAP: cycles; Dormann's 65C02 test # checks results and Z on both parts). RMB SMB BBR BBS are DOC (DS, RDS, CL # section 4) and MEASURED on the R65C02 (CAP), except where noted. class BitMixin: """TSB TRB RMB SMB BBR BBS.""" # Z from A AND memory, not from the result: DOC (DS Table 6-4); MEASURED (CAP, Dormann). def _tsb(self, value: int) -> int: self.p = (self.p & ~Z) | (0 if self.a & value else Z) # Z from the test, not the result return value | self.a def _trb(self, value: int) -> int: self.p = (self.p & ~Z) | (0 if self.a & value else Z) return value & ~self.a & 0xFF def _op_tsb(self, mode: Mode) -> None: """TSB -- set the bits of memory that are set in A; Z from A AND memory (DS Table 6-4).""" self._modify(mode(self), self._tsb) def _op_trb(self, mode: Mode) -> None: """TRB -- clear the bits of memory that are set in A; Z from A AND memory (DS Table 6-4).""" self._modify(mode(self), self._trb) def _op_rmb(self) -> None: """RMB -- clear one bit of a zero-page byte; no flags (DS Table 6-4; CAP: 5 cycles).""" # The bit number from opcode bits 4-6: DOC (DS Table 5-2); MEASURED (CAP: # the captured R65C02 runs Dormann's 65C02 test, which checks it, to success). mask = 1 << ((self.opcode >> 4) & 7) zp = self._fetch() value = self._read(zp) self._read(zp) # read-modify-write: two reads, one write (DS Table 7-1) self._write(zp, value & ~mask) def _op_smb(self) -> None: """SMB -- set one bit of a zero-page byte; no flags (DS Table 6-4; CAP: 5 cycles).""" mask = 1 << ((self.opcode >> 4) & 7) zp = self._fetch() value = self._read(zp) self._read(zp) self._write(zp, value | mask) def _bit_branch(self, wanted: int) -> None: """BBR and BBS: test one bit of a zero-page byte, then branch. 5 cycles, 6 taken (CAP on the R65C02). The zero-page byte is read twice, then the offset, then the next opcode's address (CAP: all four agree with the captured bytes). Across a page a seventh cycle reads that address again: SST's, and the rule the other branches follow in the captures; no capture takes a bit branch across a page (U4). """ # Five or six cycles and their addresses: MEASURED (CAP, R65C02). The # seventh cycle across a page: OBSERVED (SST) and HYPOTHESIS by analogy # with the captured branches (U4: a capture of a bit branch across a # page would settle it). mask = 1 << ((self.opcode >> 4) & 7) zp = self._fetch() value = self._read(zp) self._read(zp) offset = self._fetch() if (value & mask) == wanted & mask: pc = self.pc self._fetch_discard() self._extra += 1 target = (pc + offset - ((offset & 0x80) << 1)) & 0xFFFF if (target ^ pc) & 0xFF00: self._fetch_discard() self._extra += 1 self.pc = target def _op_bbr(self) -> None: """BBR -- branch if the bit of the zero-page byte is clear (DS Table 6-4).""" self._bit_branch(0x00) def _op_bbs(self) -> None: """BBS -- branch if the bit of the zero-page byte is set (DS Table 6-4).""" self._bit_branch(0xFF)