-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: skeleton fetch/decode/execute loop
- Loading branch information
Showing
5 changed files
with
167 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
_ => () | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
_ => (), | ||
} | ||
} | ||
} |