From e80fd6a73acb3c90ba7b9c77f016f1354659f611 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Mon, 20 Feb 2023 10:01:53 +1100 Subject: [PATCH] nibbles :3 --- src/util.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) 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); + } +}