A GameCube/Wii emulator and debugger written in Rust.
SPEED (AND DSP CHANGES)
Layle7ea92fb244a3
diff --git a/crates/gecko/src/flipper/dsp.rs b/crates/gecko/src/flipper/dsp.rsindex 903fa90..c83ab00 100644--- a/crates/gecko/src/flipper/dsp.rs+++ b/crates/gecko/src/flipper/dsp.rs@@ -217,18 +217,15 @@ impl GameCube {let natural_nia = self.dsp.registers.cia.wrapping_add(lut::instr_size(instr) as u16);self.dsp.registers.nia = natural_nia;- // Save state before main instruction so extensions read pre-instruction values.- let pre_snap = instr.ext_opcode().map(|_| self.dsp.registers.snapshot());+ let ext_op = instr.ext_opcode();+ if ext_op.is_some() {+ self.dsp.registers.cache_ext_ac();+ }lut::dispatch(self, instr);- // Dispatch extension opcode: it reads accumulator/status from before the main instruction- if let Some(ext) = instr.ext_opcode() {- let ext = instruction::GcDspExt(ext);- let post_snap = self.dsp.registers.snapshot();- self.dsp.registers.restore(&pre_snap.unwrap());- lut::dispatch_gc_dsp_ext(self, ext);- self.dsp.registers.restore(&post_snap);+ if let Some(ext) = ext_op {+ lut::dispatch_gc_dsp_ext(self, instruction::GcDspExt(ext));}// Check if we've reached the end of a loop.@@ -258,7 +255,7 @@ impl GameCube {break;}}- refresh_interrupts(self);+ self::refresh_interrupts(self);}}diff --git a/crates/gecko/src/flipper/dsp/core.rs b/crates/gecko/src/flipper/dsp/core.rsindex d3f9e1e..459ca68 100644--- a/crates/gecko/src/flipper/dsp/core.rs+++ b/crates/gecko/src/flipper/dsp/core.rs@@ -39,17 +39,6 @@ pub mod reg {pub const AC1M: u8 = 31;}-#[derive(Clone, Copy)]-pub struct Snapshot {- pub ac0_high: u16,- pub ac0_mid: u16,- pub ac0_low: u16,- pub ac1_high: u16,- pub ac1_mid: u16,- pub ac1_low: u16,- pub status: StatusRegister,-}-#[derive(Default)]pub struct Registers {pub pc: u16,@@ -76,31 +65,32 @@ pub struct Registers {pub ac1_low: u16,pub ac0_mid: u16,pub ac1_mid: u16,++ /// Pre-instruction accumulator cache for extension opcodes.+ /// [0]=AC0L, [1]=AC1L, [2]=AC0M(sat), [3]=AC1M(sat), [4]=AC0M(raw), [5]=AC1M(raw)+ pub ext_ac_cache: [u16; 6],}impl Registers {+ /// Cache the pre-instruction accumulator values that extension opcodes may read.+ /// Must be called before the main instruction dispatch when an extension is present.#[inline(always)]- pub fn snapshot(&self) -> Snapshot {- Snapshot {- ac0_high: self.ac0_high,- ac0_mid: self.ac0_mid,- ac0_low: self.ac0_low,- ac1_high: self.ac1_high,- ac1_mid: self.ac1_mid,- ac1_low: self.ac1_low,- status: self.status,- }- }-- #[inline(always)]- pub fn restore(&mut self, snap: &Snapshot) {- self.ac0_high = snap.ac0_high;- self.ac0_mid = snap.ac0_mid;- self.ac0_low = snap.ac0_low;- self.ac1_high = snap.ac1_high;- self.ac1_mid = snap.ac1_mid;- self.ac1_low = snap.ac1_low;- self.status = snap.status;+ pub fn cache_ext_ac(&mut self) {+ self.ext_ac_cache[0] = self.ac0_low;+ self.ext_ac_cache[1] = self.ac1_low;+ let sxm = self.sign_extended();+ self.ext_ac_cache[2] = if sxm {+ self.saturate_ac_mid(self.ac0_high, self.ac0_mid)+ } else {+ self.ac0_mid+ };+ self.ext_ac_cache[3] = if sxm {+ self.saturate_ac_mid(self.ac1_high, self.ac1_mid)+ } else {+ self.ac1_mid+ };+ self.ext_ac_cache[4] = self.ac0_mid;+ self.ext_ac_cache[5] = self.ac1_mid;}/// Increment address register by 1 with WR wrapping.diff --git a/crates/gecko/src/flipper/dsp/interpreter.rs b/crates/gecko/src/flipper/dsp/interpreter.rsindex b580635..e06ab15 100644--- a/crates/gecko/src/flipper/dsp/interpreter.rs+++ b/crates/gecko/src/flipper/dsp/interpreter.rs@@ -1094,7 +1094,7 @@ pub fn ext_addr<const OP: u32>(ctx: &mut GameCube, instr: GcDspExt) {pub fn ext_mv(ctx: &mut GameCube, instr: GcDspExt) {let d = instr.d_4_5();let s = instr.s_6_7();- let value = ctx.dsp.registers.read::<true>(reg::AC0L + s);+ let value = ctx.dsp.registers.ext_ac_cache[s as usize];ctx.dsp.registers.write::<true>(reg::AX0L + d, value);}@@ -1102,7 +1102,7 @@ pub fn ext_mv(ctx: &mut GameCube, instr: GcDspExt) {pub fn ext_store<const OP: u32>(ctx: &mut GameCube, instr: GcDspExt) {let d = instr.d_6_7() as usize;let s = instr.s_3_4();- let value = ctx.dsp.registers.read::<true>(reg::AC0L + s);+ let value = ctx.dsp.registers.ext_ac_cache[s as usize];let addr = ctx.dsp.registers.ar[d];dsp::write_dmem(ctx, addr, value);match OP {@@ -1148,13 +1148,13 @@ pub fn ext_load_store<const OP: u32>(ctx: &mut GameCube, instr: GcDspExt) {let load_addr = ctx.dsp.registers.ar[0];let load_value = dsp::read_dmem(ctx, load_addr);ctx.dsp.registers.write::<true>(reg::AX0L + d, load_value);- let store_value = ctx.dsp.registers.ac_mid(s as u8);+ let store_value = ctx.dsp.registers.ext_ac_cache[4 + s];let store_addr = ctx.dsp.registers.ar[3];dsp::write_dmem(ctx, store_addr, store_value);}_ => {// SL: Store first, then Load- let store_value = ctx.dsp.registers.ac_mid(s as u8);+ let store_value = ctx.dsp.registers.ext_ac_cache[4 + s];let store_addr = ctx.dsp.registers.ar[0];dsp::write_dmem(ctx, store_addr, store_value);let load_addr = ctx.dsp.registers.ar[3];diff --git a/crates/gecko/src/gamecube.rs b/crates/gecko/src/gamecube.rsindex f89a8f1..36a921b 100644--- a/crates/gecko/src/gamecube.rs+++ b/crates/gecko/src/gamecube.rs@@ -183,7 +183,7 @@ impl GameCube {self.cpu.nia = self.cpu.cia.wrapping_add(4);let instr = Instruction(self.mmio.fetch_instruction(self.cpu.cia));cpu::lut::dispatch(self, instr);- self.scheduler.cycles += 1;+ self.scheduler.cycles += 2; // TODO: Track properly?// CPU post-hook#[cfg(feature = "hooks")]diff --git a/crates/tinybench/src/bin/branch_to_self.rs b/crates/tinybench/src/bin/branch_to_self.rsnew file mode 100644index 0000000..dbad913--- /dev/null+++ b/crates/tinybench/src/bin/branch_to_self.rs@@ -0,0 +1,12 @@+use gecko::gamecube::GameCube;++fn main() {+ let entry: u32 = 0x8000_0000;+ let mut emu = GameCube::new(entry);++ emu.mmio.virt_write_u32(entry, 0x4800_0000);++ loop {+ emu.run_until_vsync();+ }+}