From a2ff6998232a0ec0db20afaf1262900f12200f00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Antoine=20Gigu=C3=A8re?= Date: Wed, 1 May 2024 20:29:58 +0000 Subject: [PATCH] add operation - BNE --- src/cpu.rs | 7 +++++ tests/bcc_tests.rs | 1 + tests/bne_tests.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 tests/bne_tests.rs diff --git a/src/cpu.rs b/src/cpu.rs index 740fd8f..3bc9d83 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -73,6 +73,7 @@ impl CPU { (0xB0, Operation::new(BCS, Relative, 2)), (0xF0, Operation::new(BEQ, Relative, 2)), (0x30, Operation::new(BMI, Relative, 2)), + (0xD0, Operation::new(BNE, Relative, 2)), (0x24, Operation::new(BIT, ZeroPage, 2)), (0x2C, Operation::new(BIT, Absolute, 3)), @@ -207,6 +208,7 @@ impl CPU { BCS => self.bcs(), BEQ => self.beq(), BMI => self.bmi(), + BNE => self.bne(), BIT => self.bit(op.addressing_mode), BRK => return, LDA => self.lda(op.addressing_mode), @@ -282,6 +284,11 @@ impl CPU { self.branch(condition); } + fn bne(&mut self) { + let condition = !self.zero_flag(); + self.branch(condition); + } + fn branch(&mut self, branching_condition: bool) { if !branching_condition { return; diff --git a/tests/bcc_tests.rs b/tests/bcc_tests.rs index 41f93c7..007ff08 100644 --- a/tests/bcc_tests.rs +++ b/tests/bcc_tests.rs @@ -56,6 +56,7 @@ fn test_0x90_bcc_relative_ignores_branching_when_condition_is_not_met() { /*LDA*/ 0xA9, 0xFF, /*ADC*/ 0x69, 0x01, /*BCC-4*/ 0x90, 0x84, + 0x00 ]); assert_eq!(cpu.register_a, 0x00); diff --git a/tests/bne_tests.rs b/tests/bne_tests.rs new file mode 100644 index 0000000..b663d18 --- /dev/null +++ b/tests/bne_tests.rs @@ -0,0 +1,64 @@ +use std::vec; +use nes_emulator::cpu::{CPU, Flags}; + +mod common; +use common::{assert_no_flags, assert_flags}; + +#[test] +fn test_0xd0_bne_relative_branches_forward_correctly() { + let mut cpu = CPU::new(); + + cpu.load_and_run_without_reset(vec![ + /*BNE+2*/ 0xD0, 0x02, + /*LDA*/ 0xA9, 0x02, + 0x00 + ]); + + assert_eq!(cpu.register_a, 0x00); + assert_no_flags(&cpu); +} + +#[test] +fn test_0xd0_bne_relative_branches_backward_correctly() { + let mut cpu = CPU::new(); + + cpu.load_and_run_without_reset(vec![ + /*LDA*/ 0xA9, 0xFE, + /*ADC*/ 0x69, 0x01, + /*BNE-4*/ 0xD0, 0x84, + 0x00 + ]); + + assert_eq!(cpu.register_a, 0x00); + assert_flags(&cpu, vec![Flags::Carry, Flags::Zero]); +} + +#[test] +fn test_0xd0_bne_relative_ignores_branching_when_offset_is_zero() { + let mut cpu = CPU::new(); + + cpu.load_and_run_without_reset(vec![ + /*LDA*/ 0xA9, 0x00, + /*BNE+-0*/ 0xD0, 0x00, + /*ADC*/ 0x69, 0x01, + 0x00 + ]); + + assert_eq!(cpu.register_a, 0x01); + assert_no_flags(&cpu); +} + +#[test] +fn test_0xd0_bne_relative_ignores_branching_when_condition_is_not_met() { + let mut cpu = CPU::new(); + + cpu.load_and_run_without_reset(vec![ + /*LDA*/ 0xA9, 0xFF, + /*ADC*/ 0x69, 0x01, + /*BNE-4*/ 0xD0, 0x84, + 0x00 + ]); + + assert_eq!(cpu.register_a, 0x00); + assert_flags(&cpu, vec![Flags::Zero, Flags::Carry]); +}