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