Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

better idle detection + ipl arg

Layle20b386b1c38e
diff --git a/crates/debugger/src/main.rs b/crates/debugger/src/main.rs
index e1dca9a..a59367d 100644
--- a/crates/debugger/src/main.rs
+++ b/crates/debugger/src/main.rs
@@ -64,10 +64,6 @@ impl ApplicationHandler for App {
fn main() {
let args: Vec<String> = env::args().collect();
- if args.len() < 2 {
- eprintln!("usage: {} <path/to/game.dol> [--immediate] [--idle-skip]", args[0]);
- std::process::exit(1);
- }
let present_mode = args
.iter()
@@ -76,9 +72,21 @@ fn main() {
.unwrap_or(wgpu::PresentMode::Fifo);
let idle_skip = args.iter().any(|a| a == "--idle-skip");
- let rom_data = std::fs::read(&args[1]).expect("failed to read ROM");
- let dol = Dol::parse(rom_data);
- let emulator = Gekko::with_image(&dol, idle_skip);
+ let ipl_path = args.iter().position(|a| a == "--ipl").map(|i| &args[i + 1]);
+ let rom_path = args.iter().position(|a| a == "--rom").map(|i| &args[i + 1])
+ .or_else(|| args.get(1).filter(|a| !a.starts_with("--")));
+
+ let emulator = if let Some(ipl) = ipl_path {
+ let ipl_data = std::fs::read(ipl).expect("failed to read IPL");
+ Gekko::with_ipl(&ipl_data, idle_skip)
+ } else if let Some(rom) = rom_path {
+ let rom_data = std::fs::read(rom).expect("failed to read ROM");
+ let dol = Dol::parse(rom_data);
+ Gekko::with_image(&dol, idle_skip)
+ } else {
+ eprintln!("usage: {} <path/to/game.dol> | --ipl <path> | --rom <path> [--immediate] [--idle-skip]", args[0]);
+ std::process::exit(1);
+ };
let event_loop = EventLoop::new().unwrap();
let mut app = App {
diff --git a/crates/gekko/src/gekko.rs b/crates/gekko/src/gekko.rs
index 8d939c2..491e82e 100644
--- a/crates/gekko/src/gekko.rs
+++ b/crates/gekko/src/gekko.rs
@@ -9,6 +9,7 @@ use crate::{
},
mmio::Mmio,
scheduler::{CYCLES_PER_VSYNC, EventKind, Scheduler},
+ idle::IdleDetector,
};
use image::Executable;
@@ -22,7 +23,7 @@ pub struct Gekko {
pub dsp: Dsp,
pub exi: Exi,
pub gx: Gx,
- idle_skip: bool,
+ idle: IdleDetector,
}
impl Gekko {
@@ -37,7 +38,7 @@ impl Gekko {
dsp: Dsp::new(),
exi: Exi::dummy(),
gx: Gx::new(),
- idle_skip,
+ idle: IdleDetector::new(idle_skip),
}
}
@@ -133,9 +134,7 @@ impl Gekko {
cpu::lut::dispatch(self, instr);
self.scheduler.cycles += 1;
- // Idle skip: if the instruction branches to itself, fast-forward to the next event
- // TODO: SUPER EXPERIMENTAL, trying stuff out cause of hazel and vxpm
- if self.idle_skip && self.cpu.nia == self.cpu.cia {
+ if self.idle.check(self.cpu.cia, self.cpu.nia) {
if let Some(deadline) = self.scheduler.next_event_deadline() {
self.scheduler.cycles = deadline;
}
diff --git a/crates/gekko/src/idle.rs b/crates/gekko/src/idle.rs
new file mode 100644
index 0000000..d89830c
--- /dev/null
+++ b/crates/gekko/src/idle.rs
@@ -0,0 +1,70 @@
+/// Maximum backward branch distance (in bytes) considered a potential polling loop
+const IDLE_LOOP_MAX_SIZE: u32 = 10 * 4;
+/// Number of consecutive loop iterations before we skip to the next event
+const IDLE_LOOP_THRESHOLD: u32 = 10;
+
+pub struct IdleDetector {
+ enabled: bool,
+ loop_pc: Option<u32>,
+ loop_end: u32,
+ loop_count: u32,
+}
+
+impl IdleDetector {
+ pub fn new(enabled: bool) -> Self {
+ Self {
+ enabled,
+ loop_pc: None,
+ loop_end: 0,
+ loop_count: 0,
+ }
+ }
+
+ #[inline]
+ pub fn check(&mut self, cia: u32, nia: u32) -> bool {
+ if !self.enabled {
+ return false;
+ }
+
+ if nia == cia {
+ // Skip branch to self
+ tracing::debug!("detected branch to self at {:08X}", cia);
+ return true;
+ }
+
+ if nia < cia && cia.wrapping_sub(nia) <= IDLE_LOOP_MAX_SIZE {
+ // Potential polling loop
+ return self.track_backward_branch(nia, cia);
+ }
+
+ // We moved outside of the tracked loop, reset
+ self.try_reset(cia);
+ false
+ }
+
+ fn track_backward_branch(&mut self, target: u32, branch_pc: u32) -> bool {
+ if self.loop_pc == Some(target) {
+ self.loop_count += 1;
+ if self.loop_count >= IDLE_LOOP_THRESHOLD {
+ self.loop_count = 0;
+ tracing::debug!("detected idle loop at {:08X} (branched from {:08X})", target, branch_pc);
+ return true;
+ }
+ } else {
+ self.loop_pc = Some(target);
+ self.loop_end = branch_pc;
+ self.loop_count = 1;
+ }
+
+ false
+ }
+
+ fn try_reset(&mut self, cia: u32) {
+ if let Some(start) = self.loop_pc {
+ if cia < start || cia > self.loop_end {
+ self.loop_pc = None;
+ self.loop_count = 0;
+ }
+ }
+ }
+}
diff --git a/crates/gekko/src/lib.rs b/crates/gekko/src/lib.rs
index d5d5521..367aa12 100644
--- a/crates/gekko/src/lib.rs
+++ b/crates/gekko/src/lib.rs
@@ -4,3 +4,4 @@ pub mod flipper;
pub mod gekko;
pub mod mmio;
pub mod scheduler;
+pub mod idle;
diff --git a/crates/tinyapp/src/main.rs b/crates/tinyapp/src/main.rs
index c269345..b29ef9d 100644
--- a/crates/tinyapp/src/main.rs
+++ b/crates/tinyapp/src/main.rs
@@ -468,10 +468,6 @@ impl ApplicationHandler for App {
fn main() {
let args: Vec<String> = env::args().collect();
- if args.len() < 2 {
- eprintln!("usage: {} <path/to/game.dol>", args[0]);
- std::process::exit(1);
- }
let present_mode = std::env::args()
.any(|arg| arg == "--immediate")
@@ -479,9 +475,21 @@ fn main() {
.unwrap_or(wgpu::PresentMode::Fifo);
let idle_skip = std::env::args().any(|arg| arg == "--idle-skip");
- let rom_data = std::fs::read(&args[1]).expect("failed to read ROM");
- let dol = Dol::parse(rom_data);
- let emulator = Gekko::with_image(&dol, idle_skip);
+ let ipl_path = args.iter().position(|a| a == "--ipl").map(|i| &args[i + 1]);
+ let rom_path = args.iter().position(|a| a == "--rom").map(|i| &args[i + 1])
+ .or_else(|| args.get(1).filter(|a| !a.starts_with("--")));
+
+ let emulator = if let Some(ipl) = ipl_path {
+ let ipl_data = std::fs::read(ipl).expect("failed to read IPL");
+ Gekko::with_ipl(&ipl_data, idle_skip)
+ } else if let Some(rom) = rom_path {
+ let rom_data = std::fs::read(rom).expect("failed to read ROM");
+ let dol = Dol::parse(rom_data);
+ Gekko::with_image(&dol, idle_skip)
+ } else {
+ eprintln!("usage: {} <path/to/game.dol> | --ipl <path> | --rom <path> [--immediate] [--idle-skip]", args[0]);
+ std::process::exit(1);
+ };
let event_loop = EventLoop::new().unwrap();
let mut app = App {
diff --git a/crates/tinyemu/src/main.rs b/crates/tinyemu/src/main.rs
index 5499286..35c732b 100644
--- a/crates/tinyemu/src/main.rs
+++ b/crates/tinyemu/src/main.rs
@@ -34,6 +34,10 @@ struct Args {
/// Suppress all stdout output (tracing is unaffected)
#[arg(long)]
quiet: bool,
+
+ /// Skip idle loops to speed up emulation
+ #[arg(long)]
+ idle_skip: bool,
}
fn parse_hex_addr(s: &str) -> Result<u32, String> {
@@ -54,10 +58,10 @@ fn main() {
let mut gekko = if let Some(rom_path) = &args.rom {
let rom_data = std::fs::read(rom_path).expect("failed to read ROM");
let dol = image::Dol::parse(rom_data);
- gekko::gekko::Gekko::with_image(&dol, false)
+ gekko::gekko::Gekko::with_image(&dol, args.idle_skip)
} else if let Some(ipl_path) = &args.ipl {
let ipl_data = std::fs::read(ipl_path).expect("failed to read IPL");
- gekko::gekko::Gekko::with_ipl(&ipl_data, false)
+ gekko::gekko::Gekko::with_ipl(&ipl_data, args.idle_skip)
} else {
panic!("Either --rom or --ipl must be provided");
};