move stuff to util file

This commit is contained in:
Alex Janka 2023-02-12 09:27:41 +11:00
parent d3ee58131e
commit 03faea6ca9
8 changed files with 77 additions and 63 deletions

View file

@ -1,6 +1,7 @@
#![feature(exclusive_range_pattern, let_chains)]
mod processor;
mod util;
use clap::{ArgGroup, Parser};
use minifb::{Window, WindowOptions};

View file

@ -5,7 +5,8 @@ use self::{
},
};
use crate::{
processor::{clear_bit, get_bit, set_bit, set_or_clear_bit, SplitRegister, CPU},
processor::{SplitRegister, CPU},
util::{clear_bit, get_bit, set_bit, set_or_clear_bit},
FACTOR, HEIGHT, WIDTH,
};
use minifb::{Window, WindowOptions};

View file

@ -1,4 +1,4 @@
use crate::processor::as_signed;
use crate::util::as_signed;
#[derive(Debug, PartialEq, Clone, Copy)]
pub(super) enum DrawMode {

View file

@ -1,4 +1,7 @@
use crate::processor::{get_bit, Direction, Flags, Reg8, SplitRegister, CPU};
use crate::{
processor::{get_bit, Direction, Flags, Reg8, SplitRegister, CPU},
util::{clear_bit, set_bit},
};
impl CPU {
pub(crate) fn and(&mut self, first: u8, second: u8) -> u8 {
@ -135,9 +138,9 @@ impl CPU {
}
pub(crate) fn res(byte: u8, bit: u8) -> u8 {
byte & !(1 << bit)
clear_bit(byte, bit)
}
pub(crate) fn set(byte: u8, bit: u8) -> u8 {
byte | (1 << bit)
set_bit(byte, bit)
}

View file

@ -1,4 +1,7 @@
use crate::processor::{get_bit, get_rotation_carry, rotate, Direction, Flags, SplitRegister, CPU};
use crate::{
processor::{get_bit, Direction, Flags, SplitRegister, CPU},
util::{get_rotation_carry, rotate},
};
use std::ops::{BitAnd, BitOr};
impl CPU {

View file

@ -1,6 +1,8 @@
use self::gpu::GPU;
use self::memory::Memory;
use crate::{processor::instructions::instructions::set, verbose_println};
use self::{gpu::GPU, memory::Memory};
use crate::{
util::{clear_bit, get_bit},
verbose_println,
};
use minifb::Window;
use std::{mem::transmute, time::Duration};
@ -277,53 +279,3 @@ impl SplitRegister for u16 {
*self = (*self & !0xff00) | (val as u16) << 8;
}
}
fn as_signed(unsigned: u8) -> i8 {
unsafe {
return transmute(unsigned);
}
}
fn get_bit(byte: u8, flag: u8) -> bool {
let mask = 1 << flag;
let got = byte & mask;
return got > 0x0;
}
fn set_or_clear_bit(byte: u8, flag: u8, condition: bool) -> u8 {
if condition {
set_bit(byte, flag)
} else {
clear_bit(byte, flag)
}
}
fn set_bit(byte: u8, flag: u8) -> u8 {
byte | (1 << flag)
}
fn clear_bit(byte: u8, flag: u8) -> u8 {
byte & (!(1 << flag))
}
fn rotate(byte: u8, direction: &Direction) -> (u8, bool) {
match direction {
Direction::Left => {
let carry = get_bit(byte, 7);
let r = byte << 1;
return (r, carry);
}
Direction::Right => {
let carry = get_bit(byte, 0);
let r = byte >> 1;
return (r, carry);
}
}
}
fn get_rotation_carry(direction: &Direction) -> u8 {
match direction {
Direction::Left => 0b1,
Direction::Right => 0b10000000,
}
}

View file

@ -1,7 +1,9 @@
use crate::processor::{
as_signed,
instructions::instructions::{res, set},
Flags, Reg8, SplitRegister, CPU,
use crate::{
processor::{
instructions::instructions::{res, set},
Flags, Reg8, SplitRegister, CPU,
},
util::as_signed,
};
impl CPU {

52
src/util.rs Normal file
View file

@ -0,0 +1,52 @@
use crate::processor::Direction;
use std::mem::transmute;
pub(crate) fn as_signed(unsigned: u8) -> i8 {
unsafe {
return transmute(unsigned);
}
}
pub(crate) fn get_bit(byte: u8, flag: u8) -> bool {
let mask = 1 << flag;
let got = byte & mask;
return got > 0x0;
}
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;
return (r, carry);
}
Direction::Right => {
let carry = get_bit(byte, 0);
let r = byte >> 1;
return (r, carry);
}
}
}
pub(crate) fn get_rotation_carry(direction: &Direction) -> u8 {
match direction {
Direction::Left => 0b1,
Direction::Right => 0b10000000,
}
}