all instructions actually working

This commit is contained in:
Alex Janka 2023-02-02 11:52:45 +11:00
parent c82f00c88b
commit ba8b03693f
4 changed files with 875 additions and 380 deletions

View file

@ -37,7 +37,7 @@ impl CPU {
} }
pub(crate) fn adc(&mut self, first: u8, second: u8) -> u8 { pub(crate) fn adc(&mut self, first: u8, second: u8) -> u8 {
let val = second + self.get_flag(Flags::Carry); let val = second.wrapping_add(self.get_flag(Flags::Carry));
self.add_u8s(first, val) self.add_u8s(first, val)
} }
@ -46,7 +46,7 @@ impl CPU {
} }
pub(crate) fn sbc(&mut self, first: u8, second: u8) -> u8 { pub(crate) fn sbc(&mut self, first: u8, second: u8) -> u8 {
let val = second + self.get_flag(Flags::Carry); let val = second.wrapping_add(self.get_flag(Flags::Carry));
self.sub_u8s(first, val) self.sub_u8s(first, val)
} }
@ -104,6 +104,21 @@ impl CPU {
self.shift(byte, Direction::Right) self.shift(byte, Direction::Right)
} }
pub(crate) fn swap(&mut self, byte: u8) -> u8 {
let swapped = (byte & 0x0F) << 4 | (byte & 0xF0) >> 4;
self.set_or_clear_flag(Flags::Zero, swapped == 0x0);
self.clear_flag(Flags::Carry);
self.clear_flag(Flags::HalfCarry);
self.clear_flag(Flags::NSubtract);
swapped
}
pub(crate) fn bit(&mut self, byte: u8, bit: u8) {
self.set_or_clear_flag(Flags::Zero, !get_bit(byte, bit));
self.clear_flag(Flags::NSubtract);
self.set_flag(Flags::HalfCarry);
}
pub(crate) fn rst(&mut self, address: u8) { pub(crate) fn rst(&mut self, address: u8) {
self.push(self.reg.pc); self.push(self.reg.pc);
self.reg.pc.set_high(0x0); self.reg.pc.set_high(0x0);
@ -118,3 +133,11 @@ impl CPU {
self.reg.pc = self.reg.pc.wrapping_add_signed(jump.into()); self.reg.pc = self.reg.pc.wrapping_add_signed(jump.into());
} }
} }
pub(crate) fn res(byte: u8, bit: u8) -> u8 {
byte & !(1 << bit)
}
pub(crate) fn set(byte: u8, bit: u8) -> u8 {
byte | (1 << bit)
}

View file

@ -1,4 +1,4 @@
use std::ops::{BitAnd, BitOr, BitXor}; use std::ops::{BitAnd, BitOr};
use crate::{ use crate::{
processor::{get_bit, get_rotation_carry, rotate, Direction, Flags, SplitRegister, CPU}, processor::{get_bit, get_rotation_carry, rotate, Direction, Flags, SplitRegister, CPU},
@ -15,11 +15,6 @@ impl CPU {
word word
} }
// pub(crate) fn store_word(&mut self, address: u16, word: u16) {
// self.memory.set(address, word.get_high());
// self.memory.set(address + 1, word.get_low());
// }
pub(crate) fn push(&mut self, word: u16) { pub(crate) fn push(&mut self, word: u16) {
let address = self.reg.sp; let address = self.reg.sp;
self.memory.set(address.wrapping_sub(1), word.get_high()); self.memory.set(address.wrapping_sub(1), word.get_high());
@ -70,13 +65,12 @@ impl CPU {
if carry { if carry {
self.set_flag(Flags::Carry); self.set_flag(Flags::Carry);
} }
self.set_or_clear_flag(Flags::Zero, rotated == 0x0);
self.clear_flag(Flags::HalfCarry);
self.clear_flag(Flags::NSubtract);
return rotated; return rotated;
} }
pub(crate) fn bit(&mut self, byte: u8, bit: u8) {
self.set_or_clear_flag(Flags::Zero, !get_bit(byte, bit));
}
pub(crate) fn get_flag(&mut self, flag: Flags) -> u8 { pub(crate) fn get_flag(&mut self, flag: Flags) -> u8 {
if get_bit(self.reg.af.get_low(), flag as u8) { if get_bit(self.reg.af.get_low(), flag as u8) {
0x1 0x1

View file

@ -53,6 +53,7 @@ impl CPU {
} }
} }
#[allow(dead_code)]
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub enum Reg8 { pub enum Reg8 {
A, A,
@ -175,15 +176,3 @@ fn get_rotation_carry(direction: &Direction) -> u8 {
Direction::Right => 0b10000000, Direction::Right => 0b10000000,
} }
} }
fn swap_nibbles(byte: u8) -> u8 {
(byte & 0x0F) << 4 | (byte & 0xF0) >> 4
}
fn res(byte: u8, bit: u8) -> u8 {
byte & !(1 << bit)
}
fn set(byte: u8, bit: u8) -> u8 {
byte | (1 << bit)
}

File diff suppressed because it is too large Load diff