// SPDX-License-Identifier: MIT //! The bit instructions: TSB TRB on every part; RMB SMB BBR BBS where the row //! has them (`_bits.py`). //! //! `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. //! //! Differences forced by Rust: `TSB` and `TRB` receive their address (the //! dispatch has run the mode). use crate::core::{Bus, Cmos65c02, Z}; // 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. impl Cmos65c02 { // Z from A AND memory, not from the result: DOC (DS Table 6-4); MEASURED (CAP, Dormann). fn tsb(&mut self, value: u8) -> u8 { self.p = (self.p & !Z) | if self.a & value != 0 { 0 } else { Z }; // Z from the test, not the result value | self.a } fn trb(&mut self, value: u8) -> u8 { self.p = (self.p & !Z) | if self.a & value != 0 { 0 } else { Z }; value & !self.a } /// TSB -- set the bits of memory that are set in A; Z from A AND memory (DS Table 6-4). pub(crate) fn op_tsb(&mut self, address: u16) { self.modify(address, Self::tsb); } /// TRB -- clear the bits of memory that are set in A; Z from A AND memory (DS Table 6-4). pub(crate) fn op_trb(&mut self, address: u16) { self.modify(address, Self::trb); } /// RMB -- clear one bit of a zero-page byte; no flags (DS Table 6-4; CAP: 5 cycles). pub(crate) fn op_rmb(&mut self) { // 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). let mask = 1u8 << ((self.opcode >> 5) & 7); let zp = self.fetch_byte() as u16; let value = self.read(zp); self.read(zp); // read-modify-write: two reads, one write (DS Table 7-1) self.write(zp, value & !mask); } /// SMB -- set one bit of a zero-page byte; no flags (DS Table 6-4; CAP: 5 cycles). pub(crate) fn op_smb(&mut self) { let mask = 1u8 << ((self.opcode >> 4) & 7); let zp = self.fetch_byte() as u16; let value = self.read(zp); self.read(zp); self.write(zp, value | mask); } /// 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). fn bit_branch(&mut self, wanted: u8) { // 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). let mask = 1u8 << ((self.opcode >> 4) & 7); let zp = self.fetch_byte() as u16; let value = self.read(zp); self.read(zp); let offset = self.fetch_byte(); if (value & mask) == wanted & mask { let pc = self.pc; self.fetch_discard(); self.extra += 1; let target = pc.wrapping_add(offset as i8 as u16); if (target ^ pc) & 0xFF00 != 0 { self.fetch_discard(); self.extra += 1; } self.pc = target; } } /// BBR -- branch if the bit of the zero-page byte is clear (DS Table 6-4). pub(crate) fn op_bbr(&mut self) { self.bit_branch(0x00); } /// BBS -- branch if the bit of the zero-page byte is set (DS Table 6-4). pub(crate) fn op_bbs(&mut self) { self.bit_branch(0xFF); } }