Skip to content

Commit

Permalink
fix tests I broke last night
Browse files Browse the repository at this point in the history
  • Loading branch information
pcasaretto committed Apr 14, 2024
1 parent 99a8d2b commit 5568808
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 66 deletions.
6 changes: 3 additions & 3 deletions src/gameboy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Gameboy<'a> {
pub bus: MemoryBus<'a>,
pub opcode_info: OpcodeInfo,
pub interrupts_enabled: bool,
scanline_counter: u64,
pub scanline_counter: u64,
}

impl<'a> Default for Gameboy<'a> {
Expand Down Expand Up @@ -190,7 +190,7 @@ mod tests {
a: 3,
c: 4,
f: FlagsRegister::from(0),
pc: 1245,
pc: 0xC050,
..Default::default()
},
..Default::default()
Expand All @@ -203,6 +203,6 @@ mod tests {
};
gameboy.run_next_instruction();
assert_eq!(gameboy.cpu.registers.get_u8(RegisterTarget::A), 7);
assert_eq!(gameboy.cpu.registers.get_u16(Register16bTarget::PC), 1246);
assert_eq!(gameboy.cpu.registers.get_u16(Register16bTarget::PC), 0xC051);
}
}
6 changes: 3 additions & 3 deletions src/instructions/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ mod tests {
#[test]
fn test_call_a16() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0x0301;
gameboy.cpu.registers.pc = 0xC050;
gameboy.cpu.registers.sp = 0xFFFE;
gameboy.bus.memory[gameboy.cpu.registers.pc as usize + 1] = 0xCD;
gameboy.bus.memory[gameboy.cpu.registers.pc as usize + 2] = 0xAB;
call_a16(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0xABCD);
assert_eq!(gameboy.cpu.registers.sp, 0xFFFC);
assert_eq!(gameboy.bus.memory[0xFFFD], 0x03);
assert_eq!(gameboy.bus.memory[0xFFFC], 0x04);
assert_eq!(gameboy.bus.memory[0xFFFD], 0xC0);
assert_eq!(gameboy.bus.memory[0xFFFC], 0x53); // address of next instruction
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/instructions/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod tests {
},
..Default::default()
};
gameboy.bus.write_byte(gameboy.cpu.registers.pc, 13);
gameboy.bus.write_byte(gameboy.cpu.registers.pc + 1, 13);
cp_d8(&mut gameboy);
assert!(gameboy.cpu.registers.f.zero);
}
Expand Down
77 changes: 39 additions & 38 deletions src/instructions/jmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,58 +91,59 @@ mod tests {
fn test_jmp_a16() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0xC050;
gameboy.bus.memory[0xC050] = 0x01;
gameboy.bus.memory[0xC051] = 0x02;
gameboy.bus.memory[0xC051] = 0x01;
gameboy.bus.memory[0xC052] = 0x02;
jmp_a16(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0x0201);
}

#[test]
fn test_jr() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0x1000;
gameboy.bus.memory[0x1000] = 0x05;
gameboy.cpu.registers.pc = 0xC050;
gameboy.bus.memory[0xC051] = 0x05;
jr(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0x1005);
// jump is relative to next instruction
assert_eq!(gameboy.cpu.registers.pc, 0xC057);
}

#[test]
fn test_jr_signed_negative() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0x1000;
gameboy.bus.memory[0x1000] = -5i8 as u8;
gameboy.cpu.registers.pc = 0xC050;
gameboy.bus.memory[0xC051] = -5i8 as u8;
jr(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0x0FFB);
assert_eq!(gameboy.cpu.registers.pc, 0xC04D);
}

#[test]
fn test_jr_z_flag_unset() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0x1000;
gameboy.cpu.registers.pc = 0xC050;
gameboy.cpu.registers.f.zero = false;
gameboy.bus.memory[0x1000] = 0x01;
gameboy.bus.memory[0xC051] = 0x01;
jr_z(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0x1000);
assert_eq!(gameboy.cpu.registers.pc, 0xC052);
}

#[test]
fn test_jr_z_flag_set() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0x1000;
gameboy.cpu.registers.pc = 0xC050;
gameboy.cpu.registers.f.zero = true;
gameboy.bus.memory[0x1000] = 0x05;
gameboy.bus.memory[0xC051] = 0x05;
jr_z(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0x1005);
assert_eq!(gameboy.cpu.registers.pc, 0xC057);
}

#[test]
fn test_jr_z_flag_set_signed_negative() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0x1000;
gameboy.cpu.registers.pc = 0xC050;
gameboy.cpu.registers.f.zero = true;
gameboy.bus.memory[0x1005] = -5i8 as u8;
gameboy.bus.memory[0xC051] = -5i8 as u8;
jr_z(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0x1000);
assert_eq!(gameboy.cpu.registers.pc, 0xC04D);
}

