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:
commit
113f95afba
17
src/cpu.rs
17
src/cpu.rs
@ -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();
|
||||||
|
Loading…
Reference in New Issue
Block a user