From 2473aa8fc42a266e5002f66fc25e65504de7ec5c 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 22:23:27 +0100
Subject: [PATCH] feat: new instructions Added boot roms to separate dir New
 static boot roms

---
 .../res/dmg_rom.bin => res/boot/dmg_boot.bin  | Bin
 res/boot/sgb_boot.bin                         | Bin 0 -> 256 bytes
 res/dmg_rom.bin                               | Bin 256 -> 0 bytes
 src/data.rs                                   |  31 +++++++++++++++
 src/gb.rs                                     |  23 ++---------
 src/inst.rs                                   |  36 +++++++++++++++---
 src/lib.rs                                    |   1 +
 7 files changed, 67 insertions(+), 24 deletions(-)
 rename examples/sdl/res/dmg_rom.bin => res/boot/dmg_boot.bin (100%)
 create mode 100644 res/boot/sgb_boot.bin
 delete mode 100644 res/dmg_rom.bin
 create mode 100644 src/data.rs

diff --git a/examples/sdl/res/dmg_rom.bin b/res/boot/dmg_boot.bin
similarity index 100%
rename from examples/sdl/res/dmg_rom.bin
rename to res/boot/dmg_boot.bin
diff --git a/res/boot/sgb_boot.bin b/res/boot/sgb_boot.bin
new file mode 100644
index 0000000000000000000000000000000000000000..2bece74809a9d8ed6d3b92e021093c69c28a94fe
GIT binary patch
literal 256
zcmXr~_utOo0mFL5|MQJb*C_l}RQt~-XxCu$h{x{pBO|-=a=Skd+!f;w@Nuj+;#K$~
z=+9{Pn~&`)8-tXzQG+|L!Vja5MtpqNnEt(2cqPcfs3_1Nb@npD*((gf)&EQsJ_|l$
zVB?SyR#IkF_-QBkN?@j<{!Bg|TMZ5)UWK1}{1Tt-CO&XbWH`XbV8`&t&fpQ7fE<Tb
z_-Q9Q0R<*I1(3*TDOrX0tO_q6yt9WGYX2oDT;db9JNSslj{T8@l^Dwhmwyu!e#tS&
uD*P2y_;k{rjpgWRLGgp91;tqvzA7mzDW9})+PQS+QYRY*6kx~rAOHZGby)NO

literal 0
HcmV?d00001

diff --git a/res/dmg_rom.bin b/res/dmg_rom.bin
deleted file mode 100644
index afa0ee4792c2ba80afb6b0c1962e249e195e6fc0..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001

literal 256
zcmXr~_kX?O|M^CzYZQJfs{Q8^v}-VW#AEmQk&#__x!s=!?t(0giUJK%XQwipoyH(s
z{m(?<v)~N|HV!FaC1qBHpLUY31ZFDg&*bB=)!;DVRrsmLFY!6uE+zbdlikDz4lHs^
zd^{gq{!LK$#jEgLR^bz$uu6?w^S>kwHo1fUQWV&#AMx0=Kl<P_;enH=!X;J(e=!A#
z)BbEMM^6iiA3QB6&Z_WLNm)tx<hi$LX=ivDxS5L?ni+T)co{eZ<vThV`0kv^dv^Ek
z&6&G*rzhv}y*YFD&dm9{cN*K-IPF}zbE%V!A`7G73I-|ReG31SYyX)jd}WiYXj5iY
N_}!-P&yMjy008b+Vs8Kd

diff --git a/src/data.rs b/src/data.rs
new file mode 100644
index 00000000..244483f5
--- /dev/null
+++ b/src/data.rs
@@ -0,0 +1,31 @@
+/// Static data corresponding to the DMG boot ROM
+/// allows freely using the emulator without external dependency.
+pub const DMB_BOOT: [u8; 256] = [
+    49, 254, 255, 175, 33, 255, 159, 50, 203, 124, 32, 251, 33, 38, 255, 14, 17, 62, 128, 50, 226,
+    12, 62, 243, 226, 50, 62, 119, 119, 62, 252, 224, 71, 17, 4, 1, 33, 16, 128, 26, 205, 149, 0,
+    205, 150, 0, 19, 123, 254, 52, 32, 243, 17, 216, 0, 6, 8, 26, 19, 34, 35, 5, 32, 249, 62, 25,
+    234, 16, 153, 33, 47, 153, 14, 12, 61, 40, 8, 50, 13, 32, 249, 46, 15, 24, 243, 103, 62, 100,
+    87, 224, 66, 62, 145, 224, 64, 4, 30, 2, 14, 12, 240, 68, 254, 144, 32, 250, 13, 32, 247, 29,
+    32, 242, 14, 19, 36, 124, 30, 131, 254, 98, 40, 6, 30, 193, 254, 100, 32, 6, 123, 226, 12, 62,
+    135, 226, 240, 66, 144, 224, 66, 21, 32, 210, 5, 32, 79, 22, 32, 24, 203, 79, 6, 4, 197, 203,
+    17, 23, 193, 203, 17, 23, 5, 32, 245, 34, 35, 34, 35, 201, 206, 237, 102, 102, 204, 13, 0, 11,
+    3, 115, 0, 131, 0, 12, 0, 13, 0, 8, 17, 31, 136, 137, 0, 14, 220, 204, 110, 230, 221, 221, 217,
+    153, 187, 187, 103, 99, 110, 14, 236, 204, 221, 220, 153, 159, 187, 185, 51, 62, 60, 66, 185,
+    165, 185, 165, 66, 60, 33, 4, 1, 17, 168, 0, 26, 19, 190, 32, 254, 35, 125, 254, 52, 32, 245,
+    6, 25, 120, 134, 35, 5, 32, 251, 134, 32, 254, 62, 1, 224, 80,
+];
+
+pub const SGB_BOOT: [u8; 256] = [
+    49, 254, 255, 62, 48, 224, 0, 175, 33, 255, 159, 50, 203, 124, 32, 251, 33, 38, 255, 14, 17,
+    62, 128, 50, 226, 12, 62, 243, 226, 50, 62, 119, 119, 62, 252, 224, 71, 33, 95, 192, 14, 8,
+    175, 50, 13, 32, 252, 17, 79, 1, 62, 251, 14, 6, 245, 6, 0, 26, 27, 50, 128, 71, 13, 32, 248,
+    50, 241, 50, 14, 14, 214, 2, 254, 239, 32, 234, 17, 4, 1, 33, 16, 128, 26, 205, 211, 0, 205,
+    212, 0, 19, 123, 254, 52, 32, 243, 17, 230, 0, 6, 8, 26, 19, 34, 35, 5, 32, 249, 62, 25, 234,
+    16, 153, 33, 47, 153, 14, 12, 61, 40, 8, 50, 13, 32, 249, 46, 15, 24, 243, 62, 145, 224, 64,
+    33, 0, 192, 14, 0, 62, 0, 226, 62, 48, 226, 6, 16, 30, 8, 42, 87, 203, 66, 62, 16, 32, 2, 62,
+    32, 226, 62, 48, 226, 203, 26, 29, 32, 239, 5, 32, 232, 62, 32, 226, 62, 48, 226, 205, 194, 0,
+    125, 254, 96, 32, 210, 14, 19, 62, 193, 226, 12, 62, 7, 226, 24, 58, 22, 4, 240, 68, 254, 144,
+    32, 250, 30, 0, 29, 32, 253, 21, 32, 242, 201, 79, 6, 4, 197, 203, 17, 23, 193, 203, 17, 23, 5,
+    32, 245, 34, 35, 34, 35, 201, 60, 66, 185, 165, 185, 165, 66, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 62, 1, 224, 80,
+];
diff --git a/src/gb.rs b/src/gb.rs
index 46292902..7a0ac812 100644
--- a/src/gb.rs
+++ b/src/gb.rs
@@ -1,5 +1,6 @@
 use crate::{
     cpu::Cpu,
+    data::SGB_BOOT,
     mmu::Mmu,
     ppu::{Ppu, FRAME_BUFFER_SIZE},
     util::read_file,
@@ -8,23 +9,6 @@ use crate::{
 #[cfg(feature = "wasm")]
 use wasm_bindgen::prelude::*;
 
-/// Static data corresponding to the DMG boot ROM
-/// allows freely using the emulator without external dependency.
-pub const BOOT_DATA: [u8; 256] = [
-    49, 254, 255, 175, 33, 255, 159, 50, 203, 124, 32, 251, 33, 38, 255, 14, 17, 62, 128, 50, 226,
-    12, 62, 243, 226, 50, 62, 119, 119, 62, 252, 224, 71, 17, 4, 1, 33, 16, 128, 26, 205, 149, 0,
-    205, 150, 0, 19, 123, 254, 52, 32, 243, 17, 216, 0, 6, 8, 26, 19, 34, 35, 5, 32, 249, 62, 25,
-    234, 16, 153, 33, 47, 153, 14, 12, 61, 40, 8, 50, 13, 32, 249, 46, 15, 24, 243, 103, 62, 100,
-    87, 224, 66, 62, 145, 224, 64, 4, 30, 2, 14, 12, 240, 68, 254, 144, 32, 250, 13, 32, 247, 29,
-    32, 242, 14, 19, 36, 124, 30, 131, 254, 98, 40, 6, 30, 193, 254, 100, 32, 6, 123, 226, 12, 62,
-    135, 226, 240, 66, 144, 224, 66, 21, 32, 210, 5, 32, 79, 22, 32, 24, 203, 79, 6, 4, 197, 203,
-    17, 23, 193, 203, 17, 23, 5, 32, 245, 34, 35, 34, 35, 201, 206, 237, 102, 102, 204, 13, 0, 11,
-    3, 115, 0, 131, 0, 12, 0, 13, 0, 8, 17, 31, 136, 137, 0, 14, 220, 204, 110, 230, 221, 221, 217,
-    153, 187, 187, 103, 99, 110, 14, 236, 204, 221, 220, 153, 159, 187, 185, 51, 62, 60, 66, 185,
-    165, 185, 165, 66, 60, 33, 4, 1, 17, 168, 0, 26, 19, 190, 32, 254, 35, 125, 254, 52, 32, 245,
-    6, 25, 120, 134, 35, 5, 32, 251, 134, 32, 254, 62, 1, 224, 80,
-];
-
 /// Top level structure that abstracts the usage of the
 /// Game Boy system under the Boytacean emulator.
 /// Should serve as the main entry-point API.
@@ -76,15 +60,16 @@ impl GameBoy {
 
     pub fn load_boot_file(&mut self, path: &str) {
         let data = read_file(path);
+        println!("{:?}", data);
         self.load_boot(&data);
     }
 
     pub fn load_boot_default(&mut self) {
-        self.load_boot_file("./res/dmg_rom.bin");
+        self.load_boot_file("./res/boot/dmg_boot.bin");
     }
 
     pub fn load_boot_static(&mut self) {
-        self.load_boot(&BOOT_DATA);
+        self.load_boot(&SGB_BOOT);
     }
 
     pub fn frame_buffer_eager(&mut self) -> Vec<u8> {
diff --git a/src/inst.rs b/src/inst.rs
index f5b6cb94..401d3e40 100644
--- a/src/inst.rs
+++ b/src/inst.rs
@@ -30,7 +30,7 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [
     (jr_i8, 12, "JR i8"),
     (noimpl, 4, "! UNIMP !"),
     (ld_a_mde, 8, "LD A, [DE]"),
-    (noimpl, 4, "! UNIMP !"),
+    (dec_de, 8, "DEC DE"),
     (inc_e, 4, "INC E"),
     (dec_e, 4, "DEC E"),
     (ld_e_u8, 8, "LD E, u8"),
@@ -71,8 +71,8 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [
     (noimpl, 4, "! UNIMP !"),
     // 0x4 opcodes
     (ld_b_b, 4, "LD B, B"),
-    (noimpl, 4, "! UNIMP !"),
-    (noimpl, 4, "! UNIMP !"),
+    (ld_b_c, 4, "LD B, C"),
+    (ld_b_d, 4, "LD B, D"),
     (ld_b_e, 4, "LD B, E"),
     (ld_b_h, 4, "LD B, H"),
     (noimpl, 4, "! UNIMP !"),
@@ -138,7 +138,7 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [
     (ld_a_mhl, 8, "LD A, [HL]"),
     (noimpl, 4, "! UNIMP !"),
     // 0x8 opcodes
-    (noimpl, 4, "! UNIMP !"),
+    (add_a_b, 4, "ADD A, B"),
     (add_a_c, 4, "ADD A, C"),
     (noimpl, 4, "! UNIMP !"),
     (add_a_e, 4, "ADD A, E"),
@@ -347,7 +347,7 @@ pub const EXTENDED: [(fn(&mut Cpu), u8, &'static str); 256] = [
     // 0x4 opcodes
     (noimpl, 4, "! UNIMP !"),
     (noimpl, 4, "! UNIMP !"),
-    (noimpl, 4, "! UNIMP !"),
+    (bit_0_d, 8, "BIT 0, D"),
     (noimpl, 4, "! UNIMP !"),
     (noimpl, 4, "! UNIMP !"),
     (noimpl, 4, "! UNIMP !"),
@@ -708,6 +708,10 @@ fn ld_a_mde(cpu: &mut Cpu) {
     cpu.a = byte;
 }
 
+fn dec_de(cpu: &mut Cpu) {
+    cpu.set_de(cpu.de().wrapping_sub(1));
+}
+
 fn inc_e(cpu: &mut Cpu) {
     let value = cpu.e.wrapping_add(1);
 
@@ -919,6 +923,14 @@ fn ld_b_b(cpu: &mut Cpu) {
     cpu.b = cpu.b;
 }
 
+fn ld_b_c(cpu: &mut Cpu) {
+    cpu.b = cpu.c;
+}
+
+fn ld_b_d(cpu: &mut Cpu) {
+    cpu.b = cpu.d;
+}
+
 fn ld_b_e(cpu: &mut Cpu) {
     cpu.b = cpu.e;
 }
@@ -1011,6 +1023,10 @@ fn ld_a_mhl(cpu: &mut Cpu) {
     cpu.a = byte;
 }
 
+fn add_a_b(cpu: &mut Cpu) {
+    cpu.a = add_set_flags(cpu, cpu.a, cpu.b);
+}
+
 fn add_a_c(cpu: &mut Cpu) {
     cpu.a = add_set_flags(cpu, cpu.a, cpu.c);
 }
@@ -1349,6 +1365,10 @@ fn srl_b(cpu: &mut Cpu) {
     cpu.b = srl(cpu, cpu.b);
 }
 
+fn bit_0_d(cpu: &mut Cpu) {
+    bit_d(cpu, 0);
+}
+
 fn bit_7_h(cpu: &mut Cpu) {
     bit_h(cpu, 7);
 }
@@ -1414,6 +1434,12 @@ fn bit_zero(val: u8, bit: u8) -> bool {
     (val & (1u8 << (bit as usize))) == 0
 }
 
+fn bit_d(cpu: &mut Cpu, bit: u8) {
+    cpu.set_sub(false);
+    cpu.set_zero(bit_zero(cpu.d, bit));
+    cpu.set_half_carry(true);
+}
+
 fn bit_h(cpu: &mut Cpu, bit: u8) {
     cpu.set_sub(false);
     cpu.set_zero(bit_zero(cpu.h, bit));
diff --git a/src/lib.rs b/src/lib.rs
index d585d8ec..23d5ff7a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,5 @@
 pub mod cpu;
+pub mod data;
 pub mod gb;
 pub mod inst;
 pub mod mmu;
-- 
GitLab