#[test]
Expand All @@ -152,76 +153,76 @@ mod tests {
gameboy.cpu.registers.f.zero = true;
gameboy.bus.memory[0xC050] = 0x01;
jr_nz(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0xC050);
assert_eq!(gameboy.cpu.registers.pc, 0xC052);
}

#[test]
fn test_jr_nz_flag_unset_signed_negative() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0xC050;
gameboy.cpu.registers.f.zero = false;
gameboy.bus.memory[0xC050] = -5 as i8 as u8;
gameboy.bus.memory[0xC051] = -5 as i8 as u8;
jr_nz(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0xC04B);
assert_eq!(gameboy.cpu.registers.pc, 0xC04D);
}

#[test]
fn test_jr_c_flag_unset() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0x1000;
gameboy.cpu.registers.pc = 0xC050;
gameboy.cpu.registers.f.carry = false;
gameboy.bus.memory[0x1000] = 0x01;
gameboy.bus.memory[0xC050] = 0x01;
jr_c(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0x1000);
assert_eq!(gameboy.cpu.registers.pc, 0xC052);
}

#[test]
fn test_jr_c_flag_set() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0x1000;
gameboy.cpu.registers.pc = 0xC050;
gameboy.cpu.registers.f.carry = true;
gameboy.bus.memory[0x1000] = 0x05;
gameboy.bus.memory[0xC051] = 0x05;
jr_c(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0x1005);
assert_eq!(gameboy.cpu.registers.pc, 0xC057);
}

#[test]
fn test_jr_c_flag_set_signed_negative() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0x1000;
gameboy.cpu.registers.pc = 0xC050;
gameboy.cpu.registers.f.carry = true;
gameboy.bus.memory[0x1000] = -5i8 as u8;
gameboy.bus.memory[0xC051] = -5i8 as u8;
jr_c(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0x0ffb);
assert_eq!(gameboy.cpu.registers.pc, 0xC04D);
}

#[test]
fn test_jr_nc_flag_unset() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0x1000;
gameboy.cpu.registers.pc = 0xC050;
gameboy.cpu.registers.f.carry = false;
gameboy.bus.memory[0x1000] = 0x05;
gameboy.bus.memory[0xC051] = 0x05;
jr_nc(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0x1005);
assert_eq!(gameboy.cpu.registers.pc, 0xC057);
}

#[test]
fn test_jr_nc_flag_set() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0x1000;
gameboy.cpu.registers.pc = 0xC050;
gameboy.cpu.registers.f.carry = true;
gameboy.bus.memory[0x1000] = 0x05;
gameboy.bus.memory[0xC051] = 0x05;
jr_nc(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0x1000);
assert_eq!(gameboy.cpu.registers.pc, 0xC052);
}

