2021-03-16 03:41:41 +00:00
|
|
|
use bitfield::bitfield;
|
|
|
|
|
2021-01-19 04:54:38 +00:00
|
|
|
use crate::instruction::Cycles;
|
|
|
|
|
2021-01-03 07:21:19 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct Timer {
|
|
|
|
pub control: TimerControl,
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:54:38 +00:00
|
|
|
impl Timer {
|
|
|
|
pub fn step(&mut self, _cycles: Cycles) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 07:21:19 +00:00
|
|
|
impl Default for Timer {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
control: 0x00.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
2021-03-16 03:48:15 +00:00
|
|
|
pub enum TimerSpeed {
|
2021-01-19 07:36:44 +00:00
|
|
|
Freq4096Hz = 0,
|
|
|
|
Freq262144Hz = 1,
|
|
|
|
Freq65536Hz = 2,
|
|
|
|
Freq16384Hz = 3,
|
2021-01-03 07:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u8> for TimerSpeed {
|
|
|
|
fn from(byte: u8) -> Self {
|
|
|
|
match byte {
|
2021-03-16 04:51:40 +00:00
|
|
|
0b00 => Self::Freq4096Hz,
|
|
|
|
0b01 => Self::Freq262144Hz,
|
|
|
|
0b10 => Self::Freq65536Hz,
|
|
|
|
0b11 => Self::Freq16384Hz,
|
|
|
|
_ => unreachable!("{:#04X} is not a valid value for TimerSpeed", byte),
|
2021-01-03 07:21:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 03:41:41 +00:00
|
|
|
impl From<TimerSpeed> for u8 {
|
|
|
|
fn from(speed: TimerSpeed) -> Self {
|
2021-03-21 02:10:48 +00:00
|
|
|
speed as Self
|
2021-03-16 03:41:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bitfield! {
|
|
|
|
pub struct TimerControl(u8);
|
|
|
|
impl Debug;
|
2021-03-16 03:48:15 +00:00
|
|
|
pub enabled, set_enabled: 2;
|
|
|
|
pub from into TimerSpeed, speed, set_speed: 1, 0;
|
2021-03-16 03:41:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Copy for TimerControl {}
|
|
|
|
impl Clone for TimerControl {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TimerControl {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(0)
|
|
|
|
}
|
2021-01-03 07:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u8> for TimerControl {
|
|
|
|
fn from(byte: u8) -> Self {
|
2021-03-16 03:41:41 +00:00
|
|
|
Self(byte)
|
2021-01-03 07:21:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<TimerControl> for u8 {
|
2021-03-16 03:41:41 +00:00
|
|
|
fn from(ctrl: TimerControl) -> Self {
|
|
|
|
ctrl.0
|
2021-01-03 07:21:19 +00:00
|
|
|
}
|
|
|
|
}
|