Skip to content

Commit

Permalink
feat: skeleton fetch/decode/execute loop
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoof committed Oct 1, 2023
1 parent cf3a60a commit bc1d941
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 17 deletions.
67 changes: 67 additions & 0 deletions src/.state.rs.rustfmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use crate::display::Display;
use crate::operation::Operation;

const FONT: [u8; 80] = [
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
];

pub struct State {
memory: [u8; 4096],
pc: usize,
index: usize,
stack: Vec<u16>,
delay_timer: u8,
sound_timer: u8,
variables: [u8; 16],
display: Display,
}

impl State {
pub fn new() -> Self {
Self {
memory: [0; 4096],
pc: 0,
index: 0,
stack: vec![],
delay_timer: 0,
sound_timer: 0,
variables: [0; 16],
display: Display::new()
}
}

pub fn default() -> Self {
let instance = Self::new();

// TODO: Initialise memory with font (and other) data

instance
}

pub fn next_operation(&mut self) -> Operation {
let opcode = (self.memory[self.pc] as u16) << 8 + self.memory[self.pc + 1] as u16;
self.pc += 2;
Operation::new(opcode)
}

pub fn update(&mut self, op: Operation) {
match op {
_ => ()
}
}
}
11 changes: 11 additions & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub struct Display {
buffer: [u32; 64 * 32],
}

impl Display {
pub fn new() -> Self {
Self {
buffer: [0; 64 * 32],
}
}
}
25 changes: 8 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
use minifb::{Key, Window, WindowOptions};
use state::State;

const WIDTH: usize = 640;
const HEIGHT: usize = 360;
pub mod display;
pub mod operation;
pub mod state;

fn main() {
let mut buf = vec![0u32; WIDTH * HEIGHT];
let mut state = State::default();

let mut window = Window::new("Test", WIDTH, HEIGHT, WindowOptions::default())
.unwrap_or_else(|e| panic!("{}", e));

window.limit_update_rate(Some(std::time::Duration::from_micros(16600)));

while window.is_open() && !window.is_key_down(Key::Escape) {
for (i, pixel) in buf.iter_mut().enumerate() {
let x: u32 = (i % WIDTH) as u32;
let y: u32 = (i / WIDTH) as u32;
*pixel = (x % (y + 1)) % u32::MAX;
}

window.update_with_buffer(&buf, WIDTH, HEIGHT).unwrap()
loop {
let op = state.next_operation();
state.update(op);
}
}
12 changes: 12 additions & 0 deletions src/operation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum Operation {
ClearScreen,
}

impl Operation {
pub fn new(opcode: u16) -> Self {
match opcode {
_ => Self::ClearScreen,
}
}
}
69 changes: 69 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::display::Display;
use crate::operation::Operation;

const FONT: [u8; 80] = [
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80, // F
];

pub struct State {
memory: [u8; 4096],
pc: usize,
index: usize,
stack: Vec<u16>,
delay_timer: u8,
sound_timer: u8,
variables: [u8; 16],
display: Display,
}

impl State {
pub fn new() -> Self {
Self {
memory: [0; 4096],
pc: 0,
index: 0,
stack: vec![],
delay_timer: 0,
sound_timer: 0,
variables: [0; 16],
display: Display::new(),
}
}

pub fn default() -> Self {
let mut memory = [0u8; 4096];
memory[0x50..=0x9F].copy_from_slice(&FONT);

Self {
memory,
..Self::new()
}
}

pub fn next_operation(&mut self) -> Operation {
let opcode = (self.memory[self.pc] as u16) << 8 + self.memory[self.pc + 1] as u16;
self.pc += 2;
Operation::new(opcode)
}

pub fn update(&mut self, op: Operation) {
match op {
_ => (),
}
}
}

0 comments on commit bc1d941

Please sign in to comment.