Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

allow switching between interpreter/JIT, fix issue #10 MTMSR terminator

Laylef98b586e9b6e
diff --git a/README.md b/README.md
index 1b18c01..f937675 100644
--- a/README.md
+++ b/README.md
@@ -40,6 +40,7 @@ Gecko is developed with homebrew development and reverse engineering in mind, bu
- Lua scripting system for runtime introspection
- A beautiful yet advanced egui-based debugging UI
- Symbol parsing from ELFs and IDA Pro databases
+- IDA Pro loaders for DOL and Apploader
- RenderDoc captures with all sorts of debug markers
- ISO and RVZ support; also supports either packed as a ZIP
- Included multitool, supports:
@@ -82,7 +83,7 @@ It scans the configured GameCube and Wii folders for `.iso`, `.rvz` and `.zip` f
### Controls
-Keybindings are currently hardcoded into `tinyapp` and `debugger`!
+Keybindings are currently hardcoded!
#### GameCube
diff --git a/crates/app/src/game.rs b/crates/app/src/game.rs
index 024977f..5c87c05 100644
--- a/crates/app/src/game.rs
+++ b/crates/app/src/game.rs
@@ -116,6 +116,15 @@ impl CpuMode {
}
}
+impl From<CpuMode> for gecko::ExecutionMode {
+ fn from(mode: CpuMode) -> Self {
+ match mode {
+ CpuMode::Jit => gecko::ExecutionMode::Jit,
+ CpuMode::Interpreter => gecko::ExecutionMode::Interpreter,
+ }
+ }
+}
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum ThemePreference {
diff --git a/crates/app/src/player/state.rs b/crates/app/src/player/state.rs
index 4aa04bf..29e2668 100644
--- a/crates/app/src/player/state.rs
+++ b/crates/app/src/player/state.rs
@@ -21,6 +21,7 @@ struct BootParams {
coef_path: Option<PathBuf>,
ipl_path: Option<PathBuf>,
skip_ipl: bool,
+ execution_mode: gecko::ExecutionMode,
}
struct Initialized {
@@ -76,6 +77,7 @@ impl PlayerState {
coef_path: coef,
ipl_path: ipl,
skip_ipl: config.skip_ipl,
+ execution_mode: config.cpu_mode.into(),
})),
initialized: OnceLock::new(),
shutdown: Arc::new(AtomicBool::new(false)),
@@ -295,6 +297,7 @@ fn configure_emu<const S: gecko::system::SystemId>(
params: &BootParams,
sink: backend_wgpu::sink::ThreadedSink,
) {
+ emu.set_execution_mode(params.execution_mode);
self::load_dsp_roms(emu, params.dsp_path.as_deref(), params.coef_path.as_deref());
emu.render_sink = Box::new(sink);
}
diff --git a/crates/debugger/src/main.rs b/crates/debugger/src/main.rs
index ad4fdf8..bc79335 100644
--- a/crates/debugger/src/main.rs
+++ b/crates/debugger/src/main.rs
@@ -71,6 +71,13 @@ impl EmulatorVariant {
}
}
+ fn set_execution_mode(&mut self, mode: gecko::ExecutionMode) {
+ match self {
+ Self::Gc(e) => e.set_execution_mode(mode),
+ Self::Wii(e) => e.set_execution_mode(mode),
+ }
+ }
+
fn install_audio_sink(&mut self, sink: Box<dyn gecko::audio::AudioSink>) {
match self {
Self::Gc(e) => e.audio_sink = sink,
@@ -380,6 +387,10 @@ struct Args {
/// Display aspect ratio: auto (16:9 Wii / 4:3 GC), 4:3, 16:9, stretch
#[arg(long, default_value = "auto")]
aspect: String,
+
+ /// Force interpreter dispatch for CPU/DSP/Vertex (default: JIT)
+ #[arg(long)]
+ interpreter: bool,
}
fn initial_window_size(target_aspect: TargetAspect) -> (u32, u32) {
@@ -462,6 +473,12 @@ fn main() {
panic!("provide one of --dol, --ipl, or --dvd");
};
+ emulator.set_execution_mode(if args.interpreter {
+ gecko::ExecutionMode::Interpreter
+ } else {
+ gecko::ExecutionMode::Jit
+ });
+
if let Some(ref dsp_path) = args.dsp {
let dsp_data = std::fs::read(dsp_path).expect("failed to read DSP IROM");
emulator.load_dsp_irom(&dsp_data);
diff --git a/crates/gecko/src/flipper/dsp.rs b/crates/gecko/src/flipper/dsp.rs
index edc9914..56e3b5c 100644
--- a/crates/gecko/src/flipper/dsp.rs
+++ b/crates/gecko/src/flipper/dsp.rs
@@ -21,7 +21,7 @@ pub mod lut_wii {
use crate::flipper::dsp::instruction::Instruction;
use crate::mmio::Mmio;
-use crate::system::{System, SystemId};
+use crate::system::{ExecutionMode, System, SystemId};
#[cfg(feature = "jit")]
pub const DSP_JIT_CHAIN_BUDGET: u32 = 16;
@@ -339,8 +339,26 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
true
}
- #[cfg(feature = "jit")]
pub fn execute_dsp_batch(&mut self) {
+ #[cfg(feature = "jit")]
+ if self.execution_mode == ExecutionMode::Jit {
+ self.execute_dsp_batch_jit();
+ return;
+ }
+ self.execute_dsp_batch_interp();
+ }
+
+ pub fn drain_dsp_synchronous(&mut self, max_steps: u32) {
+ #[cfg(feature = "jit")]
+ if self.execution_mode == ExecutionMode::Jit {
+ self.drain_dsp_synchronous_jit(max_steps);
+ return;
+ }
+ self.drain_dsp_synchronous_interp(max_steps);
+ }
+
+ #[cfg(feature = "jit")]
+ fn execute_dsp_batch_jit(&mut self) {
if self.dsp.csr.reset() || self.dsp.csr.halt() {
self::refresh_interrupts(self);
return;
@@ -398,9 +416,8 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
self::refresh_interrupts(self);
}
- #[cfg(not(feature = "jit"))]
#[inline(always)]
- pub fn execute_dsp_batch(&mut self) {
+ fn execute_dsp_batch_interp(&mut self) {
for _ in 0..crate::scheduler::DSP_BATCH_SIZE {
if !self.step_dsp_instruction() {
break;
@@ -410,7 +427,7 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
}
#[cfg(feature = "jit")]
- pub fn drain_dsp_synchronous(&mut self, max_steps: u32) {
+ fn drain_dsp_synchronous_jit(&mut self, max_steps: u32) {
let already_busy = self.dsp.mailbox_to_cpu_hi.busy();
if self.dsp.csr.reset() || self.dsp.csr.halt() {
@@ -473,8 +490,7 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
self::refresh_interrupts(self);
}
- #[cfg(not(feature = "jit"))]
- pub fn drain_dsp_synchronous(&mut self, max_steps: u32) {
+ fn drain_dsp_synchronous_interp(&mut self, max_steps: u32) {
let already_busy = self.dsp.mailbox_to_cpu_hi.busy();
for _ in 0..max_steps {
diff --git a/crates/gecko/src/flipper/gx.rs b/crates/gecko/src/flipper/gx.rs
index e0ea621..d71d6fd 100644
--- a/crates/gecko/src/flipper/gx.rs
+++ b/crates/gecko/src/flipper/gx.rs
@@ -18,7 +18,7 @@ use crate::flipper::gx::regs::{
AlphaCompare, BlendMode, ChanCtrl, TevAlphaEnv, TevColorEnv, TevRegisterH, TevRegisterL, ZMode,
};
use crate::host::{GxAction, LightData, TextureKey, XfbPart};
-use crate::system::{System, SystemId};
+use crate::system::{ExecutionMode, System, SystemId};
use rustc_hash::FxHashMap;
pub struct GraphicsProcessor {
@@ -94,6 +94,7 @@ pub struct GraphicsProcessor {
// sends. Keyed by the same `TextureKey` sent to the renderer in
// [`GxAction::LoadTexture`].
pub texture_hashes: FxHashMap<TextureKey, u64>,
+ pub execution_mode: ExecutionMode,
}
/// A single EFB-to-XFB copy, stored until `present_xfb` computes the layout.
@@ -175,6 +176,7 @@ impl GraphicsProcessor {
#[cfg(feature = "gx-stats")]
stats: GxStats::default(),
texture_hashes: FxHashMap::default(),
+ execution_mode: ExecutionMode::default(),
}
}
}
diff --git a/crates/gecko/src/flipper/gx/vertex.rs b/crates/gecko/src/flipper/gx/vertex.rs
index c95e0ae..78af7a5 100644
--- a/crates/gecko/src/flipper/gx/vertex.rs
+++ b/crates/gecko/src/flipper/gx/vertex.rs
@@ -4,7 +4,7 @@ use super::regs::{self, *};
use super::{GraphicsProcessor, draw};
use crate::host::{DrawData, DrawVertex, GxAction, RenderSink};
use crate::mmio::{Mmio, RamView};
-use crate::system::SystemId;
+use crate::system::{ExecutionMode, SystemId};
use std::io::{Cursor, Read};
#[cfg(feature = "jit")]
@@ -715,7 +715,7 @@ fn dispatch_decode<const SYSTEM: SystemId>(
verts: &mut Vec<DrawVertex>,
) {
#[cfg(feature = "jit")]
- {
+ if gp.execution_mode == ExecutionMode::Jit {
let vat_index = (cmd & 0b111) as usize;
let key = jit::VtxKey::from_cp_regs(&gp.cp_regs, vat_index);
let view = mmio.ram_view();
diff --git a/crates/gecko/src/gekko/jit/block.rs b/crates/gecko/src/gekko/jit/block.rs
index 6807ffb..f391f83 100644
--- a/crates/gecko/src/gekko/jit/block.rs
+++ b/crates/gecko/src/gekko/jit/block.rs
@@ -126,6 +126,7 @@ pub fn classify_terminator(instr: Instruction) -> Option<TermKind> {
_ => None,
},
31 => match instr.xo10() {
+ 146 => Some(TermKind::Mtmsr),
467 => {
let spr_num = instr.spr_swapped() as u16;
if mtspr_is_block_safe(spr_num) {
diff --git a/crates/gecko/src/lib.rs b/crates/gecko/src/lib.rs
index 80e80d8..246f7d1 100644
--- a/crates/gecko/src/lib.rs
+++ b/crates/gecko/src/lib.rs
@@ -21,7 +21,7 @@ pub mod wii;
pub use gamecube::GameCube;
pub use input::HostInput;
-pub use system::{GC, System, SystemId, WII};
+pub use system::{ExecutionMode, GC, System, SystemId, WII};
pub use wii::Wii;
#[cfg(feature = "hooks")]
diff --git a/crates/gecko/src/system.rs b/crates/gecko/src/system.rs
index d3d39b5..4d9069b 100644
--- a/crates/gecko/src/system.rs
+++ b/crates/gecko/src/system.rs
@@ -28,9 +28,18 @@ pub type SystemId = u8;
pub const GC: SystemId = 0;
pub const WII: SystemId = 1;
+/// This only matters if `jit` feature is enabled.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub enum ExecutionMode {
+ #[default]
+ Jit,
+ Interpreter,
+}
+
pub struct System<const SYSTEM: SystemId> {
pub vsync_pending: bool,
pub vi_present_seen_this_frame: bool,
+ pub execution_mode: ExecutionMode,
pub gekko: Gekko,
pub scheduler: Scheduler<SYSTEM>,
pub mmio: Mmio<SYSTEM>,
@@ -87,6 +96,7 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
System {
vsync_pending: false,
vi_present_seen_this_frame: false,
+ execution_mode: ExecutionMode::default(),
gekko: Gekko::new(entrypoint),
scheduler,
mmio: Mmio::new(),
@@ -188,6 +198,12 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
self.gekko.pc = self.gekko.nia;
}
+ /// To JIT or not to JIT, that is the question.
+ pub fn set_execution_mode(&mut self, mode: ExecutionMode) {
+ self.execution_mode = mode;
+ self.gx.execution_mode = mode;
+ }
+
/// Drain pending scheduler events, then execute one CPU instruction.
#[inline(always)]
pub fn step(&mut self) {
@@ -215,15 +231,13 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
while !self.vsync_pending {
self.scheduler.refresh_deadline();
#[cfg(feature = "jit")]
- {
+ if self.execution_mode == ExecutionMode::Jit {
self.run_until_deadline_jit();
+ } else {
+ self.run_until_deadline_interp();
}
#[cfg(not(feature = "jit"))]
- {
- while self.scheduler.cycles < self.scheduler.next_deadline() {
- self.step_cpu();
- }
- }
+ self.run_until_deadline_interp();
// Drain all events that are now due
self.drain_events();
}
@@ -518,6 +532,14 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
}
}
+ #[inline(always)]
+ #[cfg_attr(feature = "hotpath", hotpath::measure)]
+ fn run_until_deadline_interp(&mut self) {
+ while self.scheduler.cycles < self.scheduler.next_deadline() {
+ self.step_cpu();
+ }
+ }
+
/// JIT inner loop: runs compiled blocks back-to-back until
/// `scheduler.cycles >= next_deadline`. Interrupts are checked at block
/// boundaries.
diff --git a/crates/tinyapp/src/main.rs b/crates/tinyapp/src/main.rs
index b1465ce..2ca2f99 100644
--- a/crates/tinyapp/src/main.rs
+++ b/crates/tinyapp/src/main.rs
@@ -166,6 +166,10 @@ struct Args {
/// Show a small centered window and block until space is pressed before booting
#[arg(long)]
wait: bool,
+
+ /// Force interpreter dispatch for CPU/DSP/Vertex (default: JIT)
+ #[arg(long)]
+ interpreter: bool,
}
fn resolve_aspect(arg: &str, system: SystemId) -> TargetAspect {
@@ -268,6 +272,12 @@ fn main() {
}
fn configure<const SYSTEM: SystemId>(emulator: &mut System<SYSTEM>, args: &Args) {
+ emulator.set_execution_mode(if args.interpreter {
+ gecko::ExecutionMode::Interpreter
+ } else {
+ gecko::ExecutionMode::Jit
+ });
+
if let Some(ref dsp_path) = args.dsp {
let dsp_data = std::fs::read(dsp_path).expect("failed to read DSP IROM");
emulator.dsp.load_irom(&dsp_data);