use crate::gameboy::{testutils::*, StepResult};
use crate::registers::{ByteRegister as br, Flag as f};
#[test]
fn test_set_bit() -> StepResult<()> {
let gb = run_program(
2,
&[
0x2E, 0x00, 0xCB, 0xFD, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::L), 0x80);
assert_eq!(gb.clocks_elapsed(), 16);
Ok(())
}
#[test]
fn test_set_memory_bit() -> StepResult<()> {
let gb = run_program(
3,
&[
0x26, 0x80, 0x2E, 0x00, 0xCB, 0xD6, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0x4);
assert_eq!(gb.clocks_elapsed(), 32);
Ok(())
}
#[test]
fn reset_memory_bit() -> StepResult<()> {
let gb = run_program(
4,
&[
0x26, 0x80, 0x2E, 0x00, 0x36, 0xFF, 0xCB, 0x96, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0xFB);
assert_eq!(gb.clocks_elapsed(), 44);
Ok(())
}
#[test]
fn test_reset_bit() -> StepResult<()> {
let gb = run_program(
2,
&[
0x2E, 0xFF, 0xCB, 0x85, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::L), 0xFE);
assert_eq!(gb.clocks_elapsed(), 16);
Ok(())
}
#[test]
fn test_test_bit_set() -> StepResult<()> {
let gb = run_program(
2,
&[
0x1E, 0x08, 0xCB, 0x5B, ],
)?;
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 16);
Ok(())
}
#[test]
fn test_test_bit_not_set() -> StepResult<()> {
let gb = run_program(
2,
&[
0x1E, 0x08, 0xCB, 0x63, ],
)?;
assert_eq!(gb.cpu.read_flag(f::Zero), true);
assert_eq!(gb.clocks_elapsed(), 16);
Ok(())
}
#[test]
fn test_test_memory_bit_set() -> StepResult<()> {
let gb = run_program(
4,
&[
0x26, 0x80, 0x2E, 0x00, 0x36, 0x08, 0xCB, 0x5E, ],
)?;
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 40);
Ok(())
}
#[test]
fn test_test_memory_bit_not_set() -> StepResult<()> {
let gb = run_program(
4,
&[
0x26, 0x80, 0x2E, 0x00, 0x36, 0x08, 0xCB, 0x66, ],
)?;
assert_eq!(gb.cpu.read_flag(f::Zero), true);
assert_eq!(gb.clocks_elapsed(), 40);
Ok(())
}
#[test]
fn test_rotate_reg_left_carry() -> StepResult<()> {
let gb = run_program(
3,
&[
0x3F, 0x1E, 0x88, 0xCB, 0x03, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::E), 0x11);
assert_eq!(gb.cpu.read_flag(f::Carry), true);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 20);
let gb = run_program(
3,
&[
0x37, 0x1E, 0x48, 0xCB, 0x03, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::E), 0x90);
assert_eq!(gb.cpu.read_flag(f::Carry), false);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 20);
Ok(())
}
#[test]
fn test_rotate_reg_left() -> StepResult<()> {
let gb = run_program(
3,
&[
0x3F, 0x1E, 0x88, 0xCB, 0x13, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::E), 0x10);
assert_eq!(gb.cpu.read_flag(f::Carry), true);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 20);
let gb = run_program(
3,
&[
0x37, 0x1E, 0x48, 0xCB, 0x13, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::E), 0x91);
assert_eq!(gb.cpu.read_flag(f::Carry), false);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 20);
Ok(())
}
#[test]
fn test_rotate_reg_left_zero() -> StepResult<()> {
let gb = run_program(
3,
&[
0x3F, 0x1E, 0x80, 0xCB, 0x13, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::E), 0x00);
assert_eq!(gb.cpu.read_flag(f::Carry), true);
assert_eq!(gb.cpu.read_flag(f::Zero), true);
assert_eq!(gb.clocks_elapsed(), 20);
Ok(())
}
#[test]
fn test_rotate_reg_right_carry() -> StepResult<()> {
let gb = run_program(
3,
&[
0x3F, 0x1E, 0x89, 0xCB, 0x0B, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::E), 0xC4);
assert_eq!(gb.cpu.read_flag(f::Carry), true);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 20);
let gb = run_program(
3,
&[
0x37, 0x1E, 0x48, 0xCB, 0x0B, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::E), 0x24);
assert_eq!(gb.cpu.read_flag(f::Carry), false);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 20);
Ok(())
}
#[test]
fn test_rotate_reg_right() -> StepResult<()> {
let gb = run_program(
3,
&[
0x3F, 0x1E, 0x89, 0xCB, 0x1B, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::E), 0x44);
assert_eq!(gb.cpu.read_flag(f::Carry), true);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 20);
let gb = run_program(
3,
&[
0x37, 0x1E, 0x48, 0xCB, 0x1B, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::E), 0xA4);
assert_eq!(gb.cpu.read_flag(f::Carry), false);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 20);
Ok(())
}
#[test]
fn test_rotate_reg_right_zero() -> StepResult<()> {
let gb = run_program(
3,
&[
0x3F, 0x1E, 0x01, 0xCB, 0x1B, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::E), 0x00);
assert_eq!(gb.cpu.read_flag(f::Carry), true);
assert_eq!(gb.cpu.read_flag(f::Zero), true);
assert_eq!(gb.clocks_elapsed(), 20);
Ok(())
}
#[test]
fn test_rotate_mem_left_carry() -> StepResult<()> {
let gb = run_program(
5,
&[
0x26, 0x80, 0x2E, 0x00, 0x3F, 0x36, 0x88, 0xCB, 0x06, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0x11);
assert_eq!(gb.cpu.read_flag(f::Carry), true);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 48);
let gb = run_program(
5,
&[
0x26, 0x80, 0x2E, 0x00, 0x37, 0x36, 0x48, 0xCB, 0x06, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0x90);
assert_eq!(gb.cpu.read_flag(f::Carry), false);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 48);
Ok(())
}
#[test]
fn test_rotate_mem_left() -> StepResult<()> {
let gb = run_program(
5,
&[
0x26, 0x80, 0x2E, 0x00, 0x3F, 0x36, 0x88, 0xCB, 0x16, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0x10);
assert_eq!(gb.cpu.read_flag(f::Carry), true);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 48);
let gb = run_program(
5,
&[
0x26, 0x80, 0x2E, 0x00, 0x37, 0x36, 0x48, 0xCB, 0x16, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0x91);
assert_eq!(gb.cpu.read_flag(f::Carry), false);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 48);
Ok(())
}
#[test]
fn test_rotate_mem_right_carry() -> StepResult<()> {
let gb = run_program(
5,
&[
0x26, 0x80, 0x2E, 0x00, 0x3F, 0x36, 0x89, 0xCB, 0x0E, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0xC4);
assert_eq!(gb.cpu.read_flag(f::Carry), true);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 48);
let gb = run_program(
5,
&[
0x26, 0x80, 0x2E, 0x00, 0x37, 0x36, 0x48, 0xCB, 0x0e, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0x24);
assert_eq!(gb.cpu.read_flag(f::Carry), false);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 48);
Ok(())
}
#[test]
fn test_rotate_mem_right() -> StepResult<()> {
let gb = run_program(
5,
&[
0x26, 0x80, 0x2E, 0x00, 0x3F, 0x36, 0x89, 0xCB, 0x1E, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0x44);
assert_eq!(gb.cpu.read_flag(f::Carry), true);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 48);
let gb = run_program(
5,
&[
0x26, 0x80, 0x2E, 0x00, 0x37, 0x36, 0x48, 0xCB, 0x1E, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0xA4);
assert_eq!(gb.cpu.read_flag(f::Carry), false);
assert_eq!(gb.cpu.read_flag(f::Zero), false);
assert_eq!(gb.clocks_elapsed(), 48);
Ok(())
}
#[test]
fn test_reg_shift_low_right() -> StepResult<()> {
let gb = run_program(
2,
&[
0x06, 0xF0, 0xCB, 0x38, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::B), 0x78);
assert_eq!(gb.cpu.read_flag(f::Carry), false);
assert_eq!(gb.clocks_elapsed(), 16);
let gb = run_program(
2,
&[
0x06, 0xF1, 0xCB, 0x38, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::B), 0x78);
assert_eq!(gb.cpu.read_flag(f::Carry), true);
assert_eq!(gb.clocks_elapsed(), 16);
Ok(())
}
#[test]
fn test_reg_shift_low_left() -> StepResult<()> {
let gb = run_program(
2,
&[
0x06, 0x71, 0xCB, 0x20, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::B), 0xE2);
assert_eq!(gb.cpu.read_flag(f::Carry), false);
assert_eq!(gb.clocks_elapsed(), 16);
let gb = run_program(
2,
&[
0x06, 0xF1, 0xCB, 0x20, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::B), 0xE2);
assert_eq!(gb.cpu.read_flag(f::Carry), true);
assert_eq!(gb.clocks_elapsed(), 16);
Ok(())
}
#[test]
fn test_mem_shift_low_right() -> StepResult<()> {
let gb = run_program(
4,
&[
0x26, 0x80, 0x2E, 0x00, 0x36, 0xF0, 0xCB, 0x3E, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0x78);
assert_eq!(gb.cpu.read_flag(f::Carry), false);
assert_eq!(gb.clocks_elapsed(), 44);
let gb = run_program(
4,
&[
0x26, 0x80, 0x2E, 0x00, 0x36, 0xF1, 0xCB, 0x3E, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0x78);
assert_eq!(gb.cpu.read_flag(f::Carry), true);
assert_eq!(gb.clocks_elapsed(), 44);
Ok(())
}
#[test]
fn test_mem_shift_low_left() -> StepResult<()> {
let gb = run_program(
4,
&[
0x26, 0x80, 0x2E, 0x00, 0x36, 0x71, 0xCB, 0x26, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0xE2);
assert_eq!(gb.cpu.read_flag(f::Carry), false);
assert_eq!(gb.clocks_elapsed(), 44);
let gb = run_program(
4,
&[
0x26, 0x80, 0x2E, 0x00, 0x36, 0xF1, 0xCB, 0x26, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0xE2);
assert_eq!(gb.cpu.read_flag(f::Carry), true);
assert_eq!(gb.clocks_elapsed(), 44);
Ok(())
}
#[test]
fn test_reg_swap() -> StepResult<()> {
let gb = run_program(
2,
&[
0x16, 0xFA, 0xCB, 0x32, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::D), 0xAF);
assert_eq!(gb.clocks_elapsed(), 16);
Ok(())
}
#[test]
fn test_mem_swap() -> StepResult<()> {
let gb = run_program(
4,
&[
0x26, 0x80, 0x2E, 0x00, 0x36, 0xFA, 0xCB, 0x36, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0xAF);
assert_eq!(gb.clocks_elapsed(), 44);
Ok(())
}
#[test]
fn test_reg_shift_extend_right() -> StepResult<()> {
let gb = run_program(
3,
&[
0x06, 0x80, 0xCB, 0x28, 0xCB, 0x28, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::B), 0xE0);
assert_eq!(gb.clocks_elapsed(), 24);
Ok(())
}
#[test]
fn test_mem_shift_extend_right() -> StepResult<()> {
let gb = run_program(
5,
&[
0x26, 0x80, 0x2E, 0x00, 0x36, 0x80, 0xCB, 0x2E, 0xCB, 0x2E, ],
)?;
assert_eq!(gb.read_memory_u8(0x8000)?, 0xE0);
assert_eq!(gb.clocks_elapsed(), 60);
Ok(())
}