1
0
mirror of https://github.com/mre/mos6502.git synced 2024-11-28 07:49:19 +00:00

Merge pull request #57 from mre/overflow_panic

Add or subtract: some values result in panics
This commit is contained in:
omarandlorraine 2022-10-23 09:46:48 +01:00 committed by GitHub
commit 113f95afba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -629,7 +629,8 @@ impl CPU {
let over = let over =
((nc == 0 && value < 0) || (nc == 1 && value < -1)) && a_before >= 0 && a_after < 0; ((nc == 0 && value < 0) || (nc == 1 && value < -1)) && a_before >= 0 && a_after < 0;
let under = (a_before < 0) && (-value - nc < 0) && a_after >= 0; let under =
(a_before < 0) && (0i8.wrapping_sub(value).wrapping_sub(nc) < 0) && a_after >= 0;
let did_overflow = over || under; let did_overflow = over || under;
@ -837,6 +838,20 @@ mod tests {
use super::*; use super::*;
use num::range_inclusive; use num::range_inclusive;
#[test]
fn dont_panic_for_overflow() {
let mut cpu = CPU::new();
cpu.add_with_carry(-128);
assert_eq!(cpu.registers.accumulator, -128);
cpu.add_with_carry(-128);
assert_eq!(cpu.registers.accumulator, 0);
cpu.subtract_with_carry(-128);
assert_eq!(cpu.registers.accumulator, -128);
cpu.subtract_with_carry(-128);
assert_eq!(cpu.registers.accumulator, 0);
}
#[cfg_attr(feature = "decimal_mode", test)] #[cfg_attr(feature = "decimal_mode", test)]
fn decimal_add_test() { fn decimal_add_test() {
let mut cpu = CPU::new(); let mut cpu = CPU::new();