From 5be5bc4709c5dd16a373894362989c33cb6a5454 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 3 Jun 2024 00:19:26 +0300 Subject: [PATCH] Emulate a new APU glitch, tweak another. Fixes #617 (Telefang sample audio being broken on CGB-D and CGB-E) --- Core/apu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Core/apu.c b/Core/apu.c index 0f0ed16b4..fb0abc816 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -1215,8 +1215,12 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) case GB_IO_NR13: case GB_IO_NR23: { GB_channel_t index = reg == GB_IO_NR23? GB_SQUARE_2: GB_SQUARE_1; + bool just_reloaded = gb->apu.square_channels[index].sample_countdown == (gb->apu.square_channels[index].sample_length ^ 0x7FF) * 2 + 1; gb->apu.square_channels[index].sample_length &= ~0xFF; gb->apu.square_channels[index].sample_length |= value & 0xFF; + if (just_reloaded) { + gb->apu.square_channels[index].sample_countdown = (gb->apu.square_channels[index].sample_length ^ 0x7FF) * 2 + 1; + } break; } @@ -1264,7 +1268,8 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) else { unsigned extra_delay = 0; if (gb->model == GB_MODEL_CGB_E || gb->model == GB_MODEL_CGB_D) { - if (!(value & 4) && !(((gb->apu.square_channels[index].sample_countdown - 1 - gb->apu.square_channels[index].delay) / 2) & 0x400)) { + bool just_reloaded = gb->apu.square_channels[index].sample_countdown == (old_sample_length ^ 0x7FF) * 2 + 1; + if (!just_reloaded && !(value & 4) && !(((gb->apu.square_channels[index].sample_countdown - 1 - gb->apu.square_channels[index].delay) / 2) & 0x400)) { gb->apu.square_channels[index].current_sample_index++; gb->apu.square_channels[index].current_sample_index &= 0x7; gb->apu.square_channels[index].sample_surpressed = false;