From 8fea8eb1ff9ca376b06cab01cb880602f6970421 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Tue, 17 Aug 2021 21:26:22 -0500 Subject: [PATCH] fix(apu): better emulate behaviour of apu channel DACs --- src/apu.rs | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/apu.rs b/src/apu.rs index b78fd29..98f4d3f 100644 --- a/src/apu.rs +++ b/src/apu.rs @@ -412,9 +412,12 @@ pub(crate) struct Channel1 { impl Channel1 { fn amplitude(&self) -> f32 { - let dac_input = self.duty.wave_pattern().amplitude(self.duty_pos) * self.current_volume; - - (dac_input as f32 / 7.5) - 1.0 + if self.is_dac_enabled() && self.enabled { + let input = self.duty.wave_pattern().amplitude(self.duty_pos) * self.current_volume; + (input as f32 / 7.5) - 1.0 + } else { + 0.0 + } } fn clock(&mut self) { @@ -565,9 +568,12 @@ pub(crate) struct Channel2 { impl Channel2 { fn amplitude(&self) -> f32 { - let dac_input = self.duty.wave_pattern().amplitude(self.duty_pos) * self.current_volume; - - (dac_input as f32 / 7.5) - 1.0 + if self.is_dac_enabled() && self.enabled { + let input = self.duty.wave_pattern().amplitude(self.duty_pos) * self.current_volume; + (input as f32 / 7.5) - 1.0 + } else { + 0.0 + } } fn clock(&mut self) { @@ -745,10 +751,12 @@ impl Channel3 { } fn amplitude(&self) -> f32 { - let dac_input = - (self.read_sample(self.offset) >> self.volume.shift_count()) * self.enabled as u8; - - (dac_input as f32 / 7.5) - 1.0 + if self.enabled { + let input = self.read_sample(self.offset) >> self.volume.shift_count(); + (input as f32 / 7.5) - 1.0 + } else { + 0.0 + } } fn clock(&mut self) { @@ -863,9 +871,12 @@ impl Channel4 { } fn amplitude(&self) -> f32 { - let dac_input = (!self.lf_shift & 0x01) as u8 * self.current_volume; - - (dac_input as f32 / 7.5) - 1.0 + if self.is_dac_enabled() && self.enabled { + let input = (!self.lf_shift & 0x01) as u8 * self.current_volume; + (input as f32 / 7.5) - 1.0 + } else { + 0.0 + } } fn clock(&mut self) {