mirror of
https://github.com/FranLMSP/rmg-001.git
synced 2024-11-23 10:12:11 +00:00
Functions and tests for detecting half carry
This commit is contained in:
parent
b2190d1868
commit
724cb27c49
@ -769,8 +769,10 @@ mod tests {
|
|||||||
// INC
|
// INC
|
||||||
let mut cpu = CPU::new();
|
let mut cpu = CPU::new();
|
||||||
cpu.registers.set(Register::A, 0);
|
cpu.registers.set(Register::A, 0);
|
||||||
cpu.exec(Opcode::INC(false, Register::A), &mut bus);
|
cpu.exec(Opcode::INC(true, Register::A), &mut bus);
|
||||||
assert_eq!(cpu.registers.get(Register::A), 1);
|
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);
|
assert_eq!(cpu.registers.get(Register::PC), 0x101);
|
||||||
|
|
||||||
// DEC
|
// DEC
|
||||||
|
24
src/utils.rs
24
src/utils.rs
@ -37,6 +37,18 @@ pub fn join_bytes(byte1: u8, byte2: u8) -> u16 {
|
|||||||
((byte1 as u16) << 8) | (byte2 as 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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -94,4 +106,16 @@ mod tests {
|
|||||||
assert_eq!(join_bytes(0b10101010, 0b11111111), 0b1010101011111111);
|
assert_eq!(join_bytes(0b10101010, 0b11111111), 0b1010101011111111);
|
||||||
assert_eq!(join_bytes(0b11111111, 0b10101010), 0b1111111110101010);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user