huc6280/
hudson.rs

1//! Hudson's additions: SXY SAX SAY CLA CLX CLY ST0 ST1 ST2 TMA TAM CSL CSH
2//! SET TST (`_hudson.py`).
3//!
4//! None exists on the 65C02; all but the swaps and clears are about the
5//! chip's own extras: the VDC ports, the mapper, the clock switch and the T
6//! flag.
7
8use crate::core::{Bus, HuC6280, N, T, V, Z};
9use crate::dispatch::Mode;
10
11/// U12: ST0-ST2 write physical $1FE000, $1FE002 and $1FE003 whatever the
12/// mapper holds (HW HB-8, Table 2-2-3: A20 high, A1:A0 = 00, 10, 11; pcetech
13/// 2.0). The VDC's registers are every 4 bytes.
14pub const VDC_ADDRESS: u32 = 0x1FE000;
15
16/// C6: TMA with several mask bits. Mesen ORs the registers, MAME takes the
17/// highest bit's; pcetech: "combined together... I have not figured out
18/// exactly how this works".
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub enum TmaMultiple {
21    /// The default (emulator tier).
22    Or,
23    Highest,
24    Lowest,
25}
26
27impl<B: Bus> HuC6280<B> {
28    /// SXY -- swap X and Y (SW SB-87).
29    pub(crate) fn op_sxy(&mut self) {
30        self.implied();
31        self.io();
32        std::mem::swap(&mut self.x, &mut self.y);
33    }
34
35    /// SAX -- swap A and X (SW SB-72).
36    pub(crate) fn op_sax(&mut self) {
37        self.implied();
38        self.io();
39        std::mem::swap(&mut self.a, &mut self.x);
40    }
41
42    /// SAY -- swap A and Y (SW SB-73).
43    pub(crate) fn op_say(&mut self) {
44        self.implied();
45        self.io();
46        std::mem::swap(&mut self.a, &mut self.y);
47    }
48
49    /// CLA -- 00 -> A, no flags (SW SB-34).
50    pub(crate) fn op_cla(&mut self) {
51        self.implied();
52        self.a = 0;
53    }
54
55    /// CLX -- 00 -> X, no flags (SW SB-39).
56    pub(crate) fn op_clx(&mut self) {
57        self.implied();
58        self.x = 0;
59    }
60
61    /// CLY -- 00 -> Y, no flags (SW SB-40).
62    pub(crate) fn op_cly(&mut self) {
63        self.implied();
64        self.y = 0;
65    }
66
67    fn store_vdc(&mut self, offset: u32) {
68        let value = self.fetch();
69        self.io();
70        self.cycle_write_physical(VDC_ADDRESS + offset, value);
71    }
72
73    /// ST0 -- immediate -> VDC register 0, physical $1FE000 (U12) (SW SB-80).
74    pub(crate) fn op_st0(&mut self) {
75        self.store_vdc(0);
76    }
77
78    /// ST1 -- immediate -> VDC register 2, physical $1FE002 (U12) (SW SB-81).
79    pub(crate) fn op_st1(&mut self) {
80        self.store_vdc(2);
81    }
82
83    /// ST2 -- immediate -> VDC register 3, physical $1FE003 (U12) (SW SB-82).
84    pub(crate) fn op_st2(&mut self) {
85        self.store_vdc(3);
86    }
87
88    /// TMAi -- the mapping register named by the mask -> A (SW SB-101).
89    pub(crate) fn op_tma(&mut self) {
90        let mask = self.fetch();
91        self.io();
92        self.io();
93        if mask == 0 {
94            // U11: an operand of $00 returns the last value moved by TAM or
95            // TMA (pcetech 2.0).
96            self.a = self.mpr_latch;
97            return;
98        }
99        let mut chosen = (0..8)
100            .filter(|bit| (mask >> bit) & 1 != 0)
101            .map(|bit| self.mpr[bit]);
102        let value = match self.tma_multiple {
103            TmaMultiple::Or => chosen.fold(0, |value, register| value | register),
104            TmaMultiple::Highest => chosen.next_back().unwrap_or(0),
105            TmaMultiple::Lowest => chosen.next().unwrap_or(0),
106        };
107        self.a = value;
108        self.mpr_latch = value;
109    }
110
111    /// TAMi -- A -> every mapping register the mask names (SW SB-90).
112    pub(crate) fn op_tam(&mut self) {
113        let mask = self.fetch();
114        self.io();
115        self.io();
116        self.io();
117        if mask == 0 {
118            return; // U11: a mask of $00 writes nothing and leaves the latch alone
119        }
120        self.mpr_latch = self.a;
121        for (bit, register) in self.mpr.iter_mut().enumerate() {
122            if (mask >> bit) & 1 != 0 {
123                *register = self.a;
124            }
125        }
126    }
127
128    fn speed(&mut self, high: bool) {
129        self.implied();
130        // C7: the new speed holds for the instruction's last cycle (Mesen,
131        // Mednafen); MAME applies it after. 3 cycles when the speed does not
132        // change (pcetech 2.4, U10).
133        self.pins.high_speed = high;
134        self.io();
135    }
136
137    /// CSL -- select the low clock speed, 1.79 MHz (HW HB-25; pcetech 2.4).
138    pub(crate) fn op_csl(&mut self) {
139        self.speed(false);
140    }
141
142    /// CSH -- select the high clock speed, 7.16 MHz (HW HB-25; pcetech 2.4).
143    pub(crate) fn op_csh(&mut self) {
144        self.speed(true);
145    }
146
147    /// SET -- 1 -> T, for the next instruction only (SW SB-78).
148    pub(crate) fn op_set(&mut self) {
149        self.implied();
150        // U4: the next instruction's fetch clears it again.
151        self.p |= T;
152    }
153
154    /// TST -- immediate AND M sets Z; M bits 7 and 6 -> N and V (SW SB-104).
155    pub(crate) fn op_tst(&mut self, mode: Mode) {
156        let mask = self.fetch();
157        let address = self.ea(mode);
158        self.io();
159        let value = self.cycle_read(address);
160        self.io();
161        // U19: N and V come from the memory operand, Z from the AND.
162        self.p =
163            (self.p & !(N | V | Z)) | (value & (N | V)) | if value & mask != 0 { 0 } else { Z };
164    }
165}