huc6280/
shifts.rs

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