feat: comply with test 03 of blargg's cpu_instrs test rom

This commit is contained in:
Rekai Nyangadzayi Musuka 2021-04-07 02:27:57 -05:00
parent 6db132ad48
commit 067ed03de3
2 changed files with 26 additions and 20 deletions

View File

@ -84,9 +84,9 @@ impl Cpu {
} }
pub fn step(&mut self) -> Cycle { pub fn step(&mut self) -> Cycle {
// if !self.bus.boot_enabled() { if !self.bus.boot_enabled() {
// self.log_state().unwrap(); self.log_state().unwrap();
// } }
let cycles = match self.halted() { let cycles = match self.halted() {
Some(state) => { Some(state) => {

View File

@ -392,7 +392,10 @@ impl Instruction {
// ADD SP, d | Add d (is signed) to register pair SP. // ADD SP, d | Add d (is signed) to register pair SP.
let mut flags: Flags = *cpu.flags(); let mut flags: Flags = *cpu.flags();
let d = d as i8; let d = d as i8;
let sum = Self::add_u16_i8(cpu.register_pair(RegisterPair::SP), d, &mut flags); let sum = Self::add_u16_i8(cpu.register_pair(RegisterPair::SP), d, &mut flags);
cpu.set_flags(flags);
cpu.set_register_pair(RegisterPair::SP, sum); cpu.set_register_pair(RegisterPair::SP, sum);
Cycle::new(16) Cycle::new(16)
} }
@ -442,7 +445,7 @@ impl Instruction {
match pair { match pair {
BC | DE | HL | SP => { BC | DE | HL | SP => {
let value = cpu.register_pair(pair); let value = cpu.register_pair(pair);
cpu.set_register_pair(pair, value + 1); cpu.set_register_pair(pair, value.wrapping_add(1));
} }
_ => unreachable!("There is no \"INC {:?}\" instruction", pair), _ => unreachable!("There is no \"INC {:?}\" instruction", pair),
} }
@ -484,7 +487,7 @@ impl Instruction {
match pair { match pair {
BC | DE | HL | SP => { BC | DE | HL | SP => {
let value = cpu.register_pair(pair); let value = cpu.register_pair(pair);
cpu.set_register_pair(pair, value - 1); cpu.set_register_pair(pair, value.wrapping_sub(1));
} }
_ => unreachable!("There is no \"DEC {:?}\" instruction", pair), _ => unreachable!("There is no \"DEC {:?}\" instruction", pair),
}; };
@ -1474,7 +1477,7 @@ impl Instruction {
flags.set_z(diff == 0); flags.set_z(diff == 0);
flags.set_n(true); flags.set_n(true);
flags.set_h(Self::sub_u8_half_carry(left, right)); flags.set_h(Self::bit_4_borrow(left, right));
diff diff
} }
@ -1485,21 +1488,25 @@ impl Instruction {
flags.update( flags.update(
diff == 0, diff == 0,
true, true,
Self::sub_u8_half_carry(left, right), Self::bit_4_borrow(left, right),
did_overflow, did_overflow,
); );
diff diff
} }
fn add_u16_i8_no_flags(left: u16, right: i8) -> u16 { fn add_u16_i8_no_flags(left: u16, right: i8) -> u16 {
(left as i16 + right as i16) as u16 if right < 0 {
left.wrapping_sub((!right + 1) as u16)
} else {
left.wrapping_add(right as u16)
}
} }
fn add_u16_i8(left: u16, right: i8, flags: &mut Flags) -> u16 { fn add_u16_i8(left: u16, right: i8, flags: &mut Flags) -> u16 {
let (sum, did_overflow) = left.overflowing_add(right as u16); let (_, did_overflow) = (left as u8).overflowing_add(right as u8);
let sum = Self::add_u16_i8_no_flags(left, right);
// FIXME: Is this more correct? let half_carry = Self::bit_3_overflow(left as u8, right as u8);
let half_carry = Self::add_u8_half_carry((left >> 8) as u8, right as u8);
flags.update(false, false, half_carry, did_overflow); flags.update(false, false, half_carry, did_overflow);
sum sum
} }
@ -1509,7 +1516,7 @@ impl Instruction {
flags.set_z(sum == 0); flags.set_z(sum == 0);
flags.set_n(false); flags.set_n(false);
flags.set_h(Self::add_u8_half_carry(left, right)); flags.set_h(Self::bit_3_overflow(left, right));
sum sum
} }
@ -1519,7 +1526,7 @@ impl Instruction {
flags.update( flags.update(
sum == 0, sum == 0,
false, false,
Self::add_u8_half_carry(left, right), Self::bit_3_overflow(left, right),
did_overflow, did_overflow,
); );
sum sum
@ -1529,23 +1536,22 @@ impl Instruction {
let (sum, did_overflow) = left.overflowing_add(right); let (sum, did_overflow) = left.overflowing_add(right);
flags.set_n(false); flags.set_n(false);
flags.set_h(Self::add_u16_half_carry(left, right)); flags.set_h(Self::bit_11_overflow(left, right));
flags.set_c(did_overflow); flags.set_c(did_overflow);
sum sum
} }
fn add_u16_half_carry(left: u16, right: u16) -> bool { fn bit_11_overflow(left: u16, right: u16) -> bool {
Self::add_u8_half_carry((left >> 8) as u8, (right >> 8) as u8) (((left & 0x0FFF) + (right & 0x0FFF)) & 0x1000) == 0x1000
// left + right > 0xFFF // Thanks @Nectar Boy#1003
} }
fn add_u8_half_carry(left: u8, right: u8) -> bool { fn bit_3_overflow(left: u8, right: u8) -> bool {
(((left & 0xF) + (right & 0xF)) & 0x10) == 0x10 (((left & 0xF) + (right & 0xF)) & 0x10) == 0x10
} }
fn sub_u8_half_carry(left: u8, right: u8) -> bool { fn bit_4_borrow(left: u8, right: u8) -> bool {
(left & 0xF) < (right & 0xF) (left & 0x0F) < (right & 0x0F)
} }
fn rl_thru_carry(byte: u8, carry: bool) -> (u8, bool) { fn rl_thru_carry(byte: u8, carry: bool) -> (u8, bool) {