From b54a72d9b9a72dc243cd37216b3adb4c99511a44 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 2 Jan 2021 14:56:45 +0200 Subject: [PATCH] Fixing a bug where where zero-shift sweep wouldn't tick --- Core/apu.c | 7 ++++--- Core/apu.h | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 3abca7d..4d66e24 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -394,7 +394,7 @@ static void trigger_sweep_calculation(GB_gameboy_t *gb) /* Recalculation and overflow check only occurs after a delay */ gb->apu.square_sweep_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 5 - gb->apu.lf_div; - + gb->apu.unshifted_sweep = !(gb->io_registers[GB_IO_NR10] & 0x7); gb->apu.square_sweep_countdown = ((gb->io_registers[GB_IO_NR10] >> 4) & 7) ^ 7; } } @@ -524,7 +524,7 @@ void GB_apu_run(GB_gameboy_t *gb) gb->apu.noise_channel.alignment += cycles; if (gb->apu.square_sweep_calculate_countdown && - ((gb->io_registers[GB_IO_NR10] & 7) || + (((gb->io_registers[GB_IO_NR10] & 7) || gb->apu.unshifted_sweep) || gb->apu.square_sweep_calculate_countdown <= (gb->model > GB_MODEL_CGB_C? 3 : 1))) { // Calculation is paused if the lower bits if (gb->apu.square_sweep_calculate_countdown > cycles) { gb->apu.square_sweep_calculate_countdown -= cycles; @@ -875,6 +875,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) if (gb->io_registers[GB_IO_NR10] & 7) { /* APU bug: if shift is nonzero, overflow check also occurs on trigger */ gb->apu.square_sweep_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 5 - gb->apu.lf_div; + gb->apu.unshifted_sweep = false; if (gb->model > GB_MODEL_CGB_C) { gb->apu.square_sweep_calculate_countdown += 2; } @@ -884,7 +885,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) else { gb->apu.sweep_length_addend = 0; } - gb->apu.channel_1_restart_hold = 4 - gb->apu.lf_div; + gb->apu.channel_1_restart_hold = 2 - gb->apu.lf_div + GB_is_cgb(gb) * 2; gb->apu.square_sweep_countdown = ((gb->io_registers[GB_IO_NR10] >> 4) & 7) ^ 7; } } diff --git a/Core/apu.h b/Core/apu.h index 69cea16..eadae1b 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -66,7 +66,7 @@ typedef struct uint8_t square_sweep_calculate_countdown; // In 2 MHz uint16_t sweep_length_addend; uint16_t shadow_sweep_sample_length; - GB_PADDING(bool, sweep_enabled); + bool unshifted_sweep; GB_PADDING(bool, sweep_decreasing); struct {