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    pub(crate) fn op_rmb(&mut self, mode: Mode) {
48        let address = self.ea(mode);
49        self.bit_write(address, true);
50    }
51
52    /// SMBi -- set bit i of the zero-page byte (SW SB-79).
53    pub(crate) fn op_smb(&mut self, mode: Mode) {
54        let address = self.ea(mode);
55        self.bit_write(address, false);
56    }
57
58    fn test_and_modify(&mut self, address: u16, operation: fn(&Self, u8) -> u8) {
59        // N and V from the memory byte before the change (HW HB-5, SW TSB).
60        let value = self.cycle_read(address);
61        let result = operation(self, value);
62        let zero = if self.tsb_z_from_test {
63            value & self.a == 0
64        } else {
65            result == 0
66        };
67        self.p = (self.p & !(N | V | Z)) | (value & (N | V)) | if zero { Z } else { 0 };
68        self.io();
69        self.cycle_write(address, result);
70    }
71
72    /// TSB -- A OR M -> M; N and V from M, Z from the result (U23) (SW SB-103).
73    pub(crate) fn op_tsb(&mut self, mode: Mode) {
74        let address = self.ea(mode);
75        self.test_and_modify(address, |cpu, value| value | cpu.a);
76    }
77
78    /// TRB -- (NOT A) AND M -> M; N and V from M, Z from the result (U23)
79    /// (SW SB-102).
80    pub(crate) fn op_trb(&mut self, mode: Mode) {
81        let address = self.ea(mode);
82        self.test_and_modify(address, |cpu, value| value & !cpu.a);
83    }
84}