Skip to content

Commit

Permalink
Decode ld r8, [hl] opcode
Browse files Browse the repository at this point in the history
This opcode is treated differently from ld r8, n8
because it requires an extra machine cycle to pull
data from memory.
  • Loading branch information
notskm committed Aug 2, 2024
1 parent 24ed106 commit 5cc7fba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl SharpSM83 {
Opcode::NOP => self.no_op(),
Opcode::LDRI8(dest) => self.ld_r_n8(dest, bus),
Opcode::Unimplemented(_) => {}
_ => {}
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
pub enum Opcode {
NOP,
LDRI8(Dest8Bit),
LdR8HLAddr,
Unimplemented(u8),
}

Expand All @@ -39,7 +40,11 @@ impl Opcode {
(0b00000000, 0b00000110) => {
let reg_num = (data & 0b00111000) >> 3;
let register = Dest8Bit::from_u8(reg_num);
Opcode::LDRI8(register)
if register == Dest8Bit::HLAddr {
Opcode::LdR8HLAddr
} else {
Opcode::LDRI8(register)
}
}
_ => Opcode::Unimplemented(data),
}
Expand Down Expand Up @@ -125,6 +130,12 @@ mod tests {
assert_eq!(opcode, Opcode::LDRI8(destination));
}

#[test]
fn should_return_ld_r8_addr_hl_given_00110110() {
let opcode = Opcode::decode(0b00110110);
assert_eq!(opcode, Opcode::LdR8HLAddr);
}

#[test]
fn should_return_b_when_given_0() {
let register = Dest8Bit::from_u8(0);
Expand Down

0 comments on commit 5cc7fba

Please sign in to comment.