Skip to content

Commit

Permalink
overflow in add, cary flag sbc
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurinZ committed Jun 2, 2024
1 parent 3c59d46 commit e106b9f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/cpu/instructions/arithmetic_and_logic/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ impl CPU {
} else {
0
};
let (result, overflow) = target_value.overflowing_add(value + carry);

let (partial, overflow) = value.overflowing_add(carry);
let (result, overflow2) = target_value.overflowing_add(partial);
let overflow = overflow || overflow2;

self.set_8bit_register(target, result);

ConditionCodes {
Expand Down
5 changes: 4 additions & 1 deletion src/cpu/instructions/arithmetic_and_logic/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ impl CPU {
},
subtract: FlagState::Set,
half_carry: if ((a ^ value) & 0x10) != (result & 0x10) {FlagState::Set} else {FlagState::Unset},
carry: if value > a {FlagState::Set} else {FlagState::Unset},
carry: if add_carry {
if value.wrapping_add(carry) > a {FlagState::Set} else {FlagState::Unset}
}else {
if value > a {FlagState::Set} else {FlagState::Unset}},
},
}
}
Expand Down

0 comments on commit e106b9f

Please sign in to comment.