Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add approximate feature #19

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions rusty-boy-sdl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ categories = ["embedded", "gaming"]
keywords = ["embedded", "gameboy", "playdate"]
readme = "../README.md"

[features]
approximate = []
profile = ["nix"]

[dependencies]
cartridge = { path = "../cartridge" }
ppu = { path = "../ppu" }
sm83 = { path = "../sm83" }
rusty-boy = { path = "../rusty-boy" }
anyhow = "1.0"
png = "0.17"
sdl2 = "0.36"
clap = { version = "4.5.4", features = ["derive"] }
env_logger = "0.11.3"
log = "0.4"
nix = { version = "0.28", features = ["sched"], optional = true }
27 changes: 26 additions & 1 deletion rusty-boy-sdl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,19 @@ fn save_file(rom_path: &Path, data: &[u8]) -> anyhow::Result<()> {
Ok(())
}

#[cfg(feature = "profile")]
fn configure_sched_affinity() -> anyhow::Result<()> {
log::info!("Setting CPU affinity");
let mut cpuset = nix::sched::CpuSet::new();
cpuset.set(0)?; // Just one cpu
nix::sched::sched_setaffinity(nix::unistd::Pid::this(), &cpuset)?;
Ok(())
}

fn main() -> anyhow::Result<()> {
#[cfg(feature = "profile")]
configure_sched_affinity()?;

env_logger::init();

let args = Args::parse();
Expand All @@ -138,6 +150,9 @@ fn main() -> anyhow::Result<()> {
.map_err(|e| anyhow::format_err!("Invalid cartridge: {}", e))?;
let mut rusty_boy = RustyBoy::new_with_cartridge(cartridge);

#[cfg(feature = "approximate")]
rusty_boy.configure_cpu_step(sm83::core::Cycles::new(60));

if rusty_boy.supports_battery_backed_ram() {
attempt_restore_save_file(&mut rusty_boy, &args.rom_path)?;
}
Expand Down Expand Up @@ -217,6 +232,10 @@ fn main() -> anyhow::Result<()> {

let frame = {
let frame_start = Instant::now();

#[cfg(feature = "approximate")]
rusty_boy.run_until_next_frame(false);

let frame = rusty_boy.run_until_next_frame(true);
let frame_end = Instant::now();
load += frame_end - frame_start;
Expand Down Expand Up @@ -244,7 +263,13 @@ fn main() -> anyhow::Result<()> {
}
}

next_deadline += Duration::from_nanos(16_666_667); // 60 fps
#[cfg(feature = "approximate")]
const FRAME_TIME: Duration = Duration::from_nanos(33_333_333); // 30 fps
#[cfg(not(feature = "approximate"))]
const FRAME_TIME: Duration = Duration::from_nanos(16_666_667); // 60 fps

next_deadline += FRAME_TIME;

sleep_until(next_deadline);

frame_id += 1;
Expand Down
Loading