From 038777e90c4530387259c54dd95d98d40da861be Mon Sep 17 00:00:00 2001 From: Jonathan Balls Date: Tue, 3 Sep 2024 21:28:15 +0100 Subject: [PATCH] emulate the joypad (no buttons pressed) --- src/gameboy.rs | 47 ++++++++++++++++++++++++++++++++++++++--------- src/renderer.rs | 17 +++++++++++++++++ 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/gameboy.rs b/src/gameboy.rs index cff7739..f5c6e0d 100644 --- a/src/gameboy.rs +++ b/src/gameboy.rs @@ -20,6 +20,9 @@ pub struct GameBoy { ime: bool, ie: u8, + // joypad + joypad: u8, + // timers div: u8, tima: u8, @@ -34,12 +37,14 @@ impl GameBoy { pub fn new(rom_data: Vec, tx: Sender) -> GameBoy { GameBoy { boot_rom_enabled: true, + debugger_enabled: false, registers: Registers::new(), rom_data, ram: [0x0; 0xFFFF], ppu: PPU::new(tx), - debugger_enabled: false, + + joypad: 0, ime: false, ie: 0, @@ -284,6 +289,15 @@ impl GameBoy { self.registers.f.half_carry = (value & 0x0F) == 0x0F; // Hmmmm... self.registers.f.subtract = true; } + Instruction::SubAImm8(value) => { + let result = self.registers.a.wrapping_sub(value); + self.registers.a = result; + + self.registers.f.zero = result == 0; + self.registers.f.carry = value > self.registers.a; + self.registers.f.half_carry = (value & 0x0F) == 0x0F; // Hmmmm... + self.registers.f.subtract = true; + } Instruction::AddAR8(reg) => { let value = self.get_r8_byte(reg); @@ -293,7 +307,7 @@ impl GameBoy { self.registers.f.zero = result == 0; self.registers.f.carry = value > self.registers.a; self.registers.f.half_carry = (value & 0x0F) == 0x0F; // Hmmmm... - self.registers.f.subtract = true; + self.registers.f.subtract = false; } Instruction::OrAImm8(value) => { let result = value | self.registers.a; @@ -320,7 +334,7 @@ impl GameBoy { self.registers.f.zero = result == 0; self.registers.f.carry = value > self.registers.a; self.registers.f.half_carry = (value & 0x0F) == 0x0F; // Hmmmm... - self.registers.f.subtract = true; + self.registers.f.subtract = false; } Instruction::AddHlR16(reg) => { let a = self.registers.get_r16(R16::HL); @@ -398,6 +412,20 @@ impl GameBoy { let result = self.get_r8_byte(reg.clone()) | 1 << bit_offset; self.set_r8_byte(reg, result); } + Instruction::AdcAR8(reg) => { + let value = self.get_r8_byte(reg); + let result = self + .registers + .a + .wrapping_add(value) + .wrapping_add(self.registers.f.carry as u8); + self.registers.a = result; + + self.registers.f.zero = result == 0; + self.registers.f.carry = value > self.registers.a; + self.registers.f.half_carry = (value & 0x0F) == 0x0F; // Hmmmm... + self.registers.f.subtract = false; + } _ => { println!("{}", "Sorry cowboy but it looks like that instruction just ain't handled \nyet - get back out to the ranch and fix that dang emulator!".yellow()); self.debugger_cli(); @@ -464,8 +492,10 @@ impl GameBoy { // wrong! 0xE000..=0xFDFF => unreachable!(), + // Joy pad + 0xFF00 => self.joypad | 0xF, + // Interrupt registers - 0xFF00 => 0x0, 0xFF04 => self.div, 0xFF05 => self.tima, 0xFF06 => self.tma, @@ -490,10 +520,6 @@ impl GameBoy { } pub fn set_memory_byte(&mut self, addr: u16, byte: u8) { - //if addr == 0xff00 { - // self.debugger_cli(); - //} - match addr { // ROM bank - ignore 0x2000 => (), @@ -522,7 +548,10 @@ impl GameBoy { } // Joy pad input - 0xFF00 => (), + 0xFF00 => { + self.joypad = byte; + dbg!(byte); + } // Serial Transfer. I will simply just not support this... 0xFF01 => (), diff --git a/src/renderer.rs b/src/renderer.rs index 4cd3b5a..3623756 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -63,6 +63,23 @@ pub fn window_loop(rx: Receiver) { ((i as usize / 32) * 8).wrapping_sub(ppu.scy as usize), ) } + + for i in 0..40 { + let start_position = 0xFE00 + i * 4; + + let y_position = ppu.get_byte(start_position); + + let x_position = ppu.get_byte(start_position + 1); + let tile_index = ppu.get_byte(start_position + 2); + let flags = ppu.get_byte(start_position + 3); + + render_tile( + ppu.get_tile(tile_index as usize), + &mut buffer, + x_position.wrapping_sub(8) as usize, + y_position.wrapping_sub(16) as usize, + ); + } } _ => (), }