Skip to content

Commit

Permalink
implement ld_d8_u8 family
Browse files Browse the repository at this point in the history
  • Loading branch information
pcasaretto committed Apr 7, 2024
1 parent fb27d4d commit 87e7f90
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/instructions/ld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ pub fn ld_d16_u16(reg: Register16bTarget) -> impl Fn(&mut CPU) {
}
}

pub fn ld_d8_u8(reg: RegisterTarget) -> impl Fn(&mut CPU) {
move |cpu: &mut CPU| {
let value = cpu.bus.memory[(cpu.pc + 1) as usize];
cpu.registers.set_u8(reg, value);
cpu.pc += 2;
}
}

pub fn ld_r_r(src: RegisterTarget, dest: RegisterTarget) -> impl Fn(&mut CPU) {
move |cpu: &mut CPU| {
let value = cpu.registers.get_u8(src);
Expand Down Expand Up @@ -46,4 +54,13 @@ mod tests {
assert_eq!(cpu.registers.get_u8(RegisterTarget::A), 0x01);
assert_eq!(cpu.pc, 1);
}

#[test]
fn test_d8_u8() {
let mut cpu = CPU::default();
cpu.bus.memory[1] = 0x01;
ld_d8_u8(RegisterTarget::B)(&mut cpu);
assert_eq!(cpu.registers.get_u8(RegisterTarget::B), 0x01);
assert_eq!(cpu.pc, 2);
}
}
6 changes: 6 additions & 0 deletions src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ use super::*;
pub fn from_byte(byte: u8) -> Box<dyn Fn(&mut CPU)> {
match byte {
0x00 => Box::new(nop::nop()),
0x06 => Box::new(ld::ld_d8_u8(RegisterTarget::B)),
0x16 => Box::new(ld::ld_d8_u8(RegisterTarget::B)),
0x26 => Box::new(ld::ld_d8_u8(RegisterTarget::B)),
0x0E => Box::new(ld::ld_d8_u8(RegisterTarget::C)),
0x1E => Box::new(ld::ld_d8_u8(RegisterTarget::C)),
0x2E => Box::new(ld::ld_d8_u8(RegisterTarget::C)),
0x01 => Box::new(ld::ld_d16_u16(Register16bTarget::BC)),
0x11 => Box::new(ld::ld_d16_u16(Register16bTarget::DE)),
0x21 => Box::new(ld::ld_d16_u16(Register16bTarget::HL)),
Expand Down

0 comments on commit 87e7f90

Please sign in to comment.