gb-emu/src/util.rs

53 lines
1.1 KiB
Rust
Raw Normal View History

2023-02-12 09:27:41 +11:00
use crate::processor::Direction;
use std::mem::transmute;
pub(crate) fn as_signed(unsigned: u8) -> i8 {
unsafe {
2023-02-12 09:41:34 +11:00
transmute(unsigned)
2023-02-12 09:27:41 +11:00
}
}
pub(crate) fn get_bit(byte: u8, flag: u8) -> bool {
let mask = 1 << flag;
let got = byte & mask;
2023-02-12 09:41:34 +11:00
got > 0x0
2023-02-12 09:27:41 +11:00
}
pub(crate) fn set_or_clear_bit(byte: u8, flag: u8, condition: bool) -> u8 {
if condition {
set_bit(byte, flag)
} else {
clear_bit(byte, flag)
}
}
pub(crate) fn set_bit(byte: u8, flag: u8) -> u8 {
byte | (1 << flag)
}
pub(crate) fn clear_bit(byte: u8, flag: u8) -> u8 {
byte & (!(1 << flag))
}
pub(crate) fn rotate(byte: u8, direction: &Direction) -> (u8, bool) {
match direction {
Direction::Left => {
let carry = get_bit(byte, 7);
let r = byte << 1;
2023-02-12 09:41:34 +11:00
(r, carry)
2023-02-12 09:27:41 +11:00
}
Direction::Right => {
let carry = get_bit(byte, 0);
let r = byte >> 1;
2023-02-12 09:41:34 +11:00
(r, carry)
2023-02-12 09:27:41 +11:00
}
}
}
pub(crate) fn get_rotation_carry(direction: &Direction) -> u8 {
match direction {
Direction::Left => 0b1,
Direction::Right => 0b10000000,
}
}