Skip to content

Commit

Permalink
Linked timer as Option<NonZeroU8>
Browse files Browse the repository at this point in the history
  • Loading branch information
LLeny committed Dec 15, 2024
1 parent 388a457 commit ae6ec26
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/mikey/timers/audio_channel_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ pub struct AudioChannelTimer {
feedback: u8,
shift_register: u8,
output: i8,
linked_timer: Option<usize>,
linked_timer: Option<NonZeroU8>,
is_linked: bool,
count_enabled: bool,
reload_enabled: bool,
disabled: bool,
}

impl AudioChannelTimer{
pub fn new(id: u8, linked_timer: Option<usize>) -> Self {
pub fn new(id: u8, linked_timer: Option<NonZeroU8>) -> Self {
Self {
id,
backup: 0,
Expand All @@ -45,7 +45,7 @@ impl AudioChannelTimer{
}
}

pub fn linked_timer(&self) -> Option<usize> {
pub fn linked_timer(&self) -> Option<NonZeroU8> {
self.linked_timer
}

Expand Down
6 changes: 3 additions & 3 deletions src/mikey/timers/base_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ pub struct BaseTimer {
control_b: u8,
clock_ticks: Option<u32>,
next_trigger_tick: u64,
linked_timer: Option<usize>,
linked_timer: Option<NonZeroU8>,
triggered: bool,
is_linked: bool,
count_enabled: bool,
reload_enabled: bool,
}

impl BaseTimer{
pub fn new(id: u8, linked_timer: Option<usize>) -> Self {
pub fn new(id: u8, linked_timer: Option<NonZeroU8>) -> Self {
Self {
id,
int: 1 << id,
Expand All @@ -38,7 +38,7 @@ impl BaseTimer{
}
}

pub fn linked_timer(&self) -> Option<usize> {
pub fn linked_timer(&self) -> Option<NonZeroU8> {
self.linked_timer
}

Expand Down
7 changes: 4 additions & 3 deletions src/mikey/timers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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;
use crate::mikey::*;

const TIMER_TICKS_COUNT: u16 = (0.000001 / CRYSTAL_TICK_LENGTH) as u16; // 1us/62.5ns

const TIMER_LINKS: [Option<usize>; 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<NonZeroU8>; 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;
Expand Down Expand Up @@ -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),
}
Expand Down

0 comments on commit ae6ec26

Please sign in to comment.