1use crate::core::{Bus, HuC6280, C, D, N, V, Z, ZERO_PAGE};
28use crate::dispatch::Mode;
29
30#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32pub enum DecimalFormula {
33 Mednafen,
35 Mesen,
37}
38
39fn nz(value: i32) -> u8 {
41 (value as u8 & N) | if value & 0xFF != 0 { 0 } else { Z }
42}
43
44fn decimal_add(a: i32, operand: i32, carry: i32, formula: DecimalFormula) -> i32 {
50 let mut ones = a % 16 + operand % 16 + carry;
51 let sixteens = a / 16 + operand / 16;
52 if ones >= 10 {
53 ones += 6; }
55 let mut total = match formula {
56 DecimalFormula::Mednafen => 16 * sixteens + ones,
58 DecimalFormula::Mesen => 16 * (sixteens + if ones >= 16 { 1 } else { 0 }) + ones % 16,
60 };
61 if total >= 0xA0 {
62 total += 0x60;
63 }
64 total
65}
66
67fn decimal_subtract(a: i32, operand: i32, carry: i32, formula: DecimalFormula) -> i32 {
69 match formula {
70 DecimalFormula::Mednafen => {
71 let ones = a % 16 - operand % 16 - (1 - carry);
75 let sixteens = a / 16 - operand / 16 - if ones < 0 { 1 } else { 0 };
76 let mut byte = 16 * sixteens.rem_euclid(16) + ones.rem_euclid(16);
77 if ones < 0 {
78 byte -= 0x06;
79 }
80 if sixteens < 0 {
81 byte -= 0x60;
82 }
83 byte.rem_euclid(256) + if sixteens < 0 { 0 } else { 256 }
84 }
85 DecimalFormula::Mesen => {
86 let complement = 255 - operand;
90 let mut ones = a % 16 + complement % 16 + carry;
91 if ones < 16 {
92 ones -= 6;
93 }
94 let mut total = 16 * (a / 16 + complement / 16 + if ones >= 16 { 1 } else { 0 })
95 + ones.rem_euclid(16);
96 if total < 256 {
97 total -= 0x60;
98 }
99 total
100 }
101 }
102}
103
104pub fn add(a: u8, operand: u8, p: u8, formula: DecimalFormula) -> (u8, u8) {
106 let (a, operand) = (a as i32, operand as i32);
107 let carry = (p & C) as i32;
108 let mut p = p;
109 let result;
110 if p & D != 0 {
111 result = decimal_add(a, operand, carry, formula);
112 p &= !(N | Z | C); } else {
114 result = a + operand + carry;
115 p &= !(N | V | Z | C);
116 if !(a ^ operand) & (a ^ result) & 0x80 != 0 {
117 p |= V;
118 }
119 }
120 if result > 0xFF {
121 p |= C;
122 }
123 (result as u8, p | nz(result))
124}
125
126pub fn subtract(a: u8, operand: u8, p: u8, formula: DecimalFormula) -> (u8, u8) {
128 let (a, operand) = (a as i32, operand as i32);
129 let carry = (p & C) as i32;
130 let mut p = p;
131 let result;
132 if p & D != 0 {
133 result = decimal_subtract(a, operand, carry, formula);
134 p &= !(N | Z | C);
135 } else {
136 let inverted = !operand & 0xFF;
137 result = a + inverted + carry;
138 p &= !(N | V | Z | C);
139 if !(a ^ inverted) & (a ^ result) & 0x80 != 0 {
140 p |= V;
141 }
142 }
143 if result > 0xFF {
144 p |= C;
145 }
146 (result as u8, p | nz(result))
147}
148
149type Arithmetic = fn(u8, u8, u8, DecimalFormula) -> (u8, u8);
151
152impl<B: Bus> HuC6280<B> {
153 fn memory_mode(
159 &mut self,
160 operand: u8,
161 operation: impl FnOnce(&mut Self, u8, u8) -> u8,
162 extra_cycle: bool,
163 ) {
164 let address = ZERO_PAGE | self.x as u16;
165 let memory = self.cycle_read(address);
166 self.io();
167 if extra_cycle {
168 self.io();
169 }
170 let result = operation(self, memory, operand);
171 self.set_nz(result);
172 self.cycle_write(address, result);
173 }
174
175 #[inline(always)]
177 pub(crate) fn op_ora(&mut self, mode: Mode) {
178 let address = self.ea(mode);
179 let operand = self.load(address);
180 if self.t != 0 {
181 self.memory_mode(operand, |_, memory, value| memory | value, false);
182 } else {
183 self.a = self.set_nz(self.a | operand);
184 }
185 }
186
187 #[inline(always)]
189 pub(crate) fn op_and(&mut self, mode: Mode) {
190 let address = self.ea(mode);
191 let operand = self.load(address);
192 if self.t != 0 {
193 self.memory_mode(operand, |_, memory, value| memory & value, false);
194 } else {
195 self.a = self.set_nz(self.a & operand);
196 }
197 }
198
199 #[inline(always)]
201 pub(crate) fn op_eor(&mut self, mode: Mode) {
202 let address = self.ea(mode);
203 let operand = self.load(address);
204 if self.t != 0 {
205 self.memory_mode(operand, |_, memory, value| memory ^ value, false);
206 } else {
207 self.a = self.set_nz(self.a ^ operand);
208 }
209 }
210
211 #[inline(always)]
212 fn arithmetic(&mut self, mode: Mode, operation: Arithmetic, uses_t: bool) {
213 let address = self.ea(mode);
214 let operand = self.load(address);
215 let decimal = self.p & D != 0;
216 if uses_t && self.t != 0 {
217 self.memory_mode(
219 operand,
220 |cpu, memory, value| {
221 let (result, p) = operation(memory, value, cpu.p, cpu.decimal_formula);
222 cpu.p = p;
223 result
224 },
225 decimal,
226 );
227 return;
228 }
229 if decimal {
230 if self.t != 0 {
234 self.io();
235 } else {
236 self.dummy(self.pc);
237 }
238 }
239 (self.a, self.p) = operation(self.a, operand, self.p, self.decimal_formula);
240 }
241
242 #[inline(always)]
244 pub(crate) fn op_adc(&mut self, mode: Mode) {
245 self.arithmetic(mode, add, true);
246 }
247
248 #[inline(always)]
250 pub(crate) fn op_sbc(&mut self, mode: Mode) {
251 self.arithmetic(mode, subtract, self.sbc_honours_t);
252 }
253
254 fn compare(&mut self, register: u8, operand: u8) {
255 let difference = register.wrapping_sub(operand);
256 let mut p = self.p & !(N | Z | C);
257 if register >= operand {
258 p |= C;
259 }
260 self.p = p | crate::core::nz(difference);
261 }
262
263 #[inline(always)]
265 pub(crate) fn op_cmp(&mut self, mode: Mode) {
266 let address = self.ea(mode);
267 let operand = self.load(address);
268 self.compare(self.a, operand);
269 }
270
271 #[inline(always)]
273 pub(crate) fn op_cpx(&mut self, mode: Mode) {
274 let address = self.ea(mode);
275 let operand = self.load(address);
276 self.compare(self.x, operand);
277 }
278
279 #[inline(always)]
281 pub(crate) fn op_cpy(&mut self, mode: Mode) {
282 let address = self.ea(mode);
283 let operand = self.load(address);
284 self.compare(self.y, operand);
285 }
286
287 fn bit_flags(&mut self, operand: u8, mask_with: u8) {
288 let mut p = (self.p & !(N | V | Z)) | (operand & (N | V));
290 if mask_with & operand == 0 {
291 p |= Z;
292 }
293 self.p = p;
294 }
295
296 #[inline(always)]
299 pub(crate) fn op_bit(&mut self, mode: Mode) {
300 let address = self.ea(mode);
301 let operand = self.load(address);
302 self.bit_flags(operand, self.a);
303 }
304}