2020-06-25 06:40:09 +00:00
|
|
|
// I used: http://devernay.free.fr/hacks/chip8/C8TECH10.HTM#00E0
|
|
|
|
// and https://en.wikipedia.org/wiki/CHIP-8#Opcode_table
|
|
|
|
// for information about how to implement a CHIP-8 Emulator
|
2020-06-25 18:13:09 +00:00
|
|
|
use chip8::emu::Chip8;
|
2020-06-25 06:40:09 +00:00
|
|
|
use std::path::Path;
|
2020-06-25 19:22:37 +00:00
|
|
|
|
2020-06-26 00:06:27 +00:00
|
|
|
use pixels::{wgpu::Surface, Pixels, SurfaceTexture};
|
2020-06-25 19:22:37 +00:00
|
|
|
use winit::dpi::LogicalSize;
|
|
|
|
use winit::event::{Event, VirtualKeyCode};
|
|
|
|
use winit::event_loop::{ControlFlow, EventLoop};
|
2020-06-26 00:06:27 +00:00
|
|
|
use winit::window::{Window, WindowBuilder};
|
2020-06-25 19:22:37 +00:00
|
|
|
use winit_input_helper::WinitInputHelper;
|
|
|
|
|
|
|
|
static WIDTH: u32 = 64;
|
|
|
|
static HEIGHT: u32 = 32;
|
|
|
|
|
2020-06-15 05:45:49 +00:00
|
|
|
fn main() {
|
2020-07-14 03:16:42 +00:00
|
|
|
let mut chip8: Chip8 = Default::default();
|
|
|
|
let mut input = WinitInputHelper::new();
|
2020-06-25 19:22:37 +00:00
|
|
|
let event_loop = EventLoop::new();
|
2020-06-26 00:06:27 +00:00
|
|
|
let window = init_window(&event_loop);
|
2020-07-14 03:16:42 +00:00
|
|
|
let mut pixels = init_pixels(&window);
|
2020-06-25 19:22:37 +00:00
|
|
|
|
2020-07-15 03:44:26 +00:00
|
|
|
let rom_path = Path::new("./games/c8games/INVADERS");
|
2020-07-14 03:16:42 +00:00
|
|
|
chip8.load_rom(rom_path).expect("Unable to load ROM");
|
2020-06-25 06:40:09 +00:00
|
|
|
|
2020-06-25 19:22:37 +00:00
|
|
|
event_loop.run(move |event, _, control_flow| {
|
2020-06-26 00:06:27 +00:00
|
|
|
if let Event::RedrawRequested(_) = event {
|
|
|
|
draw(&chip8.display.buf, pixels.get_frame());
|
|
|
|
if pixels
|
|
|
|
.render()
|
|
|
|
.map_err(|e| eprintln!("pixels.render() failed: {}", e))
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 19:22:37 +00:00
|
|
|
|
|
|
|
if input.update(&event) {
|
2020-07-15 03:44:26 +00:00
|
|
|
handle_input(&mut chip8, &mut input, control_flow);
|
2020-06-25 19:22:37 +00:00
|
|
|
|
|
|
|
if let Some(size) = input.window_resized() {
|
|
|
|
pixels.resize(size.width, size.height);
|
|
|
|
}
|
|
|
|
|
2020-06-26 00:06:27 +00:00
|
|
|
chip8.execute_cycle();
|
2020-07-15 03:44:26 +00:00
|
|
|
if chip8.request_redraw {
|
|
|
|
window.request_redraw();
|
|
|
|
}
|
2020-06-26 00:06:27 +00:00
|
|
|
}
|
2020-06-25 19:22:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-15 03:44:26 +00:00
|
|
|
fn handle_input(chip8: &mut Chip8, input: &mut WinitInputHelper, control_flow: &mut ControlFlow) {
|
|
|
|
if input.key_pressed(VirtualKeyCode::Escape) || input.quit() {
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 04:35:00 +00:00
|
|
|
// 1 -> 1
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::Key1) {
|
|
|
|
chip8.key.set_key(0x1);
|
2020-07-15 04:35:00 +00:00
|
|
|
}
|
|
|
|
if input.key_released(VirtualKeyCode::Key1) {
|
2020-07-15 03:44:26 +00:00
|
|
|
chip8.key.unset_key(0x1);
|
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
// 2 -> 2
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::Key2) {
|
|
|
|
chip8.key.set_key(0x2);
|
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
if input.key_released(VirtualKeyCode::Key2) {
|
|
|
|
chip8.key.unset_key(0x2);
|
|
|
|
}
|
|
|
|
// 3 -> 3
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::Key3) {
|
|
|
|
chip8.key.set_key(0x3);
|
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
if input.key_released(VirtualKeyCode::Key3) {
|
|
|
|
chip8.key.unset_key(0x3);
|
|
|
|
}
|
|
|
|
// 4 -> C
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::Key4) {
|
|
|
|
chip8.key.set_key(0xC);
|
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
if input.key_released(VirtualKeyCode::Key4) {
|
|
|
|
chip8.key.unset_key(0xC);
|
|
|
|
}
|
|
|
|
// Q -> 4
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::Q) {
|
|
|
|
chip8.key.set_key(0x4);
|
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
if input.key_released(VirtualKeyCode::Q) {
|
|
|
|
chip8.key.unset_key(0x4);
|
|
|
|
}
|
|
|
|
// W -> 5
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::W) {
|
|
|
|
chip8.key.set_key(0x5);
|
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
if input.key_released(VirtualKeyCode::W) {
|
|
|
|
chip8.key.unset_key(0x5);
|
|
|
|
}
|
|
|
|
// E -> 6
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::E) {
|
|
|
|
chip8.key.set_key(0x6);
|
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
if input.key_released(VirtualKeyCode::E) {
|
|
|
|
chip8.key.unset_key(0x6);
|
|
|
|
}
|
|
|
|
// R -> D
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::R) {
|
|
|
|
chip8.key.set_key(0xD);
|
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
if input.key_released(VirtualKeyCode::R) {
|
|
|
|
chip8.key.unset_key(0xD);
|
|
|
|
}
|
|
|
|
// A -> 7
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::A) {
|
|
|
|
chip8.key.set_key(0x7);
|
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
if input.key_released(VirtualKeyCode::A) {
|
|
|
|
chip8.key.unset_key(0x7);
|
|
|
|
}
|
|
|
|
// S -> 8
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::S) {
|
|
|
|
chip8.key.set_key(0x8);
|
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
if input.key_released(VirtualKeyCode::S) {
|
|
|
|
chip8.key.unset_key(0x8);
|
|
|
|
}
|
|
|
|
// D -> 9
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::D) {
|
|
|
|
chip8.key.set_key(0x9);
|
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
if input.key_released(VirtualKeyCode::D) {
|
|
|
|
chip8.key.unset_key(0x9);
|
|
|
|
}
|
|
|
|
// F -> A
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::F) {
|
2020-07-15 04:35:00 +00:00
|
|
|
chip8.key.set_key(0xE);
|
|
|
|
}
|
|
|
|
if input.key_released(VirtualKeyCode::F) {
|
|
|
|
chip8.key.unset_key(0xE);
|
2020-07-15 03:44:26 +00:00
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
// Z -> A
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::Z) {
|
|
|
|
chip8.key.set_key(0xA);
|
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
if input.key_released(VirtualKeyCode::Z) {
|
|
|
|
chip8.key.unset_key(0xA);
|
|
|
|
}
|
|
|
|
// X -> 0
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::X) {
|
|
|
|
chip8.key.set_key(0x0);
|
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
if input.key_released(VirtualKeyCode::X) {
|
|
|
|
chip8.key.unset_key(0x0);
|
|
|
|
}
|
|
|
|
// C -> B
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::C) {
|
|
|
|
chip8.key.set_key(0xB);
|
|
|
|
}
|
2020-07-15 04:35:00 +00:00
|
|
|
if input.key_released(VirtualKeyCode::C) {
|
|
|
|
chip8.key.unset_key(0xB);
|
|
|
|
}
|
|
|
|
// V -> F
|
2020-07-15 03:44:26 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::V) {
|
|
|
|
chip8.key.set_key(0xF);
|
2020-07-15 04:35:00 +00:00
|
|
|
}
|
|
|
|
if input.key_released(VirtualKeyCode::V) {
|
|
|
|
chip8.key.unset_key(0xF);
|
2020-07-15 03:44:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 00:06:27 +00:00
|
|
|
fn init_pixels(window: &Window) -> Pixels {
|
|
|
|
let surface = Surface::create(window);
|
|
|
|
let texture = SurfaceTexture::new(WIDTH, HEIGHT, surface);
|
|
|
|
Pixels::new(WIDTH, HEIGHT, texture).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init_window(event_loop: &EventLoop<()>) -> Window {
|
2020-07-14 03:16:42 +00:00
|
|
|
const SCALE: u32 = 10;
|
|
|
|
let min_size = LogicalSize::new(WIDTH as f64, HEIGHT as f64);
|
|
|
|
let size = LogicalSize::new((WIDTH * SCALE) as f64, (HEIGHT * SCALE) as f64);
|
2020-06-26 00:06:27 +00:00
|
|
|
WindowBuilder::new()
|
|
|
|
.with_title("Chip8 Emulator")
|
|
|
|
.with_inner_size(size)
|
2020-07-14 03:16:42 +00:00
|
|
|
.with_min_inner_size(min_size)
|
2020-06-26 00:06:27 +00:00
|
|
|
.build(event_loop)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2020-06-25 19:22:37 +00:00
|
|
|
fn draw(chip8_gfx: &[u8], frame: &mut [u8]) {
|
|
|
|
for (i, pixel) in frame.chunks_exact_mut(4).enumerate() {
|
2020-07-13 20:28:41 +00:00
|
|
|
let rgba = if chip8_gfx[i] == 1 {
|
2020-07-14 04:04:58 +00:00
|
|
|
[0xFF; 4]
|
2020-06-25 19:22:37 +00:00
|
|
|
} else {
|
2020-07-14 04:04:58 +00:00
|
|
|
[0x00; 4]
|
2020-06-25 19:22:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pixel.copy_from_slice(&rgba);
|
2020-06-25 06:40:09 +00:00
|
|
|
}
|
|
|
|
}
|