Skip to content

Commit

Permalink
finished desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
aowalke2 committed Mar 14, 2024
1 parent 598e102 commit da13f21
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/target
*.ch8
*.rom
/roms

51 changes: 50 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
[package]
name = "chip-8"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.5"
workspace = { members = ["desktop", "core"]}
9 changes: 9 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "core"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.5"
23 changes: 18 additions & 5 deletions src/interpreter.rs → core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const PROGRAM_START: u16 = 0x200;
const STACK_SIZE: usize = 16;
const NUMBER_OF_REGISTERS: usize = 16;
const MEMORY_SIZE: usize = 4096;
const SCREEN_WIDTH: usize = 64;
const SCREEN_HEIGHT: usize = 32;
pub const SCREEN_WIDTH: usize = 64;
pub const SCREEN_HEIGHT: usize = 32;
const NUMBER_OF_KEYS: usize = 16;
const FONTSET_SIZE: usize = 80;
const FONTSET: [u8; FONTSET_SIZE] = [
Expand Down Expand Up @@ -402,13 +402,13 @@ impl Interpreter {
self.program_counter = PROGRAM_START;
}

pub fn fetch(&mut self) -> u16 {
fn fetch(&mut self) -> u16 {
let opcode = self.mem_read_16(self.program_counter);
self.program_counter += 2;
opcode
}

pub fn execute(&mut self, opcode: u16) {
fn execute(&mut self, opcode: u16) {
let nibble1 = (opcode & 0xF000) >> 12;
let nibble2 = (opcode & 0x0F00) >> 8;
let nibble3 = (opcode & 0x00F0) >> 4;
Expand Down Expand Up @@ -453,6 +453,19 @@ impl Interpreter {
(_, _, _, _) => unimplemented!("Opcode not defined: {opcode}"),
}
}

pub fn tick(&mut self) {
let opcode = self.fetch();
self.execute(opcode);
}

pub fn get_screen(&self) -> &[[bool; SCREEN_WIDTH]; SCREEN_HEIGHT] {
&self.screen
}

pub fn keypress(&mut self, key: usize, pressed: bool) {
self.keys[key] = pressed;
}
}

#[cfg(test)]
Expand Down Expand Up @@ -855,7 +868,7 @@ mod test {
interpreter.mem_write(0x0, 0x84);
interpreter.mem_write(0x1, 0x85);
interpreter.mem_write(0x2, 0x86);
interpreter.ld_registers_with_mem(0xF255);
interpreter.ld_registers_with_mem(0xF265);
assert_eq!(interpreter.registers[0], 0x84);
assert_eq!(interpreter.registers[1], 0x85);
assert_eq!(interpreter.registers[2], 0x86);
Expand Down
10 changes: 10 additions & 0 deletions desktop/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "desktop"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
core = {path = "../core"}
sdl2 = "0.36.0"
119 changes: 119 additions & 0 deletions desktop/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use core::{Interpreter, SCREEN_HEIGHT, SCREEN_WIDTH};
use sdl2::{
event::Event, keyboard::Keycode, pixels::Color, rect::Rect, render::Canvas, video::Window,
};
use std::{env, fs::File, io::Read};

const SCALE: u32 = 24;
const WINDOW_WIDTH: u32 = (SCREEN_WIDTH as u32) * SCALE;
const WINDOW_HEIGHT: u32 = (SCREEN_HEIGHT as u32) * SCALE;
const TICKS_PER_FRAME: usize = 5;

fn main() {
let args: Vec<_> = env::args().collect();
if args.len() != 2 {
println!("Hello");
return;
}

let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem
.window("Chip-8", WINDOW_WIDTH, WINDOW_HEIGHT)
.position_centered()
.opengl()
.build()
.unwrap();

let mut canvas = window.into_canvas().present_vsync().build().unwrap();
canvas.clear();
canvas.present();

let mut event_pump = sdl_context.event_pump().unwrap();

let mut chip8 = Interpreter::new();

let mut rom = File::open(&args[1]).expect("Unable to open file");
let mut buffer = Vec::new();
rom.read_to_end(&mut buffer).unwrap();
chip8.load(&buffer);

'gameloop: loop {
for event in event_pump.poll_iter() {
match event {
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
..
} => {
break 'gameloop;
}
Event::KeyDown {
keycode: Some(key), ..
} => {
if let Some(k) = match_key_to_btn(key) {
chip8.keypress(k, true)
}
}
Event::KeyUp {
keycode: Some(key), ..
} => {
if let Some(k) = match_key_to_btn(key) {
chip8.keypress(k, false)
}
}
_ => (),
}
}

for _ in 0..TICKS_PER_FRAME {
chip8.tick();
}
chip8.tick_timers();
draw_screen(&chip8, &mut canvas)
}
}

fn draw_screen(interpreter: &Interpreter, canvas: &mut Canvas<Window>) {
canvas.set_draw_color(Color::RGB(0, 0, 0));
canvas.clear();

let screen_buffer = interpreter.get_screen();
canvas.set_draw_color(Color::RGB(255, 255, 255));
for row in 0..screen_buffer.len() {
for col in 0..screen_buffer[0].len() {
if screen_buffer[row][col] {
let rect = Rect::new(
(col as u32 * SCALE) as i32,
(row as u32 * SCALE) as i32,
SCALE,
SCALE,
);
canvas.fill_rect(rect).unwrap();
}
}
}
canvas.present();
}

fn match_key_to_btn(key: Keycode) -> Option<usize> {
match key {
Keycode::Num1 => Some(0x1),
Keycode::Num2 => Some(0x2),
Keycode::Num3 => Some(0x3),
Keycode::Num4 => Some(0xC),
Keycode::Q => Some(0x4),
Keycode::W => Some(0x5),
Keycode::E => Some(0x6),
Keycode::R => Some(0xD),
Keycode::A => Some(0x7),
Keycode::S => Some(0x8),
Keycode::D => Some(0x9),
Keycode::F => Some(0xE),
Keycode::Z => Some(0xA),
Keycode::X => Some(0x0),
Keycode::C => Some(0xB),
Keycode::V => Some(0xF),
_ => None,
}
}
3 changes: 0 additions & 3 deletions src/main.rs

This file was deleted.

0 comments on commit da13f21

Please sign in to comment.