From 388e3e05788e80bd4520780c371f9a9683e5a70e 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 17:05:07 +0100
Subject: [PATCH] feat: new test for jumps

---
 examples/sdl/src/main.rs          |   3 +-
 res/roms/07-jr,jp,call,ret,rst.gb | Bin 0 -> 32768 bytes
 src/inst.rs                       |  57 ++++++++++++++++++++++++++----
 3 files changed, 52 insertions(+), 8 deletions(-)
 create mode 100644 res/roms/07-jr,jp,call,ret,rst.gb

diff --git a/examples/sdl/src/main.rs b/examples/sdl/src/main.rs
index 22232e3d..1a34b02f 100644
--- a/examples/sdl/src/main.rs
+++ b/examples/sdl/src/main.rs
@@ -80,7 +80,8 @@ fn main() {
 
     let mut game_boy = GameBoy::new();
     game_boy.load_boot_sgb();
-    game_boy.load_rom_file("../../res/roms.prop/tetris.gb");
+    //game_boy.load_rom_file("../../res/roms.prop/tetris.gb");
+    game_boy.load_rom_file("../../res/roms/07-jr,jp,call,ret,rst.gb");
     //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/res/roms/07-jr,jp,call,ret,rst.gb b/res/roms/07-jr,jp,call,ret,rst.gb
new file mode 100644
index 0000000000000000000000000000000000000000..5c8d20bb3a1a0458f29afe40b8d3ca125b13a391
GIT binary patch
literal 32768
zcmeI4Z){W76~K?3#H7JEf3-bALS7(vAxefRTZy#StF%K!)0(C02P#!X7!@8ZwUQ7J
zIK=jxWa_$U8m%_9>pqlK>)0P{FeYsjvJZ9~+j%pOkq+1ck>t4$P%WC-pf?f&_Rf6{
ztm~$Hm^2Ah=jq*h-?{hvx##!pz0aR4DPUgG|EVo_e~9Zywe`%$vV3|OEG^m#!(fMH
zP+HmW+IPWm`E>WS$;pdvkBsbkvD<Ou^yKBY509MOz&@2t5M_(mCwG7IQ)3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`OMs|GN}Tyo;UZw7&1Ab`i?F@HdK
zavr<^1m>1(J^^Hp;Bv=ag}TS;+I!@$fZc94^&E9*SLcA<c5K^w0c0sJ$v7kk0d!cn
zTrM|01QsL!w;QZBJ38zDN$%b+%Mu9OUI?Jf2LceV3w9g!+v_Ax0AmH*J{y-Mx7)|{
z16<AJLVcOvyk$0fJCDonh1LZ;ic8>g#qfCJGq)_n!F)0P?rt{&0UZQn8GN$LZ|rxw
z-RRfrk!9KA#rSTY+l})wTQ@MbyBk9Sj-r_i5}&>xfsJ2ihc>-vi>dM}0I}KJg;*>G
z^8P>o4f%ZWlFWFhV9NVFkC`%t<nbl$2O;u{$HB{tCrgq5=<7mm7X8T*pE@7sQz&r3
zcq>h_mC(Vnw^_k!%3i2w_rxB&2Y_72?OG5e8$<slUd$gjX*bV@nQ>8;11Q(Ie2AUf
zU_Y}V6U2q_1!%_z_#TlyUcCs<!}D)9MG5t9Z~)jA^tnHte?j)SO@HW+%bW+weBt4H
z<OCcj^Dz96p12u*!3e;|*QyDi(2w)Mmu#*dhakAPof#gv^a_F<r-gd%o9`5sE<OXO
z9dZ8q#<j6+cVFx7-qshldpxauJNLEr?cG<#fBhJ`g$?RfgO&d4QyQ6zt7qft?-RJE
zokH%xCNe!zMFfZd5g-CYfCvx)B0vO)01+SpM1Tko0U|&IhyW2F0z`la5CI}U1c(3;
uAOb{y2oM1xKm>>Y5g-CYfCvx)B0vO)01+SpM1Tko0U|&Ih`?u;z&`;#5pP@o

literal 0
HcmV?d00001

diff --git a/src/inst.rs b/src/inst.rs
index 025bfcf3..3983bbb8 100644
--- a/src/inst.rs
+++ b/src/inst.rs
@@ -213,7 +213,7 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [
     (call_nz_u16, 12, "CALL NZ, u16"),
     (push_bc, 16, "PUSH BC"),
     (add_a_u8, 8, "ADD A, u8"),
-    (noimpl, 4, "! UNIMP !"),
+    (rst_00h, 16, "RST 00h"),
     (ret_z, 8, "RET Z"),
     (ret, 16, "RET"),
     (jp_z_u16, 12, "JP Z, u16"),
@@ -225,12 +225,12 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [
     // 0xd opcodes
     (ret_nc, 8, "RET NC"),
     (pop_de, 12, "POP DE"),
-    (noimpl, 4, "! UNIMP !"),
+    (jp_nc_u16, 12, "JP NC, u16"),
     (illegal, 4, "ILLEGAL"),
-    (noimpl, 4, "! UNIMP !"),
+    (call_nc_u16, 12, "CALL NC, u16 "),
     (push_de, 16, "PUSH DE"),
     (sub_a_u8, 8, "SUB A, u8"),
-    (noimpl, 4, "! UNIMP !"),
+    (rst_10h, 16, "RST 10h"),
     (ret_c, 8, "RET C"),
     (reti, 16, "RETI"),
     (jp_c_u16, 12, "JP C, u16"),
@@ -247,7 +247,7 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [
     (illegal, 4, "ILLEGAL"),
     (push_hl, 16, "PUSH HL"),
     (and_a_u8, 8, "AND A, u8"),
-    (noimpl, 4, "! UNIMP !"),
+    (rst_20h, 16, "RST 20h"),
     (noimpl, 4, "! UNIMP !"),
     (jp_hl, 4, "JP HL"),
     (ld_mu16_a, 16, "LD [u16], A"),
@@ -264,9 +264,9 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [
     (illegal, 4, "ILLEGAL"),
     (push_af, 16, "PUSH AF"),
     (noimpl, 4, "! UNIMP !"),
+    (rst_30h, 16, "RST 30h"),
     (noimpl, 4, "! UNIMP !"),
-    (noimpl, 4, "! UNIMP !"),
-    (noimpl, 4, "! UNIMP !"),
+    (ld_sp_hl, 8, "LD SP, HL"),
     (ld_a_mu16, 16, "LD A [u16]"),
     (ei, 4, "EI"),
     (illegal, 4, "ILLEGAL"),
@@ -1493,6 +1493,10 @@ fn add_a_u8(cpu: &mut Cpu) {
     cpu.a = add_set_flags(cpu, cpu.a, byte);
 }
 
+fn rst_00h(cpu: &mut Cpu) {
+    rst(cpu, 0x0000);
+}
+
 fn ret_z(cpu: &mut Cpu) {
     if !cpu.get_zero() {
         return;
@@ -1558,6 +1562,29 @@ fn pop_de(cpu: &mut Cpu) {
     cpu.set_de(word);
 }
 
+fn jp_nc_u16(cpu: &mut Cpu) {
+    let word = cpu.read_u16();
+
+    if cpu.get_carry() {
+        return;
+    }
+
+    cpu.pc = word;
+    cpu.ticks = cpu.ticks.wrapping_add(4);
+}
+
+fn call_nc_u16(cpu: &mut Cpu) {
+    let word = cpu.read_u16();
+
+    if cpu.get_carry() {
+        return;
+    }
+
+    cpu.push_word(cpu.pc);
+    cpu.pc = word;
+    cpu.ticks = cpu.ticks.wrapping_add(12);
+}
+
 fn push_de(cpu: &mut Cpu) {
     cpu.push_word(cpu.de());
 }
@@ -1567,6 +1594,10 @@ fn sub_a_u8(cpu: &mut Cpu) {
     cpu.a = sub_set_flags(cpu, cpu.a, byte);
 }
 
+fn rst_10h(cpu: &mut Cpu) {
+    rst(cpu, 0x0010);
+}
+
 fn ret_c(cpu: &mut Cpu) {
     if !cpu.get_carry() {
         return;
@@ -1637,6 +1668,10 @@ fn and_a_u8(cpu: &mut Cpu) {
     cpu.set_carry(false);
 }
 
+fn rst_20h(cpu: &mut Cpu) {
+    rst(cpu, 0x0020);
+}
+
 fn jp_hl(cpu: &mut Cpu) {
     cpu.pc = cpu.hl();
 }
@@ -1678,6 +1713,14 @@ fn push_af(cpu: &mut Cpu) {
     cpu.push_word(cpu.af());
 }
 
+fn rst_30h(cpu: &mut Cpu) {
+    rst(cpu, 0x0030);
+}
+
+fn ld_sp_hl(cpu: &mut Cpu) {
+    cpu.sp = cpu.hl();
+}
+
 fn ld_a_mu16(cpu: &mut Cpu) {
     let word = cpu.read_u16();
     let byte = cpu.mmu.read(word);
-- 
GitLab