gb/src/serial.rs

87 lines
1.7 KiB
Rust
Raw Normal View History

use bitfield::bitfield;
2021-01-19 07:36:44 +00:00
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct Serial {
2021-06-09 18:43:46 +00:00
/// 0xFF01 | SB - Serial Transfer Data
pub(crate) next: u8,
2021-06-09 18:43:46 +00:00
/// 0xFF02 | SC - Serial Transfer Control
pub(crate) ctrl: SerialControl,
2021-01-19 07:36:44 +00:00
}
bitfield! {
pub struct SerialControl(u8);
impl Debug;
pub transfer_start, set_transfer_start: 7;
// pub from into ClockSpeed, speed, set_speed: 1; (CGB Only)
pub from into ShiftClock, shift, set_shift: 0;
}
impl Copy for SerialControl {}
impl Clone for SerialControl {
fn clone(&self) -> Self {
*self
}
}
impl Default for SerialControl {
fn default() -> Self {
Self(0)
}
2021-01-19 07:36:44 +00:00
}
impl From<u8> for SerialControl {
fn from(byte: u8) -> Self {
Self(byte)
2021-01-19 07:36:44 +00:00
}
}
impl From<SerialControl> for u8 {
fn from(ctrl: SerialControl) -> Self {
ctrl.0
2021-01-19 07:36:44 +00:00
}
}
#[derive(Debug, Clone, Copy)]
enum ShiftClock {
External = 0,
Internal = 1,
}
impl Default for ShiftClock {
fn default() -> Self {
Self::External
}
}
impl From<u8> for ShiftClock {
fn from(byte: u8) -> Self {
match byte & 0b01 {
2021-01-19 07:36:44 +00:00
0b00 => Self::External,
0b01 => Self::Internal,
_ => unreachable!("{:#04X} is not a valid value for ShiftClock", byte),
}
}
}
#[derive(Debug, Clone, Copy)]
enum ClockSpeed {
Normal = 0,
Fast = 1,
}
impl Default for ClockSpeed {
fn default() -> Self {
Self::Normal
}
}
impl From<u8> for ClockSpeed {
fn from(byte: u8) -> Self {
match byte & 0b01 {
2021-01-19 07:36:44 +00:00
0b00 => Self::Normal,
0b01 => Self::Fast,
_ => unreachable!("{:#04X} is not a valid value for ClockSpeed", byte),
}
}
}