almost timing

This commit is contained in:
Alex Janka 2023-02-02 16:52:33 +11:00
parent bb7e0fbc51
commit 0a6e0b6833
3 changed files with 22 additions and 3 deletions

11
Cargo.lock generated
View file

@ -77,6 +77,7 @@ name = "gb-emu"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"spin_sleep",
] ]
[[package]] [[package]]
@ -196,6 +197,16 @@ dependencies = [
"windows-sys", "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]] [[package]]
name = "strsim" name = "strsim"
version = "0.10.0" version = "0.10.0"

View file

@ -7,3 +7,4 @@ edition = "2021"
[dependencies] [dependencies]
clap = {version = "4.1.1", features = ["derive"]} clap = {version = "4.1.1", features = ["derive"]}
spin_sleep = "1.1.1"

View file

@ -1,4 +1,4 @@
use std::mem::transmute; use std::{mem::transmute, time::Duration};
use crate::{verbose_println, Memory}; use crate::{verbose_println, Memory};
@ -25,6 +25,10 @@ pub struct CPU {
pub last_instruction_addr: u16, pub last_instruction_addr: u16,
} }
// MHz
const CLOCK_SPEED: f64 = 4.194304 * 1000000.;
const SPEEDUP: f64 = 1.;
impl CPU { impl CPU {
pub fn exec_next(&mut self) { pub fn exec_next(&mut self) {
self.last_instruction_addr = self.reg.pc; self.last_instruction_addr = self.reg.pc;
@ -43,7 +47,10 @@ impl CPU {
opcode, opcode,
self.last_instruction_addr 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 { fn next_opcode(&mut self) -> u8 {