Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

cycle accuracy

Layle822a39095595
diff --git a/crates/gecko/src/gekko.rs b/crates/gecko/src/gekko.rs
index fdf31d5..35a1e20 100644
--- a/crates/gecko/src/gekko.rs
+++ b/crates/gecko/src/gekko.rs
@@ -1,4 +1,5 @@
pub mod condition;
+pub mod cycles;
pub mod dec;
pub mod fpscr;
#[allow(dead_code, unused_variables, non_upper_case_globals, clippy::all)]
diff --git a/crates/gecko/src/gekko/cycles.rs b/crates/gecko/src/gekko/cycles.rs
new file mode 100644
index 0000000..1d474b2
--- /dev/null
+++ b/crates/gecko/src/gekko/cycles.rs
@@ -0,0 +1,27 @@
+use crate::gekko::lut::*;
+
+pub const DEFAULT_CYCLES: i64 = 1;
+
+#[inline]
+pub const fn cycles_for_op(op: u32) -> i64 {
+ match op {
+ OP_MULLI => 3,
+ OP_MULLWX | OP_MULHWX | OP_MULHWUX => 5,
+ OP_DIVWX | OP_DIVWUX => 40,
+ OP_FDIVSX => 17,
+ OP_FDIVX => 31,
+ OP_PS_DIV => 17,
+ OP_PS_RSQRTE => 2,
+ OP_LMW | OP_STMW => 11,
+ OP_ICBI => 4,
+ OP_DCBF | OP_DCBI | OP_DCBST | OP_DCBZ | OP_DCBZ_L => 5,
+ OP_DCBT | OP_DCBTST => 2,
+ OP_SYNC => 3,
+ OP_MTSPR => 2,
+ OP_MFSR | OP_MFSRIN => 3,
+ OP_MTFSB0X | OP_MTFSB1X | OP_MTFSFIX | OP_MTFSFX => 3,
+ OP_TW => 2,
+ OP_RFI | OP_SC => 2,
+ _ => DEFAULT_CYCLES,
+ }
+}
diff --git a/crates/gecko/src/gekko/interpreter/alu.rs b/crates/gecko/src/gekko/interpreter/alu.rs
index 7e69f7a..f633afd 100644
--- a/crates/gecko/src/gekko/interpreter/alu.rs
+++ b/crates/gecko/src/gekko/interpreter/alu.rs
@@ -19,6 +19,7 @@ fn add_overflow(a: u32, b: u32, result: u32) -> bool {
#[inline(always)]
pub fn alu<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
match OP {
OP_ADDX => {
let a = ctx.gekko.read_gpr(instr.ra());
@@ -280,6 +281,7 @@ pub fn alu<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, inst
#[inline(always)]
pub fn logical<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
let rs = ctx.gekko.read_gpr(instr.rs());
let rb = ctx.gekko.read_gpr(instr.rb());
diff --git a/crates/gecko/src/gekko/interpreter/branch.rs b/crates/gecko/src/gekko/interpreter/branch.rs
index 3059cdb..729fce2 100644
--- a/crates/gecko/src/gekko/interpreter/branch.rs
+++ b/crates/gecko/src/gekko/interpreter/branch.rs
@@ -5,6 +5,7 @@ use crate::system::{System, SystemId};
#[inline(always)]
pub fn branch<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
match OP {
OP_BX => {
ctx.gekko.nia = if instr.aa() {
diff --git a/crates/gecko/src/gekko/interpreter/compare.rs b/crates/gecko/src/gekko/interpreter/compare.rs
index f245dee..5db4c0e 100644
--- a/crates/gecko/src/gekko/interpreter/compare.rs
+++ b/crates/gecko/src/gekko/interpreter/compare.rs
@@ -5,6 +5,7 @@ use crate::system::{System, SystemId};
#[inline(always)]
pub fn compare<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
let mut field = match OP {
OP_CMP => ConditionRegister::field_from_ord(
(ctx.gekko.read_gpr(instr.ra()) as i32).cmp(&(ctx.gekko.read_gpr(instr.rb()) as i32)),
diff --git a/crates/gecko/src/gekko/interpreter/cr_ops.rs b/crates/gecko/src/gekko/interpreter/cr_ops.rs
index 3f93b2c..41ecf6a 100644
--- a/crates/gecko/src/gekko/interpreter/cr_ops.rs
+++ b/crates/gecko/src/gekko/interpreter/cr_ops.rs
@@ -5,6 +5,7 @@ use crate::system::{System, SystemId};
#[inline(always)]
pub fn mcrxr<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_MCRXR) as u64;
let xer = ctx.gekko.spr.xer;
let field = ConditionField::new()
.with_lt(xer.summary_overflow())
@@ -17,6 +18,7 @@ pub fn mcrxr<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instructio
#[inline(always)]
pub fn cr_ops<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
match OP {
OP_MTCRF => {
let crm = instr.crm();
diff --git a/crates/gecko/src/gekko/interpreter/fp_ops.rs b/crates/gecko/src/gekko/interpreter/fp_ops.rs
index 766f3a1..03d394e 100644
--- a/crates/gecko/src/gekko/interpreter/fp_ops.rs
+++ b/crates/gecko/src/gekko/interpreter/fp_ops.rs
@@ -34,6 +34,7 @@ const FPSCR_ANY_X: u32 = FPSCR_OX
#[inline(always)]
pub fn fp_ops<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
if !ctx.check_fp_available() {
return;
}
diff --git a/crates/gecko/src/gekko/interpreter/ps_ops.rs b/crates/gecko/src/gekko/interpreter/ps_ops.rs
index 13073e6..af8d12c 100644
--- a/crates/gecko/src/gekko/interpreter/ps_ops.rs
+++ b/crates/gecko/src/gekko/interpreter/ps_ops.rs
@@ -5,6 +5,7 @@ use crate::system::{System, SystemId};
#[inline(always)]
pub fn ps_ops<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
if !ctx.check_fp_available() {
return;
}
diff --git a/crates/gecko/src/gekko/interpreter/psq.rs b/crates/gecko/src/gekko/interpreter/psq.rs
index c75c8c9..cff172c 100644
--- a/crates/gecko/src/gekko/interpreter/psq.rs
+++ b/crates/gecko/src/gekko/interpreter/psq.rs
@@ -120,6 +120,7 @@ fn psq_store<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, fs: u8, addr: u32
#[inline(always)]
pub fn store_load_psq<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
if !ctx.check_fp_available() {
return;
}
diff --git a/crates/gecko/src/gekko/interpreter/rotate.rs b/crates/gecko/src/gekko/interpreter/rotate.rs
index aa7b0f2..807b980 100644
--- a/crates/gecko/src/gekko/interpreter/rotate.rs
+++ b/crates/gecko/src/gekko/interpreter/rotate.rs
@@ -11,6 +11,7 @@ fn mask(mb: u32, me: u32) -> u32 {
#[inline(always)]
pub fn rotate<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
let rs = ctx.gekko.read_gpr(instr.rs());
let mb = instr.mb() as u32;
let me = instr.me() as u32;
diff --git a/crates/gecko/src/gekko/interpreter/store_load.rs b/crates/gecko/src/gekko/interpreter/store_load.rs
index 92def56..f0b15ef 100644
--- a/crates/gecko/src/gekko/interpreter/store_load.rs
+++ b/crates/gecko/src/gekko/interpreter/store_load.rs
@@ -5,6 +5,7 @@ use crate::system::{System, SystemId};
#[inline(always)]
pub fn store_load<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
match OP {
OP_STW | OP_STWU => {
let addr = ctx.gekko.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
@@ -185,6 +186,7 @@ pub fn store_load<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM
#[inline(always)]
pub fn lwarx<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_LWARX) as u64;
let addr = ctx
.gekko
.read_gpr_or_zero(instr.ra())
@@ -196,6 +198,7 @@ pub fn lwarx<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instructio
#[inline(always)]
pub fn stwcx_dot<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_STWCX_DOT) as u64;
let addr = ctx
.gekko
.read_gpr_or_zero(instr.ra())
@@ -213,6 +216,7 @@ pub fn stwcx_dot<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instru
#[inline(always)]
pub fn store_load_fp<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
if !ctx.check_fp_available() {
return;
}
@@ -305,6 +309,7 @@ pub fn store_load_fp<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYS
#[inline(always)]
pub fn lswx<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_LSWX) as u64;
let ea = ctx
.gekko
.read_gpr_or_zero(instr.ra())
@@ -336,6 +341,7 @@ pub fn lswx<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction
#[inline(always)]
pub fn stswx<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_STSWX) as u64;
let ea = ctx
.gekko
.read_gpr_or_zero(instr.ra())
@@ -361,6 +367,7 @@ pub fn stswx<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instructio
#[inline(always)]
pub fn lswi<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_LSWI) as u64;
let ea = ctx.gekko.read_gpr_or_zero(instr.ra());
let nb = instr.nb();
let mut n = if nb == 0 { 32u32 } else { nb as u32 };
@@ -387,6 +394,7 @@ pub fn lswi<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction
#[inline(always)]
pub fn stswi<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_STSWI) as u64;
let ea = ctx.gekko.read_gpr_or_zero(instr.ra());
let nb = instr.nb();
let mut n = if nb == 0 { 32u32 } else { nb as u32 };
@@ -410,6 +418,7 @@ pub fn stswi<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instructio
#[inline(always)]
pub fn eciwx<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_ECIWX) as u64;
let ea = ctx
.gekko
.read_gpr_or_zero(instr.ra())
@@ -420,6 +429,7 @@ pub fn eciwx<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instructio
#[inline(always)]
pub fn ecowx<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_ECOWX) as u64;
let ea = ctx
.gekko
.read_gpr_or_zero(instr.ra())
diff --git a/crates/gecko/src/gekko/interpreter/system.rs b/crates/gecko/src/gekko/interpreter/system.rs
index ef4209c..7ce5f0a 100644
--- a/crates/gecko/src/gekko/interpreter/system.rs
+++ b/crates/gecko/src/gekko/interpreter/system.rs
@@ -5,6 +5,7 @@ use crate::system::{System, SystemId};
#[inline(always)]
pub fn msr<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
match OP {
OP_MTMSR => {
ctx.gekko.msr = crate::gekko::msr::Msr::from(ctx.gekko.read_gpr(instr.rs()));
@@ -24,6 +25,7 @@ pub fn msr<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, inst
#[inline(always)]
pub fn spr<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
match OP {
OP_MTSPR => {
let spr_num = instr.spr_swapped();
@@ -77,6 +79,7 @@ pub fn spr<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, inst
#[inline(always)]
pub fn segment<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
match OP {
OP_MTSR => {
ctx.gekko.sr[instr.sr() as usize] = Sr::from_raw(ctx.gekko.read_gpr(instr.rs()));
@@ -90,18 +93,21 @@ pub fn segment<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>,
#[inline(always)]
pub fn mtsrin<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_MTSRIN) as u64;
let sr_idx = (ctx.gekko.read_gpr(instr.rb()) >> 28) as usize;
ctx.gekko.sr[sr_idx] = Sr::from_raw(ctx.gekko.read_gpr(instr.rs()));
}
#[inline(always)]
pub fn mfsrin<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_MFSRIN) as u64;
let sr_idx = (ctx.gekko.read_gpr(instr.rb()) >> 28) as usize;
ctx.gekko.write_gpr(instr.rd(), ctx.gekko.sr[sr_idx].raw());
}
#[inline(always)]
pub fn mftb<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_MFTB) as u64;
let tbr = instr.spr_swapped();
let val = match tbr {
268 => ctx.scheduler.timebase_lower(),
@@ -113,6 +119,7 @@ pub fn mftb<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction
#[inline(always)]
pub fn twi<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_TWI) as u64;
let a = ctx.gekko.read_gpr(instr.ra()) as i32;
let simm = instr.simm();
let to = instr.to();
@@ -130,6 +137,7 @@ pub fn twi<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction)
#[inline(always)]
pub fn tw<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_TW) as u64;
let a = ctx.gekko.read_gpr(instr.ra()) as i32;
let b = ctx.gekko.read_gpr(instr.rb()) as i32;
let to = instr.to();
@@ -146,9 +154,12 @@ pub fn tw<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, instr: Instruction)
}
#[inline(always)]
-pub fn nop<const OP: u32, const SYSTEM: SystemId>(_ctx: &mut System<SYSTEM>, _instr: Instruction) {}
+pub fn nop<const OP: u32, const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, _instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP) as u64;
+}
#[inline(always)]
pub fn sc<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, _instr: Instruction) {
+ ctx.scheduler.cycles += crate::gekko::cycles::cycles_for_op(OP_SC) as u64;
ctx.cause_syscall_interrupt();
}
diff --git a/crates/gecko/src/gekko/jit/handlers.rs b/crates/gecko/src/gekko/jit/handlers.rs
index eba0950..189f5fe 100644
--- a/crates/gecko/src/gekko/jit/handlers.rs
+++ b/crates/gecko/src/gekko/jit/handlers.rs
@@ -19,6 +19,7 @@ unsafe fn parts<'a>(builder_ptr: usize, local_ptr: usize) -> (&'a mut FunctionBu
#[inline(always)]
pub fn alu<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let (builder, _) = unsafe { parts(t.builder_ptr, t.local_ptr) };
match OP {
@@ -268,6 +269,7 @@ pub fn alu<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr:
#[inline(always)]
pub fn rotate<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let (builder, _) = unsafe { parts(t.builder_ptr, t.local_ptr) };
let r = match OP {
@@ -286,6 +288,7 @@ pub fn rotate<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, inst
#[inline(always)]
pub fn logical<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let (builder, _) = unsafe { parts(t.builder_ptr, t.local_ptr) };
let r = match OP {
@@ -320,6 +323,7 @@ pub fn logical<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, ins
#[inline(always)]
pub fn compare<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let (builder, _) = unsafe { parts(t.builder_ptr, t.local_ptr) };
match OP {
@@ -335,6 +339,7 @@ pub fn compare<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, ins
#[inline(always)]
pub fn cr_ops<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let (builder, _) = unsafe { parts(t.builder_ptr, t.local_ptr) };
match OP {
@@ -378,6 +383,7 @@ pub fn cr_ops<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, inst
#[inline(always)]
pub fn segment<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let (builder, _) = unsafe { parts(t.builder_ptr, t.local_ptr) };
match OP {
@@ -399,6 +405,7 @@ pub fn segment<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, ins
#[inline(always)]
pub fn msr<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let (builder, _) = unsafe { parts(t.builder_ptr, t.local_ptr) };
match OP {
@@ -454,6 +461,7 @@ pub fn msr<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr:
#[inline(always)]
pub fn spr<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
match OP {
@@ -476,6 +484,7 @@ pub fn spr<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr:
#[inline(always)]
pub fn store_load<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
match OP {
@@ -653,9 +662,10 @@ pub fn store_load<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator,
#[inline(always)]
pub fn store_load_fp<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
- translator::emit_msr_fp_guard::<SYSTEM>(builder, t.ctx_ptr, instr, t.pc, t.exit_block, local);
+ translator::emit_msr_fp_guard::<SYSTEM>(builder, t.ctx_ptr, instr, t.pc, t.exit_block, local, t.op_cycles);
match OP {
lut::OP_LFS => translator::emit_lfs::<SYSTEM>(builder, t.ctx_ptr, instr, local.read_f32, local.read_u32),
@@ -703,9 +713,10 @@ pub fn store_load_fp<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslato
#[inline(always)]
pub fn store_load_psq<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
- translator::emit_msr_fp_guard::<SYSTEM>(builder, t.ctx_ptr, instr, t.pc, t.exit_block, local);
+ translator::emit_msr_fp_guard::<SYSTEM>(builder, t.ctx_ptr, instr, t.pc, t.exit_block, local, t.op_cycles);
match OP {
lut::OP_PSQ_L => translator::emit_psq_l::<SYSTEM>(builder, t.ctx_ptr, instr, t.pc, t.exit_block, local, false),
@@ -730,6 +741,7 @@ pub fn store_load_psq<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslat
#[inline(always)]
pub fn nop<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let _ = (instr, SYSTEM);
let (builder, _) = unsafe { parts(t.builder_ptr, t.local_ptr) };
@@ -759,7 +771,7 @@ pub fn nop<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr:
#[inline(always)]
pub fn icbi<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
- let _ = OP;
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
let ra = instr.ra();
@@ -915,9 +927,10 @@ pub fn branch<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, inst
#[inline(always)]
pub fn fp_ops<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
- translator::emit_msr_fp_guard::<SYSTEM>(builder, t.ctx_ptr, instr, t.pc, t.exit_block, local);
+ translator::emit_msr_fp_guard::<SYSTEM>(builder, t.ctx_ptr, instr, t.pc, t.exit_block, local, t.op_cycles);
if translator::emit_fp_arith::<OP, SYSTEM>(builder, t.ctx_ptr, instr) {
if instr.rc() {
@@ -929,9 +942,10 @@ pub fn fp_ops<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, inst
#[inline(always)]
pub fn ps_ops<const OP: u32, const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(OP);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
- translator::emit_msr_fp_guard::<SYSTEM>(builder, t.ctx_ptr, instr, t.pc, t.exit_block, local);
+ translator::emit_msr_fp_guard::<SYSTEM>(builder, t.ctx_ptr, instr, t.pc, t.exit_block, local, t.op_cycles);
if translator::emit_ps_arith::<OP, SYSTEM>(builder, t.ctx_ptr, instr) {
if instr.rc() {
@@ -967,6 +981,7 @@ pub fn sc<const SYSTEM: SystemId>(t: &mut JitTranslator, _instr: Instruction) {
#[inline(always)]
pub fn mcrxr<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(lut::OP_MCRXR);
let (builder, _) = unsafe { parts(t.builder_ptr, t.local_ptr) };
translator::emit_mcrxr::<SYSTEM>(builder, t.ctx_ptr, instr);
@@ -976,6 +991,7 @@ pub fn mcrxr<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction)
#[inline(always)]
pub fn lwarx<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(lut::OP_LWARX);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
translator::emit_lwarx::<SYSTEM>(builder, t.ctx_ptr, instr, local);
@@ -985,6 +1001,7 @@ pub fn lwarx<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction)
#[inline(always)]
pub fn stwcx_dot<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(lut::OP_STWCX_DOT);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
translator::emit_stwcx_dot::<SYSTEM>(builder, t.ctx_ptr, instr, local);
@@ -994,6 +1011,7 @@ pub fn stwcx_dot<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instructi
#[inline(always)]
pub fn lswi<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(lut::OP_LSWI);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
let n = if instr.nb() == 0 { 32 } else { instr.nb() as u32 };
@@ -1012,6 +1030,7 @@ pub fn lswi<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
#[inline(always)]
pub fn stswi<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(lut::OP_STSWI);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
let n = if instr.nb() == 0 { 32 } else { instr.nb() as u32 };
@@ -1030,6 +1049,7 @@ pub fn stswi<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction)
#[inline(always)]
pub fn lswx<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(lut::OP_LSWX);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
let ea = translator::emit_x_form_ea::<SYSTEM>(builder, t.ctx_ptr, instr);
@@ -1041,6 +1061,7 @@ pub fn lswx<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
#[inline(always)]
pub fn stswx<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(lut::OP_STSWX);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
let ea = translator::emit_x_form_ea::<SYSTEM>(builder, t.ctx_ptr, instr);
@@ -1052,6 +1073,7 @@ pub fn stswx<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction)
#[inline(always)]
pub fn eciwx<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(lut::OP_ECIWX);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
translator::emit_load_xform::<SYSTEM>(builder, t.ctx_ptr, instr, MemSize::U32, local.read_u32, false);
@@ -1061,6 +1083,7 @@ pub fn eciwx<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction)
#[inline(always)]
pub fn ecowx<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(lut::OP_ECOWX);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
translator::emit_store_xform::<SYSTEM>(
@@ -1078,6 +1101,7 @@ pub fn ecowx<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction)
#[inline(always)]
pub fn mfsrin<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(lut::OP_MFSRIN);
let (builder, _) = unsafe { parts(t.builder_ptr, t.local_ptr) };
let b = translator::gpr_load::<SYSTEM>(builder, t.ctx_ptr, instr.rb());
@@ -1099,6 +1123,7 @@ pub fn mfsrin<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction)
#[inline(always)]
pub fn mtsrin<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(lut::OP_MTSRIN);
let (builder, _) = unsafe { parts(t.builder_ptr, t.local_ptr) };
let b = translator::gpr_load::<SYSTEM>(builder, t.ctx_ptr, instr.rb());
@@ -1120,6 +1145,7 @@ pub fn mtsrin<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction)
#[inline(always)]
pub fn mftb<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(lut::OP_MFTB);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
let tbr = instr.spr_swapped() as u16;
@@ -1160,6 +1186,7 @@ pub fn mftb<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
#[inline(always)]
pub fn tw<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(lut::OP_TW);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
translator::emit_tw::<SYSTEM>(builder, t.ctx_ptr, instr, t.pc, t.exit_block, local);
@@ -1169,6 +1196,7 @@ pub fn tw<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
#[inline(always)]
pub fn twi<const SYSTEM: SystemId>(t: &mut JitTranslator, instr: Instruction) {
+ t.op_cycles = crate::gekko::cycles::cycles_for_op(lut::OP_TWI);
let (builder, local) = unsafe { parts(t.builder_ptr, t.local_ptr) };
translator::emit_twi::<SYSTEM>(builder, t.ctx_ptr, instr, t.pc, t.exit_block, local);
diff --git a/crates/gecko/src/gekko/jit/translator.rs b/crates/gecko/src/gekko/jit/translator.rs
index 1dc8609..fccec60 100644
--- a/crates/gecko/src/gekko/jit/translator.rs
+++ b/crates/gecko/src/gekko/jit/translator.rs
@@ -47,7 +47,7 @@ impl<'a> ChainContext<'a> {
}
}
-pub(crate) const CYCLES_PER_INSTR: i64 = 2;
+pub(crate) const DEFAULT_CYCLES: i64 = crate::gekko::cycles::DEFAULT_CYCLES;
#[allow(dead_code)]
pub(crate) struct LocalFuncs {
@@ -100,6 +100,7 @@ pub struct JitTranslator {
pub chained: bool,
pub last_terminator_nia: Option<Value>,
pub handled_natively: bool,
+ pub op_cycles: i64,
}
pub fn translate<const SYSTEM: SystemId>(
@@ -206,7 +207,8 @@ pub fn translate<const SYSTEM: SystemId>(
let cyc_val = builder.ins().load(types::I64, vmctx_flags(), ctx_ptr, cyc_off);
let ctr_64 = builder.ins().uextend(types::I64, ctr_val);
- let per_iter = builder.ins().iconst(types::I64, 3 * CYCLES_PER_INSTR);
+ let per_iter_cycles = pointer_iter_loop_cycles_per_iter(&spec.instrs);
+ let per_iter = builder.ins().iconst(types::I64, per_iter_cycles);
let cyc_delta = builder.ins().imul(ctr_64, per_iter);
let new_cyc = builder.ins().iadd(cyc_val, cyc_delta);
builder.ins().store(vmctx_flags(), new_cyc, ctx_ptr, cyc_off);
@@ -245,6 +247,7 @@ pub fn translate<const SYSTEM: SystemId>(
chained: false,
last_terminator_nia: None,
handled_natively: false,
+ op_cycles: DEFAULT_CYCLES,
};
for (i, &word) in spec.instrs.iter().enumerate() {
@@ -255,19 +258,22 @@ pub fn translate<const SYSTEM: SystemId>(
t.pc = pc;
t.is_terminator = is_terminator;
t.handled_natively = false;
+ t.op_cycles = DEFAULT_CYCLES;
let chained_before = t.chained;
if is_terminator {
- let total = pending_cycles + CYCLES_PER_INSTR;
+ let term_cycles = terminator_op_cycles(spec.terminator, instr);
+ let total = pending_cycles + term_cycles;
emit_cycles_add::<SYSTEM>(&mut builder, ctx_ptr, total);
pending_cycles = 0;
+ t.op_cycles = term_cycles;
}
super::dispatch::<SYSTEM>(&mut t, instr);
if t.handled_natively {
if !is_terminator && (!t.chained || chained_before) {
- pending_cycles += CYCLES_PER_INSTR;
+ pending_cycles += t.op_cycles;
}
if t.chained && !chained_before {
@@ -290,7 +296,7 @@ pub fn translate<const SYSTEM: SystemId>(
.call(local.cause_invalid_opcode, &[ctx_ptr, raw_const, pc_const]);
let nia = builder.inst_results(call)[0];
- emit_cycles_add::<SYSTEM>(&mut builder, ctx_ptr, CYCLES_PER_INSTR);
+ emit_cycles_add::<SYSTEM>(&mut builder, ctx_ptr, DEFAULT_CYCLES);
if is_terminator {
last_terminator_nia = Some(nia);
@@ -1681,6 +1687,49 @@ pub(crate) enum LogicalOp {
Xor,
}
+fn pointer_iter_loop_cycles_per_iter(instrs: &[u32]) -> i64 {
+ use crate::gekko::cycles::{DEFAULT_CYCLES, cycles_for_op};
+ use crate::gekko::lut::{OP_DCBA, OP_DCBF, OP_DCBI, OP_DCBST, OP_DCBT, OP_DCBTST, OP_DCBZ, OP_ICBI};
+
+ let cache = instrs.first().copied().map(Instruction).map(|i| i.xo10()).unwrap_or(0);
+ let cache_op = match cache {
+ 86 => OP_DCBF,
+ 470 => OP_DCBI,
+ 54 => OP_DCBST,
+ 278 => OP_DCBT,
+ 246 => OP_DCBTST,
+ 1014 => OP_DCBZ,
+ 982 => OP_ICBI,
+ 758 => OP_DCBA,
+ _ => return 3 * DEFAULT_CYCLES,
+ };
+
+ cycles_for_op(cache_op) + 2 * DEFAULT_CYCLES
+}
+
+fn terminator_op_cycles(terminator: TermKind, instr: Instruction) -> i64 {
+ use crate::gekko::cycles::cycles_for_op;
+ use crate::gekko::lut::{OP_BCCTRX, OP_BCLRX, OP_BCX, OP_BX, OP_ISYNC, OP_MTMSR, OP_MTSPR, OP_RFI, OP_SC};
+ let op = match terminator {
+ TermKind::Branch => OP_BX,
+ TermKind::BranchCond => OP_BCX,
+ TermKind::BranchToReg => {
+ if instr.xo10() == 16 {
+ OP_BCLRX
+ } else {
+ OP_BCCTRX
+ }
+ }
+ TermKind::SystemCall => OP_SC,
+ TermKind::Rfi => OP_RFI,
+ TermKind::Mtmsr => OP_MTMSR,
+ TermKind::Mtspr => OP_MTSPR,
+ TermKind::Isync => OP_ISYNC,
+ TermKind::LengthCap => return crate::gekko::cycles::DEFAULT_CYCLES,
+ };
+ cycles_for_op(op)
+}
+
fn terminator_fall_pc(spec: &BlockSpec) -> Option<u32> {
let last_idx = spec.instrs.len().checked_sub(1)?;
let pc = spec.pc_of(last_idx);
@@ -2649,6 +2698,7 @@ pub(crate) fn emit_msr_fp_guard<const SYSTEM: SystemId>(
pc: u32,
exit_block: Block,
local: &LocalFuncs,
+ op_cycles: i64,
) {
if local.fp_guard_emitted.get() {
return;
@@ -2675,7 +2725,7 @@ pub(crate) fn emit_msr_fp_guard<const SYSTEM: SystemId>(
let pc_plus4 = builder.ins().iconst(types::I32, pc.wrapping_add(4) as i64);
builder.ins().store(vmctx_flags(), pc_plus4, ctx_ptr, nia_off);
let nia = emit_exception_dispatch::<SYSTEM>(builder, ctx_ptr, pc_const, 0, 0x0000_0800);
- emit_cycles_add::<SYSTEM>(builder, ctx_ptr, CYCLES_PER_INSTR);
+ emit_cycles_add::<SYSTEM>(builder, ctx_ptr, op_cycles);
builder.ins().jump(exit_block, &[nia.into()]);
builder.switch_to_block(cont_block);
diff --git a/crates/gecko/src/system.rs b/crates/gecko/src/system.rs
index 4d9069b..43a3cb1 100644
--- a/crates/gecko/src/system.rs
+++ b/crates/gecko/src/system.rs
@@ -175,12 +175,10 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
}
}
- // Fetch and execute next instruction
self.gekko.cia = self.gekko.pc;
self.gekko.nia = self.gekko.cia.wrapping_add(4);
let instr = crate::gekko::instruction::Instruction(self.mmio.fetch_instruction(self.gekko.cia));
crate::gekko::dispatch(self, instr);
- self.scheduler.cycles += 2; // TODO: Track properly?
// CPU post-hook
#[cfg(feature = "hooks")]