diff --git a/src/processor/memory/mmio/apu.rs b/src/processor/memory/mmio/apu.rs index 785d8e7..3f34c40 100644 --- a/src/processor/memory/mmio/apu.rs +++ b/src/processor/memory/mmio/apu.rs @@ -1,5 +1,7 @@ -use std::sync::mpsc::{channel, Sender}; - +use self::{ + channels::EnvelopeMode, + types::{Channels, DacSample, Mixer, VinEnable, Volume}, +}; use crate::{ processor::{ memory::{masked_update, Address}, @@ -13,10 +15,10 @@ use cpal::{ Device, Stream, StreamConfig, }; use samplerate::{ConverterType, Samplerate}; - -use self::channels::{EnvelopeMode, NoiseChannel, PwmChannel, WaveChannel}; +use std::sync::mpsc::{channel, Sender}; mod channels; +mod types; const MEM_START: usize = 0xFF10; const MEM_SIZE: usize = 0xFF40 - MEM_START; @@ -25,107 +27,6 @@ const fn reg(a: Address) -> usize { (a as usize) - MEM_START } -struct Channels { - one: PwmChannel, - two: PwmChannel, - three: WaveChannel, - four: NoiseChannel, -} - -impl Default for Channels { - fn default() -> Self { - Self { - one: PwmChannel::new(true), - two: PwmChannel::new(false), - three: WaveChannel::new(false), - four: NoiseChannel::new(false), - } - } -} - -#[derive(Default)] -struct VinEnable { - left: bool, - right: bool, -} - -enum Volume { - Muted, - Enabled, -} - -impl Volume { - fn scale(&self) -> f32 { - match self { - Volume::Muted => 0., - Volume::Enabled => 1., - } - } - - fn bool(&self) -> bool { - match self { - Volume::Muted => false, - Volume::Enabled => true, - } - } - - fn from_bool(val: bool) -> Self { - if val { - Self::Enabled - } else { - Self::Muted - } - } -} - -struct ChannelVol { - left: Volume, - right: Volume, -} - -impl Default for ChannelVol { - fn default() -> Self { - Self { - left: Volume::Muted, - right: Volume::Muted, - } - } -} - -struct Mixer { - vol_left: u8, - vol_right: u8, - ch1: ChannelVol, - ch2: ChannelVol, - ch3: ChannelVol, - ch4: ChannelVol, -} - -impl Default for Mixer { - fn default() -> Self { - Self { - vol_left: 7, - vol_right: 7, - ch1: Default::default(), - ch2: Default::default(), - ch3: Default::default(), - ch4: Default::default(), - } - } -} - -#[derive(Clone, Copy)] -struct DacSample { - one: f32, - two: f32, -} - -impl Default for DacSample { - fn default() -> Self { - Self { one: 0., two: 0. } - } -} - impl DacSample { fn mixed(&self, mixer: &Mixer) -> [f32; 2] { let left = (self.one * mixer.ch1.left.scale()) + (self.two * mixer.ch2.left.scale()); diff --git a/src/processor/memory/mmio/apu/types.rs b/src/processor/memory/mmio/apu/types.rs new file mode 100644 index 0000000..3da9f7a --- /dev/null +++ b/src/processor/memory/mmio/apu/types.rs @@ -0,0 +1,102 @@ +use super::channels::{NoiseChannel, PwmChannel, WaveChannel}; + +pub(super) struct Channels { + pub(super) one: PwmChannel, + pub(super) two: PwmChannel, + pub(super) three: WaveChannel, + pub(super) four: NoiseChannel, +} + +impl Default for Channels { + fn default() -> Self { + Self { + one: PwmChannel::new(true), + two: PwmChannel::new(false), + three: WaveChannel::new(false), + four: NoiseChannel::new(false), + } + } +} + +#[derive(Default)] +pub(super) struct VinEnable { + pub(super) left: bool, + pub(super) right: bool, +} + +pub(super) enum Volume { + Muted, + Enabled, +} + +impl Volume { + pub(super) fn scale(&self) -> f32 { + match self { + Volume::Muted => 0., + Volume::Enabled => 1., + } + } + + pub(super) fn bool(&self) -> bool { + match self { + Volume::Muted => false, + Volume::Enabled => true, + } + } + + pub(super) fn from_bool(val: bool) -> Self { + if val { + Self::Enabled + } else { + Self::Muted + } + } +} + +pub(super) struct ChannelVol { + pub(super) left: Volume, + pub(super) right: Volume, +} + +impl Default for ChannelVol { + fn default() -> Self { + Self { + left: Volume::Muted, + right: Volume::Muted, + } + } +} + +pub(super) struct Mixer { + pub(super) vol_left: u8, + pub(super) vol_right: u8, + pub(super) ch1: ChannelVol, + pub(super) ch2: ChannelVol, + pub(super) ch3: ChannelVol, + pub(super) ch4: ChannelVol, +} + +impl Default for Mixer { + fn default() -> Self { + Self { + vol_left: 7, + vol_right: 7, + ch1: Default::default(), + ch2: Default::default(), + ch3: Default::default(), + ch4: Default::default(), + } + } +} + +#[derive(Clone, Copy)] +pub(super) struct DacSample { + pub(super) one: f32, + pub(super) two: f32, +} + +impl Default for DacSample { + fn default() -> Self { + Self { one: 0., two: 0. } + } +} diff --git a/src/processor/timer.rs b/src/processor/timer.rs index dfaa1b0..965e074 100644 --- a/src/processor/timer.rs +++ b/src/processor/timer.rs @@ -1,9 +1,8 @@ -use std::time::Duration; - use crate::{ processor::Cpu, util::{get_bit, set_bit}, }; +use std::time::Duration; pub(super) struct Timers { div_counter: usize,