fix: resolve off-by-one errors when scheduling freq timer expirations

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-05-21 13:25:05 -03:00
parent af2ad6c924
commit 4e35184be3
1 changed files with 7 additions and 4 deletions

View File

@ -983,7 +983,7 @@ const WaveDevice = struct {
self.offset = (self.offset + 1) % 0x20; // 0x10 bytes, which contain 2 samples each self.offset = (self.offset + 1) % 0x20; // 0x10 bytes, which contain 2 samples each
} }
self.sched.push(.{ .ApuChannel = 2 }, timer * tickInterval -| late); self.sched.push(.{ .ApuChannel = 2 }, (timer + 1) * tickInterval -| late);
} }
fn sample(self: *const Self, cnt: io.WaveSelect) u4 { fn sample(self: *const Self, cnt: io.WaveSelect) u4 {
@ -1094,7 +1094,7 @@ const SquareWave = struct {
self.timer = @truncate(u12, timer); self.timer = @truncate(u12, timer);
self.pos +%= 1; self.pos +%= 1;
self.sched.push(.{ .ApuChannel = if (kind == .Ch1) 0 else 1 }, timer * tickInterval -| late); self.sched.push(.{ .ApuChannel = if (kind == .Ch1) 0 else 1 }, (timer + 1) * tickInterval -| late);
} }
fn reloadTimer(self: *Self, comptime kind: ChannelKind, value: u11) void { fn reloadTimer(self: *Self, comptime kind: ChannelKind, value: u11) void {
@ -1165,20 +1165,23 @@ const Lfsr = struct {
const div = Self.divisor(poly.div_ratio.read()); const div = Self.divisor(poly.div_ratio.read());
const timer = @as(u64, div << poly.shift.read()); const timer = @as(u64, div << poly.shift.read());
self.sched.push(.{ .ApuChannel = 3 }, timer * tickInterval); self.sched.push(.{ .ApuChannel = 3 }, (timer + 1) * tickInterval);
} }
fn handleTimerOverflow(self: *Self, poly: io.PolyCounter, late: u64) void { fn handleTimerOverflow(self: *Self, poly: io.PolyCounter, late: u64) void {
const div = Self.divisor(poly.div_ratio.read()); const div = Self.divisor(poly.div_ratio.read());
const timer = @as(u64, div << poly.shift.read()); const timer = @as(u64, div << poly.shift.read());
// Obscure: Clock Shift of 14 or 15 results in LSFR receiving no clocks
if (poly.shift.read() > 14) return;
const tmp = (self.shift & 1) ^ ((self.shift & 2) >> 1); const tmp = (self.shift & 1) ^ ((self.shift & 2) >> 1);
self.shift = (self.shift >> 1) | (tmp << 14); self.shift = (self.shift >> 1) | (tmp << 14);
if (poly.width.read()) if (poly.width.read())
self.shift = (self.shift & ~@as(u15, 0x40)) | tmp << 6; self.shift = (self.shift & ~@as(u15, 0x40)) | tmp << 6;
self.sched.push(.{ .ApuChannel = 3 }, timer * tickInterval -| late); self.sched.push(.{ .ApuChannel = 3 }, (timer + 1) * tickInterval -| late);
} }
fn divisor(code: u3) u16 { fn divisor(code: u3) u16 {