use crate::gameboy::{testutils::*, StepResult};
use crate::registers::{ByteRegister as br, WordRegister as wr};
#[test]
fn test_loads() -> StepResult<()> {
let gb = run_program(
6,
&[
0x26, 0x80, 0x2E, 0x00, 0x06, 0x25, 0x50, 0x72, 0x5E, ],
)?;
assert_eq!(gb.cpu.read_register_u8(br::B), 0x25);
assert_eq!(gb.cpu.read_register_u8(br::D), 0x25);
assert_eq!(gb.cpu.read_register_u8(br::E), 0x25);
assert_eq!(gb.read_memory_u8(0x8000)?, 0x25);
assert_eq!(gb.clocks_elapsed(), 44);
Ok(())
}
#[test]
fn test_offset_load() -> StepResult<()> {
let gb = run_program(
6,
&[
0x3E, 0xFF, 0xE0, 0xA0, 0x26, 0xFF, 0x2E, 0xA1, 0x75, 0xF0, 0xA1, ],
)?;
assert_eq!(gb.read_memory_u8(0xFFA1)?, 0xA1);
assert_eq!(gb.read_memory_u8(0xFFA0)?, 0xFF);
assert_eq!(gb.read_register_u8(br::A), 0xA1);
assert_eq!(gb.clocks_elapsed(), 56);
Ok(())
}
#[test]
fn test_load_constant_16() -> StepResult<()> {
let gb = run_program(
4,
&[
0x01, 0x23, 0x45, 0x11, 0x45, 0x56, 0x21, 0x11, 0x22, 0x31, 0x45, 0x67, ],
)?;
assert_eq!(gb.read_register_u16(wr::BC), 0x4523);
assert_eq!(gb.read_register_u16(wr::DE), 0x5645);
assert_eq!(gb.read_register_u16(wr::HL), 0x2211);
assert_eq!(gb.read_register_u16(wr::SP), 0x6745);
assert_eq!(gb.clocks_elapsed(), 48);
Ok(())
}
#[test]
fn load_post_increment() -> StepResult<()> {
let gb = run_program(
7,
&[
0x21, 0x05, 0xC0, 0x36, 0xAB, 0x2E, 0x03, 0x3E, 0x45, 0x22, 0x22, 0x2A, ],
)?;
assert_eq!(gb.read_memory_u8(0xc003)?, 0x45);
assert_eq!(gb.read_memory_u8(0xc004)?, 0x45);
assert_eq!(gb.read_memory_u8(0xC005)?, 0xAB);
assert_eq!(gb.read_register_u8(br::A), 0xAB);
assert_eq!(gb.read_register_u16(wr::HL), 0xC006);
assert_eq!(gb.clocks_elapsed(), 64);
Ok(())
}
#[test]
fn load_post_decrement() -> StepResult<()> {
let gb = run_program(
7,
&[
0x21, 0x03, 0xC0, 0x36, 0xAB, 0x2E, 0x05, 0x3E, 0x45, 0x32, 0x32, 0x3A, ],
)?;
assert_eq!(gb.read_memory_u8(0xc003)?, 0xAB);
assert_eq!(gb.read_memory_u8(0xc004)?, 0x45);
assert_eq!(gb.read_memory_u8(0xC005)?, 0x45);
assert_eq!(gb.read_register_u8(br::A), 0xAB);
assert_eq!(gb.read_register_u16(wr::HL), 0xC002);
assert_eq!(gb.clocks_elapsed(), 64);
Ok(())
}
#[test]
fn test_load_memory_offset_a() -> StepResult<()> {
let gb = run_program(
3,
&[
0x0E, 0x81, 0x3E, 0x45, 0xE2, ],
)?;
assert_eq!(gb.read_memory_u8(0xFF81)?, 0x45);
assert_eq!(gb.clocks_elapsed(), 24);
Ok(())
}
#[test]
fn test_load_a_memory_offset() -> StepResult<()> {
let gb = run_program(
4,
&[
0x0E, 0x81, 0x21, 0x81, 0xff, 0x36, 0x45, 0xF2, ],
)?;
assert_eq!(gb.read_register_u8(br::A), 0x45);
assert_eq!(gb.clocks_elapsed(), 40);
Ok(())
}
#[test]
fn test_load_a_indirect() -> StepResult<()> {
let gb = run_program(
3,
&[
0x21, 0x02, 0xC0, 0x36, 0x78, 0xFA, 0x02, 0xC0, ],
)?;
assert_eq!(gb.read_register_u8(br::A), 0x78);
assert_eq!(gb.clocks_elapsed(), 40);
Ok(())
}
#[test]
fn test_load_indirect_a() -> StepResult<()> {
let gb = run_program(
2,
&[
0x3E, 0x87, 0xEA, 0x12, 0xC1, ],
)?;
assert_eq!(gb.read_memory_u8(0xC112)?, 0x87);
assert_eq!(gb.clocks_elapsed(), 24);
Ok(())
}