ferris-boi/tests/cpu/misc.rs

69 lines
2.0 KiB
Rust
Raw Normal View History

use ferris_boi::{
cpu::{instructions::RunnableInstruction, Processor},
register,
};
use test_case::test_case;
use crate::testutil;
#[test_case(1)]
#[test_case(0)]
fn test_set_carry_flag_always_sets_to_1(starting_value: u8) {
let mut processor = Processor::default();
processor
.registers
.set_flag_bit(register::Flag::Carry, starting_value);
let data = [0x37, 0x03];
let (ins, extra_data) =
RunnableInstruction::from_data(&data).expect("could not parse instruction");
assert_eq!(extra_data, &[0x03]);
processor.run_instruction(&ins);
let new_carry_bit = processor.registers.get_flag_bit(register::Flag::Carry);
assert_eq!(1, new_carry_bit);
}
#[test_case(1, 0)]
#[test_case(0, 1)]
fn test_complement_carry_bit(starting_value: u8, expected_value: u8) {
let mut processor = Processor::default();
processor
.registers
.set_flag_bit(register::Flag::Carry, starting_value);
let data = [0x3F, 0x03];
let (ins, extra_data) =
RunnableInstruction::from_data(&data).expect("could not parse instruction");
assert_eq!(extra_data, &[0x03]);
processor.run_instruction(&ins);
let new_carry_bit = processor.registers.get_flag_bit(register::Flag::Carry);
assert_eq!(expected_value, new_carry_bit);
}
#[test_case(0x37)]
#[test_case(0x3F)]
fn test_all_carry_bit_instructions_adjust_flags(opcode: u8) {
let mut processor = Processor::default();
testutil::set_opposite_of_expected_flags(&mut processor, (0, 0, 0, 1));
let data = [opcode, 0x03];
let (ins, extra_data) =
RunnableInstruction::from_data(&data).expect("could not parse instruction");
assert_eq!(extra_data, &[0x03]);
processor.run_instruction(&ins);
testutil::assert_flags_eq!(
processor,
// Always zero
(register::Flag::HalfCarry, 0),
(register::Flag::Carry, 1),
(register::Flag::Subtract, 0),
// Value is preserved
(register::Flag::Zero, 1),
);
}