Skip to content

Commit

Permalink
16-bit register operations
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Nov 14, 2023
1 parent 32c3d98 commit 603efe7
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::mode::GBMode;

pub struct Registers {
pub a: u8,
f: Flags,
f: u8,
pub b: u8,
pub c: u8,
pub d: u8,
Expand All @@ -28,6 +28,44 @@ bitflags! {
}

impl Registers {
// Registers can be paired to be used for 16-bit operations
// A+F, B+C, D+E, H+L
pub fn get_af(&self) -> u16 {
(u16::from(self.a) << 8) | u16::from(self.f)
}

pub fn get_bc(&self) -> u16 {
(u16::from(self.b) << 8) | u16::from(self.c)
}

pub fn get_de(&self) -> u16 {
(u16::from(self.d) << 8) | u16::from(self.e)
}

pub fn get_hl(&self) -> u16 {
(u16::from(self.h) << 8) | u16::from(self.l)
}

pub fn set_af(&mut self, x: u16) {
self.a = (x >> 8) as u8;
self.f = (x & 0x00F0) as u8;
}

pub fn set_bc(&mut self, x: u16) {
self.b = (x >> 8) as u8;
self.c = (x & 0x00FF) as u8;
}

pub fn set_de(&mut self, x: u16) {
self.d = (x >> 8) as u8;
self.e = (x & 0x00FF) as u8;
}

pub fn set_hl(&mut self, x: u16) {
self.h = (x >> 8) as u8;
self.l = (x & 0x00FF) as u8;
}

pub fn get_flag(&self, flag: Flags) -> bool {
self.f.contains(flag)
}
Expand All @@ -45,7 +83,7 @@ impl Registers {
GBMode::Classic => {
Registers {
a: 0x01,
f: Flags::C | Flags::H | Flags::Z,
f: (Flags::C | Flags::H | Flags::Z).bits(),
b: 0x00,
c: 0x13,
d: 0x00,
Expand Down

0 comments on commit 603efe7

Please sign in to comment.