From f57cf2b312cb993050557d0bc0075f5f502fd624 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Mon, 15 Mar 2021 22:51:58 -0500 Subject: [PATCH] fix: switch to bitfield macros in serial.rs --- src/serial.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/serial.rs b/src/serial.rs index 618660d..dcebdbb 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -1,29 +1,41 @@ +use bitfield::bitfield; + #[derive(Debug, Clone, Copy, Default)] pub struct Serial { pub next: u8, pub control: SerialControl, } -#[derive(Debug, Clone, Copy, Default)] -pub struct SerialControl { - transfer_start: bool, - speed: ClockSpeed, // CGB Only - shift: ShiftClock, +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) + } } impl From for SerialControl { fn from(byte: u8) -> Self { - Self { - transfer_start: byte >> 7 == 0x01, - speed: ((byte >> 1) & 0x01).into(), - shift: (byte & 0x01).into(), - } + Self(byte) } } impl From for u8 { - fn from(control: SerialControl) -> Self { - (control.transfer_start as u8) << 7 | (control.speed as u8) << 1 | control.shift as u8 + fn from(ctrl: SerialControl) -> Self { + ctrl.0 } }