Create Chip8 Struct and implement opcode parsing

This commit is contained in:
Rekai Musuka 2020-06-15 00:45:49 -05:00
parent fe2735c102
commit 632eab5e34
2 changed files with 118 additions and 2 deletions

5
Cargo.lock generated Normal file
View File

@ -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"

View File

@ -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<u8>,
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");
}