From bfc02e0aa8abb39b87601b1beaa0bb1f612d08bb Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Thu, 18 Nov 2021 22:22:55 +0000 Subject: [PATCH] Make sound now take the first class timer object --- agb/examples/mixer_basic.rs | 2 +- agb/examples/stereo_sound.rs | 4 ++-- agb/src/lib.rs | 4 ++-- agb/src/sound/mixer/hw.rs | 13 ++++--------- agb/src/sound/mixer/mod.rs | 5 +++-- agb/src/sound/mixer/sw_mixer.rs | 11 ++++++++--- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/agb/examples/mixer_basic.rs b/agb/examples/mixer_basic.rs index e9b03dcb..a9761403 100644 --- a/agb/examples/mixer_basic.rs +++ b/agb/examples/mixer_basic.rs @@ -17,7 +17,7 @@ fn main() -> ! { let mut input = ButtonController::new(); let vblank_provider = agb::interrupt::VBlank::get(); - let mut mixer = gba.mixer.mixer(); + let mut mixer = gba.mixer.mixer(gba.timers.timer0); mixer.enable(); let channel = SoundChannel::new(DEAD_CODE); diff --git a/agb/examples/stereo_sound.rs b/agb/examples/stereo_sound.rs index 4e581500..8050f602 100644 --- a/agb/examples/stereo_sound.rs +++ b/agb/examples/stereo_sound.rs @@ -14,11 +14,11 @@ fn main() -> ! { let mut gba = Gba::new(); let vblank_provider = agb::interrupt::VBlank::get(); - let timer_controller = gba.timer; + let timer_controller = gba.timers; let mut timer = timer_controller.timer1; timer.set_enabled(true); - let mut mixer = gba.mixer.mixer(); + let mut mixer = gba.mixer.mixer(timer_controller.timer0); mixer.enable(); let mut channel = SoundChannel::new(LET_IT_IN); diff --git a/agb/src/lib.rs b/agb/src/lib.rs index a3831b2e..82bf68ef 100644 --- a/agb/src/lib.rs +++ b/agb/src/lib.rs @@ -63,7 +63,7 @@ pub struct Gba { pub display: display::Display, pub sound: sound::dmg::Sound, pub mixer: sound::mixer::MixerController, - pub timer: timer::TimerController, + pub timers: timer::TimerController, } impl Gba { @@ -76,7 +76,7 @@ impl Gba { display: display::Display::new(), sound: sound::dmg::Sound::new(), mixer: sound::mixer::MixerController::new(), - timer: timer::TimerController::new(), + timers: timer::TimerController::new(), } } } diff --git a/agb/src/sound/mixer/hw.rs b/agb/src/sound/mixer/hw.rs index c0556383..c617dc40 100644 --- a/agb/src/sound/mixer/hw.rs +++ b/agb/src/sound/mixer/hw.rs @@ -1,4 +1,5 @@ use crate::memory_mapped::MemoryMapped; +use crate::timer::Timer; const fn dma_source_addr(dma: usize) -> usize { 0x0400_00b0 + 0x0c * dma @@ -24,10 +25,6 @@ const DMA2_CONTROL: MemoryMapped = unsafe { MemoryMapped::new(dma_control_a const FIFOA_DEST_ADDR: u32 = 0x0400_00a0; const FIFOB_DEST_ADDR: u32 = 0x0400_00a4; -// Similarly for proper timer support -const TIMER0_COUNTER: MemoryMapped = unsafe { MemoryMapped::new(0x0400_0100) }; -const TIMER0_CONTROL: MemoryMapped = unsafe { MemoryMapped::new(0x0400_0102) }; - const SOUND_CONTROL: MemoryMapped = unsafe { MemoryMapped::new(0x0400_0082) }; const SOUND_CONTROL_X: MemoryMapped = unsafe { MemoryMapped::new(0x0400_0084) }; @@ -94,9 +91,7 @@ pub(super) fn set_sound_control_register_for_mixer() { SOUND_CONTROL_X.set(1 << 7); } -pub(super) fn set_timer_counter_for_frequency_and_enable(frequency: i32) { - let counter = 65536 - (16777216 / frequency); - TIMER0_COUNTER.set(counter as u16); - - TIMER0_CONTROL.set(1 << 7); // enable the timer +pub(super) fn set_timer_counter_for_frequency_and_enable(timer: &mut Timer, frequency: i32) { + timer.set_overflow_amount((16777216 / frequency) as u16); + timer.set_enabled(true); } diff --git a/agb/src/sound/mixer/mod.rs b/agb/src/sound/mixer/mod.rs index c5441795..e6f598ff 100644 --- a/agb/src/sound/mixer/mod.rs +++ b/agb/src/sound/mixer/mod.rs @@ -5,6 +5,7 @@ pub use sw_mixer::ChannelId; pub use sw_mixer::Mixer; use crate::number::Num; +use crate::timer::Timer; #[non_exhaustive] pub struct MixerController {} @@ -14,8 +15,8 @@ impl MixerController { MixerController {} } - pub fn mixer(&mut self) -> Mixer { - Mixer::new() + pub fn mixer(&mut self, timer: Timer) -> Mixer { + Mixer::new(timer) } } diff --git a/agb/src/sound/mixer/sw_mixer.rs b/agb/src/sound/mixer/sw_mixer.rs index c90a071d..f35bf6d5 100644 --- a/agb/src/sound/mixer/sw_mixer.rs +++ b/agb/src/sound/mixer/sw_mixer.rs @@ -2,6 +2,7 @@ use super::hw; use super::hw::LeftOrRight; use super::{SoundChannel, SoundPriority}; use crate::number::Num; +use crate::timer::Timer; // Defined in mixer.s extern "C" { @@ -22,21 +23,25 @@ pub struct Mixer { buffer: MixerBuffer, channels: [Option; 8], indices: [i32; 8], + + timer: Timer, } pub struct ChannelId(usize, i32); impl Mixer { - pub(super) fn new() -> Self { + pub(super) fn new(timer: Timer) -> Self { Mixer { buffer: MixerBuffer::new(), channels: Default::default(), indices: Default::default(), + + timer, } } - pub fn enable(&self) { - hw::set_timer_counter_for_frequency_and_enable(SOUND_FREQUENCY); + pub fn enable(&mut self) { + hw::set_timer_counter_for_frequency_and_enable(&mut self.timer, SOUND_FREQUENCY); hw::set_sound_control_register_for_mixer(); }