Skip to content

Commit e16ea75

Browse files
committed
Actually fix adc and sbb :)
1 parent ede1fec commit e16ea75

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/emulator.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -288,28 +288,34 @@ bitflags! {
288288
}
289289

290290
trait Notified {
291-
fn notified_add(self, other: Self, sreg: &mut SReg) -> Self;
292-
fn notified_sub(self, other: Self, sreg: &mut SReg) -> Self;
291+
fn notified_add(self, other: Self, sreg: &mut SReg, remove: bool) -> Self;
292+
fn notified_sub(self, other: Self, sreg: &mut SReg, remove: bool) -> Self;
293293
}
294294

295295
impl Notified for u8 {
296-
fn notified_add(self, other: Self, sreg: &mut SReg) -> Self {
296+
fn notified_add(self, other: Self, sreg: &mut SReg, remove: bool) -> Self {
297+
println!("notified; check: {:?}", self.checked_add(other));
297298
match self.checked_add(other) {
298299
Some(val) => {
299-
sreg.remove(SReg::C);
300+
if remove {
301+
sreg.remove(SReg::C);
302+
}
300303
val
301304
}
302305
None => {
306+
println!("insert");
303307
sreg.insert(SReg::C);
304308
self.wrapping_add(other)
305309
}
306310
}
307311
}
308312

309-
fn notified_sub(self, other: Self, sreg: &mut SReg) -> Self {
313+
fn notified_sub(self, other: Self, sreg: &mut SReg, remove: bool) -> Self {
310314
match self.checked_sub(other) {
311315
Some(val) => {
312-
sreg.remove(SReg::C);
316+
if remove {
317+
sreg.remove(SReg::C);
318+
}
313319
val
314320
},
315321
None => {
@@ -329,16 +335,16 @@ struct Alu {
329335
impl Alu {
330336
fn compute(&self, aol: bool, aom: bool, aoh: bool, sreg: &mut SReg) -> u8 {
331337
match (aoh, aom, aol) {
332-
(false, false, false) => self.primary.notified_add(self.secondary, sreg),
333-
(false, false, true) => self.primary.notified_sub(self.secondary, sreg),
338+
(false, false, false) => self.primary.notified_add(self.secondary, sreg, true),
339+
(false, false, true) => self.primary.notified_sub(self.secondary, sreg, true),
334340
(false, true, false) => self
335341
.primary
336-
.notified_add(self.secondary, sreg)
337-
.notified_add(sreg.contains(SReg::C) as u8, sreg),
342+
.notified_add(sreg.contains(SReg::C) as u8, sreg, true)
343+
.notified_add(self.secondary, sreg, false),
338344
(false, true, true) => self
339345
.primary
340-
.notified_sub(self.secondary, sreg)
341-
.notified_sub(sreg.contains(SReg::C) as u8, sreg),
346+
.notified_sub(sreg.contains(SReg::C) as u8, sreg, true)
347+
.notified_sub(self.secondary, sreg, false),
342348
(true, false, false) => !(self.primary & self.secondary),
343349
(true, false, true) => self.primary | self.secondary,
344350
_ => 0x00,
@@ -352,17 +358,17 @@ impl Alu {
352358
sreg.insert(SReg::L);
353359
sreg.remove(SReg::E);
354360
sreg.remove(SReg::G);
355-
},
361+
}
356362
Ordering::Equal => {
357363
sreg.remove(SReg::L);
358364
sreg.insert(SReg::E);
359365
sreg.remove(SReg::G);
360-
},
366+
}
361367
Ordering::Greater => {
362368
sreg.remove(SReg::L);
363369
sreg.remove(SReg::E);
364370
sreg.insert(SReg::G);
365-
},
371+
}
366372
},
367373
(false, true, false) => sreg.set(SReg::Z, self.primary == 0),
368374
(false, true, true) => self.primary = bus,
@@ -662,9 +668,7 @@ impl State {
662668
} else if cw.contains(ControlWord::LA) {
663669
match self.addr {
664670
0x0000..=0xF7FF => self.mem[self.addr as usize],
665-
0xF800..=0xFFCF => {
666-
self.text_buffer.get(self.addr - 0xF800)
667-
}
671+
0xF800..=0xFFCF => self.text_buffer.get(self.addr - 0xF800),
668672
0xFFD0..=0xFFFC => match self.peripherals.get(&((self.addr - 0xFFC0) as u8)) {
669673
Some(periph) => unsafe {
670674
if let Ok(stateful_read) =
@@ -817,7 +821,6 @@ impl State {
817821

818822
self.timer = self.timer.wrapping_add(1);
819823

820-
821824
if cw.contains(ControlWord::CR) {
822825
self.ctrl.clock = 0;
823826
} else {

0 commit comments

Comments
 (0)