Skip to content

Commit

Permalink
add operation - BNE
Browse files Browse the repository at this point in the history
  • Loading branch information
marcantoineg committed May 1, 2024
1 parent 8677c8f commit a2ff699
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/bcc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
64 changes: 64 additions & 0 deletions tests/bne_tests.rs
Original file line number Diff line number Diff line change
@@ -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]);
}

0 comments on commit a2ff699

Please sign in to comment.