From 632eab5e344383bab2a054c42c2b2933ae559952 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Mon, 15 Jun 2020 00:45:49 -0500 Subject: [PATCH] Create Chip8 Struct and implement opcode parsing --- Cargo.lock | 5 +++ src/main.rs | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 Cargo.lock diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..60fb3b2 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "chip8" +version = "0.1.0" diff --git a/src/main.rs b/src/main.rs index e7a11a9..ad3dcd0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,114 @@ -fn main() { - println!("Hello, world!"); + +const SCREEN_SIZE: usize = 64 * 32; + +struct Chip8 { + opcode: u16, + i: u16, + pc: u16, + sp: u16, + key: Option, + v: [u8; 16], + stack: [u16; 16], + memory: [u8; 4096], + delay: Timer, + sound: Timer, + display: Display, } + + +impl Default for Chip8 { + fn default() -> Self { + Chip8 { + opcode: 0, + i: 0, + pc: 0, + sp: 0, + key: None, + v: [0; 16], + stack: [0; 16], + memory: [0; 4096], + delay: Default::default(), + sound: Default::default(), + display: Display::default(), + } + } +} + + +impl Chip8 { + fn new() -> Self { + Default::default() + } + + pub fn execute_cycle(&self) { + + } + + fn get_opcode(&self) -> u16 { + let pc = self.pc as usize; + ((self.memory[pc] as u16) << 8) | self.memory[pc + 1] as u16 + } + + fn handle_opcode(&mut self) { + // Given: 0xA2F0 + let nib_1 = (self.opcode & 0xF000) >> 12; //0xA + let nib_2 = (self.opcode & 0x0F00) >> 8; // 0x2 + let nib_3 = (self.opcode & 0x00F0) >> 4; // 0xF + let nib_4 = self.opcode & 0x000F; // 0x0 + + // nib_ns are u16s so we waste 4 bytes here. + + match (nib_1, nib_2, nib_3, nib_4) { + // CLS + (0x0, 0x0, 0xE, 0x0) => self.clear_display(), + // 00EE + (0x0, 0x0, 0xE, 0xE) => {} + // 1NNN + (0x1, _, _, _) => {} + // 2NNN + (0x2, _, _, _) => {} + // 3XKK + (0x3, _, _, _) => {} + _ => unimplemented!("UNIMPLEMENTED OPCODE: {:#x}", self.opcode) + } + } + + fn clear_display(&mut self) { + self.display.clear(); + } + +} + +struct Display { + buf: [u8; SCREEN_SIZE], +} + +impl Display { + pub fn clear(&mut self) { + self.buf = [0; SCREEN_SIZE] + } +} + +impl Default for Display { + fn default() -> Self { + Display { + buf: [0; SCREEN_SIZE], + } + } +} + +struct Timer { + value: u8, +} + +impl Default for Timer { + fn default() -> Self { + Timer { + value: 0, + } + } +} + +fn main() { + println!("Chip8 Emulator"); +} \ No newline at end of file