From ae6ec26b6849eca93ffb4afde446e08074d73599 Mon Sep 17 00:00:00 2001 From: LLeny Date: Sun, 15 Dec 2024 16:33:34 +0800 Subject: [PATCH] Linked timer as Option --- src/mikey/timers/audio_channel_timer.rs | 6 +++--- src/mikey/timers/base_timer.rs | 6 +++--- src/mikey/timers/mod.rs | 7 ++++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/mikey/timers/audio_channel_timer.rs b/src/mikey/timers/audio_channel_timer.rs index ac5037c..575ab85 100644 --- a/src/mikey/timers/audio_channel_timer.rs +++ b/src/mikey/timers/audio_channel_timer.rs @@ -16,7 +16,7 @@ pub struct AudioChannelTimer { feedback: u8, shift_register: u8, output: i8, - linked_timer: Option, + linked_timer: Option, is_linked: bool, count_enabled: bool, reload_enabled: bool, @@ -24,7 +24,7 @@ pub struct AudioChannelTimer { } impl AudioChannelTimer{ - pub fn new(id: u8, linked_timer: Option) -> Self { + pub fn new(id: u8, linked_timer: Option) -> Self { Self { id, backup: 0, @@ -45,7 +45,7 @@ impl AudioChannelTimer{ } } - pub fn linked_timer(&self) -> Option { + pub fn linked_timer(&self) -> Option { self.linked_timer } diff --git a/src/mikey/timers/base_timer.rs b/src/mikey/timers/base_timer.rs index 48c35cf..60a8828 100644 --- a/src/mikey/timers/base_timer.rs +++ b/src/mikey/timers/base_timer.rs @@ -12,7 +12,7 @@ pub struct BaseTimer { control_b: u8, clock_ticks: Option, next_trigger_tick: u64, - linked_timer: Option, + linked_timer: Option, triggered: bool, is_linked: bool, count_enabled: bool, @@ -20,7 +20,7 @@ pub struct BaseTimer { } impl BaseTimer{ - pub fn new(id: u8, linked_timer: Option) -> Self { + pub fn new(id: u8, linked_timer: Option) -> Self { Self { id, int: 1 << id, @@ -38,7 +38,7 @@ impl BaseTimer{ } } - pub fn linked_timer(&self) -> Option { + pub fn linked_timer(&self) -> Option { self.linked_timer } diff --git a/src/mikey/timers/mod.rs b/src/mikey/timers/mod.rs index d0f5546..809363f 100644 --- a/src/mikey/timers/mod.rs +++ b/src/mikey/timers/mod.rs @@ -1,6 +1,7 @@ pub mod audio_channel_timer; pub mod base_timer; +use core::num::{NonZero, NonZeroU8}; use audio_channel_timer::AudioChannelTimer; use base_timer::BaseTimer; use log::trace; @@ -8,7 +9,7 @@ use crate::mikey::*; const TIMER_TICKS_COUNT: u16 = (0.000001 / CRYSTAL_TICK_LENGTH) as u16; // 1us/62.5ns -const TIMER_LINKS: [Option; 12] = [Some(2), Some(3), Some(4), Some(5), None, Some(7), None, Some(8), Some(9), Some(10), Some(11), Some(1)]; +const TIMER_LINKS: [Option; 12] = [Some(NonZero::new(2).unwrap()), Some(NonZero::new(3).unwrap()), Some(NonZero::new(4).unwrap()), Some(NonZero::new(5).unwrap()), None, Some(NonZero::new(7).unwrap()), None, Some(NonZero::new(8).unwrap()), Some(NonZero::new(9).unwrap()), Some(NonZero::new(10).unwrap()), Some(NonZero::new(11).unwrap()), Some(NonZero::new(1).unwrap())]; const TIMER_COUNT: u8 = 12; const CTRLA_INTERRUPT_BIT: u8 = 0b10000000; @@ -105,8 +106,8 @@ impl Timers { } #[inline(always)] - fn tick_linked_timer(&mut self, timer_id: usize) -> u8 { - match &mut self.timers[timer_id] { + fn tick_linked_timer(&mut self, timer_id: NonZeroU8) -> u8 { + match &mut self.timers[timer_id.get() as usize] { TimerType::Base(t) => tick_linked_timer!(self, t), TimerType::Audio(t) => tick_linked_timer!(self, t), }