From c9ed9e17207ce93bdc2bea60e908ad12e15ba2c4 Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Thu, 6 Nov 2014 19:48:48 -0500 Subject: [PATCH 1/3] Add branch_if_equal_test(). --- src/machine.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/machine.rs b/src/machine.rs index 1b90a47..aeee90b 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -910,6 +910,18 @@ fn branch_if_carry_set_test() { assert_eq!(machine.registers.program_counter, Address(0xABCD)); } +#[test] +fn branch_if_equal_test() { + let mut machine = Machine::new(); + + machine.branch_if_equal(Address(0xABCD)); + assert_eq!(machine.registers.program_counter, Address(0)); + + machine.registers.status.or(PS_ZERO); + machine.branch_if_equal(Address(0xABCD)); + assert_eq!(machine.registers.program_counter, Address(0xABCD)); +} + #[test] fn branch_if_minus_test() { { From 8febc747889d3c9a40227da96c2663b26763b7e4 Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Thu, 6 Nov 2014 19:49:54 -0500 Subject: [PATCH 2/3] Add branch_if_equal(). --- src/machine.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/machine.rs b/src/machine.rs index aeee90b..d36b3e0 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -565,6 +565,12 @@ impl Machine { } } + fn branch_if_equal(&mut self, addr: Address) { + if self.registers.status.contains(PS_ZERO) { + self.registers.program_counter = addr; + } + } + fn branch_if_minus(&mut self, addr: Address) { if self.registers.status.contains(PS_NEGATIVE) { self.registers.program_counter = addr; From a0ad2d47be1376daca9812e385af066b421e9483 Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Thu, 6 Nov 2014 19:50:35 -0500 Subject: [PATCH 3/3] Add BEQ to execute_instruction(). --- src/machine.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/machine.rs b/src/machine.rs index d36b3e0..5372e3d 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -122,6 +122,12 @@ impl Machine { self.branch_if_carry_set(addr); } + (instruction::BEQ, instruction::UseRelative(rel)) => { + let addr = self.registers.program_counter + + AddressDiff(rel as i32); + self.branch_if_equal(addr); + } + (instruction::BIT, instruction::UseAddress(addr)) => { let a: u8 = self.registers.accumulator as u8; let m: u8 = self.memory.get_byte(addr);