Skip to content

Commit

Permalink
implement add_mem_at_hl
Browse files Browse the repository at this point in the history
  • Loading branch information
pcasaretto committed Apr 14, 2024
1 parent 4310591 commit 89b4ee0
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 3 deletions.
136 changes: 134 additions & 2 deletions src/instructions/add.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cpu::RegisterTarget;
use crate::cpu::{Register16bTarget, RegisterTarget};
use crate::gameboy::Gameboy;

pub fn add(target: RegisterTarget) -> impl Fn(&mut Gameboy) -> u8 {
Expand All @@ -17,10 +17,25 @@ pub fn add(target: RegisterTarget) -> impl Fn(&mut Gameboy) -> u8 {
}
}

pub fn add_mem_at_hl(gameboy: &mut Gameboy) -> u8 {
let hl = gameboy.cpu.registers.get_u16(Register16bTarget::HL);
let target_value = gameboy.bus.read_byte(hl);
let current_value = gameboy.cpu.registers.a;
let (new_value, did_overflow) = current_value.overflowing_add(target_value);
gameboy.cpu.registers.a = new_value;

gameboy.cpu.registers.f.carry = did_overflow;
gameboy.cpu.registers.f.zero = new_value == 0;
gameboy.cpu.registers.f.subtract = false;
gameboy.cpu.registers.f.half_carry = (current_value & 0xF) + (target_value & 0xF) > 0xF;
const TICKS: u8 = 8;
TICKS
}

#[cfg(test)]
mod tests {
use super::*;
use crate::cpu::{FlagsRegister, Registers, CPU};
use crate::cpu::{FlagsRegister, Register16bTarget, Registers, CPU};

#[test]
fn test_add() {
Expand Down Expand Up @@ -132,4 +147,121 @@ mod tests {
add(RegisterTarget::C)(&mut gameboy);
assert!(gameboy.cpu.registers.f.half_carry);
}

#[test]
fn test_add_mem_at_hl() {
let mut gameboy = Gameboy {
cpu: CPU {
registers: Registers {
a: 0,
f: FlagsRegister::from(0),
..Default::default()
},
..Default::default()
},
..Default::default()
};
gameboy.cpu.registers.set_u16(Register16bTarget::HL, 0xC050);
gameboy.bus.write_byte(0xC050, 1);
add_mem_at_hl(&mut gameboy);
assert_eq!(gameboy.cpu.registers.a, 1);
}

#[test]
fn test_add_mem_at_hl_overflow() {
let mut gameboy = Gameboy {
cpu: CPU {
registers: Registers {
a: 255,
f: FlagsRegister::from(0),
..Default::default()
},
..Default::default()
},
..Default::default()
};
gameboy.cpu.registers.set_u16(Register16bTarget::HL, 0xC050);
gameboy.bus.write_byte(0xC050, 1);
add_mem_at_hl(&mut gameboy);
assert_eq!(gameboy.cpu.registers.a, 0);
}

#[test]
fn test_add_mem_at_hl_carry_flag() {
let mut gameboy = Gameboy {
cpu: CPU {
registers: Registers {
a: 255,
f: FlagsRegister::from(0),
..Default::default()
},
..Default::default()
},
..Default::default()
};
gameboy.cpu.registers.set_u16(Register16bTarget::HL, 0xC050);
gameboy.bus.write_byte(0xC050, 1);
add_mem_at_hl(&mut gameboy);
assert!(gameboy.cpu.registers.f.carry);
}

#[test]
fn test_add_mem_at_hl_zero_flag() {
let mut gameboy = Gameboy {
cpu: CPU {
registers: Registers {
a: 0,
f: FlagsRegister::from(0),
..Default::default()
},
..Default::default()
},
..Default::default()
};
gameboy.cpu.registers.set_u16(Register16bTarget::HL, 0xC050);
gameboy.bus.write_byte(0xC050, 0);
add_mem_at_hl(&mut gameboy);
assert!(gameboy.cpu.registers.f.zero);
}

#[test]
fn test_add_mem_at_hl_substract_flag() {
let mut gameboy = Gameboy {
cpu: CPU {
registers: Registers {
a: 0,
f: FlagsRegister {
subtract: true,
..Default::default()
},
..Default::default()
},
..Default::default()
},
..Default::default()
};
gameboy.cpu.registers.set_u16(Register16bTarget::HL, 0xC050);
gameboy.bus.write_byte(0xC050, 1);
add_mem_at_hl(&mut gameboy);
assert!(!gameboy.cpu.registers.f.subtract);
}

#[test]
fn test_add_mem_at_hl_half_carry_flag() {
let mut gameboy = Gameboy {
cpu: CPU {
registers: Registers {
a: 0b00001111,
f: FlagsRegister::from(0),
..Default::default()
},
..Default::default()
},
..Default::default()
};
gameboy.cpu.registers.set_u16(Register16bTarget::HL, 0xC050);
gameboy.bus.write_byte(0xC050, 0b0000_0001);
add_mem_at_hl(&mut gameboy);
assert!(gameboy.cpu.registers.f.half_carry);
}
}
3 changes: 2 additions & 1 deletion src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,14 @@ pub fn from_byte(byte: u8) -> Box<Instruction> {
0x7D => Box::new(pai(ld::ld_r_r(RegisterTarget::L, RegisterTarget::A))),
// 0x7E => Box::new(ld::ld_r_r(RegisterTarget::A, Register16bTarget::HL)),
0x7F => Box::new(pai(ld::ld_r_r(RegisterTarget::A, RegisterTarget::A))),
0x87 => Box::new(pai(add::add(RegisterTarget::A))),
0x80 => Box::new(pai(add::add(RegisterTarget::B))),
0x81 => Box::new(pai(add::add(RegisterTarget::C))),
0x82 => Box::new(pai(add::add(RegisterTarget::D))),
0x83 => Box::new(pai(add::add(RegisterTarget::E))),
0x84 => Box::new(pai(add::add(RegisterTarget::H))),
0x85 => Box::new(pai(add::add(RegisterTarget::L))),
0x86 => Box::new(pai(add::add_mem_at_hl)),
0x87 => Box::new(pai(add::add(RegisterTarget::A))),
0x8E => Box::new(pai(adc::adc_mem_at_hl())),
0x8F => Box::new(pai(adc::adc(RegisterTarget::A))),
0x88 => Box::new(pai(adc::adc(RegisterTarget::B))),
Expand Down

0 comments on commit 89b4ee0

Please sign in to comment.