huc6280/
bits.rs

1// SPDX-License-Identifier: MIT
2//! Bit instructions: RMB SMB BBR BBS TSB TRB (`_bits.py`).
3//!
4//! `RMBi`/`SMBi`/`BBRi`/`BBSi` take the bit number from the opcode
5//! (`10i + code`, SW). They are the Rockwell/WDC 65C02 additions (U22); the
6//! cycle counts are not the 65C02's.
7
8use crate::core::{Bus, HuC6280, N, V, Z, ZERO_PAGE};
9use crate::dispatch::Mode;
10
11impl<B: Bus> HuC6280<B> {
12    fn bit_mask(&self) -> u8 {
13        1 << ((self.pins.opcode >> 4) & 7)
14    }
15
16    fn bit_branch(&mut self, branch_if_set: bool) {
17        let zp = self.fetch();
18        self.io();
19        let offset = self.fetch();
20        self.io();
21        let value = self.cycle_read(ZERO_PAGE | zp as u16);
22        if (value & self.bit_mask() != 0) == branch_if_set {
23            self.io();
24            self.io();
25            self.pc = self.pc.wrapping_add(offset as i8 as u16);
26        }
27    }
28
29    /// BBRi -- branch if bit i of the zero-page byte is 0 (SW SB-20).
30    pub(crate) fn op_bbr(&mut self) {
31        self.bit_branch(false);
32    }
33
34    /// BBSi -- branch if bit i of the zero-page byte is 1 (SW SB-21).
35    pub(crate) fn op_bbs(&mut self) {
36        self.bit_branch(true);
37    }
38
39    fn bit_write(&mut self, address: u16, clear: bool) {
40        let value = self.cycle_read(address);
41        self.io();
42        self.io();
43        let mask = self.bit_mask();
44        self.cycle_write(address, if clear { value & !mask } else { value | mask });
45    }
46
47    /// RMBi -- clear bit i of the zero-page byte (SW SB-67).
48    pub(crate) fn op_rmb(&mut self, mode: Mode) {
49        let address = self.ea(mode);
50        self.bit_write(address, true);
51    }
52
53    /// SMBi -- set bit i of the zero-page byte (SW SB-79).
54    pub(crate) fn op_smb(&mut self, mode: Mode) {
55        let address = self.ea(mode);
56        self.bit_write(address, false);
57    }
58
59    fn test_and_modify(&mut self, address: u16, operation: fn(&Self, u8) -> u8) {
60        // N and V from the memory byte before the change (HW HB-5, SW TSB).
61        let value = self.cycle_read(address);
62        let result = operation(self, value);
63        let zero = if self.tsb_z_from_test {
64            value & self.a == 0
65        } else {
66            result == 0
67        };
68        self.p = (self.p & !(N | V | Z)) | (value & (N | V)) | if zero { Z } else { 0 };
69        self.io();
70        self.cycle_write(address, result);
71    }
72
73    /// TSB -- A OR M -> M; N and V from M, Z from the result (U23) (SW SB-103).
74    pub(crate) fn op_tsb(&mut self, mode: Mode) {
75        let address = self.ea(mode);
76        self.test_and_modify(address, |cpu, value| value | cpu.a);
77    }
78
79    /// TRB -- (NOT A) AND M -> M; N and V from M, Z from the result (U23)
80    /// (SW SB-102).
81    pub(crate) fn op_trb(&mut self, mode: Mode) {
82        let address = self.ea(mode);
83        self.test_and_modify(address, |cpu, value| value & !cpu.a);
84    }
85}