From f866441481dbec7e32fdd9dc3a3785a2f9027cba Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 19 Dec 2021 19:27:01 +0200 Subject: [PATCH] Improved emulation of channel 3 wave RAM read glitch --- Core/apu.c | 22 +++++++++++++++++----- Core/apu.h | 2 +- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 9a5f852..be444d3 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -598,10 +598,19 @@ void GB_apu_run(GB_gameboy_t *gb) gb->apu.apu_cycles = 0; if (!cycles) return; - if (unlikely(gb->apu.wave_channel.delayed_bugged_read)) { - gb->apu.wave_channel.delayed_bugged_read = false; - gb->apu.wave_channel.current_sample_byte = - gb->io_registers[GB_IO_WAV_START + (gb->address_bus & 0xF)]; + if (unlikely(gb->apu.wave_channel.bugged_read_countdown)) { + uint8_t cycles_left = cycles; + while (cycles_left) { + cycles_left--; + if (--gb->apu.wave_channel.bugged_read_countdown == 0) { + gb->apu.wave_channel.current_sample_byte = + gb->io_registers[GB_IO_WAV_START + (gb->address_bus & 0xF)]; + if (gb->apu.is_active[GB_WAVE]) { + update_wave_sample(gb, 0); + } + break; + } + } } bool start_ch4 = false; @@ -707,12 +716,15 @@ void GB_apu_run(GB_gameboy_t *gb) gb->io_registers[GB_IO_WAV_START + (gb->address_bus & 0xF)]; } else { - gb->apu.wave_channel.delayed_bugged_read = true; + gb->apu.wave_channel.bugged_read_countdown = 1; } } if (cycles_left) { gb->apu.wave_channel.sample_countdown -= cycles_left; } + if (gb->apu.wave_channel.sample_countdown == 0) { + gb->apu.wave_channel.bugged_read_countdown = 2; + } } // The noise channel can step even if inactive on the DMG diff --git a/Core/apu.h b/Core/apu.h index f7b125c..6242860 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -102,7 +102,7 @@ typedef struct uint8_t current_sample_byte; // Current sample byte. bool wave_form_just_read; bool pulsed; - bool delayed_bugged_read; + uint8_t bugged_read_countdown; } wave_channel; struct {