From 0a6e0b6833d9d2472c4e384c125dd31bf83c6cc0 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Thu, 2 Feb 2023 16:52:33 +1100 Subject: [PATCH] almost timing --- Cargo.lock | 11 +++++++++++ Cargo.toml | 3 ++- src/processor/mod.rs | 11 +++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dfc3079..9772426 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -77,6 +77,7 @@ name = "gb-emu" version = "0.1.0" dependencies = [ "clap", + "spin_sleep", ] [[package]] @@ -196,6 +197,16 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "spin_sleep" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cafa7900db085f4354dbc7025e25d7a839a14360ea13b5fc4fd717f2d3b23134" +dependencies = [ + "once_cell", + "winapi", +] + [[package]] name = "strsim" version = "0.10.0" diff --git a/Cargo.toml b/Cargo.toml index 893b61c..bfa26a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap = {version = "4.1.1", features = ["derive"]} \ No newline at end of file +clap = {version = "4.1.1", features = ["derive"]} +spin_sleep = "1.1.1" \ No newline at end of file diff --git a/src/processor/mod.rs b/src/processor/mod.rs index c0e04ac..cf1188f 100644 --- a/src/processor/mod.rs +++ b/src/processor/mod.rs @@ -1,4 +1,4 @@ -use std::mem::transmute; +use std::{mem::transmute, time::Duration}; use crate::{verbose_println, Memory}; @@ -25,6 +25,10 @@ pub struct CPU { pub last_instruction_addr: u16, } +// MHz +const CLOCK_SPEED: f64 = 4.194304 * 1000000.; +const SPEEDUP: f64 = 1.; + impl CPU { pub fn exec_next(&mut self) { self.last_instruction_addr = self.reg.pc; @@ -43,7 +47,10 @@ impl CPU { opcode, self.last_instruction_addr ); - self.run_opcode(opcode); + let cycles = self.run_opcode(opcode); + spin_sleep::sleep(Duration::from_secs_f64( + (cycles * 4) as f64 / (CLOCK_SPEED * SPEEDUP), + )); } fn next_opcode(&mut self) -> u8 {