Skip to content

Commit

Permalink
LengthCounter
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Feb 23, 2024
1 parent f22ad41 commit 26096df
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 151 deletions.
2 changes: 1 addition & 1 deletion src/blip/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl BlipBuf {
self.avail += off as usize >> TIME_BITS;
self.offset = off & (TIME_UNIT - 1);

assert!(self.avail <= self.size);
// assert!(self.avail <= self.size);
}

pub fn remove_samples(&mut self, count: usize) {
Expand Down
29 changes: 0 additions & 29 deletions src/sound/apu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,35 +284,6 @@ impl Memory for APU {
_ => panic!("Write to unsupported APU address ({:#06x})!", a),
}

if self.ch1.trigger {
self.ch1.trigger = false;
if self.ch1.dac_enabled {
self.is_ch_1_on = true;
}
}

if self.ch2.trigger {
self.ch2.trigger = false;
if self.ch2.dac_enabled {
self.is_ch_2_on = true;
}
}

if self.ch3.trigger {
self.ch3.trigger = false;
if self.ch3.dac_enabled {
self.is_ch_3_on = true;
}
}

if self.ch4.trigger {
self.ch4.trigger = false;
self.ch4.lfsr = 0;
if self.ch4.dac_enabled {
self.is_ch_4_on = true;
}
}

if set_apu_control {
if !self.audio_enabled {
self.is_ch_1_on = false;
Expand Down
43 changes: 13 additions & 30 deletions src/sound/ch1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::components::memory::Memory;
use crate::sound::apu::DutyCycle;
use crate::sound::blip::Blip;
use crate::sound::length_counter::LengthCounter;

pub struct CH1 {
pub blip: Blip,
Expand All @@ -9,15 +10,12 @@ pub struct CH1 {
negative_direction: bool,
sweep_step: u8,
pub duty_cycle: DutyCycle,
pub length_timer: u8,
pub volume: u8,
positive_envelope: bool,
envelope_pace: u8,
pub period: u16,
pub trigger: bool,
length_enabled: bool,
length_cycle_count: u32,
sweep_cycle_count: u32,
length_counter: LengthCounter
}

impl CH1 {
Expand All @@ -29,15 +27,12 @@ impl CH1 {
negative_direction: false,
sweep_step: 0,
duty_cycle: DutyCycle::EIGHTH,
length_timer: 0,
volume: 0,
positive_envelope: false,
envelope_pace: 0,
period: 0,
trigger: false,
length_enabled: false,
length_cycle_count: 0,
sweep_cycle_count: 0,
length_counter: LengthCounter::new()
}
}

Expand All @@ -47,31 +42,14 @@ impl CH1 {
self.negative_direction = false;
self.sweep_step = 0;
self.duty_cycle = DutyCycle::EIGHTH;
self.length_timer = 0;
self.volume = 0;
self.positive_envelope = false;
self.envelope_pace = 0;
self.period = 0;
self.trigger = false;
self.length_enabled = false;
self.length_counter.clear();
}

pub fn cycle(&mut self) {
if self.length_enabled {
self.length_cycle_count += 1;

if self.length_cycle_count >= 2 {
self.length_cycle_count = 0;

if self.length_timer >= 64 {
self.dac_enabled = false;
self.length_enabled = false;
} else {
self.length_timer += 1;
}
}
}

if self.sweep_pace != 0 {
self.sweep_cycle_count += 1;

Expand All @@ -97,6 +75,7 @@ impl CH1 {
}
}

self.length_counter.cycle();
self.blip.data.end_frame(4096);
//self.blip.from -= 4096;
}
Expand All @@ -123,7 +102,7 @@ impl Memory for CH1 {
// NR13: Period Low
0xFF13 => 0xFF,
// NR14: Period High & Control
0xFF14 => (self.length_enabled as u8) << 6 | 0xBF,
0xFF14 => (self.length_counter.enabled as u8) << 6 | 0xBF,
_ => 0xFF,
}
}
Expand All @@ -139,7 +118,7 @@ impl Memory for CH1 {
// NR11: Length Timer & Duty Cycle
0xFF11 => {
self.duty_cycle = DutyCycle::from_bits_truncate(v >> 6);
self.length_timer = v & 0b0011_1111;
self.length_counter.counter = (v & 0b0011_1111) as u16;
}
// NR12: Volume & Envelope
0xFF12 => {
Expand All @@ -158,10 +137,14 @@ impl Memory for CH1 {
}
// NR14: Period High & Control
0xFF14 => {
self.trigger = ((v & 0b1000_0000) >> 7) != 0;
self.length_enabled = ((v & 0b0100_0000) >> 6) != 0;
self.length_counter.trigger = ((v & 0b1000_0000) >> 7) != 0;
self.length_counter.enabled = ((v & 0b0100_0000) >> 6) != 0;
self.period &= 0b0000_0000_1111_1111;
self.period |= ((v & 0b0000_0111) as u16) << 8;

if self.length_counter.trigger {
self.length_counter.reload(1 << 6);
}
}
_ => panic!("Write to unsupported SC1 address ({:#06x})!", a),
}
Expand Down
43 changes: 13 additions & 30 deletions src/sound/ch2.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use crate::components::memory::Memory;
use crate::sound::apu::DutyCycle;
use crate::sound::blip::Blip;
use crate::sound::length_counter::LengthCounter;

pub struct CH2 {
pub blip: Blip,
pub dac_enabled: bool,
pub duty_cycle: DutyCycle,
length_timer: u8,
pub volume: u8,
positive_envelope: bool,
envelope_pace: u8,
pub period: u16,
pub trigger: bool,
length_enabled: bool,
length_cycle_count: u32,
length_counter: LengthCounter
}

impl CH2 {
Expand All @@ -22,45 +20,26 @@ impl CH2 {
blip,
dac_enabled: false,
duty_cycle: DutyCycle::EIGHTH,
length_timer: 0,
volume: 0,
positive_envelope: false,
envelope_pace: 0,
period: 0,
trigger: false,
length_enabled: false,
length_cycle_count: 0,
length_counter: LengthCounter::new()
}
}

pub fn clear(&mut self) {
self.dac_enabled = false;
self.duty_cycle = DutyCycle::EIGHTH;
self.length_timer = 0;
self.volume = 0;
self.positive_envelope = false;
self.envelope_pace = 0;
self.period = 0;
self.trigger = false;
self.length_enabled = false;
self.length_counter.clear();
}

pub fn cycle(&mut self) {
if self.length_enabled {
self.length_cycle_count += 1;

if self.length_cycle_count >= 2 {
self.length_cycle_count = 0;

if self.length_timer >= 64 {
self.dac_enabled = false;
self.length_enabled = false;
} else {
self.length_timer += 1;
}
}
}

self.length_counter.cycle();
self.blip.data.end_frame(4096);
//self.blip.from -= 4096;
}
Expand All @@ -80,7 +59,7 @@ impl Memory for CH2 {
// NR23: Period Low
0xFF18 => 0xFF,
// NR24: Period High & Control
0xFF19 => (self.length_enabled as u8) << 6 | 0xBF,
0xFF19 => (self.length_counter.enabled as u8) << 6 | 0xBF,
_ => 0xFF,
}
}
Expand All @@ -91,7 +70,7 @@ impl Memory for CH2 {
0xFF15 => {}
0xFF16 => {
self.duty_cycle = DutyCycle::from_bits_truncate(v >> 6);
self.length_timer = v & 0b0011_1111;
self.length_counter.counter = (v & 0b0011_1111) as u16;
}
// NR22: Volume & Envelope
0xFF17 => {
Expand All @@ -110,10 +89,14 @@ impl Memory for CH2 {
}
// NR24: Period High & Control
0xFF19 => {
self.trigger = ((v & 0b1000_0000) >> 7) != 0;
self.length_enabled = ((v & 0b0100_0000) >> 6) != 0;
self.length_counter.trigger= ((v & 0b1000_0000) >> 7) != 0;
self.length_counter.enabled = ((v & 0b0100_0000) >> 6) != 0;
self.period &= 0b0000_0000_1111_1111;
self.period |= ((v & 0b0000_0111) as u16) << 8;

if self.length_counter.trigger {
self.length_counter.reload(1 << 6);
}
}
_ => panic!("Write to unsupported SC2 address ({:#06x})!", a),
}
Expand Down
43 changes: 13 additions & 30 deletions src/sound/ch3.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use crate::components::memory::Memory;
use bitflags::bitflags;
use crate::sound::blip::Blip;
use crate::sound::length_counter::LengthCounter;

pub struct CH3 {
pub blip: Blip,
pub dac_enabled: bool,
length_timer: u8,
pub output_level: OutputLevel,
pub period: u16,
pub trigger: bool,
length_enabled: bool,
wave_ram: [u8; 16],
length_cycle_count: u32,
length_counter: LengthCounter
}

bitflags! {
Expand All @@ -29,41 +27,22 @@ impl CH3 {
Self {
blip,
dac_enabled: false,
length_timer: 0,
output_level: OutputLevel::MUTE,
period: 0,
trigger: false,
length_enabled: false,
wave_ram: [0; 16],
length_cycle_count: 0,
length_counter: LengthCounter::new()
}
}

pub fn clear(&mut self) {
self.dac_enabled = false;
self.length_timer = 0;
self.output_level = OutputLevel::MUTE;
self.period = 0;
self.trigger = false;
self.length_enabled = false;
self.length_counter.clear();
}

pub fn cycle(&mut self) {
if self.length_enabled {
self.length_cycle_count += 1;

if self.length_cycle_count >= 2 {
self.length_cycle_count = 0;

if self.length_timer >= 64 {
self.dac_enabled = false;
self.length_enabled = false;
} else {
self.length_timer += 1;
}
}
}

self.length_counter.cycle();
self.blip.data.end_frame(4096);
//self.blip.from -= 4096;
}
Expand All @@ -81,7 +60,7 @@ impl Memory for CH3 {
// NR33: Period Low
0xFF1D => 0xFF,
// NR34: Period High & Control
0xFF1E => (self.length_enabled as u8) << 6 | 0xBF,
0xFF1E => (self.length_counter.enabled as u8) << 6 | 0xBF,
0xFF30..=0xFF3F => {
if !self.dac_enabled {
self.wave_ram[a as usize - 0xFF30]
Expand All @@ -98,7 +77,7 @@ impl Memory for CH3 {
// NR30: DAC Enable
0xFF1A => self.dac_enabled = ((v & 0b1000_0000) >> 7) != 0,
// NR31: Length Timer
0xFF1B => self.length_timer = v,
0xFF1B => self.length_counter.counter = v as u16,
// NR32: Output Level
0xFF1C => self.output_level = OutputLevel::from_bits_truncate(v),
// NR33: Period Low
Expand All @@ -108,10 +87,14 @@ impl Memory for CH3 {
}
// NR34: Period High & Control
0xFF1E => {
self.trigger = ((v & 0b1000_0000) >> 7) != 0;
self.length_enabled = ((v & 0b0100_0000) >> 6) != 0;
self.length_counter.trigger = ((v & 0b1000_0000) >> 7) != 0;
self.length_counter.enabled = ((v & 0b0100_0000) >> 6) != 0;
self.period &= 0b0000_0000_1111_1111;
self.period |= ((v & 0b0000_0111) as u16) << 8;

if self.length_counter.trigger {
self.length_counter.reload(1 << 8);
}
}
0xFF30..=0xFF3F => {
if !self.dac_enabled {
Expand Down
Loading

0 comments on commit 26096df

Please sign in to comment.