diff --git a/src/util.rs b/src/util.rs index 98e8a25..356ac30 100644 --- a/src/util.rs +++ b/src/util.rs @@ -2,9 +2,7 @@ use crate::processor::Direction; use std::mem::transmute; pub(crate) fn as_signed(unsigned: u8) -> i8 { - unsafe { - transmute(unsigned) - } + unsafe { transmute(unsigned) } } pub(crate) fn get_bit(byte: u8, flag: u8) -> bool { @@ -50,3 +48,28 @@ pub(crate) fn get_rotation_carry(direction: &Direction) -> u8 { Direction::Right => 0b10000000, } } + +pub trait Nibbles { + fn get_low_nibble(&self) -> u8; + fn get_high_nibble(&self) -> u8; + fn set_low_nibble(&mut self, val: u8); + fn set_high_nibble(&mut self, val: u8); +} + +impl Nibbles for u8 { + fn get_low_nibble(&self) -> u8 { + *self & 0x0F + } + + fn get_high_nibble(&self) -> u8 { + (*self >> 4) & 0x0F + } + + fn set_low_nibble(&mut self, val: u8) { + *self = (*self & 0xF0) | (val & 0x0F); + } + + fn set_high_nibble(&mut self, val: u8) { + *self = (*self & 0x0F) | (val << 4); + } +}