From 9ba6915c85770ffb85b9e52d5dcfc4d4d79fe737 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 16 Jul 2019 21:42:57 +0300 Subject: [PATCH] ICD JOYP write API --- Core/joypad.c | 17 ++++++++++++++++- Core/joypad.h | 1 + Core/memory.c | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Core/joypad.c b/Core/joypad.c index 4ae6e67..6c0eaff 100644 --- a/Core/joypad.c +++ b/Core/joypad.c @@ -3,6 +3,8 @@ void GB_update_joyp(GB_gameboy_t *gb) { + if (gb->model & GB_MODEL_SGB_NO_SFC) return; + uint8_t key_selection = 0; uint8_t previous_state = 0; @@ -53,14 +55,27 @@ void GB_update_joyp(GB_gameboy_t *gb) break; } + /* Todo: This assumes the keys *always* bounce, which is incorrect when emulating an SGB */ if (previous_state != (gb->io_registers[GB_IO_JOYP] & 0xF)) { - /* The joypad interrupt DOES occur on CGB (Tested on CGB-CPU-06), unlike what some documents say. */ + /* The joypad interrupt DOES occur on CGB (Tested on CGB-E), unlike what some documents say. */ gb->io_registers[GB_IO_IF] |= 0x10; } gb->io_registers[GB_IO_JOYP] |= 0xC0; } +void GB_icd_set_joyp(GB_gameboy_t *gb, uint8_t value) +{ + uint8_t previous_state = gb->io_registers[GB_IO_JOYP] & 0xF; + gb->io_registers[GB_IO_JOYP] &= 0xF0; + gb->io_registers[GB_IO_JOYP] |= value & 0xF; + + if (previous_state & ~(gb->io_registers[GB_IO_JOYP] & 0xF)) { + gb->io_registers[GB_IO_IF] |= 0x10; + } + +} + void GB_set_key_state(GB_gameboy_t *gb, GB_key_t index, bool pressed) { assert(index >= 0 && index < GB_KEY_MAX); diff --git a/Core/joypad.h b/Core/joypad.h index 768d685..21fad53 100644 --- a/Core/joypad.h +++ b/Core/joypad.h @@ -17,6 +17,7 @@ typedef enum { void GB_set_key_state(GB_gameboy_t *gb, GB_key_t index, bool pressed); void GB_set_key_state_for_player(GB_gameboy_t *gb, GB_key_t index, unsigned player, bool pressed); +void GB_icd_set_joyp(GB_gameboy_t *gb, uint8_t value); #ifdef GB_INTERNAL void GB_update_joyp(GB_gameboy_t *gb); diff --git a/Core/memory.c b/Core/memory.c index e5983cb..76d8821 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -741,8 +741,8 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) return; case GB_IO_JOYP: - GB_sgb_write(gb, value); gb->io_registers[GB_IO_JOYP] = value & 0xF0; + GB_sgb_write(gb, value); GB_update_joyp(gb); return;