-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
391e13d
commit 5d2c54c
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use crate::{RegisterTarget, CPU}; | ||
|
||
pub fn dec_r(target: RegisterTarget) -> impl Fn(&mut CPU) { | ||
move |cpu: &mut CPU| { | ||
let current_value = cpu.read_single_register(target); | ||
let (new_value, did_overflow) = current_value.overflowing_sub(1); | ||
cpu.registers.set_u8(target, new_value); | ||
|
||
cpu.registers.f.zero = new_value == 0; | ||
cpu.registers.f.subtract = true; | ||
cpu.registers.f.half_carry = current_value & 0x10 == 0x10; | ||
cpu.registers.f.carry = did_overflow; | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::{FlagsRegister, Registers}; | ||
|
||
#[test] | ||
fn test_dec_r() { | ||
let mut cpu = CPU { | ||
registers: Registers { | ||
b: 2, | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}; | ||
dec_r(RegisterTarget::B)(&mut cpu); | ||
assert_eq!(cpu.registers.b, 1); | ||
} | ||
|
||
#[test] | ||
fn test_dec_r_overflow() { | ||
let mut cpu = CPU { | ||
registers: Registers { | ||
b: 0, | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}; | ||
dec_r(RegisterTarget::B)(&mut cpu); | ||
assert_eq!(cpu.registers.b, 0xFF); | ||
} | ||
|
||
#[test] | ||
fn test_dec_r_subtract_flag() { | ||
let mut cpu = CPU { | ||
registers: Registers { | ||
f: FlagsRegister { | ||
subtract: false, | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}; | ||
dec_r(RegisterTarget::B)(&mut cpu); | ||
assert!(cpu.registers.f.subtract); | ||
} | ||
|
||
#[test] | ||
fn test_dec_r_half_carry_flag() { | ||
let mut cpu = CPU { | ||
registers: Registers { | ||
b: 0x10, | ||
f: FlagsRegister::from(0), | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}; | ||
dec_r(RegisterTarget::B)(&mut cpu); | ||
assert!(cpu.registers.f.half_carry); | ||
} | ||
|
||
#[test] | ||
fn test_dec_r_carry_flag() { | ||
let mut cpu = CPU { | ||
registers: Registers { | ||
b: 0, | ||
f: FlagsRegister::from(0), | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}; | ||
dec_r(RegisterTarget::B)(&mut cpu); | ||
assert!(cpu.registers.f.carry); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters