Implement opcode-limiting (current: 500 opcodes per second)
This commit is contained in:
parent
ff1a8222a8
commit
f10ad7f8dd
|
@ -1,3 +1,4 @@
|
||||||
/target
|
/target
|
||||||
/games
|
/games
|
||||||
/.vscode
|
/.vscode
|
||||||
|
/.idea
|
12
src/main.rs
12
src/main.rs
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if Instant::now().duration_since(start) > frametime {
|
||||||
chip8.execute_cycle();
|
chip8.execute_cycle();
|
||||||
|
start = Instant::now();
|
||||||
|
}
|
||||||
|
|
||||||
if chip8.request_redraw {
|
if chip8.request_redraw {
|
||||||
window.request_redraw();
|
window.request_redraw();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue