use crate::testutil; use ferris_boi::{ cpu::{instructions::RunnableInstruction, Processor}, register, }; use test_case::test_case; #[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), ); } #[test] fn test_complement_a_register_value() { let mut processor = Processor::default(); processor.registers.a = 0xF0; let data = [0x2F, 0x03]; let (ins, extra_data) = RunnableInstruction::from_data(&data).expect("could not parse instruction"); assert_eq!(extra_data, &[0x03]); processor.run_instruction(&ins); assert_eq!(0x0F, processor.registers.a); } #[test] fn test_complement_a_register_flags() { let mut processor = Processor::default(); processor.registers.a = 0xF0; testutil::set_opposite_of_expected_flags(&mut processor, (0, 1, 1, 0)); let data = [0x2F, 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 1 (register::Flag::HalfCarry, 1), (register::Flag::Subtract, 1), // Value is preserved (register::Flag::Carry, 1), (register::Flag::Zero, 1), ); }