Implement opcode-limiting (current: 500 opcodes per second)

This commit is contained in:
Rekai Musuka 2020-07-15 14:25:29 -05:00
parent ff1a8222a8
commit f10ad7f8dd
2 changed files with 13 additions and 4 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/target /target
/games /games
/.vscode /.vscode
/.idea

View File

@ -2,9 +2,9 @@
// and https://en.wikipedia.org/wiki/CHIP-8#Opcode_table // and https://en.wikipedia.org/wiki/CHIP-8#Opcode_table
// for information about how to implement a CHIP-8 Emulator // for information about how to implement a CHIP-8 Emulator
use chip8::emu::Chip8; use chip8::emu::Chip8;
use std::path::Path;
use pixels::{wgpu::Surface, Pixels, SurfaceTexture}; use pixels::{wgpu::Surface, Pixels, SurfaceTexture};
use std::path::Path;
use std::time::{Duration, Instant};
use winit::dpi::LogicalSize; use winit::dpi::LogicalSize;
use winit::event::{Event, VirtualKeyCode}; use winit::event::{Event, VirtualKeyCode};
use winit::event_loop::{ControlFlow, EventLoop}; use winit::event_loop::{ControlFlow, EventLoop};
@ -13,6 +13,7 @@ use winit_input_helper::WinitInputHelper;
static WIDTH: u32 = 64; static WIDTH: u32 = 64;
static HEIGHT: u32 = 32; static HEIGHT: u32 = 32;
static OPCODES_PER_SECOND: u64 = 500;
fn main() { fn main() {
let mut chip8: Chip8 = Default::default(); let mut chip8: Chip8 = Default::default();
@ -24,6 +25,9 @@ fn main() {
let rom_path = Path::new("./games/c8games/INVADERS"); let rom_path = Path::new("./games/c8games/INVADERS");
chip8.load_rom(rom_path).expect("Unable to load ROM"); chip8.load_rom(rom_path).expect("Unable to load ROM");
let mut start = Instant::now();
let frametime = Duration::from_nanos(1e+9 as u64 / OPCODES_PER_SECOND);
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
if let Event::RedrawRequested(_) = event { if let Event::RedrawRequested(_) = event {
draw(&chip8.display.buf, pixels.get_frame()); draw(&chip8.display.buf, pixels.get_frame());
@ -44,7 +48,11 @@ fn main() {
pixels.resize(size.width, size.height); pixels.resize(size.width, size.height);
} }
chip8.execute_cycle(); if Instant::now().duration_since(start) > frametime {
chip8.execute_cycle();
start = Instant::now();
}
if chip8.request_redraw { if chip8.request_redraw {
window.request_redraw(); window.request_redraw();
} }