Skip to content

Commit

Permalink
Implement load from hl pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
joajfreitas committed Oct 3, 2023
1 parent ff2232d commit a03aaf8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
10 changes: 0 additions & 10 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
use std::thread;

use fpt::lr35902::LR35902;

use winit::{
event::{Event, WindowEvent},
event_loop::EventLoop,
window::WindowBuilder,
};

fn main() {
let mut lr = LR35902::new();

loop {
lr.step();
}

}

14 changes: 7 additions & 7 deletions src/lr35902.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ impl LR35902 {
}
0x46 => {
// LD B,(HL)
unimplemented!()
self.set_b(self.mem8(self.hl()));
}
0x47 => {
// LD B,A
Expand Down Expand Up @@ -580,7 +580,7 @@ impl LR35902 {
}
0x4E => {
// LD C,(HL)
unimplemented!()
self.set_c(self.mem8(self.hl()));
}
0x4F => {
// LD C,A
Expand Down Expand Up @@ -612,7 +612,7 @@ impl LR35902 {
}
0x56 => {
// LD D,(HL)
unimplemented!()
self.set_d(self.mem8(self.hl()));
}
0x57 => {
// LD D,A
Expand Down Expand Up @@ -644,7 +644,7 @@ impl LR35902 {
}
0x5E => {
// LD E,(HL)
unimplemented!()
self.set_e(self.mem8(self.hl()));
}
0x5F => {
// LD E,A
Expand Down Expand Up @@ -676,7 +676,7 @@ impl LR35902 {
}
0x66 => {
// LD H,(HL)
unimplemented!()
self.set_h(self.mem8(self.hl()));
}
0x67 => {
// LD H,A
Expand Down Expand Up @@ -708,7 +708,7 @@ impl LR35902 {
}
0x6E => {
// LD L,(HL)
unimplemented!()
self.set_l(self.mem8(self.hl()));
}
0x6F => {
// LD L,A
Expand Down Expand Up @@ -772,7 +772,7 @@ impl LR35902 {
}
0x7E => {
// LD A,(HL)
unimplemented!()
self.set_a(self.mem8(self.hl()));
}
0x7F => {
// LD A,A
Expand Down
54 changes: 54 additions & 0 deletions tests/lr35902.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,20 @@ fn test_instr_0x032_ld_hld_a(#[case] a: u8, #[case] hl: u16) {
#[case(0x6d, "l", "l", 0xFF)] // 82
#[case(0x6f, "l", "a", 0x01)] // 83
#[case(0x6f, "l", "a", 0xFF)] // 84
#[case(0x78, "a", "b", 0x01)] // 85
#[case(0x78, "a", "b", 0xFF)] // 86
#[case(0x79, "a", "c", 0x01)] // 87
#[case(0x79, "a", "c", 0xFF)] // 88
#[case(0x7a, "a", "d", 0x01)] // 89
#[case(0x7a, "a", "d", 0xFF)] // 90
#[case(0x7b, "a", "e", 0x01)] // 91
#[case(0x7b, "a", "e", 0xFF)] // 92
#[case(0x7c, "a", "h", 0x01)] // 93
#[case(0x7c, "a", "h", 0xFF)] // 94
#[case(0x7d, "a", "l", 0x01)] // 95
#[case(0x7d, "a", "l", 0xFF)] // 96
#[case(0x7f, "a", "a", 0x01)] // 97
#[case(0x7f, "a", "a", 0xFF)] // 98
fn test_load_8_bit_reg_to_8_bit_reg(
#[case] opcode: u8,
#[case] dst_reg: &str,
Expand All @@ -365,6 +379,46 @@ fn test_load_8_bit_reg_to_8_bit_reg(
assert_eq!(sut, expected);
}

#[rstest]
#[case(0x46, "b", 0x0100, 0x01)]
#[case(0x46, "b", 0x0100, 0xFF)]
#[case(0x4E, "c", 0x0100, 0x01)]
#[case(0x4E, "c", 0x0100, 0xFF)]
#[case(0x56, "d", 0x0100, 0x01)]
#[case(0x56, "d", 0x0100, 0xFF)]
#[case(0x5e, "e", 0x0100, 0x01)]
#[case(0x5e, "e", 0x0100, 0xFF)]
#[case(0x66, "h", 0x0100, 0x01)]
#[case(0x66, "h", 0x0100, 0xFF)]
#[case(0x6E, "l", 0x0100, 0x01)]
#[case(0x6E, "l", 0x0100, 0xFF)]
#[case(0x7E, "a", 0x0100, 0x01)]
#[case(0x7E, "a", 0x0100, 0xFF)]
fn test_load_8_bit_reg_from_hl_pointer(
#[case] opcode: u8,
#[case] dst_reg: &str,
#[case] hl: u16,
#[case] value: u8,
) {
// Given
let builder = LR35902Builder::new()
.with_memory_byte(0x0000, opcode)
.with_memory_byte(hl, value)
.with_hl(hl);
let mut sut = builder.clone().build();

// When
sut.step();

// Then
let expected = builder
.with_pc(1)
.with_clock_cycles(8)
.with_reg8(dst_reg, value) // hl gets decremented
.build();
assert_eq!(sut, expected);
}

#[rstest]
#[case(0x80, "b", 0xfe, 0x01, 0xff, 0b0000)] // no flags
#[case(0x80, "b", 0x0f, 0x01, 0x10, 0b0010)] // half carry
Expand Down

0 comments on commit a03aaf8

Please sign in to comment.