From aeb136292c7ae3ab6177dc0f86b7f9d9d2b4ccd4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Sun, 3 Jul 2022 23:47:03 +0100
Subject: [PATCH] feat: new instructions

---
 examples/sdl/src/main.rs |  2 +-
 src/inst.rs              | 32 +++++++++++++++++++++++++++++---
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/examples/sdl/src/main.rs b/examples/sdl/src/main.rs
index a6ccfbed..6d00e8f0 100644
--- a/examples/sdl/src/main.rs
+++ b/examples/sdl/src/main.rs
@@ -79,7 +79,7 @@ fn main() {
         .unwrap();
 
     let mut game_boy = GameBoy::new();
-    game_boy.load_boot_dmg();
+    game_boy.load_boot_sgb();
     //game_boy.load_rom_file("../../res/roms/firstwhite.gb");
     //game_boy.load_rom_file("../../res/roms/opus5.gb");
     game_boy.load_rom_file("../../res/roms/ld_r_r.gb");
diff --git a/src/inst.rs b/src/inst.rs
index 14912baf..0eeacf64 100644
--- a/src/inst.rs
+++ b/src/inst.rs
@@ -58,7 +58,7 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [
     (ld_mhld_a, 8, "LD [HL-], A"),
     (noimpl, 4, "! UNIMP !"),
     (noimpl, 4, "! UNIMP !"),
-    (noimpl, 4, "! UNIMP !"),
+    (dec_mhl, 12, "DEC [HL]"),
     (ld_mhl_u8, 12, "LD [HL], u8 "),
     (scf, 4, "SCF"),
     (jr_c_i8, 8, "JR C, i8"),
@@ -118,7 +118,7 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [
     (noimpl, 4, "! UNIMP !"),
     (noimpl, 4, "! UNIMP !"),
     (noimpl, 4, "! UNIMP !"),
-    (noimpl, 4, "! UNIMP !"),
+    (ld_l_mhl, 8, "LD L, [HL]"),
     (noimpl, 4, "! UNIMP !"),
     // 0x7 opcodes
     (ld_mhl_b, 8, "LD [HL], B"),
@@ -195,7 +195,7 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [
     (noimpl, 4, "! UNIMP !"),
     (noimpl, 4, "! UNIMP !"),
     (noimpl, 4, "! UNIMP !"),
-    (noimpl, 4, "! UNIMP !"),
+    (or_a_mhl, 8, "OR A, [HL]"),
     (or_a_a, 4, "OR A, A"),
     (noimpl, 4, "! UNIMP !"),
     (noimpl, 4, "! UNIMP !"),
@@ -885,6 +885,17 @@ fn ld_mhld_a(cpu: &mut Cpu) {
     cpu.set_hl(cpu.hl().wrapping_sub(1));
 }
 
+fn dec_mhl(cpu: &mut Cpu) {
+    let byte = cpu.mmu.read(cpu.hl());
+    let value = byte.wrapping_sub(1);
+
+    cpu.set_sub(true);
+    cpu.set_zero(value == 0);
+    cpu.set_half_carry((byte & 0xf) == 0xf);
+
+    cpu.mmu.write(cpu.hl(), value);
+}
+
 fn ld_mhl_u8(cpu: &mut Cpu) {
     let byte = cpu.read_u8();
     cpu.mmu.write(cpu.hl(), byte);
@@ -1012,6 +1023,11 @@ fn ld_h_a(cpu: &mut Cpu) {
     cpu.h = cpu.a;
 }
 
+fn ld_l_mhl(cpu: &mut Cpu) {
+    let byte = cpu.mmu.read(cpu.hl());
+    cpu.l = byte;
+}
+
 fn ld_mhl_b(cpu: &mut Cpu) {
     cpu.mmu.write(cpu.hl(), cpu.b);
 }
@@ -1141,6 +1157,16 @@ fn or_a_c(cpu: &mut Cpu) {
     cpu.set_carry(false);
 }
 
+fn or_a_mhl(cpu: &mut Cpu) {
+    let byte = cpu.mmu.read(cpu.hl());
+    cpu.a |= byte;
+
+    cpu.set_sub(false);
+    cpu.set_zero(cpu.a == 0);
+    cpu.set_half_carry(false);
+    cpu.set_carry(false);
+}
+
 fn or_a_a(cpu: &mut Cpu) {
     cpu.a |= cpu.a;
 
-- 
GitLab