fix: implement bitfield macros in timer.rs
This commit is contained in:
parent
1a8506c04f
commit
f17bb032cc
45
src/timer.rs
45
src/timer.rs
|
@ -1,3 +1,5 @@
|
||||||
|
use bitfield::bitfield;
|
||||||
|
|
||||||
use crate::instruction::Cycles;
|
use crate::instruction::Cycles;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
@ -34,32 +36,45 @@ impl From<u8> for TimerSpeed {
|
||||||
0x01 => Self::Freq262144Hz,
|
0x01 => Self::Freq262144Hz,
|
||||||
0x10 => Self::Freq65536Hz,
|
0x10 => Self::Freq65536Hz,
|
||||||
0x11 => Self::Freq16384Hz,
|
0x11 => Self::Freq16384Hz,
|
||||||
_ => unreachable!(),
|
_ => unreachable!("{:04X} is not a valid representation of TimerSpeed", byte),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
impl From<TimerSpeed> for u8 {
|
||||||
pub struct TimerControl {
|
fn from(speed: TimerSpeed) -> Self {
|
||||||
enabled: bool,
|
speed as u8
|
||||||
speed: TimerSpeed,
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bitfield! {
|
||||||
|
pub struct TimerControl(u8);
|
||||||
|
impl Debug;
|
||||||
|
enabled, set_enabled: 2;
|
||||||
|
from into TimerSpeed, speed, set_speed: 1, 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Copy for TimerControl {}
|
||||||
|
impl Clone for TimerControl {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
*self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for TimerControl {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self(0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<u8> for TimerControl {
|
impl From<u8> for TimerControl {
|
||||||
fn from(byte: u8) -> Self {
|
fn from(byte: u8) -> Self {
|
||||||
let byte = byte & 0x07; // Clear everything but last 3 bits
|
Self(byte)
|
||||||
|
|
||||||
Self {
|
|
||||||
enabled: (byte >> 2) == 0x01,
|
|
||||||
speed: (byte & 0x03).into(), // Clear everything but last 2 bits
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<TimerControl> for u8 {
|
impl From<TimerControl> for u8 {
|
||||||
fn from(control: TimerControl) -> Self {
|
fn from(ctrl: TimerControl) -> Self {
|
||||||
let byte: u8 = control.speed as u8; // Get bit 1 and 0.
|
ctrl.0
|
||||||
|
|
||||||
(byte & !(1u8 << 2)) | ((control.enabled as u8) << 2) // specifically manipulate bit 2
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue