From 785135a07441be949e023dd6b03af5b2555df754 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Wed, 12 Oct 2022 22:55:25 -0300 Subject: [PATCH] feat: rewrite device ticks --- src/core/apu.zig | 36 +++++++++++++++++++++--------------- src/core/apu/Noise.zig | 13 +++++++------ src/core/apu/Tone.zig | 13 +++++++------ src/core/apu/ToneSweep.zig | 18 ++++++++---------- src/core/apu/Wave.zig | 10 ++++++++-- 5 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/core/apu.zig b/src/core/apu.zig index 9ad8d1c..4ab9ffd 100644 --- a/src/core/apu.zig +++ b/src/core/apu.zig @@ -169,6 +169,8 @@ pub const Apu = struct { is_buffer_full: bool, + pub const Tick = enum { Length, Envelope, Sweep }; + pub fn init(sched: *Scheduler) Self { const apu: Self = .{ .ch1 = ToneSweep.init(sched), @@ -377,12 +379,12 @@ pub const Apu = struct { self.fs.tick(); switch (self.fs.step) { - 7 => self.tickEnvelopes(), // Clock Envelope - 0, 4 => self.tickLengths(), // Clock Length + 7 => self.tick(.Envelope), // Clock Envelope + 0, 4 => self.tick(.Length), // Clock Length 2, 6 => { // Clock Length and Sweep - self.tickLengths(); - self.ch1.tickSweep(); + self.tick(.Length); + self.tick(.Sweep); }, 1, 3, 5 => {}, } @@ -390,17 +392,21 @@ pub const Apu = struct { self.sched.push(.FrameSequencer, ((1 << 24) / 512) -| late); } - fn tickLengths(self: *Self) void { - self.ch1.tickLength(); - self.ch2.tickLength(); - self.ch3.tickLength(); - self.ch4.tickLength(); - } - - fn tickEnvelopes(self: *Self) void { - self.ch1.tickEnvelope(); - self.ch2.tickEnvelope(); - self.ch4.tickEnvelope(); + fn tick(self: *Self, comptime kind: Tick) void { + switch (kind) { + .Length => { + self.ch1.tick(kind); + self.ch2.tick(kind); + self.ch3.tick(kind); + self.ch4.tick(kind); + }, + .Envelope => { + self.ch1.tick(kind); + self.ch2.tick(kind); + self.ch4.tick(kind); + }, + .Sweep => self.ch1.tick(kind), + } } pub fn onDmaAudioSampleRequest(self: *Self, cpu: *Arm7tdmi, tim_id: u3) void { diff --git a/src/core/apu/Noise.zig b/src/core/apu/Noise.zig index a3ad7bd..a5fca21 100644 --- a/src/core/apu/Noise.zig +++ b/src/core/apu/Noise.zig @@ -3,6 +3,7 @@ const util = @import("../../util.zig"); const Scheduler = @import("../scheduler.zig").Scheduler; const FrameSequencer = @import("../apu.zig").FrameSequencer; +const Tick = @import("../apu.zig").Apu.Tick; const Envelope = @import("device/Envelope.zig"); const Length = @import("device/Length.zig"); const Lfsr = @import("signal/Lfsr.zig"); @@ -57,12 +58,12 @@ pub fn reset(self: *Self) void { self.enabled = false; } -pub fn tickLength(self: *Self) void { - self.len_dev.tick(self.cnt.length_enable.read(), &self.enabled); -} - -pub fn tickEnvelope(self: *Self) void { - self.env_dev.tick(self.envelope); +pub fn tick(self: *Self, comptime kind: Tick) void { + switch (kind) { + .Length => self.len_dev.tick(self.cnt.length_enable.read(), &self.enabled), + .Envelope => self.env_dev.tick(self.envelope), + .Sweep => @compileError("Channel 4 does not implement Sweep"), + } } /// NR41, NR42 diff --git a/src/core/apu/Tone.zig b/src/core/apu/Tone.zig index 3044f4f..b69b732 100644 --- a/src/core/apu/Tone.zig +++ b/src/core/apu/Tone.zig @@ -3,6 +3,7 @@ const util = @import("../../util.zig"); const Scheduler = @import("../scheduler.zig").Scheduler; const FrameSequencer = @import("../apu.zig").FrameSequencer; +const Tick = @import("../apu.zig").Apu.Tick; const Length = @import("device/Length.zig"); const Envelope = @import("device/Envelope.zig"); const Square = @import("signal/Square.zig"); @@ -50,12 +51,12 @@ pub fn reset(self: *Self) void { self.enabled = false; } -pub fn tickLength(self: *Self) void { - self.len_dev.tick(self.freq.length_enable.read(), &self.enabled); -} - -pub fn tickEnvelope(self: *Self) void { - self.env_dev.tick(self.envelope); +pub fn tick(self: *Self, comptime kind: Tick) void { + switch (kind) { + .Length => self.len_dev.tick(self.freq.length_enable.read(), &self.enabled), + .Envelope => self.env_dev.tick(self.envelope), + .Sweep => @compileError("Channel 2 does not implement Sweep"), + } } pub fn onToneEvent(self: *Self, late: u64) void { diff --git a/src/core/apu/ToneSweep.zig b/src/core/apu/ToneSweep.zig index 60d73bd..1c152b9 100644 --- a/src/core/apu/ToneSweep.zig +++ b/src/core/apu/ToneSweep.zig @@ -8,6 +8,8 @@ const Envelope = @import("device/Envelope.zig"); const Sweep = @import("device/Sweep.zig"); const Square = @import("signal/Square.zig"); +const Tick = @import("../apu.zig").Apu.Tick; + const Self = @This(); /// NR10 @@ -59,16 +61,12 @@ pub fn reset(self: *Self) void { self.enabled = false; } -pub fn tickSweep(self: *Self) void { - self.sweep_dev.tick(self); -} - -pub fn tickLength(self: *Self) void { - self.len_dev.tick(self.freq.length_enable.read(), &self.enabled); -} - -pub fn tickEnvelope(self: *Self) void { - self.env_dev.tick(self.envelope); +pub fn tick(self: *Self, comptime kind: Tick) void { + switch (kind) { + .Length => self.len_dev.tick(self.freq.length_enable.read(), &self.enabled), + .Envelope => self.env_dev.tick(self.envelope), + .Sweep => self.sweep_dev.tick(self), + } } pub fn onToneSweepEvent(self: *Self, late: u64) void { diff --git a/src/core/apu/Wave.zig b/src/core/apu/Wave.zig index 64738a1..b66f932 100644 --- a/src/core/apu/Wave.zig +++ b/src/core/apu/Wave.zig @@ -3,6 +3,8 @@ const util = @import("../../util.zig"); const Scheduler = @import("../scheduler.zig").Scheduler; const FrameSequencer = @import("../apu.zig").FrameSequencer; +const Tick = @import("../apu.zig").Apu.Tick; + const Length = @import("device/Length.zig"); const Wave = @import("signal/Wave.zig"); @@ -49,8 +51,12 @@ pub fn reset(self: *Self) void { self.enabled = false; } -pub fn tickLength(self: *Self) void { - self.len_dev.tick(self.freq.length_enable.read(), &self.enabled); +pub fn tick(self: *Self, comptime kind: Tick) void { + switch (kind) { + .Length => self.len_dev.tick(self.freq.length_enable.read(), &self.enabled), + .Envelope => @compileError("Channel 3 does not implement Envelope"), + .Sweep => @compileError("Channel 3 does not implement Sweep"), + } } /// NR30, NR31, NR32