diff --git a/examples/sdl/src/main.rs b/examples/sdl/src/main.rs index e2eae28905323dafdc354b9861cc7aca1271508a..38527b2afe5a0b426bf82a73cf2f1eec2fd2f627 100644 --- a/examples/sdl/src/main.rs +++ b/examples/sdl/src/main.rs @@ -6,5 +6,8 @@ fn main() { for _ in 0..40000 { game_boy.clock(); + if game_boy.cpu().pc() >= 0x0016 { + break; + } } } diff --git a/src/cpu.rs b/src/cpu.rs index edbf9447dc8ff72e98335745614221f58a06b456..972871688ced0f8152070c6e3b95026eee151c01 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -2,7 +2,7 @@ use crate::mmu::Mmu; pub const PREFIX: u8 = 0xcb; -pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 208] = [ +pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 256] = [ // 0x0 opcodes (nop, 4, "NOP"), (ld_bc_u16, 12, "LD BC, u16"), @@ -16,7 +16,7 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 208] = [ (add_hl_bc, 8, "ADD HL, BC"), (nop, 4, "NOP"), (nop, 4, "NOP"), - (nop, 4, "NOP"), + (inc_c, 4, "INC C"), (nop, 4, "NOP"), (ld_c_u8, 8, "LD C, u8"), (nop, 4, "NOP"), @@ -224,6 +224,57 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &'static str); 208] = [ (nop, 4, "NOP"), (nop, 4, "NOP"), (nop, 4, "NOP"), + // 0xd opcodes + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + // 0xe opcodes + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (ld_mff00c_a, 8, "LD [FF00+C], A"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + // 0xf opcodes + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), + (nop, 4, "NOP"), ]; pub const BITWISE: [(fn(&mut Cpu), u8, &'static str); 176] = [ @@ -486,6 +537,11 @@ impl Cpu { &mut self.mmu } + #[inline(always)] + pub fn pc(&self) -> u16 { + self.pc + } + #[inline(always)] fn af(&self) -> u16 { (self.a as u16) << 8 | self.f() as u16 @@ -660,6 +716,16 @@ fn add_hl_bc(cpu: &mut Cpu) { cpu.set_hl(value); } +fn inc_c(cpu: &mut Cpu) { + let value = cpu.c.wrapping_add(1); + + cpu.set_sub(false); + cpu.set_zero(value == 0); + cpu.set_half_carry((value & 0xf) == 0xf); + + cpu.c = value; +} + fn ld_c_u8(cpu: &mut Cpu) { let byte = cpu.read_u8(); cpu.c = byte; @@ -704,6 +770,10 @@ fn xor_a_a(cpu: &mut Cpu) { cpu.set_carry(false); } +fn ld_mff00c_a(cpu: &mut Cpu) { + cpu.mmu.write((0xff0c + cpu.c as u16), cpu.a); +} + fn bit_7_h(cpu: &mut Cpu) { bit_h(cpu, 7); } diff --git a/src/gb.rs b/src/gb.rs index 315e09cbe4166c5e9bd8269baa76ab67884c0d45..8b2a4a9ba336fa437f98231df8774f5939c2d0d3 100644 --- a/src/gb.rs +++ b/src/gb.rs @@ -20,6 +20,10 @@ impl GameBoy { self.cpu.clock() } + pub fn cpu(&self) -> &Cpu { + &self.cpu + } + pub fn load_boot(&mut self, path: &str) { let data = read_file(path); self.cpu.mmu().write_buffer(0x0000, &data);