From 57b8041aa6170879c4de7d8ad7910492c5a3f662 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Mon, 4 Jul 2022 15:23:38 +0100
Subject: [PATCH] feat: more instructions

---
 examples/sdl/src/main.rs |  2 +-
 src/cpu.rs               | 13 +++----
 src/inst.rs              | 84 ++++++++++++++++++++++++++++++++++++----
 3 files changed, 83 insertions(+), 16 deletions(-)

diff --git a/examples/sdl/src/main.rs b/examples/sdl/src/main.rs
index 22232e3d..198494db 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_sgb();
+    game_boy.load_boot_dmg();
     game_boy.load_rom_file("../../res/roms.prop/tetris.gb");
     //game_boy.load_rom_file("../../res/roms/firstwhite.gb");
     //game_boy.load_rom_file("../../res/roms/opus5.gb");
diff --git a/src/cpu.rs b/src/cpu.rs
index aa1e30a7..4862a4b7 100644
--- a/src/cpu.rs
+++ b/src/cpu.rs
@@ -108,13 +108,12 @@ impl Cpu {
 
         let (instruction_fn, instruction_time, instruction_str) = instruction;
 
-        // if !self.mmu.boot_active() {
-        //if *instruction_str == "! UNIMP !" {
-        println!(
-            "{}\t(0x{:02x})\t${:04x} {}",
-            instruction_str, opcode, pc, is_prefix
-        );
-        //}
+        if *instruction_str == "! UNIMP !" {
+            println!(
+                "{}\t(0x{:02x})\t${:04x} {}",
+                instruction_str, opcode, pc, is_prefix
+            );
+        }
 
         // calls the current instruction and increments the number of
         // cycles executed by the instruction time of the instruction
diff --git a/src/inst.rs b/src/inst.rs
index 123779bb..025bfcf3 100644
--- a/src/inst.rs
+++ b/src/inst.rs
@@ -172,14 +172,14 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [
     (noimpl, 4, "! UNIMP !"),
     (noimpl, 4, "! UNIMP !"),
     // 0xa opcodes
-    (noimpl, 4, "! UNIMP !"),
+    (and_a_b, 4, "AND A, B"),
     (and_a_c, 4, "AND A, C"),
-    (noimpl, 4, "! UNIMP !"),
-    (noimpl, 4, "! UNIMP !"),
-    (noimpl, 4, "! UNIMP !"),
-    (noimpl, 4, "! UNIMP !"),
-    (noimpl, 4, "! UNIMP !"),
-    (noimpl, 4, "! UNIMP !"),
+    (and_a_d, 4, "AND A, D"),
+    (and_a_e, 4, "AND A, E"),
+    (and_a_h, 4, "AND A, H"),
+    (and_a_l, 4, "AND A, L"),
+    (and_a_mhl, 4, "AND A, [HL]"),
+    (and_a_a, 4, "AND A, A"),
     (noimpl, 4, "! UNIMP !"),
     (xor_a_c, 4, "XOR A, C"),
     (noimpl, 4, "! UNIMP !"),
@@ -420,7 +420,7 @@ pub const EXTENDED: [(fn(&mut Cpu), u8, &'static str); 256] = [
     (noimpl, 4, "! UNIMP !"),
     (noimpl, 4, "! UNIMP !"),
     (noimpl, 4, "! UNIMP !"),
-    (noimpl, 4, "! UNIMP !"),
+    (res_0_a, 8, "RES 0, A"),
     (noimpl, 4, "! UNIMP !"),
     (noimpl, 4, "! UNIMP !"),
     (noimpl, 4, "! UNIMP !"),
@@ -1262,6 +1262,15 @@ fn sub_a_a(cpu: &mut Cpu) {
     cpu.a = sub_set_flags(cpu, cpu.a, cpu.a);
 }
 
+fn and_a_b(cpu: &mut Cpu) {
+    cpu.a &= cpu.b;
+
+    cpu.set_sub(false);
+    cpu.set_zero(cpu.a == 0);
+    cpu.set_half_carry(true);
+    cpu.set_carry(false);
+}
+
 fn and_a_c(cpu: &mut Cpu) {
     cpu.a &= cpu.c;
 
@@ -1271,6 +1280,61 @@ fn and_a_c(cpu: &mut Cpu) {
     cpu.set_carry(false);
 }
 
+fn and_a_d(cpu: &mut Cpu) {
+    cpu.a &= cpu.d;
+
+    cpu.set_sub(false);
+    cpu.set_zero(cpu.a == 0);
+    cpu.set_half_carry(true);
+    cpu.set_carry(false);
+}
+
+fn and_a_e(cpu: &mut Cpu) {
+    cpu.a &= cpu.e;
+
+    cpu.set_sub(false);
+    cpu.set_zero(cpu.a == 0);
+    cpu.set_half_carry(true);
+    cpu.set_carry(false);
+}
+
+fn and_a_h(cpu: &mut Cpu) {
+    cpu.a &= cpu.h;
+
+    cpu.set_sub(false);
+    cpu.set_zero(cpu.a == 0);
+    cpu.set_half_carry(true);
+    cpu.set_carry(false);
+}
+
+fn and_a_l(cpu: &mut Cpu) {
+    cpu.a &= cpu.l;
+
+    cpu.set_sub(false);
+    cpu.set_zero(cpu.a == 0);
+    cpu.set_half_carry(true);
+    cpu.set_carry(false);
+}
+
+fn and_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(true);
+    cpu.set_carry(false);
+}
+
+fn and_a_a(cpu: &mut Cpu) {
+    cpu.a &= cpu.a;
+
+    cpu.set_sub(false);
+    cpu.set_zero(cpu.a == 0);
+    cpu.set_half_carry(true);
+    cpu.set_carry(false);
+}
+
 fn xor_a_c(cpu: &mut Cpu) {
     cpu.a ^= cpu.c;
 
@@ -1669,6 +1733,10 @@ fn bit_7_h(cpu: &mut Cpu) {
     bit_h(cpu, 7);
 }
 
+fn res_0_a(cpu: &mut Cpu) {
+    cpu.a = res(cpu.a, 0);
+}
+
 fn res_7_mhl(cpu: &mut Cpu) {
     let hl = cpu.hl();
     let byte = cpu.mmu.read(hl);
-- 
GitLab