Skip to content

Commit

Permalink
ALU
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Nov 14, 2023
1 parent 38fb16b commit 4b3b3c0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::mode::GBMode;
use crate::registers::Registers;
use crate::registers::{Registers, Flags};

pub struct CPU {
reg: Registers
Expand Down Expand Up @@ -38,7 +38,17 @@ impl CPU {
0x45 => self.reg.b = self.reg.l,
0x46 => {},
0x47 => self.reg.b = self.reg.a,
_ => {},
code => panic!("Instruction {:2X} is unknown!", code),
}
}

fn alu_add(&mut self, x: u8) {
let a = self.reg.a;
let x = a.wrapping_add(x);
self.reg.set_flag(Flags::C, u16::from(a) + u16::from(x) > 0xFF);
self.reg.set_flag(Flags::H, (a & 0x0F) + (a & 0x0F) > 0x0F);
self.reg.set_flag(Flags::N, false);
self.reg.set_flag(Flags::Z, x == 0x00);
self.reg.a = x;
}
}
14 changes: 13 additions & 1 deletion src/registers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bitflags::{bitflags};
use bitflags::bitflags;
use crate::mode::GBMode;

pub struct Registers {
Expand Down Expand Up @@ -28,6 +28,18 @@ bitflags! {
}

impl Registers {
pub fn get_flag(&self, flag: Flags) -> bool {
self.f.contains(flag)
}

pub fn set_flag(&mut self, flag: Flags, state: bool) {
if state {
self.f |= flag;
} else {
self.f &= !flag;
}
}

pub fn new(mode: GBMode) -> Registers {
match mode {
GBMode::Classic => {
Expand Down

0 comments on commit 4b3b3c0

Please sign in to comment.