huc6280/
shifts.rs

1//! Shifts, rotates, INC and DEC, on the accumulator and on memory: ASL LSR
2//! ROL ROR INC DEC (`_shifts.py`).
3//!
4//! The accumulator forms are two cycles; the memory forms read, spend an
5//! internal cycle and write (no dummy write, unlike the NMOS 6502).
6
7use crate::core::{nz, Bus, HuC6280, C, N, Z};
8use crate::dispatch::Mode;
9
10impl<B: Bus> HuC6280<B> {
11    fn shifted(&mut self, result: u8, carry: u8) -> u8 {
12        self.p = (self.p & !(N | Z | C)) | nz(result) | carry;
13        result
14    }
15
16    fn asl(&mut self, value: u8) -> u8 {
17        self.shifted(value << 1, if value & 0x80 != 0 { C } else { 0 })
18    }
19
20    fn lsr(&mut self, value: u8) -> u8 {
21        self.shifted(value >> 1, value & C)
22    }
23
24    fn rol(&mut self, value: u8) -> u8 {
25        self.shifted(
26            (value << 1) | (self.p & C),
27            if value & 0x80 != 0 { C } else { 0 },
28        )
29    }
30
31    fn ror(&mut self, value: u8) -> u8 {
32        self.shifted((value >> 1) | ((self.p & C) << 7), value & C)
33    }
34
35    fn inc(&mut self, value: u8) -> u8 {
36        self.set_nz(value.wrapping_add(1))
37    }
38
39    fn dec(&mut self, value: u8) -> u8 {
40        self.set_nz(value.wrapping_sub(1))
41    }
42
43    fn accumulator(&mut self, operation: fn(&mut Self, u8) -> u8) {
44        self.implied();
45        self.a = operation(self, self.a);
46    }
47
48    /// ASL -- shift M left, bit 7 -> C (SW SB-19).
49    #[inline(always)]
50    pub(crate) fn op_asl(&mut self, mode: Mode) {
51        let address = self.ea(mode);
52        self.modify(address, Self::asl);
53    }
54
55    /// ASL A -- shift A left, bit 7 -> C (SW SB-19).
56    pub(crate) fn op_asl_a(&mut self) {
57        self.accumulator(Self::asl);
58    }
59
60    /// LSR -- shift M right, bit 0 -> C (SW SB-56).
61    #[inline(always)]
62    pub(crate) fn op_lsr(&mut self, mode: Mode) {
63        let address = self.ea(mode);
64        self.modify(address, Self::lsr);
65    }
66
67    /// LSR A -- shift A right, bit 0 -> C (SW SB-56).
68    pub(crate) fn op_lsr_a(&mut self) {
69        self.accumulator(Self::lsr);
70    }
71
72    /// ROL -- rotate M left through C (SW SB-68).
73    #[inline(always)]
74    pub(crate) fn op_rol(&mut self, mode: Mode) {
75        let address = self.ea(mode);
76        self.modify(address, Self::rol);
77    }
78
79    /// ROL A -- rotate A left through C (SW SB-68).
80    pub(crate) fn op_rol_a(&mut self) {
81        self.accumulator(Self::rol);
82    }
83
84    /// ROR -- rotate M right through C (SW SB-69).
85    #[inline(always)]
86    pub(crate) fn op_ror(&mut self, mode: Mode) {
87        let address = self.ea(mode);
88        self.modify(address, Self::ror);
89    }
90
91    /// ROR A -- rotate A right through C (SW SB-69).
92    pub(crate) fn op_ror_a(&mut self) {
93        self.accumulator(Self::ror);
94    }
95
96    /// INC -- M + 1 -> M (SW SB-48).
97    #[inline(always)]
98    pub(crate) fn op_inc(&mut self, mode: Mode) {
99        let address = self.ea(mode);
100        self.modify(address, Self::inc);
101    }
102
103    /// INC A -- A + 1 -> A (SW SB-48).
104    pub(crate) fn op_inc_a(&mut self) {
105        self.accumulator(Self::inc);
106    }
107
108    /// DEC -- M - 1 -> M (SW SB-44).
109    #[inline(always)]
110    pub(crate) fn op_dec(&mut self, mode: Mode) {
111        let address = self.ea(mode);
112        self.modify(address, Self::dec);
113    }
114
115    /// DEC A -- A - 1 -> A (SW SB-44).
116    pub(crate) fn op_dec_a(&mut self) {
117        self.accumulator(Self::dec);
118    }
119}