Skip to content

Commit ede1fec

Browse files
committed
Fix adc and sbb
1 parent d852a1c commit ede1fec

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

examples/mult.asm

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
call [_start]
2+
3+
/// math = ./math
4+
@include <math/mul.asm>
5+
6+
_start:
7+
push 5, 0, 120, 0
8+
call [mul16]
9+
pop H, L
10+
halt

src/emulator.rs

+39-8
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,39 @@ bitflags! {
287287
}
288288
}
289289

290+
trait Notified {
291+
fn notified_add(self, other: Self, sreg: &mut SReg) -> Self;
292+
fn notified_sub(self, other: Self, sreg: &mut SReg) -> Self;
293+
}
294+
295+
impl Notified for u8 {
296+
fn notified_add(self, other: Self, sreg: &mut SReg) -> Self {
297+
match self.checked_add(other) {
298+
Some(val) => {
299+
sreg.remove(SReg::C);
300+
val
301+
}
302+
None => {
303+
sreg.insert(SReg::C);
304+
self.wrapping_add(other)
305+
}
306+
}
307+
}
308+
309+
fn notified_sub(self, other: Self, sreg: &mut SReg) -> Self {
310+
match self.checked_sub(other) {
311+
Some(val) => {
312+
sreg.remove(SReg::C);
313+
val
314+
},
315+
None => {
316+
sreg.insert(SReg::C);
317+
self.wrapping_sub(other)
318+
}
319+
}
320+
}
321+
}
322+
290323
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
291324
struct Alu {
292325
primary: u8,
@@ -295,19 +328,17 @@ struct Alu {
295328

296329
impl Alu {
297330
fn compute(&self, aol: bool, aom: bool, aoh: bool, sreg: &mut SReg) -> u8 {
298-
let previous = self.primary;
299-
300331
match (aoh, aom, aol) {
301-
(false, false, false) => self.primary.wrapping_add(self.secondary),
302-
(false, false, true) => self.primary.wrapping_sub(self.secondary),
332+
(false, false, false) => self.primary.notified_add(self.secondary, sreg),
333+
(false, false, true) => self.primary.notified_sub(self.secondary, sreg),
303334
(false, true, false) => self
304335
.primary
305-
.wrapping_add(self.secondary)
306-
.wrapping_add(sreg.contains(SReg::C) as u8),
336+
.notified_add(self.secondary, sreg)
337+
.notified_add(sreg.contains(SReg::C) as u8, sreg),
307338
(false, true, true) => self
308339
.primary
309-
.wrapping_sub(self.secondary)
310-
.wrapping_sub(sreg.contains(SReg::C) as u8),
340+
.notified_sub(self.secondary, sreg)
341+
.notified_sub(sreg.contains(SReg::C) as u8, sreg),
311342
(true, false, false) => !(self.primary & self.secondary),
312343
(true, false, true) => self.primary | self.secondary,
313344
_ => 0x00,

0 commit comments

Comments
 (0)