Skip to content

Commit

Permalink
Implement CLC instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrakisk committed Apr 9, 2024
1 parent 047c395 commit 9c634dc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ A (soon to be) fully-featured NES emulator written in Rust.
- [ ] BRK
- [X] BVC
- [X] BVS
- [ ] CLC
- [X] CLC
- [ ] CLD
- [ ] CLI
- [ ] CLV
Expand Down
1 change: 1 addition & 0 deletions src/cpu/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub static INSTRUCTIONS: Lazy<HashMap<u8, Instruction>> = Lazy::new(|| {
Instruction {opcode: 0x00, name: "BRK", bytes: 1, addressing_mode: AddressingMode::Implicit},
Instruction {opcode: 0x50, name: "BVC", bytes: 2, addressing_mode: AddressingMode::Relative},
Instruction {opcode: 0x70, name: "BVS", bytes: 2, addressing_mode: AddressingMode::Relative},
Instruction {opcode: 0x18, name: "CLC", bytes: 1, addressing_mode: AddressingMode::Implicit},
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
19 changes: 19 additions & 0 deletions src/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ impl CPU {
self.bvs();
None
}
"CLC" => {
self.clc();
None
}
"BRK" => Some(0),
"LDA" => {
self.lda(&instruction.addressing_mode);
Expand Down Expand Up @@ -400,6 +404,10 @@ impl CPU {
}
}

fn clc(&mut self) {
self.clear_flag(STATUS_FLAG_MASK_CARRY);
}

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

#[test_case(0b0000_0001, 0b0000_0000)]
#[test_case(0b0000_0000, 0b0000_0000)]
#[test_case(0b0100_0001, 0b0100_0000)]
#[test_case(0b0100_0000, 0b0100_0000)]
fn test_clc(status: u8, expected_status: u8) {
let mut cpu = CPU::new();
cpu.status = status;
cpu.clc();
assert_eq!(cpu.status, expected_status)
}

#[test_case(0b0000_0001, 0x5, 0x4, 0x1, 0b0000_0001)]
#[test_case(0b0000_0001, 0x5, 0x5, 0x0, 0b0000_0011)]
#[test_case(0b0000_0001, 0x0, 0x1, 0xFF, 0b1000_0000)]
Expand Down

0 comments on commit 9c634dc

Please sign in to comment.