From 73b0a280347520e443229bb37ce1ea3cfd140f02 Mon Sep 17 00:00:00 2001 From: Theron Spiegl Date: Wed, 1 Jan 2020 15:28:28 -0600 Subject: [PATCH] fixed DK walking sound though not sure why exactly. --- src/apu/mod.rs | 14 +++++++------- src/apu/square.rs | 22 +++++++++++++--------- src/main.rs | 2 +- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/apu/mod.rs b/src/apu/mod.rs index c5725ea..40d1f44 100644 --- a/src/apu/mod.rs +++ b/src/apu/mod.rs @@ -97,6 +97,12 @@ impl Apu { sample } + fn mix(&self) -> f32 { + let square_out = self.square_table[(self.square1.sample + self.square2.sample) as usize]; + let tnd_out = self.tnd_table[((3*self.triangle.sample)+(2*self.noise.sample) + self.dmc.sample) as usize]; + square_out + tnd_out + } + pub fn write_reg(&mut self, address: usize, value: u8) { // println!("writing 0b{:08b} to 0x{:X}", value, address); match address { @@ -128,13 +134,7 @@ impl Apu { } } - fn mix(&self) -> f32 { - let square_out = self.square_table[(self.square1.sample + self.square2.sample) as usize]; - let tnd_out = self.tnd_table[((3*self.triangle.sample)+(2*self.noise.sample) + self.dmc.sample) as usize]; - square_out + tnd_out - } - - // mode 0: mode 1: function + // mode 0: mode 1: function // --------- ----------- ----------------------------- // - - - f - - - - - IRQ (if bit 6 is clear) // - l - l - l - - l Length counter and sweep diff --git a/src/apu/square.rs b/src/apu/square.rs index 56f1234..4ab3e78 100644 --- a/src/apu/square.rs +++ b/src/apu/square.rs @@ -107,17 +107,18 @@ impl Square { // When the divider is clocked while at 0, it is loaded with V and clocks the decay level counter. if self.envelope_divider == 0 { self.envelope_divider = self.envelope; + // Then one of two actions occurs: If the counter is non-zero, it is decremented, + if self.decay_counter != 0 { + self.decay_counter -= 1; + // println!("decaying to {}", self.decay_counter); + } else if self.length_counter_halt { + // otherwise if the loop flag is set, the decay level counter is loaded with 15. + println!("looping decay counter"); + self.decay_counter = 15; + } } else { self.envelope_divider -= 1; } - // Then one of two actions occurs: If the counter is non-zero, it is decremented, - if self.decay_counter != 0 { - self.decay_counter -= 1; - // println!("decaying to {}", self.decay_counter); - } else if self.length_counter_halt { - // otherwise if the loop flag is set, the decay level counter is loaded with 15. - self.decay_counter = 15; - } } pub fn clock_length_counter(&mut self) { @@ -130,7 +131,7 @@ impl Square { self.calculate_target_period(); // When the frame counter sends a half-frame clock (at 120 or 96 Hz), two things happen. // If the divider's counter is zero, the sweep is enabled, and the sweep unit is not muting the channel: The pulse's period is adjusted. - if self.sweep_counter == 0 && self.sweep_enabled && !(self.timer_period < 8 || self.timer_period > 0x7FF) { + if self.sweep_counter == 0 && self.sweep_enabled && !(self.timer_period < 8 || self.target_period > 0x7FF) { self.timer_period = self.target_period; println!("timer period adjusted to {}", self.timer_period); } @@ -138,6 +139,7 @@ impl Square { if self.sweep_counter == 0 || self.sweep_reload { self.sweep_counter = self.sweep_divider; self.sweep_reload = false; + if self.sweep_enabled { self.timer_period = self.target_period; } // This fixes the DK walking sound. Why? Not reflected in documentation. } else { self.sweep_counter -= 1; } @@ -170,6 +172,7 @@ impl Square { self.duty_cycle = DUTY_CYCLE_SEQUENCES[(value >> 6) as usize]; self.length_counter_halt = value & (1<<5) != 0; self.constant_volume_flag = value & (1<<4) != 0; + // println!("using envelope volume: {}", !self.constant_volume_flag); self.envelope = value as u16 & 0b1111; // if self.constant_volume_flag { // value as u16 & 0b1111 @@ -180,6 +183,7 @@ impl Square { // $4001/$4005 pub fn write_sweep(&mut self, value: u8) { + // println!("writing sweep 0b{:08b}", value); self.sweep_enabled = value >> 7 == 1; self.sweep_divider = ((value as u16 >> 4) & 0b111) + 1; self.sweep_negate = value & 0b1000 != 0; diff --git a/src/main.rs b/src/main.rs index fcaa57d..504cc79 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,7 +69,7 @@ fn main() -> Result<(), String> { match cpu.apu.clock() { Some(sample) => { sps += 1; - if sps < 44100 {audio_device.queue(&vec![sample]);} + if sps < 44_100 {audio_device.queue(&vec![sample]);} }, None => (), };