From 6fb17d77c6980bdd81c3f0cd2912a1c79c5294a3 Mon Sep 17 00:00:00 2001 From: Theron Date: Tue, 17 Dec 2019 21:29:00 -0600 Subject: [PATCH] apu --- src/apu/mod.rs | 31 +++++++++++++++++++++++++++++-- src/apu/noise.rs | 4 ++++ src/apu/triangle.rs | 4 ++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/apu/mod.rs b/src/apu/mod.rs index 1cd7e42..3fa51f8 100644 --- a/src/apu/mod.rs +++ b/src/apu/mod.rs @@ -35,6 +35,7 @@ pub struct Apu { tnd_table: Vec, frame_counter: u8, + current_frame: u8, mode: u8, interrupt_inhibit: u8, frame_interrupt: bool, @@ -65,6 +66,7 @@ impl Apu { tnd_table: tnd_table, frame_counter: 0, + current_frame: 0, mode: 0, interrupt_inhibit: 0, frame_interrupt: false, @@ -100,7 +102,7 @@ impl Apu { 0x4014 => (), 0x4015 => self.control(value), 0x4016 => (), - 0x4017 => self.step_frame_counter(value), + 0x4017 => self.set_frame_counter(value), _ => panic!("bad address written: 0x{:X}", address), } } @@ -116,7 +118,7 @@ impl Apu { // - - - f - - - - - IRQ (if bit 6 is clear) // - l - l - l - - l Length counter and sweep // e e e e e e e - e Envelope and linear counter - fn step_frame_counter(&mut self, value: u8) { + fn set_frame_counter(&mut self, value: u8) { // 0 selects 4-step sequence, 1 selects 5-step sequence if value & (1<<7) == 0 { self.mode = 0; @@ -132,6 +134,27 @@ impl Apu { } + fn step_frame_counter(&mut self) { + match self.frame_counter { + 4 => { + self.square1.clock_envelope(); + self.square2.clock_envelope(); + self.triangle.clock_linear_counter(); + self.noise.clock_envelope(); + if self.current_frame == 1 || self.current_frame == 3 { + + } + if self.current_frame == 3 { + self.issue_irq(); + } + }, + 5 => { + + }, + _ => panic!("invalid frame counter value"), + } + } + fn control(&mut self, value: u8) { // Writing to this register clears the DMC interrupt flag. self.dmc.interrupt = false; @@ -213,4 +236,8 @@ impl Apu { // TODO: If an interrupt flag was set at the same moment of the read, it will read back as 1 but it will not be cleared. val } + + fn issue_irq(&mut self) { + + } } diff --git a/src/apu/noise.rs b/src/apu/noise.rs index a7a3870..290672c 100644 --- a/src/apu/noise.rs +++ b/src/apu/noise.rs @@ -39,6 +39,10 @@ impl Noise { } + pub fn clock_envelope(&mut self) { + + } + pub fn loop_noise(&mut self, value: u8) { } diff --git a/src/apu/triangle.rs b/src/apu/triangle.rs index a7f050b..d0589b7 100644 --- a/src/apu/triangle.rs +++ b/src/apu/triangle.rs @@ -31,4 +31,8 @@ impl Triangle { pub fn counter(&mut self, value: u8) { } + + pub fn clock_linear_counter(&mut self) { + + } } \ No newline at end of file