From 9bf7e0b4dffc20941e4dfadffc6899ae19463844 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 14 Aug 2022 17:43:28 +0300 Subject: [PATCH] More accurate sample repeat glitch, fixes #483 --- Core/apu.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 0824f9f..e8d82a1 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -1111,19 +1111,6 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) case GB_IO_NR13: case GB_IO_NR23: { unsigned index = reg == GB_IO_NR23? GB_SQUARE_2: GB_SQUARE_1; - if (gb->apu.is_active[index]) { - /* On an AGB, as well as on CGB C and earlier (TODO: Tested: 0, B and C), it behaves slightly different on - double speed. */ - if (gb->model == GB_MODEL_CGB_E || gb->model == GB_MODEL_CGB_D || gb->apu.square_channels[index].sample_countdown & 1) { - if (gb->apu.square_channels[index].did_tick && - gb->apu.square_channels[index].sample_countdown >> 1 == (gb->apu.square_channels[index].sample_length ^ 0x7FF)) { - gb->apu.square_channels[index].current_sample_index--; - gb->apu.square_channels[index].current_sample_index &= 7; - gb->apu.square_channels[index].sample_surpressed = false; - } - } - } - gb->apu.square_channels[index].sample_length &= ~0xFF; gb->apu.square_channels[index].sample_length |= value & 0xFF; break; @@ -1133,10 +1120,10 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) case GB_IO_NR24: { unsigned index = reg == GB_IO_NR24? GB_SQUARE_2: GB_SQUARE_1; bool was_active = gb->apu.is_active[index]; - /* TODO: When the sample length changes right before being updated, the countdown should change to the - old length, but the current sample should not change. Because our write timing isn't accurate to - the T-cycle, we hack around it by stepping the sample index backwards. */ - if ((value & 0x80) == 0 && gb->apu.is_active[index]) { + /* TODO: When the sample length changes right before being updated from ≥$700 to <$700, the countdown + should change to the old length, but the current sample should not change. Because our write + timing isn't accurate to the T-cycle, we hack around it by stepping the sample index backwards. */ + if ((value & 0x80) == 0 && gb->apu.is_active[index] && (gb->io_registers[reg] & 0x7) == 7 && (value & 7) != 7) { /* On an AGB, as well as on CGB C and earlier (TODO: Tested: 0, B and C), it behaves slightly different on double speed. */ if (gb->model == GB_MODEL_CGB_E || gb->model == GB_MODEL_CGB_D || gb->apu.square_channels[index].sample_countdown & 1) {