Skip to content

Commit

Permalink
0xe8の実装
Browse files Browse the repository at this point in the history
  • Loading branch information
YuheiNakasaka committed Oct 28, 2022
1 parent cfd6671 commit 9eb800c
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ impl CPU {
self.update_zero_and_negative_flags(self.register_x);
}

fn inx(&mut self) {
if self.register_x == 0xff {
self.register_x = 0;
} else {
self.register_x += 1;
}
self.update_zero_and_negative_flags(self.register_x);
}

fn update_zero_and_negative_flags(&mut self, result: u8) {
if result == 0 {
self.status = self.status | 0b0000_0010;
Expand All @@ -46,16 +55,15 @@ impl CPU {
self.program_counter += 1;

match opcode {
// LDA
0xA9 => {
// Fetch a parameter
let param = program[self.program_counter as usize];
self.program_counter += 1;

self.lda(param);
}
// TAX
0xAA => self.tax(),
0xE8 => self.inx(),
0x00 => {
return;
}
Expand Down Expand Up @@ -107,4 +115,32 @@ mod test {
assert!(cpu.status & 0b0000_0010 == 0b00);
assert!(cpu.status & 0b1000_0000 == 0b1000_0000);
}

#[test]
fn test_0xe8_inx_increment_x() {
let mut cpu = CPU::new();
cpu.register_x = 0x7F;
assert!(cpu.status & 0b1000_0000 != 0b1000_0000);
cpu.interpret(vec![0xe8, 0x00]);
assert_eq!(cpu.register_x, 0x80);
assert!(cpu.status & 0b0000_0010 == 0b00);
assert!(cpu.status & 0b1000_0000 == 0b1000_0000);
}

#[test]
fn test_5_ops_working_together() {
let mut cpu = CPU::new();
cpu.interpret(vec![0xa9, 0xc0, 0xaa, 0xe8, 0x00]);

assert_eq!(cpu.register_x, 0xc1)
}

#[test]
fn test_inx_overflow() {
let mut cpu = CPU::new();
cpu.register_x = 0xff;
cpu.interpret(vec![0xe8, 0xe8, 0x00]);

assert_eq!(cpu.register_x, 1)
}
}

0 comments on commit 9eb800c

Please sign in to comment.