From eacc01c2c81f106d89ee08afb32887e281998373 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sat, 10 Sep 2022 12:35:15 +0200 Subject: [PATCH] CHIP-8 WIP --- ch5-data-in-depth/src/chip8.rs | 54 ++++++++++++++++++++++++++++++++-- ch5-data-in-depth/src/main.rs | 4 +-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/ch5-data-in-depth/src/chip8.rs b/ch5-data-in-depth/src/chip8.rs index 0a36044..2b6d1a4 100644 --- a/ch5-data-in-depth/src/chip8.rs +++ b/ch5-data-in-depth/src/chip8.rs @@ -1,7 +1,57 @@ +/// Operations are coded on 16 bits (Four nibbles, a nibble = half-byte): +/// 1) Opcode group (c). +/// 2) First register (x). / Memory address (nnn). +/// 3) Second register (y). / Integer (kk) / Memory address (nnn). +/// 4) Opcode subgroup (d) / Number of bytes (n). / Integer (kk) / Memory address (nnn). struct CPU { - current_operation: u16, - registers: [u8; 2], + registers: [u8; 16], + position_in_memory: usize, + memory: [u8; 0x1000], // 4 KB of memory. } +impl CPU { + fn read_opcode(&self) -> u16 { + self.current_operation + } + + fn run(&mut self) { + // loop { + let opcode = self.read_opcode(); + let c = ((opcode & 0xF000) >> 12) as u8; + let x = ((opcode & 0x0F00) >> 8) as u8; + let y = ((opcode & 0x00F0) >> 4) as u8; + let d = ((opcode & 0x000F) >> 0) as u8; + + match (c, x, y, d) { + (0x8, _, _, 0x4) => self.add_xy(x, y), + _ => todo!("opcode: {:04x}", opcode), + } + //} + } + + fn add_xy(&mut self, x: u8, y: u8) { + self.registers[x as usize] += self.registers[y as usize]; + } +} + +mod tests { + use super::*; + + #[test] + fn addition() { + let mut cpu = CPU { + current_operation: 0, + registers: [0; 2] + }; + + cpu.current_operation = 0x8014; + cpu.registers[0] = 5; + cpu.registers[1] = 10; + + cpu.run(); + + assert_eq!(cpu.registers[0], 15); + } +} \ No newline at end of file diff --git a/ch5-data-in-depth/src/main.rs b/ch5-data-in-depth/src/main.rs index 7f26aeb..ba04717 100644 --- a/ch5-data-in-depth/src/main.rs +++ b/ch5-data-in-depth/src/main.rs @@ -20,7 +20,7 @@ fn test_chip() { } fn main() { - // test_decode_f32(); - // test_q7(); + test_decode_f32(); + test_q7(); test_chip(); } -- 2.45.2