summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Perrouault <samuel.perrouault@gmail.com>2025-03-26 13:39:42 +0100
committerSamuel Perrouault <samuel.perrouault@gmail.com>2025-03-26 13:39:42 +0100
commitb74d4624faa7358836b57719e7794272f5fbe757 (patch)
treecd6b0298595c97f43ec5e5df322fb33ecb21f2eb
parente01b753e1c165db363cfef0008f4b2c7011fdd86 (diff)
hoist operand decode up out of the switch
-rw-r--r--src/chip8.rs40
1 files changed, 16 insertions, 24 deletions
diff --git a/src/chip8.rs b/src/chip8.rs
index abf79b2..99ce428 100644
--- a/src/chip8.rs
+++ b/src/chip8.rs
@@ -49,53 +49,46 @@ impl Chip8 {
pub fn cycle(&mut self) {
let pc = self.pc as usize;
let opcode = ((self.memory[pc] as u16) << 8) | self.memory[pc + 1] as u16;
+ let x = (opcode & 0x0F00) >> 8;
+ let y = (opcode & 0x00F0) >> 4;
+ let n = opcode & 0x000F;
+ let nn = opcode & 0x00FF;
+ let nnn = opcode & 0x0FFF;
+ self.pc += 2;
match opcode & 0xF000 {
0x0000 => match opcode & 0x000F {
0x0000 => {
eprintln!("CLS");
self.screen = [0; PIXELS];
self.draw = true;
- self.pc += 2;
}
0x000E => {
eprintln!("RET");
- self.pc += 2;
}
_ => unknown_opcode(opcode),
},
0x1000 => {
- let addr = opcode & 0x0FFF;
- eprintln!("JP {}", addr);
- self.pc = addr;
+ eprintln!("JP {}", nnn);
+ self.pc = nnn;
}
0x6000 => {
- let x = (opcode & 0x0F00) >> 8;
- let value = opcode & 0x00FF;
- eprintln!("LD V{}, {}", x, value);
- self.v[x as usize] = value as u8;
- self.pc += 2;
+ eprintln!("LD V{}, {}", x, nn);
+ self.v[x as usize] = nn as u8;
}
0x7000 => {
- let x = (opcode & 0x0F00) >> 8;
- let value = opcode & 0x00FF;
- eprintln!("ADD V{}, {}", x, value);
- self.v[x as usize] += value as u8;
- self.pc += 2;
+ eprintln!("ADD V{}, {}", x, nn);
+ self.v[x as usize] += nn as u8;
}
0xA000 => {
- self.i = opcode & 0x0FFF;
- self.pc += 2;
- eprintln!("LD I, {}", self.i);
+ eprintln!("LD I, {}", nnn);
+ self.i = nnn;
}
0xD000 => {
- let x = (opcode & 0x0F00) >> 8;
- let y = (opcode & 0x00F0) >> 4;
- let nibble = opcode & 0x000F;
- eprintln!("DRW V{}, V{}, {}", x, y, nibble);
+ eprintln!("DRW V{}, V{}, {}", x, y, n);
let vx = self.v[x as usize] as u16;
let vy = self.v[y as usize] as u16;
self.v[0xF] = 0;
- for yy in 0..nibble {
+ for yy in 0..n {
let pixel = self.memory[(self.i + yy) as usize];
for xx in 0..8 {
if (pixel & (0x80 >> xx)) != 0 {
@@ -108,7 +101,6 @@ impl Chip8 {
}
}
self.draw = true;
- self.pc += 2;
}
_ => unknown_opcode(opcode),
};