From 4468c8aed509b1052bb7a1a478f7235b61675641 Mon Sep 17 00:00:00 2001 From: Nathaniel Walizer Date: Thu, 9 Jan 2025 02:03:15 -0800 Subject: [PATCH] Add sweep limits to APU square channels --- src/apu.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/apu.c b/src/apu.c index 1039f58..7b756e7 100644 --- a/src/apu.c +++ b/src/apu.c @@ -239,7 +239,11 @@ static inline void nes_apu_clock_sweep(nes_apu_Channel* channel, if (channel->sweep_delay == 0) { decrement = 0; - if ( (channel->reg[1] & apu_Square_Enable) && + if (channel->period < 8 * nes_clock_cpu_div) { + channel->period = 0; + channel->length = 0; + + } else if ( (channel->reg[1] & apu_Square_Enable) && 0 != (channel->reg[1] & apu_Square_Shift)) { int delta = ( apu_channel_raw_timer_value(channel) >> @@ -249,8 +253,10 @@ static inline void nes_apu_clock_sweep(nes_apu_Channel* channel, delta = adjust - delta; } channel->period += delta * nes_clock_apu_div; - if (channel->period < 0) { + if ( channel < 0 || + channel->period > 0x7FFU * nes_clock_cpu_div) { channel->period = 0; + channel->length = 0; } } }