Skip to content

Commit

Permalink
implement jr_nz
Browse files Browse the repository at this point in the history
  • Loading branch information
pcasaretto committed Apr 7, 2024
1 parent 6c7f525 commit e083c00
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/instructions/jmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ pub fn jmp_a16() -> impl Fn(&mut CPU) {
}
}

pub fn jr_nz() -> impl Fn(&mut CPU) {
move |cpu: &mut CPU| {
if !cpu.registers.f.zero {
return;
}
let offset = cpu.bus.memory[cpu.pc as usize];
cpu.pc = cpu.pc.wrapping_add(offset as i8 as u16);
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -20,4 +30,34 @@ mod tests {
jmp_a16()(&mut cpu);
assert_eq!(cpu.pc, 0x0201);
}

#[test]
fn test_jr_nz_flag_unset() {
let mut cpu = CPU::default();
cpu.pc = 0x1000;
cpu.registers.f.zero = false;
cpu.bus.memory[0x1000] = 0x01;
jr_nz()(&mut cpu);
assert_eq!(cpu.pc, 0x1000);
}

#[test]
fn test_jr_nz_flag_set() {
let mut cpu = CPU::default();
cpu.pc = 0x1000;
cpu.registers.f.zero = true;
cpu.bus.memory[0x1000] = 0x05;
jr_nz()(&mut cpu);
assert_eq!(cpu.pc, 0x1005);
}

#[test]
fn test_jr_nz_flag_set_signed_negative() {
let mut cpu = CPU::default();
cpu.pc = 0x1000;
cpu.registers.f.zero = true;
cpu.bus.memory[0x1005] = -5i8 as u8;
jr_nz()(&mut cpu);
assert_eq!(cpu.pc, 0x1000);
}
}
2 changes: 2 additions & 0 deletions src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub fn from_byte(byte: u8) -> Box<dyn Fn(&mut CPU)> {
0x2C => Box::new(inc::inc_r(RegisterTarget::L)),
0x3C => Box::new(inc::inc_r(RegisterTarget::A)),

0x20 => Box::new(jmp::jr_nz()),

0x21 => Box::new(ld::ld_d16_u16(Register16bTarget::HL)),
0x06 => Box::new(ld::ld_d8_u8(RegisterTarget::B)),
0x16 => Box::new(ld::ld_d8_u8(RegisterTarget::B)),
Expand Down

0 comments on commit e083c00

Please sign in to comment.