Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

floating points are hurting my head

Layle6cc00126666d
diff --git a/crates/gekko/build.rs b/crates/gekko/build.rs
index b049f86..90d8f14 100644
--- a/crates/gekko/build.rs
+++ b/crates/gekko/build.rs
@@ -60,7 +60,7 @@ fn main() {
"store_load_fp",
[
"lfd", "lfdu", "stfd", "stfdu", "lfs", "lfsu", "stfs", "stfsu", "lfsx", "lfsux", "lfdx", "lfdux",
- "stfsx", "stfsux", "stfdx", "stfdux",
+ "stfsx", "stfsux", "stfdx", "stfdux", "stfiwx",
],
)
.group(
@@ -96,7 +96,41 @@ fn main() {
"mtfsfx", "mffsx", "mtfsb0x", "mtfsb1x", "mtfsfix", "mcrfs", "fmrx", "fnegx", "fabsx", "fnabsx",
"frspx", "fctiwx", "fctiwzx", "fcmpu", "fcmpo", "faddx", "fsubx", "fmulx", "fdivx", "fmaddx", "fmsubx",
"fnmaddx", "fnmsubx", "faddsx", "fsubsx", "fmulsx", "fdivsx", "fmaddsx", "fmsubsx", "fnmaddsx",
- "fnmsubsx",
+ "fnmsubsx", "fsqrtsx", "fresx", "fsqrtx", "fselx", "frsqrtex",
+ ],
+ )
+ .group(
+ "ps_ops",
+ [
+ "ps_cmpu0",
+ "ps_cmpo0",
+ "ps_cmpu1",
+ "ps_cmpo1",
+ "ps_res",
+ "ps_rsqrte",
+ "ps_neg",
+ "ps_mr",
+ "ps_nabs",
+ "ps_abs",
+ "ps_merge00",
+ "ps_merge01",
+ "ps_merge10",
+ "ps_merge11",
+ "ps_sum0",
+ "ps_sum1",
+ "ps_muls0",
+ "ps_muls1",
+ "ps_madds0",
+ "ps_madds1",
+ "ps_div",
+ "ps_sub",
+ "ps_add",
+ "ps_sel",
+ "ps_mul",
+ "ps_msub",
+ "ps_madd",
+ "ps_nmsub",
+ "ps_nmadd",
],
);
diff --git a/crates/gekko/src/cpu.rs b/crates/gekko/src/cpu.rs
index 63991f3..b999a55 100644
--- a/crates/gekko/src/cpu.rs
+++ b/crates/gekko/src/cpu.rs
@@ -15,8 +15,8 @@ pub mod lut {
pub struct Cpu {
pub gprs: [u32; 32],
- pub fprs: [f64; 32], // PS0
- pub ps1s: [f64; 32], // PS1 (paired single slot 1)
+ pub fprs: [f64; 32], // PS0
+ pub ps1s: [f64; 32], // PS1 (paired single slot 1)
pub pc: u32,
pub cr: ConditionRegister,
pub fpscr: u32, // TODO: FP Status and Control Register
diff --git a/crates/gekko/src/cpu/interpreter.rs b/crates/gekko/src/cpu/interpreter.rs
index 663beea..963df4d 100644
--- a/crates/gekko/src/cpu/interpreter.rs
+++ b/crates/gekko/src/cpu/interpreter.rs
@@ -3,6 +3,7 @@ mod branch;
mod compare;
mod cr_ops;
mod fp_ops;
+mod ps_ops;
mod psq;
mod rotate;
mod store_load;
@@ -14,6 +15,7 @@ pub use branch::branch;
pub use compare::compare;
pub use cr_ops::cr_ops;
pub use fp_ops::fp_ops;
+pub use ps_ops::ps_ops;
pub use psq::store_load_psq;
pub use rotate::rotate;
pub use store_load::{lwarx, store_load, store_load_fp, stwcx_dot};
diff --git a/crates/gekko/src/cpu/interpreter/fp_ops.rs b/crates/gekko/src/cpu/interpreter/fp_ops.rs
index 96eb2dc..24613d0 100644
--- a/crates/gekko/src/cpu/interpreter/fp_ops.rs
+++ b/crates/gekko/src/cpu/interpreter/fp_ops.rs
@@ -168,6 +168,16 @@ pub fn fp_ops<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::s
(-(ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) - ctx.cpu.read_fpr(instr.rb()))) as f32
as f64,
),
+ crate::cpu::lut::OP_FSQRTX => fp_write(ctx, &instr, ctx.cpu.read_fpr(instr.rb()).sqrt()),
+ crate::cpu::lut::OP_FSQRTSX => fp_write(ctx, &instr, (ctx.cpu.read_fpr(instr.rb()).sqrt()) as f32 as f64),
+ crate::cpu::lut::OP_FRESX => fp_write(ctx, &instr, (1.0f32 / ctx.cpu.read_fpr(instr.rb()) as f32) as f64),
+ crate::cpu::lut::OP_FRSQRTEX => fp_write(ctx, &instr, 1.0 / ctx.cpu.read_fpr(instr.rb()).sqrt()),
+ crate::cpu::lut::OP_FSELX => {
+ let fa = ctx.cpu.read_fpr(instr.ra());
+ let fb = ctx.cpu.read_fpr(instr.rb());
+ let fc = ctx.cpu.read_fpr(instr.fc());
+ fp_write(ctx, &instr, if fa >= 0.0 { fc } else { fb });
+ }
_ => todo!("FP instruction with OP = {OP:#x}"),
}
diff --git a/crates/gekko/src/cpu/interpreter/ps_ops.rs b/crates/gekko/src/cpu/interpreter/ps_ops.rs
new file mode 100644
index 0000000..e26d04e
--- /dev/null
+++ b/crates/gekko/src/cpu/interpreter/ps_ops.rs
@@ -0,0 +1,183 @@
+use crate::cpu::condition::ConditionField;
+
+pub fn ps_ops<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::semantics::Instruction) {
+ match OP {
+ crate::cpu::lut::OP_PS_ADD => {
+ let ps0 = (ctx.cpu.read_fpr(instr.ra()) + ctx.cpu.read_fpr(instr.rb())) as f32 as f64;
+ let ps1 = (ctx.cpu.read_ps1(instr.ra()) + ctx.cpu.read_ps1(instr.rb())) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_SUB => {
+ let ps0 = (ctx.cpu.read_fpr(instr.ra()) - ctx.cpu.read_fpr(instr.rb())) as f32 as f64;
+ let ps1 = (ctx.cpu.read_ps1(instr.ra()) - ctx.cpu.read_ps1(instr.rb())) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_MUL => {
+ let ps0 = (ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc())) as f32 as f64;
+ let ps1 = (ctx.cpu.read_ps1(instr.ra()) * ctx.cpu.read_ps1(instr.fc())) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_DIV => {
+ let ps0 = (ctx.cpu.read_fpr(instr.ra()) / ctx.cpu.read_fpr(instr.rb())) as f32 as f64;
+ let ps1 = (ctx.cpu.read_ps1(instr.ra()) / ctx.cpu.read_ps1(instr.rb())) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_MADD => {
+ let ps0 = (ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) + ctx.cpu.read_fpr(instr.rb()))
+ as f32 as f64;
+ let ps1 = (ctx.cpu.read_ps1(instr.ra()) * ctx.cpu.read_ps1(instr.fc()) + ctx.cpu.read_ps1(instr.rb()))
+ as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_MSUB => {
+ let ps0 = (ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) - ctx.cpu.read_fpr(instr.rb()))
+ as f32 as f64;
+ let ps1 = (ctx.cpu.read_ps1(instr.ra()) * ctx.cpu.read_ps1(instr.fc()) - ctx.cpu.read_ps1(instr.rb()))
+ as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_NMADD => {
+ let ps0 = -(ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) + ctx.cpu.read_fpr(instr.rb()))
+ as f32 as f64;
+ let ps1 = -(ctx.cpu.read_ps1(instr.ra()) * ctx.cpu.read_ps1(instr.fc()) + ctx.cpu.read_ps1(instr.rb()))
+ as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_NMSUB => {
+ let ps0 = -(ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) - ctx.cpu.read_fpr(instr.rb()))
+ as f32 as f64;
+ let ps1 = -(ctx.cpu.read_ps1(instr.ra()) * ctx.cpu.read_ps1(instr.fc()) - ctx.cpu.read_ps1(instr.rb()))
+ as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_MULS0 => {
+ let c0 = ctx.cpu.read_fpr(instr.fc());
+ let ps0 = (ctx.cpu.read_fpr(instr.ra()) * c0) as f32 as f64;
+ let ps1 = (ctx.cpu.read_ps1(instr.ra()) * c0) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_MULS1 => {
+ let c1 = ctx.cpu.read_ps1(instr.fc());
+ let ps0 = (ctx.cpu.read_fpr(instr.ra()) * c1) as f32 as f64;
+ let ps1 = (ctx.cpu.read_ps1(instr.ra()) * c1) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_MADDS0 => {
+ let c0 = ctx.cpu.read_fpr(instr.fc());
+ let ps0 = (ctx.cpu.read_fpr(instr.ra()) * c0 + ctx.cpu.read_fpr(instr.rb())) as f32 as f64;
+ let ps1 = (ctx.cpu.read_ps1(instr.ra()) * c0 + ctx.cpu.read_ps1(instr.rb())) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_MADDS1 => {
+ let c1 = ctx.cpu.read_ps1(instr.fc());
+ let ps0 = (ctx.cpu.read_fpr(instr.ra()) * c1 + ctx.cpu.read_fpr(instr.rb())) as f32 as f64;
+ let ps1 = (ctx.cpu.read_ps1(instr.ra()) * c1 + ctx.cpu.read_ps1(instr.rb())) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_SUM0 => {
+ let ps0 = (ctx.cpu.read_fpr(instr.ra()) + ctx.cpu.read_ps1(instr.rb())) as f32 as f64;
+ let ps1 = ctx.cpu.read_ps1(instr.fc());
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_SUM1 => {
+ let ps0 = ctx.cpu.read_fpr(instr.fc());
+ let ps1 = (ctx.cpu.read_fpr(instr.ra()) + ctx.cpu.read_ps1(instr.rb())) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_MR => {
+ ps_write(ctx, &instr, ctx.cpu.read_fpr(instr.rb()), ctx.cpu.read_ps1(instr.rb()));
+ }
+ crate::cpu::lut::OP_PS_NEG => {
+ ps_write(
+ ctx,
+ &instr,
+ -ctx.cpu.read_fpr(instr.rb()),
+ -ctx.cpu.read_ps1(instr.rb()),
+ );
+ }
+ crate::cpu::lut::OP_PS_ABS => {
+ ps_write(
+ ctx,
+ &instr,
+ ctx.cpu.read_fpr(instr.rb()).abs(),
+ ctx.cpu.read_ps1(instr.rb()).abs(),
+ );
+ }
+ crate::cpu::lut::OP_PS_NABS => {
+ ps_write(
+ ctx,
+ &instr,
+ -ctx.cpu.read_fpr(instr.rb()).abs(),
+ -ctx.cpu.read_ps1(instr.rb()).abs(),
+ );
+ }
+ crate::cpu::lut::OP_PS_MERGE00 => {
+ ps_write(ctx, &instr, ctx.cpu.read_fpr(instr.ra()), ctx.cpu.read_fpr(instr.rb()));
+ }
+ crate::cpu::lut::OP_PS_MERGE01 => {
+ ps_write(ctx, &instr, ctx.cpu.read_fpr(instr.ra()), ctx.cpu.read_ps1(instr.rb()));
+ }
+ crate::cpu::lut::OP_PS_MERGE10 => {
+ ps_write(ctx, &instr, ctx.cpu.read_ps1(instr.ra()), ctx.cpu.read_fpr(instr.rb()));
+ }
+ crate::cpu::lut::OP_PS_MERGE11 => {
+ ps_write(ctx, &instr, ctx.cpu.read_ps1(instr.ra()), ctx.cpu.read_ps1(instr.rb()));
+ }
+ crate::cpu::lut::OP_PS_SEL => {
+ let ps0 = if ctx.cpu.read_fpr(instr.ra()) >= 0.0 {
+ ctx.cpu.read_fpr(instr.fc())
+ } else {
+ ctx.cpu.read_fpr(instr.rb())
+ };
+ let ps1 = if ctx.cpu.read_ps1(instr.ra()) >= 0.0 {
+ ctx.cpu.read_ps1(instr.fc())
+ } else {
+ ctx.cpu.read_ps1(instr.rb())
+ };
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_CMPU0 | crate::cpu::lut::OP_PS_CMPO0 => {
+ let fa = ctx.cpu.read_fpr(instr.ra());
+ let fb = ctx.cpu.read_fpr(instr.rb());
+ ctx.cpu.cr.set_field(instr.crfd(), fp_compare(fa, fb));
+ }
+ crate::cpu::lut::OP_PS_CMPU1 | crate::cpu::lut::OP_PS_CMPO1 => {
+ let fa = ctx.cpu.read_ps1(instr.ra());
+ let fb = ctx.cpu.read_ps1(instr.rb());
+ ctx.cpu.cr.set_field(instr.crfd(), fp_compare(fa, fb));
+ }
+ crate::cpu::lut::OP_PS_RES => {
+ let ps0 = (1.0f32 / ctx.cpu.read_fpr(instr.rb()) as f32) as f64;
+ let ps1 = (1.0f32 / ctx.cpu.read_ps1(instr.rb()) as f32) as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::cpu::lut::OP_PS_RSQRTE => {
+ let ps0 = (1.0f32 / (ctx.cpu.read_fpr(instr.rb()) as f32).sqrt()) as f64;
+ let ps1 = (1.0f32 / (ctx.cpu.read_ps1(instr.rb()) as f32).sqrt()) as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ _ => todo!("PS instruction with OP = {OP:#x}"),
+ }
+}
+
+#[inline(always)]
+fn ps_write(ctx: &mut crate::gekko::Gekko, instr: &crate::cpu::semantics::Instruction, ps0: f64, ps1: f64) {
+ ctx.cpu.write_fpr(instr.rd(), ps0);
+ ctx.cpu.write_ps1(instr.rd(), ps1);
+ if instr.rc() {
+ ctx.cpu.update_cr1();
+ }
+}
+
+#[inline(always)]
+fn fp_compare(a: f64, b: f64) -> ConditionField {
+ if a.is_nan() || b.is_nan() {
+ ConditionField::new().with_so(true)
+ } else if a < b {
+ ConditionField::new().with_lt(true)
+ } else if a > b {
+ ConditionField::new().with_gt(true)
+ } else {
+ ConditionField::new().with_eq(true)
+ }
+}
diff --git a/crates/gekko/src/cpu/interpreter/store_load.rs b/crates/gekko/src/cpu/interpreter/store_load.rs
index b3f25e0..90d9c16 100644
--- a/crates/gekko/src/cpu/interpreter/store_load.rs
+++ b/crates/gekko/src/cpu/interpreter/store_load.rs
@@ -249,6 +249,13 @@ pub fn store_load_fp<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate:
ctx.cpu.write_gpr(instr.ra(), addr);
}
}
+ crate::cpu::lut::OP_STFIWX => {
+ let addr = ctx
+ .cpu
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
+ ctx.write_u32(addr, ctx.cpu.read_fpr(instr.rs()).to_bits() as u32);
+ }
_ => todo!("FP Store/Load instruction with OP = {OP:#x}"),
}
}
diff --git a/crates/gekko/src/cpu/interpreter/stubs.rs b/crates/gekko/src/cpu/interpreter/stubs.rs
index 0d40482..3d3e7a1 100644
--- a/crates/gekko/src/cpu/interpreter/stubs.rs
+++ b/crates/gekko/src/cpu/interpreter/stubs.rs
@@ -14,19 +14,11 @@ macro_rules! stub {
stub! {
twi,
- ps_cmpu0, ps_cmpo0, ps_cmpu1, ps_cmpo1,
- ps_res, ps_rsqrte, ps_neg, ps_mr, ps_nabs, ps_abs,
- ps_merge00, ps_merge01, ps_merge10, ps_merge11,
- ps_sum0, ps_sum1, ps_muls0, ps_muls1,
- ps_madds0, ps_madds1,
- ps_div, ps_sub, ps_add, ps_sel, ps_mul,
- ps_msub, ps_madd, ps_nmsub, ps_nmadd,
sc, tw,
mtsrin, mcrxr,
lwbrx, lswx, lswi, mfsrin,
stswx, stwbrx, stswi,
lhbrx,
eciwx, ecowx,
- sthbrx, stfiwx,
- fsqrtsx, fresx, fsqrtx, fselx, frsqrtex,
+ sthbrx,
}