A GameCube/Wii emulator and debugger written in Rust.
fp exceptions. fixes glitching
Laylea55ef710dad2
diff --git a/crates/dbglib/src/windows/cpu.rs b/crates/dbglib/src/windows/cpu.rsindex dda01ee..b9e420a 100644--- a/crates/dbglib/src/windows/cpu.rs+++ b/crates/dbglib/src/windows/cpu.rs@@ -39,7 +39,7 @@ pub fn show_cpu(ui.monospace(format!("{:#010X}", cpu.spr.xer.raw()));ui.allocate_space(egui::vec2(16.0, 0.0));ui.label("FPSCR");- ui.monospace(format!("{:#010X}", cpu.fpscr));+ ui.monospace(format!("{:#010X}", cpu.fpscr.raw()));ui.end_row();});diff --git a/crates/gecko/src/cpu.rs b/crates/gecko/src/cpu.rsindex 045a414..99fe0c6 100644--- a/crates/gecko/src/cpu.rs+++ b/crates/gecko/src/cpu.rs@@ -1,5 +1,6 @@pub mod condition;pub mod dec;+pub mod fpscr;#[allow(dead_code, unused_variables, non_upper_case_globals, clippy::all)]pub mod instruction;pub mod interpreter;@@ -23,7 +24,7 @@ pub struct Cpu {pub ps1s: [f64; 32], // PS1 (paired single slot 1)pub pc: u32,pub cr: ConditionRegister,- pub fpscr: u32, // TODO: FP Status and Control Register+ pub fpscr: fpscr::Fpscr,pub dec: dec::Decrementer,pub spr: spr::Spr,pub msr: msr::Msr,@@ -52,7 +53,7 @@ impl Cpu {dec: dec::Decrementer::default(),spr,msr: msr::Msr::default(),- fpscr: 0,+ fpscr: fpscr::Fpscr::default(),sr: [sr::Sr::default(); 16],reserve_addr: None,}@@ -109,10 +110,35 @@ impl Cpu {/// Update CR1 with FPSCR[0:3] (used by Rc=1 FP instructions)#[inline(always)]pub fn update_cr1(&mut self) {- let cr1 = condition::ConditionField::from((self.fpscr >> 28) as u8);+ let cr1 = condition::ConditionField::from((self.fpscr.raw() >> 28) as u8);self.cr.set_field(1, cr1);}+ /// Recompute FPSCR[VX] and FPSCR[FEX] from the underlying bits.+ ///+ /// VX = OR of all VXxxx bits.+ /// FEX = (VX & VE) | (OX & OE) | (UX & UE) | (ZX & ZE) | (XX & XE).+ #[inline(always)]+ pub fn recompute_fpscr_summary(&mut self) {+ let vx = self.fpscr.vxsnan()+ || self.fpscr.vxisi()+ || self.fpscr.vxidi()+ || self.fpscr.vxzdz()+ || self.fpscr.vximz()+ || self.fpscr.vxvc()+ || self.fpscr.vxsoft()+ || self.fpscr.vxsqrt()+ || self.fpscr.vxcvi();+ self.fpscr = self.fpscr.with_vx(vx);++ let fex = (self.fpscr.vx() && self.fpscr.ve())+ || (self.fpscr.ox() && self.fpscr.oe())+ || (self.fpscr.ux() && self.fpscr.ue())+ || (self.fpscr.zx() && self.fpscr.ze())+ || (self.fpscr.xx() && self.fpscr.xe());+ self.fpscr = self.fpscr.with_fex(fex);+ }+/// Read GPR with the PowerPC "rA|0" convention: returns 0 when index is 0#[inline(always)]pub fn read_gpr_or_zero(&self, index: u8) -> u32 {diff --git a/crates/gecko/src/cpu/fpscr.rs b/crates/gecko/src/cpu/fpscr.rsnew file mode 100644index 0000000..8d7ba64--- /dev/null+++ b/crates/gecko/src/cpu/fpscr.rs@@ -0,0 +1,82 @@+#[chapa::bitfield(u32, width = 32, order = msb0)]+#[derive(Clone, Copy, Default)]+pub struct Fpscr {+ #[bits(0)]+ pub fx: bool,++ #[bits(1)]+ pub fex: bool,++ #[bits(2)]+ pub vx: bool,++ #[bits(3)]+ pub ox: bool,++ #[bits(4)]+ pub ux: bool,++ #[bits(5)]+ pub zx: bool,++ #[bits(6)]+ pub xx: bool,++ #[bits(7)]+ pub vxsnan: bool,++ #[bits(8)]+ pub vxisi: bool,++ #[bits(9)]+ pub vxidi: bool,++ #[bits(10)]+ pub vxzdz: bool,++ #[bits(11)]+ pub vximz: bool,++ #[bits(12)]+ pub vxvc: bool,++ #[bits(13)]+ pub fr: bool,++ #[bits(14)]+ pub fi: bool,++ #[bits(15..=19)]+ pub fprf: u8,++ // bit 20 reserved+ #[bits(21)]+ pub vxsoft: bool,++ #[bits(22)]+ pub vxsqrt: bool,++ #[bits(23)]+ pub vxcvi: bool,++ #[bits(24)]+ pub ve: bool,++ #[bits(25)]+ pub oe: bool,++ #[bits(26)]+ pub ue: bool,++ #[bits(27)]+ pub ze: bool,++ #[bits(28)]+ pub xe: bool,++ #[bits(29)]+ pub ni: bool,++ #[bits(30..=31)]+ pub rn: u8,+}diff --git a/crates/gecko/src/cpu/interpreter/fp_ops.rs b/crates/gecko/src/cpu/interpreter/fp_ops.rsindex 621979b..f2df6f6 100644--- a/crates/gecko/src/cpu/interpreter/fp_ops.rs+++ b/crates/gecko/src/cpu/interpreter/fp_ops.rs@@ -1,7 +1,12 @@use crate::cpu::condition::ConditionField;+use crate::cpu::fpscr::Fpscr;#[inline(always)]pub fn fp_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {+ if !ctx.check_fp_available() {+ return;+ }+match OP {crate::cpu::lut::OP_MTFSFX => {let fm = instr.fm();@@ -12,36 +17,42 @@ pub fn fp_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::mask |= 0xF << ((7 - i) * 4);}}- ctx.cpu.fpscr = (ctx.cpu.fpscr & !mask) | (fb & mask);+ ctx.cpu.fpscr = Fpscr::from((ctx.cpu.fpscr.raw() & !mask) | (fb & mask));+ ctx.cpu.recompute_fpscr_summary();}crate::cpu::lut::OP_MFFSX => {- ctx.cpu.write_fpr(instr.rd(), f64::from_bits(ctx.cpu.fpscr as u64));+ ctx.cpu+ .write_fpr(instr.rd(), f64::from_bits(ctx.cpu.fpscr.raw() as u64));if instr.rc() {ctx.cpu.update_cr1();}}crate::cpu::lut::OP_MTFSB0X => {- ctx.cpu.fpscr &= !(1 << (31 - instr.crbd()));+ ctx.cpu.fpscr = Fpscr::from(ctx.cpu.fpscr.raw() & !(1 << (31 - instr.crbd())));+ ctx.cpu.recompute_fpscr_summary();}crate::cpu::lut::OP_MTFSB1X => {- ctx.cpu.fpscr |= 1 << (31 - instr.crbd());+ ctx.cpu.fpscr = Fpscr::from(ctx.cpu.fpscr.raw() | (1 << (31 - instr.crbd())));+ ctx.cpu.recompute_fpscr_summary();}crate::cpu::lut::OP_MTFSFIX => {let crfd = instr.crfd();let imm = (instr.0 >> 12) & 0xF;let shift = (7 - crfd) * 4;let mask = 0xFu32 << shift;- ctx.cpu.fpscr = (ctx.cpu.fpscr & !mask) | (imm << shift);+ ctx.cpu.fpscr = Fpscr::from((ctx.cpu.fpscr.raw() & !mask) | (imm << shift));+ ctx.cpu.recompute_fpscr_summary();}crate::cpu::lut::OP_MCRFS => {let src_field = instr.crfs();let shift = (7 - src_field) * 4;- let fpscr_nibble = (ctx.cpu.fpscr >> shift) & 0xF;+ let fpscr_nibble = (ctx.cpu.fpscr.raw() >> shift) & 0xF;ctx.cpu.cr.set_field(instr.crfd(), ConditionField::from(fpscr_nibble as u8));if src_field != 0 && src_field != 1 {- ctx.cpu.fpscr &= !(0xF << shift);+ ctx.cpu.fpscr = Fpscr::from(ctx.cpu.fpscr.raw() & !(0xF << shift));+ ctx.cpu.recompute_fpscr_summary();}}crate::cpu::lut::OP_FMRX => {@@ -183,6 +194,8 @@ pub fn fp_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::_ => todo!("FP instruction with OP = {OP:#x}"),}++ ctx.check_fp_program_exception();}/// Write FP result to fD and optionally update CR1diff --git a/crates/gecko/src/cpu/interpreter/ps_ops.rs b/crates/gecko/src/cpu/interpreter/ps_ops.rsindex bdff258..7776fa8 100644--- a/crates/gecko/src/cpu/interpreter/ps_ops.rs+++ b/crates/gecko/src/cpu/interpreter/ps_ops.rs@@ -2,6 +2,10 @@ use crate::cpu::condition::ConditionField;#[inline(always)]pub fn ps_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {+ if !ctx.check_fp_available() {+ return;+ }+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;@@ -159,6 +163,8 @@ pub fn ps_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::}_ => todo!("PS instruction with OP = {OP:#x}"),}++ ctx.check_fp_program_exception();}#[inline(always)]diff --git a/crates/gecko/src/cpu/interpreter/psq.rs b/crates/gecko/src/cpu/interpreter/psq.rsindex a005a96..e5acd2f 100644--- a/crates/gecko/src/cpu/interpreter/psq.rs+++ b/crates/gecko/src/cpu/interpreter/psq.rs@@ -118,6 +118,10 @@ fn psq_store(ctx: &mut crate::gamecube::GameCube, fs: u8, addr: u32, w: bool, gqpub fn store_load_psq<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {use crate::cpu::lut::*;+ if !ctx.check_fp_available() {+ return;+ }+match OP {// D-form loadsOP_PSQ_L | OP_PSQ_LU => {diff --git a/crates/gecko/src/cpu/interpreter/store_load.rs b/crates/gecko/src/cpu/interpreter/store_load.rsindex 4bc4a8f..e7b3e8c 100644--- a/crates/gecko/src/cpu/interpreter/store_load.rs+++ b/crates/gecko/src/cpu/interpreter/store_load.rs@@ -210,6 +210,10 @@ pub fn stwcx_dot(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruc#[inline(always)]pub fn store_load_fp<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {+ if !ctx.check_fp_available() {+ return;+ }+match OP {crate::cpu::lut::OP_LFD | crate::cpu::lut::OP_LFDU => {let addr = ctx.cpu.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());diff --git a/crates/gecko/src/cpu/irq.rs b/crates/gecko/src/cpu/irq.rsindex b9d5916..1a61558 100644--- a/crates/gecko/src/cpu/irq.rs+++ b/crates/gecko/src/cpu/irq.rs@@ -117,6 +117,108 @@ impl GameCube {tracing::debug!(addr = format!("{:08X}", self.cpu.nia), "trap exception triggered");}+ /// Raise a Floating-Point Unavailable exception (0x00800).+ ///+ /// Called when an FP/PS/quantized load-store/move/compute instruction is+ /// dispatched with MSR[FP] = 0. SRR0 holds the address of the offending+ /// instruction; SRR1 is a clean copy of MSR (no status bits set).+ #[inline(always)]+ pub fn cause_fp_unavailable(&mut self) {+ let base: u32 = if self.cpu.msr.exception_prefix() {+ 0xFFF0_0000+ } else {+ 0+ };++ self.cpu.spr.srr0 = Srr0::from(self.cpu.cia);+ self.cpu.spr.srr1 = chapa::extract_bits!(self.cpu.msr; 0, 5..=9, 16..=31).raw();++ self.cpu.msr = self+ .cpu+ .msr+ .with_pow(false)+ .with_fp(false)+ .with_be(false)+ .with_dr(false)+ .with_fe1(false)+ .with_pm(false)+ .with_ee(false)+ .with_fe0(false)+ .with_ri(false)+ .with_pr(false)+ .with_se(false)+ .with_ir(false)+ .with_le(self.cpu.msr.ile());++ self.cpu.nia = base | IRQ_FP_UNAVAILABLE;++ tracing::debug!(+ addr = format!("{:08X}", self.cpu.nia),+ "FP unavailable exception triggered"+ );+ }++ /// Raise a floating-point-enabled Program exception (0x00700, SRR1[11]=1).+ ///+ /// Triggered when MSR[FE0]|MSR[FE1] is set and FPSCR[FEX] is set after+ /// an instruction that updates FPSCR.+ #[inline(always)]+ pub fn cause_fp_program_exception(&mut self) {+ let base: u32 = if self.cpu.msr.exception_prefix() {+ 0xFFF0_0000+ } else {+ 0+ };++ self.cpu.spr.srr0 = Srr0::from(self.cpu.cia);+ // SRR1[11] = floating-point enabled exception indicator+ self.cpu.spr.srr1 = chapa::extract_bits!(self.cpu.msr; 0, 5..=9, 16..=31).raw() | (1 << (31 - 11));++ self.cpu.msr = self+ .cpu+ .msr+ .with_pow(false)+ .with_fp(false)+ .with_be(false)+ .with_dr(false)+ .with_fe1(false)+ .with_pm(false)+ .with_ee(false)+ .with_fe0(false)+ .with_ri(false)+ .with_pr(false)+ .with_se(false)+ .with_ir(false)+ .with_le(self.cpu.msr.ile());++ self.cpu.nia = base | IRQ_PROGRAM;++ tracing::debug!(addr = format!("{:08X}", self.cpu.nia), "FP program exception triggered");+ }++ /// Guard for FP instruction dispatch. Returns true if the instruction may+ /// proceed; false if MSR[FP]=0 and an FP-Unavailable exception was raised+ /// (the caller must return without executing the body or advancing CIA).+ /// Fuck you, motherfucker+ #[inline(always)]+ pub fn check_fp_available(&mut self) -> bool {+ if self.cpu.msr.floating_point_available() {+ true+ } else {+ self.cause_fp_unavailable();+ false+ }+ }++ /// Tail-check after FPSCR may have been updated. If FE0|FE1 is set and+ /// FPSCR[FEX] is set, raise a Program exception (0x00700).+ #[inline(always)]+ pub fn check_fp_program_exception(&mut self) {+ if (self.cpu.msr.fe0() || self.cpu.msr.fe1()) && self.cpu.fpscr.fex() {+ self.cause_fp_program_exception();+ }+ }+pub fn cause_syscall_interrupt(&mut self) {let base: u32 = if self.cpu.msr.exception_prefix() {0xFFF0_0000diff --git a/crates/scripting/src/lib.rs b/crates/scripting/src/lib.rsindex ab275ef..e45916f 100644--- a/crates/scripting/src/lib.rs+++ b/crates/scripting/src/lib.rs@@ -241,7 +241,7 @@ impl LuaHost {methods.add_method("cr", |_, this, ()| Ok(this.cpu.cr.raw()));methods.add_method("msr", |_, this, ()| Ok(this.cpu.msr.raw()));methods.add_method("xer", |_, this, ()| Ok(this.cpu.spr.xer.raw()));- methods.add_method("fpscr", |_, this, ()| Ok(this.cpu.fpscr));+ methods.add_method("fpscr", |_, this, ()| Ok(this.cpu.fpscr.raw()));methods.add_method("cycles", |_, this, ()| Ok(this.scheduler.cycles));methods.add_method("read_u8", |_, this, addr: u32| Ok(this.mmio.virt_read_u8(addr)));