ferris-boi/tests/cpu/control.rs

50 lines
1.5 KiB
Rust
Raw Normal View History

2023-11-21 19:55:40 +00:00
use ferris_boi::cpu::{instructions::Instruction, Processor};
#[test]
fn test_can_jump_to_immediate() {
let mut processor = Processor::default();
let data = [0xC3, 0x37, 0x13, 0x06];
let (ins, extra_data) = Instruction::from_data(&data).expect("could not parse instruction");
assert_eq!(extra_data, &[0x06]);
processor.run_instruction(ins);
assert_eq!(0x1337, processor.registers.program_counter);
}
#[test]
fn test_call_adjusts_program_counter_to_given_value() {
let mut processor = Processor::default();
let data = [0xCD, 0x37, 0x13, 0x06];
let (ins, extra_data) = Instruction::from_data(&data).expect("could not parse instruction");
assert_eq!(extra_data, &[0x06]);
processor.run_instruction(ins);
assert_eq!(0x1337, processor.registers.program_counter);
}
#[test]
fn test_call_pushes_pc_onto_stack() {
let mut processor = Processor::default();
processor.registers.program_counter = 0xBEEF;
let data = [0xCD, 0x37, 0x13, 0x06];
let (ins, extra_data) = Instruction::from_data(&data).expect("could not parse instruction");
assert_eq!(extra_data, &[0x06]);
let old_sp = processor.registers.stack_pointer;
processor.run_instruction(ins);
assert_eq!(0xBE, processor.memory.get((old_sp - 1).into()).unwrap());
// 0xEF + 3, which is the size of the CALL instruction
assert_eq!(0xF2, processor.memory.get((old_sp - 2).into()).unwrap());
assert_eq!(old_sp - 2, processor.registers.stack_pointer);
}