#[test]
fn test_jr_nc_flag_unset_signed_negative() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0x1000;
gameboy.cpu.registers.pc = 0xC050;
gameboy.cpu.registers.f.carry = false;
gameboy.bus.memory[0x1000] = -5i8 as u8;
gameboy.bus.memory[0xC051] = -5i8 as u8;
jr_nc(&mut gameboy);
assert_eq!(gameboy.cpu.registers.pc, 0x0ffb);
assert_eq!(gameboy.cpu.registers.pc, 0xC04D);
}
}
48 changes: 29 additions & 19 deletions src/instructions/ld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ pub fn ld_d8_r(target: RegisterTarget) -> impl Fn(&mut Gameboy) -> u8 {

pub fn ld_mem_at_d8_a(gameboy: &mut Gameboy) -> u8 {
let addr = 0xFF00 + gameboy.read_next_byte() as u16;
let value = gameboy.bus.read_byte(addr);
gameboy.cpu.registers.set_u8(RegisterTarget::A, value);
let value = gameboy.cpu.registers.get_u8(RegisterTarget::A);
gameboy.bus.write_byte(addr, value);
const TICKS: u8 = 12;
TICKS
}
Expand Down Expand Up @@ -185,11 +185,11 @@ mod tests {
use crate::cpu::{Registers, CPU};

#[test]
fn test_ld_d16_r16() {
fn test_ld_r16_n16() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0xC050;
gameboy.bus.memory[0xC050] = 0x01;
gameboy.bus.memory[0xC051] = 0x02;
gameboy.bus.memory[0xC051] = 0x01;
gameboy.bus.memory[0xC052] = 0x02;
ld_r16_n16(Register16bTarget::BC)(&mut gameboy);
assert_eq!(gameboy.cpu.registers.get_u16(Register16bTarget::BC), 0x0201);
assert_eq!(gameboy.cpu.registers.pc, 0xC052);
Expand All @@ -215,21 +215,21 @@ mod tests {
#[test]
fn test_ld_a_mem_at_hl_inc() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.set_u16(Register16bTarget::HL, 0x1000);
gameboy.bus.memory[0x1000] = 0x01;
gameboy.cpu.registers.set_u16(Register16bTarget::HL, 0xC050);
gameboy.bus.memory[0xC050] = 0x01;
ld_a_mem_at_hl_inc(&mut gameboy);
assert_eq!(gameboy.cpu.registers.get_u8(RegisterTarget::A), 0x01);
assert_eq!(gameboy.cpu.registers.get_u16(Register16bTarget::HL), 0x1001);
assert_eq!(gameboy.cpu.registers.get_u16(Register16bTarget::HL), 0xC051);
}

#[test]
fn test_ld_a_mem_at_hl_dec() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.set_u16(Register16bTarget::HL, 0x1000);
gameboy.bus.memory[0x1000] = 0x01;
gameboy.cpu.registers.set_u16(Register16bTarget::HL, 0xC050);
gameboy.bus.memory[0xC050] = 0x01;
ld_a_mem_at_hl_dec(&mut gameboy);
assert_eq!(gameboy.cpu.registers.get_u8(RegisterTarget::A), 0x01);
assert_eq!(gameboy.cpu.registers.get_u16(Register16bTarget::HL), 0x0FFF);
assert_eq!(gameboy.cpu.registers.get_u16(Register16bTarget::HL), 0xC04F);
}

#[test]
Expand Down Expand Up @@ -275,8 +275,8 @@ mod tests {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0xC050;
gameboy.cpu.registers.set_u8(RegisterTarget::A, 0x34);
gameboy.bus.memory[0xC050] = 0x01;
gameboy.bus.memory[0xC051] = 0xC1;
gameboy.bus.memory[0xC051] = 0x01;
gameboy.bus.memory[0xC052] = 0xC1;
ld_mem_at_d16_r(RegisterTarget::A)(&mut gameboy);
assert_eq!(gameboy.bus.memory[0xC101], 0x34);
}
Expand All @@ -292,7 +292,7 @@ mod tests {
fn test_ld_d8_r() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0xC050;
gameboy.bus.memory[0xC050] = 0x34;
gameboy.bus.memory[0xC051] = 0x34;
ld_d8_r(RegisterTarget::A)(&mut gameboy);
assert_eq!(gameboy.cpu.registers.get_u8(RegisterTarget::A), 0x34);
}
Expand All @@ -302,7 +302,7 @@ mod tests {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.set_u16(Register16bTarget::HL, 0xC100);
gameboy.cpu.registers.pc = 0xC050;
gameboy.bus.memory[0xC050] = 0x34;
gameboy.bus.memory[0xC051] = 0x34;
ld_d8_mem_at_r16(Register16bTarget::HL)(&mut gameboy);
assert_eq!(gameboy.bus.memory[0xC100], 0x34);
}
Expand All @@ -311,10 +311,20 @@ mod tests {
fn test_ld_a_mem_at_d8() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0xC050;
gameboy.bus.memory[0xC050] = 0x34;
gameboy.cpu.registers.set_u8(RegisterTarget::A, 0x42);
ld_a_mem_at_d8()(&mut gameboy);
assert_eq!(gameboy.bus.read_byte(0xFF34), 0x42);
gameboy.bus.memory[0xC051] = 0x34;
gameboy.bus.memory[0xFF34] = 0x43;
ld_a_mem_at_d8(&mut gameboy);
assert_eq!(gameboy.cpu.registers.get_u8(RegisterTarget::A), 0x43);
}

#[test]
fn test_ld_mem_at_d8_a() {
let mut gameboy = Gameboy::default();
gameboy.cpu.registers.pc = 0xC050;
gameboy.bus.memory[0xC051] = 0x34;
gameboy.cpu.registers.set_u8(RegisterTarget::A, 0x43);
ld_mem_at_d8_a(&mut gameboy);
assert_eq!(gameboy.bus.read_byte(0xFF34), 0x43);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ pub fn from_byte(byte: u8) -> Box<Instruction> {

0xD6 => Box::new(pai(sub::sub_d8())),

0xE0 => Box::new(pai(ld::ld_a_mem_at_d8)),
0xF0 => Box::new(pai(ld::ld_mem_at_d8_a)),
0xE0 => Box::new(pai(ld::ld_mem_at_d8_a)),
0xF0 => Box::new(pai(ld::ld_a_mem_at_d8)),
0xE2 => Box::new(pai(ld::ld_mem_at_c_a)),
0xEA => Box::new(pai(ld::ld_mem_at_d16_r(RegisterTarget::A))),

Expand Down

0 comments on commit 5568808

Please sign in to comment.