Functions and tests for detecting half carry

This commit is contained in:
Franco Colmenarez 2021-10-14 23:32:48 -05:00
parent b2190d1868
commit 724cb27c49
2 changed files with 28 additions and 2 deletions

View File

@ -769,8 +769,10 @@ mod tests {
// INC
let mut cpu = CPU::new();
cpu.registers.set(Register::A, 0);
cpu.exec(Opcode::INC(false, Register::A), &mut bus);
assert_eq!(cpu.registers.get(Register::A), 1);
cpu.exec(Opcode::INC(true, Register::A), &mut bus);
assert_eq!(cpu.registers.get_flag(FlagRegister::Zero), false);
assert_eq!(cpu.registers.get_flag(FlagRegister::Substract), false);
assert_eq!(cpu.registers.get_flag(FlagRegister::HalfCarry), false);
assert_eq!(cpu.registers.get(Register::PC), 0x101);
// DEC

View File

@ -37,6 +37,18 @@ pub fn join_bytes(byte1: u8, byte2: u8) -> u16 {
((byte1 as u16) << 8) | (byte2 as u16)
}
pub fn add_half_carry(byte1: u8, byte2: u8) -> bool {
let byte1 = byte1 & 0b00001111;
let byte2 = byte2 & 0b00001111;
get_bit(byte1 + byte2, BitIndex::I4)
}
pub fn sub_half_carry(byte1: u8, byte2: u8) -> bool {
let byte1 = byte1 & 0b00001111;
let byte2 = byte2 & 0b00001111;
byte2 > byte1
}
#[cfg(test)]
mod tests {
use super::*;
@ -94,4 +106,16 @@ mod tests {
assert_eq!(join_bytes(0b10101010, 0b11111111), 0b1010101011111111);
assert_eq!(join_bytes(0b11111111, 0b10101010), 0b1111111110101010);
}
#[test]
fn test_half_carry() {
assert_eq!(add_half_carry(0b10101010, 0b11111111), true);
assert_eq!(add_half_carry(0b00000100, 0b00001100), true);
assert_eq!(add_half_carry(0b00000100, 0b00000100), false);
assert_eq!(add_half_carry(0b00000100, 0b00001000), false);
assert_eq!(sub_half_carry(0b00010000, 0b00001000), true);
assert_eq!(sub_half_carry(0b00000000, 0b00000001), true);
assert_eq!(sub_half_carry(0b00001000, 0b00001000), false);
}
}