A GameCube/Wii emulator and debugger written in Rust.
load store fpr
Layle18dc83229f59
diff --git a/crates/gekko/build.rs b/crates/gekko/build.rsindex 1cad1fe..7972374 100644--- a/crates/gekko/build.rs+++ b/crates/gekko/build.rs@@ -30,6 +30,7 @@ fn main() {.group("spr", ["mtspr", "mfspr"]).group("segment", ["mtsr", "mfsr"]).group("store_load", ["stw", "stwu", "sth", "sthu", "lwz", "lwzu"])+ .group("store_load_fp", ["lfd", "lfdu", "stfd", "stfdu"]).group("compare", ["cmp", "cmpi"]).group("nop", ["isync", "sync"]); // instructions that we can treat as no-opsdiff --git a/crates/gekko/src/cpu.rs b/crates/gekko/src/cpu.rsindex adb2d5c..56bdf5b 100644--- a/crates/gekko/src/cpu.rs+++ b/crates/gekko/src/cpu.rs@@ -54,6 +54,16 @@ impl Cpu {self.gprs[index as usize] = value;}+ #[inline(always)]+ pub fn read_fpr(&self, index: u8) -> f64 {+ self.fprs[index as usize]+ }++ #[inline(always)]+ pub fn write_fpr(&mut self, index: u8, value: f64) {+ self.fprs[index as usize] = value;+ }+#[inline]pub fn update_cr0(&mut self, val: u32) {let so = (self.spr.xer >> 31) != 0; // SO is copied from XER[SO]diff --git a/crates/gekko/src/cpu/interpreter.rs b/crates/gekko/src/cpu/interpreter.rsindex b30d6db..b9de68d 100644--- a/crates/gekko/src/cpu/interpreter.rs+++ b/crates/gekko/src/cpu/interpreter.rs@@ -240,6 +240,41 @@ pub fn store_load<const OP: u32>(}}+pub fn store_load_fp<const OP: u32>(+ ctx: &mut crate::gekko::Gekko,+ instr: crate::cpu::semantics::Instruction,+) {+ match OP {+ crate::cpu::lut::OP_LFD | crate::cpu::lut::OP_LFDU => {+ let addr = ctx+ .cpu+ .read_gpr(instr.ra())+ .wrapping_add_signed(instr.disp());+ let value =+ (ctx.read_u32(addr) as u64 | ((ctx.read_u32(addr + 4) as u64) << 32)) as f64;+ ctx.cpu.write_fpr(instr.rd(), value);++ if OP == crate::cpu::lut::OP_LFDU {+ ctx.cpu.write_gpr(instr.ra(), addr);+ }+ }+ crate::cpu::lut::OP_STFD | crate::cpu::lut::OP_STFDU => {+ let addr = ctx+ .cpu+ .read_gpr(instr.ra())+ .wrapping_add_signed(instr.disp());+ let value = ctx.cpu.read_fpr(instr.rs()) as u64;+ ctx.write_u32(addr, value as u32);+ ctx.write_u32(addr + 4, (value >> 32) as u32);++ if OP == crate::cpu::lut::OP_STFDU {+ ctx.cpu.write_gpr(instr.ra(), addr);+ }+ }+ _ => todo!("FP Store/Load instruction with OP = {OP:#x}"),+ }+}+pub fn compare<const OP: u32>(ctx: &mut crate::gekko::Gekko,instr: crate::cpu::semantics::Instruction,diff --git a/crates/tinyemu/src/fmt.rs b/crates/tinyemu/src/fmt.rsindex 0c7fc9d..29b678e 100644--- a/crates/tinyemu/src/fmt.rs+++ b/crates/tinyemu/src/fmt.rs@@ -45,14 +45,34 @@ pub fn gpr_refs(instr: &disasm::gekko::GekkoInstruction) -> Vec<u8> {refs}-pub fn reg_comment(gprs: &[u32; 32], refs: &[u8]) -> String {- if refs.is_empty() {- return String::new();+pub fn fpr_refs(instr: &disasm::gekko::GekkoInstruction) -> Vec<u8> {+ let text = format!("{}", instr);+ let tokens = tokenizer::tokenize(&text);+ let mut seen = [false; 32];+ let mut refs = Vec::new();+ for tok in tokens {+ if let AsmToken::Fpr(n) = tok {+ let n = n as usize;+ if !seen[n] {+ seen[n] = true;+ refs.push(n as u8);+ }+ }}- let parts: Vec<String> = refs+ refs+}++pub fn reg_comment(gprs: &[u32; 32], gpr_refs: &[u8], fprs: &[f64; 32], fpr_refs: &[u8]) -> String {+ let mut parts: Vec<String> = gpr_refs.iter().map(|&n| format!("r{}={:08X}", n, gprs[n as usize])).collect();+ for &n in fpr_refs {+ parts.push(format!("f{}={:.6e}", n, fprs[n as usize]));+ }+ if parts.is_empty() {+ return String::new();+ }format!("; {}", parts.join(", ")).dimmed().to_string()}diff --git a/crates/tinyemu/src/main.rs b/crates/tinyemu/src/main.rsindex 9867513..a5bf780 100644--- a/crates/tinyemu/src/main.rs+++ b/crates/tinyemu/src/main.rs@@ -135,7 +135,8 @@ fn print_instruction(gekko: &gekko::gekko::Gekko, prev_snapshot: &CpuSnapshot, d}let refs = fmt::gpr_refs(&instr);- let comment = fmt::reg_comment(&prev_snapshot.gprs, &refs);+ let fpr_refs = fmt::fpr_refs(&instr);+ let comment = fmt::reg_comment(&prev_snapshot.gprs, &refs, &prev_snapshot.fprs, &fpr_refs);let prefix = format!("{}: {}",format!("{:08X}", gekko.cpu.pc).bold(),diff --git a/crates/tinyemu/src/snaptshot.rs b/crates/tinyemu/src/snaptshot.rsindex 465f20e..cb841ad 100644--- a/crates/tinyemu/src/snaptshot.rs+++ b/crates/tinyemu/src/snaptshot.rs@@ -3,6 +3,7 @@ use gekko::cpu::condition::ConditionRegister;#[derive(Clone, Copy)]pub struct CpuSnapshot {pub gprs: [u32; 32],+ pub fprs: [f64; 32],pub lr: u32,pub ctr: u32,pub cr: ConditionRegister,@@ -12,9 +13,10 @@ impl CpuSnapshot {pub fn from_cpu(cpu: &gekko::cpu::Cpu) -> Self {Self {gprs: cpu.gprs,+ fprs: cpu.fprs,lr: cpu.spr.lr,ctr: cpu.spr.ctr,cr: cpu.cr,}}-}\ No newline at end of file+}