Skip to content

Commit

Permalink
Added CP opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
velllu committed Sep 6, 2023
1 parent f31ad33 commit 82cbfc0
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Cycles = u8;

// NAMING CONVENTIONS:
// r -> one byte register
// ra -> register a in particular
// rr -> two byte register
// ii -> the two bytes of immediate data
// i -> the first byte of immediate data
Expand Down Expand Up @@ -78,20 +79,24 @@ impl GameBoy {
}
}

// Bitwise operation functions (not all of them as of now)
// CP functions
impl GameBoy {
pub(crate) fn xor_r(&mut self, register: OneByteRegister) {
let register_a = *self.registers.get_r(OneByteRegister::A);
let register = self.registers.get_r(register);
pub(crate) fn compare_ra_to_i(&mut self) {
self.flags.update_zero_flag(self.registers.a.wrapping_sub(self.next(1)));
}

let result = *register ^ register_a;
*register = result;
pub(crate) fn compare_ra_to_r(&mut self, register: OneByteRegister) {
let register = *self.registers.get_r(register);
self.flags.update_zero_flag(self.registers.a.wrapping_sub(register));
}

self.flags.update_zero_flag(result);
pub(crate) fn compare_ra_to_ram(&mut self, address: u16) {
let ram = self.bus.read(address);
self.flags.update_zero_flag(self.registers.a.wrapping_sub(ram));
}
}

// Other functions
// Jump Functions
impl GameBoy {
pub(crate) fn jump(&mut self, address: u16) {
self.registers.pc = address;
Expand All @@ -108,6 +113,19 @@ impl GameBoy {
}
}

// Bitwise operation functions (not all of them as of now)
impl GameBoy {
pub(crate) fn xor_r(&mut self, register: OneByteRegister) {
let register_a = *self.registers.get_r(OneByteRegister::A);
let register = self.registers.get_r(register);

let result = *register ^ register_a;
*register = result;

self.flags.update_zero_flag(result);
}
}

impl GameBoy {
pub(crate) fn interpret_cb_opcode(&mut self, opcode: u8) -> (Bytes, Cycles) {
match opcode {
Expand Down Expand Up @@ -240,6 +258,17 @@ impl GameBoy {
// Load IO into R (still only one of this)
0xF0 => { self.load_io_into_r(OneByteRegister::A); (2, 3) },

// Compare
0xB8 => { self.compare_ra_to_r(OneByteRegister::B); (1, 1) },
0xB9 => { self.compare_ra_to_r(OneByteRegister::C); (1, 1) },
0xBA => { self.compare_ra_to_r(OneByteRegister::D); (1, 1) },
0xBB => { self.compare_ra_to_r(OneByteRegister::E); (1, 1) },
0xBC => { self.compare_ra_to_r(OneByteRegister::H); (1, 1) },
0xBD => { self.compare_ra_to_r(OneByteRegister::L); (1, 1) },
0xBE => { self.compare_ra_to_ram(self.registers.get_hl()); (1, 1) },
0xBF => { self.compare_ra_to_r(OneByteRegister::A); (1, 1) },
0xFE => { self.compare_ra_to_i(); (2, 2) },

// Jump
// When we jump, we set 0 bytes, because if we returned the "correct" amount
// of bytes, the program will add them to PC after the jump
Expand Down

0 comments on commit 82cbfc0

Please sign in to comment.