-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod apubus; | ||
pub mod timers; | ||
|
||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::tickable::Ticks; | ||
|
||
pub const APU_TIMERS: usize = 3; | ||
|
||
/// One APU timer | ||
pub struct Timer { | ||
/// The top register | ||
top: usize, | ||
|
||
/// Internal tick counter | ||
ticks: usize, | ||
|
||
/// Counter register (4-bit) | ||
cnt: u8, | ||
|
||
/// Divider | ||
div: Ticks, | ||
} | ||
|
||
impl Timer { | ||
pub fn new(div: Ticks) -> Self { | ||
Self { | ||
top: 0, | ||
ticks: 0, | ||
cnt: 0, | ||
div, | ||
} | ||
} | ||
|
||
pub fn reset(&mut self) { | ||
self.ticks = 0; | ||
self.cnt = 0; | ||
} | ||
|
||
pub fn tick(&mut self, ticks: Ticks) { | ||
// This is supposed to be called at the APU frequency, | ||
// the timer will scale down by its divider. | ||
|
||
self.ticks += ticks; | ||
if self.ticks / self.div >= usize::from(self.top) { | ||
self.cnt = (self.cnt + 1) & 0x0F; | ||
self.ticks -= self.top * self.div; | ||
} | ||
} | ||
|
||
pub fn set_top(&mut self, top: u8) { | ||
self.top = usize::from(top) | ||
} | ||
|
||
pub fn get_cnt(&self) -> u8 { | ||
self.cnt | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters