From e46697c9c757597c7abfb130fdf3973e3307f15e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com> Date: Wed, 29 Jun 2022 10:43:01 +0100 Subject: [PATCH] feat: more opcodes --- examples/sdl/src/main.rs | 4 ++-- src/cpu.rs | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/examples/sdl/src/main.rs b/examples/sdl/src/main.rs index bd1aa0c5..93bf270e 100644 --- a/examples/sdl/src/main.rs +++ b/examples/sdl/src/main.rs @@ -4,9 +4,9 @@ fn main() { let mut game_boy = GameBoy::new(); game_boy.load_boot_default(); - for i in 0..24700 { + for i in 0..37000 { game_boy.clock(); - if game_boy.cpu().pc() >= 0x3032 { + if game_boy.cpu().pc() >= 0x6032 { println!("{}", i); break; } diff --git a/src/cpu.rs b/src/cpu.rs index 84ea6bdc..02f7d692 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -35,7 +35,7 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [ (nop, 4, "! UNIMP !"), (nop, 4, "! UNIMP !"), (nop, 4, "! UNIMP !"), - (nop, 4, "! UNIMP !"), + (ld_e_u8, 8, "LD E, u8"), (nop, 4, "! UNIMP !"), // 0x2 opcodes (jr_nz_i8, 8, "JR NZ, i8"), @@ -68,7 +68,7 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [ (nop, 4, "! UNIMP !"), (nop, 4, "! UNIMP !"), (nop, 4, "! UNIMP !"), - (nop, 4, "! UNIMP !"), + (dec_a, 4, "DEC A"), (ld_a_u8, 8, "LD A, u8"), (nop, 4, "! UNIMP !"), // 0x4 opcodes @@ -786,6 +786,11 @@ fn ld_a_mde(cpu: &mut Cpu) { cpu.a = byte; } +fn ld_e_u8(cpu: &mut Cpu) { + let byte = cpu.read_u8(); + cpu.e = byte; +} + fn jr_nz_i8(cpu: &mut Cpu) { let byte = cpu.read_u8() as i8; @@ -820,6 +825,16 @@ fn ld_mhld_a(cpu: &mut Cpu) { cpu.set_hl(cpu.hl().wrapping_sub(1)); } +fn dec_a(cpu: &mut Cpu) { + let value = cpu.a.wrapping_sub(1); + + cpu.set_sub(true); + cpu.set_zero(value == 0); + cpu.set_half_carry((value & 0xf) == 0xf); + + cpu.a = value; +} + fn ld_a_u8(cpu: &mut Cpu) { let byte = cpu.read_u8(); cpu.a = byte; @@ -934,7 +949,7 @@ fn sub_set_flags(cpu: &mut Cpu, x: u8, y: u8) -> u8 { cpu.set_carry(value & 0x100 == 0x100); cpu.set_zero(value_b == 0); cpu.set_half_carry((x ^ y ^ value) & 0x10 == 0x10); - + value_b } -- GitLab