Split out to a timer controller to keep the GBA struct zero sized

This commit is contained in:
Gwilym Kuiper 2021-11-22 19:27:08 +00:00
parent f7a10677dd
commit 75bd475f08
3 changed files with 26 additions and 7 deletions

View file

@ -17,7 +17,8 @@ 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(&mut gba.timers.timer0); let mut timers = gba.timers.timers();
let mut mixer = gba.mixer.mixer(&mut timers.timer0);
mixer.enable(); mixer.enable();
let channel = SoundChannel::new(DEAD_CODE); let channel = SoundChannel::new(DEAD_CODE);

View file

@ -14,7 +14,7 @@ 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 mut timer_controller = gba.timers; let mut timer_controller = gba.timers.timers();
let mut timer = timer_controller.timer1; let mut timer = timer_controller.timer1;
timer.set_enabled(true); timer.set_enabled(true);

View file

@ -33,20 +33,21 @@ impl Divider {
} }
} }
#[non_exhaustive]
pub struct Timer { pub struct Timer {
timer_number: u16, timer_number: u16,
} }
#[non_exhaustive] #[non_exhaustive]
pub struct TimerController { pub struct Timers {
pub timer0: Timer, pub timer0: Timer,
pub timer1: Timer, pub timer1: Timer,
pub timer2: Timer, pub timer2: Timer,
pub timer3: Timer, pub timer3: Timer,
} }
impl TimerController { impl Timers {
pub(crate) const unsafe fn new() -> Self { pub(crate) unsafe fn new() -> Self {
Self { Self {
timer0: Timer::new(0), timer0: Timer::new(0),
timer1: Timer::new(1), timer1: Timer::new(1),
@ -57,8 +58,12 @@ impl TimerController {
} }
impl Timer { impl Timer {
const unsafe fn new(timer_number: u16) -> Self { unsafe fn new(timer_number: u16) -> Self {
Self { timer_number } let new_timer = Self { timer_number };
new_timer.data_register().set(0);
new_timer.control_register().set(0);
new_timer
} }
pub fn set_overflow_amount(&mut self, n: u16) { pub fn set_overflow_amount(&mut self, n: u16) {
@ -97,3 +102,16 @@ impl Timer {
self.timer_number as usize self.timer_number as usize
} }
} }
#[non_exhaustive]
pub struct TimerController {}
impl TimerController {
pub(crate) const fn new() -> Self {
Self {}
}
pub fn timers(&mut self) -> Timers {
unsafe { Timers::new() }
}
}