// SPDX-License-Identifier: MIT //! Shifts, rotates, and memory increment and decrement: ASL LSR ROL ROR INC DEC //! (`_shifts.py`). //! //! 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. //! //! Differences forced by Rust: a memory handler receives its address (the //! dispatch has run the `ea_` mode) and passes the operation to `modify` as a //! function pointer. use crate::core::{Bus, Cmos65c02, C, N, NZ, Z}; // 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). impl Cmos65c02 { // -- the operations: value in, value out, flags set ------------------------ pub(crate) fn asl(&mut self, value: u8) -> u8 { let result = value << 1; self.p = (self.p & !(N | Z | C)) | NZ[result as usize] | (value >> 7); // C = old bit 7 result } pub(crate) fn lsr(&mut self, value: u8) -> u8 { let result = value >> 1; self.p = (self.p & !(N | Z | C)) | NZ[result as usize] | (value & C); // C = old bit 0 result } pub(crate) fn rol(&mut self, value: u8) -> u8 { let result = (value << 1) | (self.p & C); self.p = (self.p & !(N | Z | C)) | NZ[result as usize] | (value >> 7); result } pub(crate) fn ror(&mut self, value: u8) -> u8 { let result = (value >> 1) | ((self.p & C) << 7); self.p = (self.p & !(N | Z | C)) | NZ[result as usize] | (value & C); result } pub(crate) fn inc(&mut self, value: u8) -> u8 { self.set_nz(value.wrapping_add(1)) } pub(crate) fn dec(&mut self, value: u8) -> u8 { self.set_nz(value.wrapping_sub(1)) } // -- on memory --------------------------------------------------------------- /// ASL -- shift memory left; C takes bit 7 (DS Table 6-4 note 1). pub(crate) fn op_asl(&mut self, address: u16) { self.modify(address, Self::asl); } /// LSR -- shift memory right; C takes bit 0 (DS Table 6-4 note 3). pub(crate) fn op_lsr(&mut self, address: u16) { self.modify(address, Self::lsr); } /// ROL -- rotate memory left through C (DS Table 6-4 note 4). pub(crate) fn op_rol(&mut self, address: u16) { self.modify(address, Self::rol); } /// ROR -- rotate memory right through C (DS Table 6-4 note 5). pub(crate) fn op_ror(&mut self, address: u16) { self.modify(address, Self::ror); } /// INC -- increment memory (DS Table 6-4). pub(crate) fn op_inc(&mut self, address: u16) { self.modify(address, Self::inc); } /// DEC -- decrement memory (DS Table 6-4). pub(crate) fn op_dec(&mut self, address: u16) { self.modify(address, Self::dec); } // -- on the accumulator: two cycles, the second a dropped read of the next byte // MEASURED (CAP: 2 cycles, both reads). /// ASL A -- shift A left (DS Table 6-4 note 1). pub(crate) fn op_asl_a(&mut self) { self.fetch_discard(); self.a = self.asl(self.a); } /// LSR A -- shift A right (DS Table 6-4 note 3). pub(crate) fn op_lsr_a(&mut self) { self.fetch_discard(); self.a = self.lsr(self.a); } /// ROL A -- rotate A left through C (DS Table 6-4 note 4). pub(crate) fn op_rol_a(&mut self) { self.fetch_discard(); self.a = self.rol(self.a); } /// ROR A -- rotate A right through C (DS Table 6-4 note 5). pub(crate) fn op_ror_a(&mut self) { self.fetch_discard(); self.a = self.ror(self.a); } /// INC A -- increment A (DS Table 6-4; CL section 3). pub(crate) fn op_inc_a(&mut self) { self.fetch_discard(); self.a = self.inc(self.a); } /// DEC A -- decrement A (DS Table 6-4; CL section 3). pub(crate) fn op_dec_a(&mut self) { self.fetch_discard(); self.a = self.dec(self.a); } }