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    pub(crate) fn op_asl(&mut self, mode: Mode) {
50        let address = self.ea(mode);
51        self.modify(address, Self::asl);
52    }
53
54    /// ASL A -- shift A left, bit 7 -> C (SW SB-19).
55    pub(crate) fn op_asl_a(&mut self) {
56        self.accumulator(Self::asl);
57    }
58
59    /// LSR -- shift M right, bit 0 -> C (SW SB-56).
60    pub(crate) fn op_lsr(&mut self, mode: Mode) {
61        let address = self.ea(mode);
62        self.modify(address, Self::lsr);
63    }
64
65    /// LSR A -- shift A right, bit 0 -> C (SW SB-56).
66    pub(crate) fn op_lsr_a(&mut self) {
67        self.accumulator(Self::lsr);
68    }
69
70    /// ROL -- rotate M left through C (SW SB-68).
71    pub(crate) fn op_rol(&mut self, mode: Mode) {
72        let address = self.ea(mode);
73        self.modify(address, Self::rol);
74    }
75
76    /// ROL A -- rotate A left through C (SW SB-68).
77    pub(crate) fn op_rol_a(&mut self) {
78        self.accumulator(Self::rol);
79    }
80
81    /// ROR -- rotate M right through C (SW SB-69).
82    pub(crate) fn op_ror(&mut self, mode: Mode) {
83        let address = self.ea(mode);
84        self.modify(address, Self::ror);
85    }
86
87    /// ROR A -- rotate A right through C (SW SB-69).
88    pub(crate) fn op_ror_a(&mut self) {
89        self.accumulator(Self::ror);
90    }
91
92    /// INC -- M + 1 -> M (SW SB-48).
93    pub(crate) fn op_inc(&mut self, mode: Mode) {
94        let address = self.ea(mode);
95        self.modify(address, Self::inc);
96    }
97
98    /// INC A -- A + 1 -> A (SW SB-48).
99    pub(crate) fn op_inc_a(&mut self) {
100        self.accumulator(Self::inc);
101    }
102
103    /// DEC -- M - 1 -> M (SW SB-44).
104    pub(crate) fn op_dec(&mut self, mode: Mode) {
105        let address = self.ea(mode);
106        self.modify(address, Self::dec);
107    }
108
109    /// DEC A -- A - 1 -> A (SW SB-44).
110    pub(crate) fn op_dec_a(&mut self) {
111        self.accumulator(Self::dec);
112    }
113}