Skip to content

Commit

Permalink
Implement BVC instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrakisk committed Apr 5, 2024
1 parent c72989b commit 31efdef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ A (soon to be) fully-featured NES emulator written in Rust.
- [X] BNE
- [X] BPL
- [ ] BRK
- [ ] BVC
- [X] BVC
- [ ] BVS
- [ ] CLC
- [ ] CLD
Expand Down
3 changes: 2 additions & 1 deletion src/cpu/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ pub static INSTRUCTIONS: Lazy<HashMap<u8, Instruction>> = Lazy::new(|| {
Instruction {opcode: 0xB0, name: "BCS", bytes: 2, addressing_mode: AddressingMode::Relative},
Instruction {opcode: 0xF0, name: "BEQ", bytes: 2, addressing_mode: AddressingMode::Relative},
Instruction {opcode: 0x30, name: "BMI", bytes: 2, addressing_mode: AddressingMode::Relative},
Instruction {opcode: 0x10, name: "BPL", bytes: 2, addressing_mode: AddressingMode::Relative},
Instruction {opcode: 0xD0, name: "BNE", bytes: 2, addressing_mode: AddressingMode::Relative},
Instruction {opcode: 0x10, name: "BPL", bytes: 2, addressing_mode: AddressingMode::Relative},
Instruction {opcode: 0x00, name: "BRK", bytes: 1, addressing_mode: AddressingMode::Implicit},
Instruction {opcode: 0x50, name: "BVC", bytes: 2, addressing_mode: AddressingMode::Relative},
Instruction {opcode: 0xA9, name: "LDA", bytes: 2, addressing_mode: AddressingMode::Immediate},
Instruction {opcode: 0xA5, name: "LDA", bytes: 2, addressing_mode: AddressingMode::ZeroPage},
Instruction {opcode: 0xB5, name: "LDA", bytes: 2, addressing_mode: AddressingMode::ZeroPage_X},
Expand Down
23 changes: 20 additions & 3 deletions src/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ impl CPU {
self.bpl();
None
}
"BVC" => {
self.bvc();
None
}
"BRK" => Some(0),
"LDA" => {
self.lda(&instruction.addressing_mode);
Expand Down Expand Up @@ -366,6 +370,19 @@ impl CPU {
}
}

fn bvc (&mut self) {
match self.get_flag_state(STATUS_FLAG_MASK_OVERFLOW) {
FlagStates::CLEAR => {
let distance = self.mem_read(self.program_counter);
self.program_counter =
self.branch_off_program_counter(self.program_counter, distance as u16);
}
FlagStates::SET => {
return;
}
}
}

fn lda(&mut self, addressing_mode: &AddressingMode) {
let operand = self.get_operand(addressing_mode);
self.register_a = operand;
Expand Down Expand Up @@ -646,15 +663,15 @@ mod test_cpu {
assert_eq!(cpu.program_counter, expected_program_counter);
}

#[test_case(0b1000_0000, 0x8080, 0x8080, 0x06)]
#[test_case(0b0100_0000, 0x8080, 0x8080, 0x06)]
#[test_case(0b0000_0000, 0xE004, 0xE009, 0x06)]
#[test_case(0b0000_0000, 0xE009, 0xE003, 0xFA)]
fn test_bpl(status: u8, program_counter: u16, expected_program_counter: u16, distance: u8) {
fn test_bvc(status: u8, program_counter: u16, expected_program_counter: u16, distance: u8) {
let mut cpu = CPU::new();
cpu.status = status;
cpu.program_counter = program_counter;
cpu.memory[cpu.program_counter as usize] = distance;
cpu.bpl();
cpu.bvc();
assert_eq!(cpu.program_counter, expected_program_counter);
}

Expand Down

0 comments on commit 31efdef

Please sign in to comment.