From 669e7565c9048e0504a4d301e0a3c514abb7205e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com> Date: Fri, 1 Jul 2022 18:44:58 +0100 Subject: [PATCH] feat: better structure of instruction execution --- examples/sdl/src/main.rs | 2 +- src/cpu.rs | 35 +++++++++++++++++++++++++++-------- src/mmu.rs | 2 +- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/examples/sdl/src/main.rs b/examples/sdl/src/main.rs index 4a56914a..98732d67 100644 --- a/examples/sdl/src/main.rs +++ b/examples/sdl/src/main.rs @@ -79,7 +79,7 @@ fn main() { let mut counter = 0; 'main: loop { - if counter >= 7000000 { + if counter >= 700000000 { break; } diff --git a/src/cpu.rs b/src/cpu.rs index 96f5e9f9..4ad39386 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -26,7 +26,7 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [ (noimpl, 4, "! UNIMP !"), (inc_de, 8, "INC DE"), (noimpl, 4, "! UNIMP !"), - (noimpl, 4, "! UNIMP !"), + (dec_d, 4, "DEC D"), (noimpl, 4, "! UNIMP !"), (rla, 4, "RLA"), (jr_i8, 12, "JR i8"), @@ -157,7 +157,7 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [ (noimpl, 4, "! UNIMP !"), (noimpl, 4, "! UNIMP !"), // 0x9 opcodes - (noimpl, 4, "! UNIMP !"), + (sub_a_b, 4, "SUB A, B"), (noimpl, 4, "! UNIMP !"), (noimpl, 4, "! UNIMP !"), (noimpl, 4, "! UNIMP !"), @@ -527,10 +527,16 @@ impl Cpu { let (instruction_fn, instruction_time, instruction_str) = instruction; - 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 + ); + } + + if pc == 0x0080 { + println!("GOING TO PLAY BOOT SOUND"); + } instruction_fn(self); self.ticks = self.ticks.wrapping_add(*instruction_time as u32); @@ -802,6 +808,16 @@ fn inc_de(cpu: &mut Cpu) { cpu.set_de(cpu.de().wrapping_add(1)); } +fn dec_d(cpu: &mut Cpu) { + let value = cpu.d.wrapping_sub(1); + + cpu.set_sub(true); + cpu.set_zero(value == 0); + cpu.set_half_carry((value & 0xf) == 0xf); + + cpu.d = value; +} + fn rla(cpu: &mut Cpu) { let carry = cpu.get_carry(); @@ -942,6 +958,10 @@ fn ld_a_h(cpu: &mut Cpu) { cpu.a = cpu.h; } +fn sub_a_b(cpu: &mut Cpu) { + cpu.a = sub_set_flags(cpu, cpu.a, cpu.b); +} + fn xor_a_a(cpu: &mut Cpu) { cpu.a ^= cpu.a; @@ -1041,12 +1061,11 @@ fn bit_h(cpu: &mut Cpu, bit: u8) { } fn sub_set_flags(cpu: &mut Cpu, x: u8, y: u8) -> u8 { - // Check for borrow using 32bit arithmetics + // checks for borrow using 32bit arithmetics let x = x as u32; let y = y as u32; let value = x.wrapping_sub(y); - let value_b = value as u8; cpu.set_sub(true); diff --git a/src/mmu.rs b/src/mmu.rs index 8c79e05e..3dcec160 100644 --- a/src/mmu.rs +++ b/src/mmu.rs @@ -148,7 +148,7 @@ impl Mmu { self.ppu.write(addr, value); } _ => { - println!("Writing to Unknown IO control 0x{:04x}", addr); + println!("Writing to unknown IO control 0x{:04x}", addr); } } } -- GitLab