This commit is contained in:
Alex Janka 2023-02-24 17:10:44 +11:00
parent d6c8be4bc5
commit 5fbc661f5c
4 changed files with 24 additions and 25 deletions

View file

@ -1,4 +1,10 @@
#![feature(exclusive_range_pattern, let_chains, slice_flatten, async_closure)] #![feature(
exclusive_range_pattern,
let_chains,
slice_flatten,
async_closure,
bigint_helper_methods
)]
mod constants; mod constants;
mod processor; mod processor;

View file

@ -32,25 +32,25 @@ impl Cpu {
} }
pub(crate) fn cp(&mut self, first: u8, second: u8) { pub(crate) fn cp(&mut self, first: u8, second: u8) {
self.sub_u8s(first, second); self.sub_u8s(first, second, false);
} }
pub(crate) fn add(&mut self, first: u8, second: u8) -> u8 { pub(crate) fn add(&mut self, first: u8, second: u8) -> u8 {
self.add_u8s(first, second) self.add_u8s(first, second, false)
} }
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.wrapping_add(self.get_flag(Flags::Carry)); let carry = self.is_flag(Flags::Carry);
self.add_u8s(first, val) self.clear_flag(Flags::Carry);
self.add_u8s(first, second, carry)
} }
pub(crate) fn sub(&mut self, first: u8, second: u8) -> u8 { pub(crate) fn sub(&mut self, first: u8, second: u8) -> u8 {
self.sub_u8s(first, second) self.sub_u8s(first, second, false)
} }
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.wrapping_add(self.get_flag(Flags::Carry)); self.sub_u8s(first, second, self.is_flag(Flags::Carry))
self.sub_u8s(first, val)
} }
pub(crate) fn inc(&mut self, reg: Reg8) { pub(crate) fn inc(&mut self, reg: Reg8) {

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
processor::{Cpu, Direction, Flags, SplitRegister}, processor::{Cpu, Direction, Flags, SplitRegister},
util::{get_bit, get_rotation_carry, rotate}, util::{get_bit, get_rotation_carry, rotate, Nibbles},
}; };
use std::ops::{BitAnd, BitOr}; use std::ops::{BitAnd, BitOr};
@ -82,14 +82,6 @@ impl Cpu {
rotated rotated
} }
pub(crate) fn get_flag(&mut self, flag: Flags) -> u8 {
if get_bit(self.reg.af.get_low(), flag as u8) {
0x1
} else {
0x0
}
}
pub(crate) fn is_flag(&self, flag: Flags) -> bool { pub(crate) fn is_flag(&self, flag: Flags) -> bool {
get_bit(self.reg.af.get_low(), flag as u8) get_bit(self.reg.af.get_low(), flag as u8)
} }
@ -128,14 +120,14 @@ impl Cpu {
result result
} }
pub(crate) fn add_u8s(&mut self, first: u8, second: u8) -> u8 { pub(crate) fn add_u8s(&mut self, first: u8, second: u8, with_carry: bool) -> u8 {
let (result, carry) = first.overflowing_add(second); let (result, carry) = first.carrying_add(second, with_carry);
self.clear_flag(Flags::NSubtract); self.clear_flag(Flags::NSubtract);
self.set_or_clear_flag(Flags::Carry, carry); self.set_or_clear_flag(Flags::Carry, carry);
self.set_or_clear_flag(Flags::Zero, result == 0x0); self.set_or_clear_flag(Flags::Zero, result == 0x0);
self.set_or_clear_flag( self.set_or_clear_flag(
Flags::HalfCarry, Flags::HalfCarry,
(((first & 0xF).wrapping_add(second & 0xF)) & 0x10) == 0x10, first.get_low_nibble() + second.get_low_nibble() + if with_carry { 1 } else { 0 } > 0xF,
); );
result result
} }
@ -151,14 +143,15 @@ impl Cpu {
result result
} }
pub(crate) fn sub_u8s(&mut self, first: u8, second: u8) -> u8 { pub(crate) fn sub_u8s(&mut self, first: u8, second: u8, with_carry: bool) -> u8 {
let (result, carry) = first.overflowing_sub(second); let (result, carry) = first.borrowing_sub(second, with_carry);
self.set_flag(Flags::NSubtract); self.set_flag(Flags::NSubtract);
self.set_or_clear_flag(Flags::Carry, carry); self.set_or_clear_flag(Flags::Carry, carry);
self.set_or_clear_flag(Flags::Zero, result == 0x0); self.set_or_clear_flag(Flags::Zero, result == 0x0);
self.set_or_clear_flag( self.set_or_clear_flag(
Flags::HalfCarry, Flags::HalfCarry,
(((first & 0xF).wrapping_sub(second & 0xF)) & 0x10) == 0x10, first.get_low_nibble() - (second.get_low_nibble() + if with_carry { 1 } else { 0 })
> 0xF,
); );
result result
} }

View file

@ -951,7 +951,7 @@ impl Cpu {
} }
0xC6 => { 0xC6 => {
let byte = self.ld_immediate_byte(); let byte = self.ld_immediate_byte();
let val = self.add_u8s(self.reg.get_8(Reg8::A), byte); let val = self.add_u8s(self.reg.get_8(Reg8::A), byte, false);
self.reg.set_8(Reg8::A, val); self.reg.set_8(Reg8::A, val);
2 2
} }
@ -1047,7 +1047,7 @@ impl Cpu {
} }
0xD6 => { 0xD6 => {
let byte = self.ld_immediate_byte(); let byte = self.ld_immediate_byte();
let val = self.sub_u8s(self.reg.get_8(Reg8::A), byte); let val = self.sub_u8s(self.reg.get_8(Reg8::A), byte, false);
self.reg.set_8(Reg8::A, val); self.reg.set_8(Reg8::A, val);
2 2
} }