huc6280/
bits.rs

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