diff --git a/src/machine.rs b/src/machine.rs index 1980cf0..f1d907c 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -163,6 +163,12 @@ impl Machine { self.branch_if_positive(addr); } + (instruction::BVC, instruction::UseRelative(rel)) => { + let addr = self.registers.program_counter + + AddressDiff(rel as i32); + self.branch_if_overflow_clear(addr); + } + (instruction::CLC, instruction::UseImplied) => { self.registers.status.and(!PS_CARRY); } @@ -595,6 +601,12 @@ impl Machine { } } + fn branch_if_overflow_clear(&mut self, addr: Address) { + if !self.registers.status.contains(PS_OVERFLOW) { + self.registers.program_counter = addr; + } + } + fn push_on_stack(&mut self, val: u8) { let addr = self.registers.stack_pointer.to_address(); self.memory.set_byte(addr, val); @@ -981,3 +993,16 @@ fn branch_if_positive_test() { machine.branch_if_positive(Address(0xABCD)); assert_eq!(machine.registers.program_counter, Address(0xABCD)); } + +#[test] +fn branch_if_overflow_clear_test() { + let mut machine = Machine::new(); + + machine.registers.status.insert(PS_OVERFLOW); + machine.branch_if_overflow_clear(Address(0xABCD)); + assert_eq!(machine.registers.program_counter, Address(0)); + + machine.registers.status.remove(PS_OVERFLOW); + machine.branch_if_overflow_clear(Address(0xABCD)); + assert_eq!(machine.registers.program_counter, Address(0xABCD)); +}