From 7e1db05f697a42b8f239b4e0ced894af7951e2bb 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:50:06 +0100 Subject: [PATCH] feat: more instructions --- examples/sdl/src/main.rs | 3 ++- src/gb.rs | 2 +- src/inst.rs | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/examples/sdl/src/main.rs b/examples/sdl/src/main.rs index 6d2b8990..a6ccfbed 100644 --- a/examples/sdl/src/main.rs +++ b/examples/sdl/src/main.rs @@ -81,7 +81,8 @@ fn main() { let mut game_boy = GameBoy::new(); game_boy.load_boot_dmg(); //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/opus5.gb"); + game_boy.load_rom_file("../../res/roms/ld_r_r.gb"); let mut counter = 0; diff --git a/src/gb.rs b/src/gb.rs index 026f6ce6..ca34032c 100644 --- a/src/gb.rs +++ b/src/gb.rs @@ -1,6 +1,6 @@ use crate::{ cpu::Cpu, - data::{SGB_BOOT, DMG_BOOT}, + data::{DMG_BOOT, SGB_BOOT}, mmu::Mmu, ppu::{Ppu, FRAME_BUFFER_SIZE}, util::read_file, diff --git a/src/inst.rs b/src/inst.rs index e66596fb..14912baf 100644 --- a/src/inst.rs +++ b/src/inst.rs @@ -214,13 +214,13 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [ (push_bc, 16, "PUSH BC"), (add_a_u8, 8, "ADD A, u8"), (noimpl, 4, "! UNIMP !"), - (noimpl, 4, "! UNIMP !"), + (ret_z, 8, "RET Z"), (ret, 16, "RET"), (jp_z_u16, 12, "JP Z, u16"), (noimpl, 4, "! UNIMP !"), (call_z_u16, 12, "CALL Z, u16"), (call_u16, 24, "CALL u16"), - (noimpl, 4, "! UNIMP !"), + (adc_a_u8, 8, "ADC A, u8 "), (rst_08h, 16, "RST 08h"), // 0xd opcodes (ret_nc, 8, "RET NC"), @@ -1206,6 +1206,15 @@ fn add_a_u8(cpu: &mut Cpu) { cpu.a = add_set_flags(cpu, cpu.a, byte); } +fn ret_z(cpu: &mut Cpu) { + if !cpu.get_zero() { + return; + } + + cpu.pc = cpu.pop_word(); + cpu.ticks = cpu.ticks.wrapping_add(12); +} + fn ret(cpu: &mut Cpu) { cpu.pc = cpu.pop_word(); } @@ -1239,6 +1248,11 @@ fn call_u16(cpu: &mut Cpu) { cpu.pc = word; } +fn adc_a_u8(cpu: &mut Cpu) { + let byte = cpu.read_u8(); + cpu.a = add_carry_set_flags(cpu, cpu.a, byte); +} + fn rst_08h(cpu: &mut Cpu) { rst(cpu, 0x0008); } @@ -1499,6 +1513,22 @@ fn add_set_flags(cpu: &mut Cpu, first: u8, second: u8) -> u8 { result_b } +fn add_carry_set_flags(cpu: &mut Cpu, first: u8, second: u8) -> u8 { + let first = first as u32; + let second = second as u32; + let carry = cpu.get_carry() as u32; + + let result = first.wrapping_add(second).wrapping_add(carry); + let result_b = result as u8; + + cpu.set_sub(false); + cpu.set_zero(result_b == 0); + cpu.set_half_carry((first ^ second ^ result) & 0x10 == 0x10); + cpu.set_carry(result & 0x100 == 0x100); + + result_b +} + fn sub_set_flags(cpu: &mut Cpu, first: u8, second: u8) -> u8 { let first = first as u32; let second = second as u32; -- GitLab