Make sound now take the first class timer object

This commit is contained in:
Gwilym Kuiper 2021-11-18 22:22:55 +00:00
parent 8876c47aa9
commit bfc02e0aa8
6 changed files with 20 additions and 19 deletions

View file

@ -17,7 +17,7 @@ fn main() -> ! {
let mut input = ButtonController::new(); let mut input = ButtonController::new();
let vblank_provider = agb::interrupt::VBlank::get(); let vblank_provider = agb::interrupt::VBlank::get();
let mut mixer = gba.mixer.mixer(); let mut mixer = gba.mixer.mixer(gba.timers.timer0);
mixer.enable(); mixer.enable();
let channel = SoundChannel::new(DEAD_CODE); let channel = SoundChannel::new(DEAD_CODE);

View file

@ -14,11 +14,11 @@ fn main() -> ! {
let mut gba = Gba::new(); let mut gba = Gba::new();
let vblank_provider = agb::interrupt::VBlank::get(); let vblank_provider = agb::interrupt::VBlank::get();
let timer_controller = gba.timer; let timer_controller = gba.timers;
let mut timer = timer_controller.timer1; let mut timer = timer_controller.timer1;
timer.set_enabled(true); timer.set_enabled(true);
let mut mixer = gba.mixer.mixer(); let mut mixer = gba.mixer.mixer(timer_controller.timer0);
mixer.enable(); mixer.enable();
let mut channel = SoundChannel::new(LET_IT_IN); let mut channel = SoundChannel::new(LET_IT_IN);

View file

@ -63,7 +63,7 @@ pub struct Gba {
pub display: display::Display, pub display: display::Display,
pub sound: sound::dmg::Sound, pub sound: sound::dmg::Sound,
pub mixer: sound::mixer::MixerController, pub mixer: sound::mixer::MixerController,
pub timer: timer::TimerController, pub timers: timer::TimerController,
} }
impl Gba { impl Gba {
@ -76,7 +76,7 @@ impl Gba {
display: display::Display::new(), display: display::Display::new(),
sound: sound::dmg::Sound::new(), sound: sound::dmg::Sound::new(),
mixer: sound::mixer::MixerController::new(), mixer: sound::mixer::MixerController::new(),
timer: timer::TimerController::new(), timers: timer::TimerController::new(),
} }
} }
} }

View file

@ -1,4 +1,5 @@
use crate::memory_mapped::MemoryMapped; use crate::memory_mapped::MemoryMapped;
use crate::timer::Timer;
const fn dma_source_addr(dma: usize) -> usize { const fn dma_source_addr(dma: usize) -> usize {
0x0400_00b0 + 0x0c * dma 0x0400_00b0 + 0x0c * dma
@ -24,10 +25,6 @@ const DMA2_CONTROL: MemoryMapped<u16> = unsafe { MemoryMapped::new(dma_control_a
const FIFOA_DEST_ADDR: u32 = 0x0400_00a0; const FIFOA_DEST_ADDR: u32 = 0x0400_00a0;
const FIFOB_DEST_ADDR: u32 = 0x0400_00a4; const FIFOB_DEST_ADDR: u32 = 0x0400_00a4;
// Similarly for proper timer support
const TIMER0_COUNTER: MemoryMapped<u16> = unsafe { MemoryMapped::new(0x0400_0100) };
const TIMER0_CONTROL: MemoryMapped<u16> = unsafe { MemoryMapped::new(0x0400_0102) };
const SOUND_CONTROL: MemoryMapped<u16> = unsafe { MemoryMapped::new(0x0400_0082) }; const SOUND_CONTROL: MemoryMapped<u16> = unsafe { MemoryMapped::new(0x0400_0082) };
const SOUND_CONTROL_X: MemoryMapped<u16> = unsafe { MemoryMapped::new(0x0400_0084) }; const SOUND_CONTROL_X: MemoryMapped<u16> = unsafe { MemoryMapped::new(0x0400_0084) };
@ -94,9 +91,7 @@ pub(super) fn set_sound_control_register_for_mixer() {
SOUND_CONTROL_X.set(1 << 7); SOUND_CONTROL_X.set(1 << 7);
} }
pub(super) fn set_timer_counter_for_frequency_and_enable(frequency: i32) { pub(super) fn set_timer_counter_for_frequency_and_enable(timer: &mut Timer, frequency: i32) {
let counter = 65536 - (16777216 / frequency); timer.set_overflow_amount((16777216 / frequency) as u16);
TIMER0_COUNTER.set(counter as u16); timer.set_enabled(true);
TIMER0_CONTROL.set(1 << 7); // enable the timer
} }

View file

@ -5,6 +5,7 @@ pub use sw_mixer::ChannelId;
pub use sw_mixer::Mixer; pub use sw_mixer::Mixer;
use crate::number::Num; use crate::number::Num;
use crate::timer::Timer;
#[non_exhaustive] #[non_exhaustive]
pub struct MixerController {} pub struct MixerController {}
@ -14,8 +15,8 @@ impl MixerController {
MixerController {} MixerController {}
} }
pub fn mixer(&mut self) -> Mixer { pub fn mixer(&mut self, timer: Timer) -> Mixer {
Mixer::new() Mixer::new(timer)
} }
} }

View file

@ -2,6 +2,7 @@ use super::hw;
use super::hw::LeftOrRight; use super::hw::LeftOrRight;
use super::{SoundChannel, SoundPriority}; use super::{SoundChannel, SoundPriority};
use crate::number::Num; use crate::number::Num;
use crate::timer::Timer;
// Defined in mixer.s // Defined in mixer.s
extern "C" { extern "C" {
@ -22,21 +23,25 @@ pub struct Mixer {
buffer: MixerBuffer, buffer: MixerBuffer,
channels: [Option<SoundChannel>; 8], channels: [Option<SoundChannel>; 8],
indices: [i32; 8], indices: [i32; 8],
timer: Timer,
} }
pub struct ChannelId(usize, i32); pub struct ChannelId(usize, i32);
impl Mixer { impl Mixer {
pub(super) fn new() -> Self { pub(super) fn new(timer: Timer) -> Self {
Mixer { Mixer {
buffer: MixerBuffer::new(), buffer: MixerBuffer::new(),
channels: Default::default(), channels: Default::default(),
indices: Default::default(), indices: Default::default(),
timer,
} }
} }
pub fn enable(&self) { pub fn enable(&mut self) {
hw::set_timer_counter_for_frequency_and_enable(SOUND_FREQUENCY); hw::set_timer_counter_for_frequency_and_enable(&mut self.timer, SOUND_FREQUENCY);
hw::set_sound_control_register_for_mixer(); hw::set_sound_control_register_for_mixer();
} }