From 921ecb4decf9a5e3214038c95e84fe92b011bc3b Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 24 Feb 2019 05:28:36 -0800 Subject: [PATCH] interp: Correct Vscl prefix handling. --- Core/MIPS/MIPSIntVFPU.cpp | 62 ++++++++++++++++++++----------------- Core/MIPS/MIPSVFPUUtils.cpp | 5 +++ Core/MIPS/MIPSVFPUUtils.h | 26 ++++++++++++++++ 3 files changed, 64 insertions(+), 29 deletions(-) diff --git a/Core/MIPS/MIPSIntVFPU.cpp b/Core/MIPS/MIPSIntVFPU.cpp index 0d239de236..14db6b8e31 100644 --- a/Core/MIPS/MIPSIntVFPU.cpp +++ b/Core/MIPS/MIPSIntVFPU.cpp @@ -551,13 +551,14 @@ namespace MIPSInt ReadVector(s, sz, vs); // S prefix forces the negate flags. - u32 sprefix = currentMIPS->vfpuCtrl[VFPU_CTRL_SPREFIX]; - ApplyPrefixST(s, sprefix | 0x000F0000, sz); + u32 sprefixAdd = VFPU_NEGATE(1, 1, 1, 1); + ApplyPrefixST(s, VFPURewritePrefix(VFPU_CTRL_SPREFIX, 0, sprefixAdd), sz); // T prefix forces constants on and regnum to 1. // That means negate still works, and abs activates a different constant. - u32 tprefix = currentMIPS->vfpuCtrl[VFPU_CTRL_TPREFIX]; - ApplyPrefixST(t, (tprefix & ~0x000000FF) | 0x00000055 | 0x0000F000, sz); + u32 tprefixRemove = VFPU_ANY_SWIZZLE(); + u32 tprefixAdd = VFPU_SWIZZLE(1, 1, 1, 1) | VFPU_CONST(1, 1, 1, 1); + ApplyPrefixST(t, VFPURewritePrefix(VFPU_CTRL_TPREFIX, tprefixRemove, tprefixAdd), sz); for (int i = 0; i < GetNumVectorElements(sz); i++) { // Always positive NaN. Note that s is always negated from the registers. @@ -580,13 +581,15 @@ namespace MIPSInt // S prefix forces negate in even/odd and xxyy swizzle. // abs works, and applies to final position (not source.) - u32 sprefix = currentMIPS->vfpuCtrl[VFPU_CTRL_SPREFIX]; - ApplyPrefixST(s, (sprefix & ~0x000F00FF) | 0x00000050 | 0x00050000, outSize); + u32 sprefixRemove = VFPU_ANY_SWIZZLE() | VFPU_NEGATE(1, 1, 1, 1); + u32 sprefixAdd = VFPU_SWIZZLE(0, 0, 1, 1) | VFPU_NEGATE(1, 0, 1, 0); + ApplyPrefixST(s, VFPURewritePrefix(VFPU_CTRL_SPREFIX, sprefixRemove, sprefixAdd), outSize); // T prefix forces constants on and regnum to 0, 1, 0, 1. // That means negate still works, and abs activates a different constant. - u32 tprefix = currentMIPS->vfpuCtrl[VFPU_CTRL_TPREFIX]; - ApplyPrefixST(t, (tprefix & ~0x000000FF) | 0x00000011 | 0x0000F000, outSize); + u32 tprefixRemove = VFPU_ANY_SWIZZLE(); + u32 tprefixAdd = VFPU_SWIZZLE(1, 0, 1, 0) | VFPU_CONST(1, 1, 1, 1); + ApplyPrefixST(t, VFPURewritePrefix(VFPU_CTRL_TPREFIX, tprefixRemove, tprefixAdd), outSize); int n = GetNumVectorElements(sz); // Essentially D prefix saturation is forced. @@ -1053,13 +1056,14 @@ namespace MIPSInt if (op & 0x10000) { // vbfy2 // S prefix forces the negate flags (so z and w are negative.) - u32 sprefix = currentMIPS->vfpuCtrl[VFPU_CTRL_SPREFIX]; - ApplyPrefixST(s, sprefix | 0x000C0000, sz); + u32 sprefixAdd = VFPU_NEGATE(0, 0, 1, 1); + ApplyPrefixST(s, VFPURewritePrefix(VFPU_CTRL_SPREFIX, 0, sprefixAdd), sz); // T prefix forces swizzle (zwxy.) // That means negate still works, but constants are a bit weird. - u32 tprefix = currentMIPS->vfpuCtrl[VFPU_CTRL_TPREFIX]; - ApplyPrefixST(t, (tprefix & ~0x000000FF) | 0x0000004E, sz); + u32 tprefixRemove = VFPU_ANY_SWIZZLE(); + u32 tprefixAdd = VFPU_SWIZZLE(2, 3, 0, 1); + ApplyPrefixST(t, VFPURewritePrefix(VFPU_CTRL_TPREFIX, tprefixRemove, tprefixAdd), sz); // Other sizes don't seem completely predictable. if (sz != V_Quad) { @@ -1068,13 +1072,14 @@ namespace MIPSInt } else { // vbfy1 // S prefix forces the negate flags (so y and w are negative.) - u32 sprefix = currentMIPS->vfpuCtrl[VFPU_CTRL_SPREFIX]; - ApplyPrefixST(s, sprefix | 0x000A0000, sz); + u32 sprefixAdd = VFPU_NEGATE(0, 1, 0, 1); + ApplyPrefixST(s, VFPURewritePrefix(VFPU_CTRL_SPREFIX, 0, sprefixAdd), sz); // T prefix forces swizzle (yxwz.) // That means negate still works, but constants are a bit weird. - u32 tprefix = currentMIPS->vfpuCtrl[VFPU_CTRL_TPREFIX]; - ApplyPrefixST(t, (tprefix & ~0x000000FF) | 0x000000B1, sz); + u32 tprefixRemove = VFPU_ANY_SWIZZLE(); + u32 tprefixAdd = VFPU_SWIZZLE(1, 0, 3, 2); + ApplyPrefixST(t, VFPURewritePrefix(VFPU_CTRL_TPREFIX, tprefixRemove, tprefixAdd), sz); if (sz != V_Quad && sz != V_Pair) { ERROR_LOG_REPORT_ONCE(vbfy2, CPU, "vfby1 with incorrect size"); @@ -1274,26 +1279,25 @@ namespace MIPSInt EatPrefixes(); } - void Int_VScl(MIPSOpcode op) - { - float s[4]; - float d[4]; + void Int_VScl(MIPSOpcode op) { + float s[4], t[4], d[4]; int vd = _VD; int vs = _VS; int vt = _VT; VectorSize sz = GetVecSize(op); ReadVector(s, sz, vs); ApplySwizzleS(s, sz); - float scale = V(vt); - if (currentMIPS->vfpuCtrl[VFPU_CTRL_TPREFIX] != 0xE4) - { - // WARN_LOG(CPU, "Broken T prefix used with VScl: %08x / %08x", currentMIPS->vfpuCtrl[VFPU_CTRL_TPREFIX], op); - ApplySwizzleT(&scale, V_Single); - } + + // T prefix forces swizzle (zzzz for some reason, so we force V_Quad.) + // That means negate still works, but constants are a bit weird. + t[2] = V(vt); + u32 tprefixRemove = VFPU_ANY_SWIZZLE(); + u32 tprefixAdd = VFPU_SWIZZLE(2, 2, 2, 2); + ApplyPrefixST(t, VFPURewritePrefix(VFPU_CTRL_TPREFIX, tprefixRemove, tprefixAdd), V_Quad); + int n = GetNumVectorElements(sz); - for (int i = 0; i < n; i++) - { - d[i] = s[i] * scale; + for (int i = 0; i < n; i++) { + d[i] = s[i] * t[i]; } ApplyPrefixD(d, sz); WriteVector(d, sz, vd); diff --git a/Core/MIPS/MIPSVFPUUtils.cpp b/Core/MIPS/MIPSVFPUUtils.cpp index e493074665..f2630800d8 100644 --- a/Core/MIPS/MIPSVFPUUtils.cpp +++ b/Core/MIPS/MIPSVFPUUtils.cpp @@ -214,6 +214,11 @@ void WriteVector(const float *rd, VectorSize size, int reg) { } } +u32 VFPURewritePrefix(int ctrl, u32 remove, u32 add) { + u32 prefix = currentMIPS->vfpuCtrl[ctrl]; + return (prefix & ~remove) | add; +} + void ReadMatrix(float *rd, MatrixSize size, int reg) { int mtx = (reg >> 2) & 7; int col = reg & 3; diff --git a/Core/MIPS/MIPSVFPUUtils.h b/Core/MIPS/MIPSVFPUUtils.h index 8e25821b1d..884eb128e1 100644 --- a/Core/MIPS/MIPSVFPUUtils.h +++ b/Core/MIPS/MIPSVFPUUtils.h @@ -119,6 +119,32 @@ enum MatrixSize { M_Invalid = -1 }; +static u32 VFPU_SWIZZLE(int x, int y, int z, int w) { + return (x << 0) | (y << 2) | (z << 4) | (w << 6); +} + +static u32 VFPU_MASK(int x, int y, int z, int w) { + return (x << 0) | (y << 1) | (z << 2) | (w << 3); +} + +static u32 VFPU_ANY_SWIZZLE() { + return 0x000000FF; +} + +static u32 VFPU_ABS(int x, int y, int z, int w) { + return VFPU_MASK(x, y, z, w) << 8; +} + +static u32 VFPU_CONST(int x, int y, int z, int w) { + return VFPU_MASK(x, y, z, w) << 12; +} + +static u32 VFPU_NEGATE(int x, int y, int z, int w) { + return VFPU_MASK(x, y, z, w) << 16; +} + +u32 VFPURewritePrefix(int ctrl, u32 remove, u32 add); + void ReadMatrix(float *rd, MatrixSize size, int reg); void WriteMatrix(const float *rs, MatrixSize size, int reg);