From 9450eb9575b8fbf7ff22efbcac473fc3a8c4d91a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 16 Feb 2026 01:10:07 +0200 Subject: [PATCH] Fix timing inaccuracy --- Core/apu.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index d2dc301c..69b07142 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -1248,14 +1248,19 @@ static void prepare_noise_start(GB_gameboy_t *gb) divisor = 8; } else if (divisor == 1) { - /* TODO: I'm not 100% sure this only affects NR43 = 1X */ - if ((gb->io_registers[GB_IO_NR43] & 0xf0) == 0x10 && (gb->apu.noise_channel.counter & 1)) { - instant_step = true; - } - /* TODO: needs further research, the conditions are very odd. What if NR43 changes before stepping? */ - if ((gb->io_registers[GB_IO_NR43] & 0xf0) == 0x10 && !gb->apu.noise_channel.did_step_counter) { + if (!gb->apu.noise_channel.did_step_counter) { div_1_glitch = true; } + + uint16_t mask = 1 << (gb->io_registers[GB_IO_NR43] >> 4); + bool old_bit = gb->apu.noise_channel.counter & mask; + gb->apu.noise_channel.counter++; + gb->apu.noise_channel.counter &= 0x3FFF; + bool new_bit = gb->apu.noise_channel.counter & mask; + + if ((new_bit && !old_bit)) { + instant_step = true; + } } } gb->apu.noise_channel.counter_countdown = divisor == 0? 6 : divisor * 4 + 6;