A GameCube/Wii emulator and debugger written in Rust.
psq is hurting my head
Layle77ea66cdf864
diff --git a/crates/gekko/src/cpu.rs b/crates/gekko/src/cpu.rsindex 0d2de89..63991f3 100644--- a/crates/gekko/src/cpu.rs+++ b/crates/gekko/src/cpu.rs@@ -15,7 +15,8 @@ pub mod lut {pub struct Cpu {pub gprs: [u32; 32],- pub fprs: [f64; 32],+ 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@@ -35,6 +36,7 @@ impl Cpu {Cpu {gprs: [0; 32],fprs: [0.0; 32],+ ps1s: [1.0; 32],pc: initial_pc,cia: initial_pc,nia: initial_pc.wrapping_add(4),@@ -67,6 +69,16 @@ impl Cpu {self.fprs[index as usize] = value;}+ #[inline(always)]+ pub fn read_ps1(&self, index: u8) -> f64 {+ self.ps1s[index as usize]+ }++ #[inline(always)]+ pub fn write_ps1(&mut self, index: u8, value: f64) {+ self.ps1s[index as usize] = value;+ }+#[inline]pub fn update_cr0(&mut self, val: u32) {let so = self.spr.xer.summary_overflow(); // SO is copied from XER[SO]diff --git a/crates/gekko/src/cpu/interpreter.rs b/crates/gekko/src/cpu/interpreter.rsindex ed798a8..663beea 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 psq;mod rotate;mod store_load;mod stubs;@@ -13,7 +14,8 @@ pub use branch::branch;pub use compare::compare;pub use cr_ops::cr_ops;pub use fp_ops::fp_ops;+pub use psq::store_load_psq;pub use rotate::rotate;-pub use store_load::{lwarx, store_load, store_load_fp, store_load_psq, stwcx_dot};+pub use store_load::{lwarx, store_load, store_load_fp, stwcx_dot};pub use stubs::*;pub use system::{mftb, msr, nop, segment, spr};diff --git a/crates/gekko/src/cpu/interpreter/psq.rs b/crates/gekko/src/cpu/interpreter/psq.rsnew file mode 100644index 0000000..30400d1--- /dev/null+++ b/crates/gekko/src/cpu/interpreter/psq.rs@@ -0,0 +1,171 @@+// Dequantize lookup table (64 entries, indexed by 6-bit unsigned scale)+// Scale 0-31: 1.0 / 2^i, Scale 32-63: 2^(64-i) (signed interpretation: -32..-1)+const DEQUANT_TABLE: [f32; 64] = {+ let mut table = [0.0f32; 64];+ let mut i = 0u32;+ while i < 32 {+ table[i as usize] = 1.0 / (1u64 << i) as f32;+ i += 1;+ }+ while i < 64 {+ table[i as usize] = (1u64 << (64 - i)) as f32;+ i += 1;+ }+ table+};++// Quantize lookup table (inverse of dequantize)+// Scale 0-31: 2^i, Scale 32-63: 1.0 / 2^(64-i)+const QUANT_TABLE: [f32; 64] = {+ let mut table = [0.0f32; 64];+ let mut i = 0u32;+ while i < 32 {+ table[i as usize] = (1u64 << i) as f32;+ i += 1;+ }+ while i < 64 {+ table[i as usize] = 1.0 / (1u64 << (64 - i)) as f32;+ i += 1;+ }+ table+};++fn gqr_ld_type(gqr: u32) -> u8 {+ ((gqr >> 16) & 0x7) as u8+}+fn gqr_ld_scale(gqr: u32) -> u8 {+ ((gqr >> 24) & 0x3f) as u8+}+fn gqr_st_type(gqr: u32) -> u8 {+ (gqr & 0x7) as u8+}+fn gqr_st_scale(gqr: u32) -> u8 {+ ((gqr >> 8) & 0x3f) as u8+}++fn quant_element_size(qtype: u8) -> u32 {+ match qtype {+ 0 => 4, // f32+ 4 | 6 => 1, // u8 / s8+ 5 | 7 => 2, // u16 / s16+ _ => 4,+ }+}++fn dequantize(ctx: &mut crate::gekko::Gekko, addr: u32, ld_type: u8, ld_scale: u8) -> f64 {+ let scale = DEQUANT_TABLE[ld_scale as usize];+ match ld_type {+ 0 => ctx.read_f32(addr),+ 4 => (ctx.read_u8(addr) as f32 * scale) as f64,+ 5 => (ctx.read_u16(addr) as f32 * scale) as f64,+ 6 => (ctx.read_u8(addr) as i8 as f32 * scale) as f64,+ 7 => (ctx.read_u16(addr) as i16 as f32 * scale) as f64,+ _ => 0.0,+ }+}++fn quantize(ctx: &mut crate::gekko::Gekko, addr: u32, value: f64, st_type: u8, st_scale: u8) {+ let scale = QUANT_TABLE[st_scale as usize];+ match st_type {+ 0 => ctx.write_f32(addr, value),+ 4 => {+ let v = (value as f32 * scale).clamp(u8::MIN as f32, u8::MAX as f32) as u8;+ ctx.write_u8(addr, v);+ }+ 5 => {+ let v = (value as f32 * scale).clamp(u16::MIN as f32, u16::MAX as f32) as u16;+ ctx.write_u16(addr, v);+ }+ 6 => {+ let v = (value as f32 * scale).clamp(i8::MIN as f32, i8::MAX as f32) as i8;+ ctx.write_u8(addr, v as u8);+ }+ 7 => {+ let v = (value as f32 * scale).clamp(i16::MIN as f32, i16::MAX as f32) as i16;+ ctx.write_u16(addr, v as u16);+ }+ _ => {}+ }+}++fn psq_load(ctx: &mut crate::gekko::Gekko, fd: u8, addr: u32, w: bool, gqr: u32) {+ let ld_type = gqr_ld_type(gqr);+ let ld_scale = gqr_ld_scale(gqr);+ let ps0 = dequantize(ctx, addr, ld_type, ld_scale);+ let ps1 = if w {+ 1.0+ } else {+ let elem_size = quant_element_size(ld_type);+ dequantize(ctx, addr.wrapping_add(elem_size), ld_type, ld_scale)+ };+ ctx.cpu.write_fpr(fd, ps0);+ ctx.cpu.write_ps1(fd, ps1);+}++fn psq_store(ctx: &mut crate::gekko::Gekko, fs: u8, addr: u32, w: bool, gqr: u32) {+ let st_type = gqr_st_type(gqr);+ let st_scale = gqr_st_scale(gqr);+ let ps0 = ctx.cpu.read_fpr(fs);+ quantize(ctx, addr, ps0, st_type, st_scale);+ if !w {+ let ps1 = ctx.cpu.read_ps1(fs);+ let elem_size = quant_element_size(st_type);+ quantize(ctx, addr.wrapping_add(elem_size), ps1, st_type, st_scale);+ }+}++pub fn store_load_psq<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::semantics::Instruction) {+ use crate::cpu::lut::*;++ match OP {+ // D-form loads+ OP_PSQ_L | OP_PSQ_LU => {+ let ea = ctx+ .cpu+ .read_gpr_or_zero(instr.ra())+ .wrapping_add_signed(instr.disp_psq());+ let gqr = ctx.cpu.spr.read_gqr(instr.psq_i());+ psq_load(ctx, instr.fd(), ea, instr.psq_w(), gqr);+ if OP == OP_PSQ_LU {+ ctx.cpu.write_gpr(instr.ra(), ea);+ }+ }+ // D-form stores+ OP_PSQ_ST | OP_PSQ_STU => {+ let ea = ctx+ .cpu+ .read_gpr_or_zero(instr.ra())+ .wrapping_add_signed(instr.disp_psq());+ let gqr = ctx.cpu.spr.read_gqr(instr.psq_i());+ psq_store(ctx, instr.fs(), ea, instr.psq_w(), gqr);+ if OP == OP_PSQ_STU {+ ctx.cpu.write_gpr(instr.ra(), ea);+ }+ }+ // X-form loads+ OP_PSQ_LX | OP_PSQ_LUX => {+ let ea = ctx+ .cpu+ .read_gpr_or_zero(instr.ra())+ .wrapping_add(ctx.cpu.read_gpr(instr.rb()));+ let gqr = ctx.cpu.spr.read_gqr(instr.psq_ix());+ psq_load(ctx, instr.fd(), ea, instr.psq_wx(), gqr);+ if OP == OP_PSQ_LUX {+ ctx.cpu.write_gpr(instr.ra(), ea);+ }+ }+ // X-form stores+ OP_PSQ_STX | OP_PSQ_STUX => {+ let ea = ctx+ .cpu+ .read_gpr_or_zero(instr.ra())+ .wrapping_add(ctx.cpu.read_gpr(instr.rb()));+ let gqr = ctx.cpu.spr.read_gqr(instr.psq_ix());+ psq_store(ctx, instr.fs(), ea, instr.psq_wx(), gqr);+ if OP == OP_PSQ_STUX {+ ctx.cpu.write_gpr(instr.ra(), ea);+ }+ }+ _ => unreachable!(),+ }+}diff --git a/crates/gekko/src/cpu/interpreter/store_load.rs b/crates/gekko/src/cpu/interpreter/store_load.rsindex 5cdd0f7..b3f25e0 100644--- a/crates/gekko/src/cpu/interpreter/store_load.rs+++ b/crates/gekko/src/cpu/interpreter/store_load.rs@@ -252,16 +252,3 @@ pub fn store_load_fp<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate:_ => todo!("FP Store/Load instruction with OP = {OP:#x}"),}}--pub fn store_load_psq<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::semantics::Instruction) {- match OP {- crate::cpu::lut::OP_PSQ_L => {- let addr = ctx- .cpu- .read_gpr_or_zero(instr.ra())- .wrapping_add_signed(instr.disp_psq());-- }- _ => todo!("PSQ Store/Load instruction with OP = {OP:#x}"),- }-}diff --git a/crates/gekko/src/cpu/interpreter/stubs.rs b/crates/gekko/src/cpu/interpreter/stubs.rsindex e2983d7..0d40482 100644--- a/crates/gekko/src/cpu/interpreter/stubs.rs+++ b/crates/gekko/src/cpu/interpreter/stubs.rs@@ -17,7 +17,6 @@ stub! {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,- psq_lx, psq_stx, psq_lux, psq_stux,ps_sum0, ps_sum1, ps_muls0, ps_muls1,ps_madds0, ps_madds1,ps_div, ps_sub, ps_add, ps_sel, ps_mul,@@ -29,6 +28,5 @@ stub! {lhbrx,eciwx, ecowx,sthbrx, stfiwx,- psq_l, psq_lu, psq_st, psq_stu,fsqrtsx, fresx, fsqrtx, fselx, frsqrtex,}diff --git a/crates/gekko/src/cpu/semantics.rs b/crates/gekko/src/cpu/semantics.rsindex ecfca6e..8b9e21d 100644--- a/crates/gekko/src/cpu/semantics.rs+++ b/crates/gekko/src/cpu/semantics.rs@@ -11,8 +11,30 @@ impl Instruction {self.d_20_31()}- /// The SPR field in PowerPC instructions has a special encoding where- /// the two 5-bit halves are swapped. This method returns the decoded value+ // D-form PSQ fields (psq_l, psq_lu, psq_st, psq_stu)+ #[inline]+ pub fn psq_w(&self) -> bool {+ self.w_16_16()+ }++ #[inline]+ pub fn psq_i(&self) -> u8 {+ self.i_17_19()+ }++ // X-form PSQ fields (psq_lx, psq_lux, psq_stx, psq_stux)+ #[inline]+ pub fn psq_wx(&self) -> bool {+ self.w_21_21()+ }++ #[inline]+ pub fn psq_ix(&self) -> u8 {+ self.i_22_24()+ }++ // The SPR field in PowerPC instructions has a special encoding where+ // the two 5-bit halves are swapped. This method returns the decoded value#[inline]pub fn spr_swapped(&self) -> u32 {let raw = self.spr() as u32;diff --git a/crates/gekko/src/cpu/spr.rs b/crates/gekko/src/cpu/spr.rsindex b3496b5..701e1ed 100644--- a/crates/gekko/src/cpu/spr.rs+++ b/crates/gekko/src/cpu/spr.rs@@ -93,6 +93,20 @@ pub struct Spr {}impl Spr {+ pub fn read_gqr(&self, index: u8) -> u32 {+ match index {+ 0 => self.gqr0,+ 1 => self.gqr1,+ 2 => self.gqr2,+ 3 => self.gqr3,+ 4 => self.gqr4,+ 5 => self.gqr5,+ 6 => self.gqr6,+ 7 => self.gqr7,+ _ => unreachable!(),+ }+ }+pub fn read(&self, spr_num: u32) -> u32 {match spr_num {1 => self.xer.raw(),