This commit is contained in:
2023-12-26 22:57:23 -05:00
parent cc127890d1
commit 774010bcf7
2 changed files with 68 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
use crate::cpu::{bus::Bus, registers::Registers};
use crate::cpu::cycles;
use super::{CPUInstruction, Decode};
use super::decoder_common;
use super::branch_common;
static INSTR_NAME: &'static str = "BNE";
pub struct BNE {}
impl CPUInstruction for BNE {
fn execute(&self, registers: &mut Registers, bus: &mut Bus) {
let nearlabel = bus.read(registers.get_pc_address().wrapping_add(1));
let (bytes, cycles) = cycles::increment_cycles_branch();
registers.increment_pc(bytes); registers.cycles += cycles;
if !registers.get_zero_flag() {
let page_boundary_crossed = branch_common::do_branch(nearlabel, registers);
let (bytes, cycles) = cycles::increment_cycles_branch_taken(page_boundary_crossed);
registers.increment_pc(bytes); registers.cycles += cycles;
}
}
}
impl Decode for BNE {
fn mnemonic(&self, registers: &Registers, bus: &Bus, opcode: u8) -> String {
decoder_common::mnemonic_branch_nearlabel(opcode, INSTR_NAME, registers, bus)
}
}
#[cfg(test)]
mod cpu_instructions_tests {
use super::*;
#[test]
fn test_beq() {
let instruction = BNE{};
// test with positive nearlabel
// branch not taken
let mut registers = Registers::new();
let mut bus = Bus::new();
registers.pc = 0x0000;
registers.cycles = 0;
registers.set_zero_flag(true);
bus.write(0x02, 0b00001111);
instruction.execute(&mut registers, &mut bus);
assert_eq!(registers.pc, 0x02);
assert_eq!(registers.cycles, 2);
// branch taken
registers.pc = 0x0000;
registers.cycles = 0;
registers.set_zero_flag(false);
bus.write(0x01, 0b00001111);
instruction.execute(&mut registers, &mut bus);
assert_eq!(registers.pc, 0x02 + 0b00001111);
assert_eq!(registers.cycles, 3);
// test with negative nearlabel and boundary cross
registers.pc = 0x0100;
registers.cycles = 0;
registers.set_zero_flag(false);
bus.write(0x101, 0xFB); // write -5
instruction.execute(&mut registers, &mut bus);
assert_eq!(registers.pc, 0xFD);
assert_eq!(registers.cycles, 4);
}
}
@@ -8,6 +8,7 @@ pub mod asl;
pub mod bcc;
pub mod bcs;
pub mod beq;
pub mod bne;
pub mod decoder_common;
pub mod branch_common;