From bf95eee3f19a256f1cb939d2d61cfcf58fb35d45 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Mon, 5 Dec 2022 11:08:04 -0400 Subject: [PATCH] fix(apu): resolve bug in NR10 obscure behaviour --- src/core/apu/ToneSweep.zig | 7 +++---- src/core/apu/device/Sweep.zig | 6 ++++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/core/apu/ToneSweep.zig b/src/core/apu/ToneSweep.zig index 0c63938..4ade2fa 100644 --- a/src/core/apu/ToneSweep.zig +++ b/src/core/apu/ToneSweep.zig @@ -94,10 +94,9 @@ pub fn sound1CntL(self: *const Self) u8 { pub fn setSound1CntL(self: *Self, value: u8) void { const new = io.Sweep{ .raw = value }; - if (self.sweep.direction.read() and !new.direction.read()) { - // Sweep Negate bit has been cleared - // If At least 1 Sweep Calculation has been made since - // the last trigger, the channel is immediately disabled + if (!new.direction.read()) { + // If at least one (1) sweep calculation has been made with + // the negate bit set (since last trigger), disable the channel if (self.sweep_dev.calc_performed) self.enabled = false; } diff --git a/src/core/apu/device/Sweep.zig b/src/core/apu/device/Sweep.zig index fb5473f..605697d 100644 --- a/src/core/apu/device/Sweep.zig +++ b/src/core/apu/device/Sweep.zig @@ -31,7 +31,6 @@ pub fn tick(self: *Self, ch1: *ToneSweep) void { if (self.timer == 0) { const period = ch1.sweep.period.read(); self.timer = if (period == 0) 8 else period; - if (!self.calc_performed) self.calc_performed = true; if (self.enabled and period != 0) { const new_freq = self.calculate(ch1.sweep, &ch1.enabled); @@ -52,7 +51,10 @@ pub fn calculate(self: *Self, sweep: io.Sweep, ch_enable: *bool) u12 { const shadow_shifted = shadow >> sweep.shift.read(); const decrease = sweep.direction.read(); - const freq = if (decrease) shadow - shadow_shifted else shadow + shadow_shifted; + const freq = if (decrease) blk: { + self.calc_performed = true; + break :blk shadow - shadow_shifted; + } else shadow + shadow_shifted; if (freq > 0x7FF) ch_enable.* = false; return freq;