gte: backport fix for RTPS/RTPT
[pcsx_rearmed.git] / libpcsxcore / gte.c
index 26d3d34..8ef601a 100644 (file)
@@ -243,12 +243,23 @@ static inline u32 limE_(psxCP2Regs *regs, u32 result) {
 #define limG2(a) LIM((a), 0x3ff, -0x400, (1 << 31) | (1 << 13))
 #define limH(a) LIM((a), 0x1000, 0x0000, (1 << 12))
 
+#ifndef __arm__
+#define A1U A1
+#define A2U A2
+#define A3U A3
+#else
+/* these are unlikely to be hit and usually waste cycles, don't want them on ARM */
+#define A1U(x) (x)
+#define A2U(x) (x)
+#define A3U(x) (x)
+#endif
+
 #include "gte_divider.h"
 
 #ifndef FLAGLESS
 
 static inline u32 MFC2(int reg) {
-       psxCP2Regs *regs = (psxCP2Regs *)&psxRegs.CP2D;
+       psxCP2Regs *regs = &psxRegs.CP2;
        switch (reg) {
                case 1:
                case 3:
@@ -283,7 +294,7 @@ static inline u32 MFC2(int reg) {
 }
 
 static inline void MTC2(u32 value, int reg) {
-       psxCP2Regs *regs = (psxCP2Regs *)&psxRegs.CP2D;
+       psxCP2Regs *regs = &psxRegs.CP2;
        switch (reg) {
                case 15:
                        gteSXY0 = gteSXY1;
@@ -393,6 +404,7 @@ static u32 DIVIDE_(s16 n, u16 d) {
 
 void gteRTPS(psxCP2Regs *regs) {
        int quotient;
+       s64 tmp;
 
 #ifdef GTE_LOG
        GTE_LOG("GTE RTPS\n");
@@ -415,14 +427,16 @@ void gteRTPS(psxCP2Regs *regs) {
        gteSX2 = limG1(F((s64)gteOFX + ((s64)gteIR1 * quotient)) >> 16);
        gteSY2 = limG2(F((s64)gteOFY + ((s64)gteIR2 * quotient)) >> 16);
 
-       gteMAC0 = F((s64)gteDQB + ((s64)gteDQA * quotient));
-       gteIR0 = limH(gteMAC0 >> 12);
+       tmp = (s64)gteDQB + ((s64)gteDQA * quotient);
+       gteMAC0 = F(tmp);
+       gteIR0 = limH(tmp >> 12);
 }
 
 void gteRTPT(psxCP2Regs *regs) {
        int quotient;
        int v;
        s32 vx, vy, vz;
+       s64 tmp;
 
 #ifdef GTE_LOG
        GTE_LOG("GTE RTPT\n");
@@ -445,8 +459,10 @@ void gteRTPT(psxCP2Regs *regs) {
                fSX(v) = limG1(F((s64)gteOFX + ((s64)gteIR1 * quotient)) >> 16);
                fSY(v) = limG2(F((s64)gteOFY + ((s64)gteIR2 * quotient)) >> 16);
        }
-       gteMAC0 = F((s64)gteDQB + ((s64)gteDQA * quotient));
-       gteIR0 = limH(gteMAC0 >> 12);
+
+       tmp = (s64)gteDQB + ((s64)gteDQA * quotient);
+       gteMAC0 = F(tmp);
+       gteIR0 = limH(tmp >> 12);
 }
 
 void gteMVMVA(psxCP2Regs *regs) {
@@ -613,9 +629,9 @@ void gteNCDS(psxCP2Regs *regs) {
        gteIR1 = limB1(gteMAC1, 1);
        gteIR2 = limB2(gteMAC2, 1);
        gteIR3 = limB3(gteMAC3, 1);
-       gteMAC1 = (((gteR << 4) * gteIR1) + (gteIR0 * limB1(gteRFC - ((gteR * gteIR1) >> 8), 0))) >> 12;
-       gteMAC2 = (((gteG << 4) * gteIR2) + (gteIR0 * limB2(gteGFC - ((gteG * gteIR2) >> 8), 0))) >> 12;
-       gteMAC3 = (((gteB << 4) * gteIR3) + (gteIR0 * limB3(gteBFC - ((gteB * gteIR3) >> 8), 0))) >> 12;
+       gteMAC1 = (((gteR << 4) * gteIR1) + (gteIR0 * limB1(A1U((s64)gteRFC - ((gteR * gteIR1) >> 8)), 0))) >> 12;
+       gteMAC2 = (((gteG << 4) * gteIR2) + (gteIR0 * limB2(A2U((s64)gteGFC - ((gteG * gteIR2) >> 8)), 0))) >> 12;
+       gteMAC3 = (((gteB << 4) * gteIR3) + (gteIR0 * limB3(A3U((s64)gteBFC - ((gteB * gteIR3) >> 8)), 0))) >> 12;
        gteIR1 = limB1(gteMAC1, 1);
        gteIR2 = limB2(gteMAC2, 1);
        gteIR3 = limB3(gteMAC3, 1);
@@ -653,9 +669,9 @@ void gteNCDT(psxCP2Regs *regs) {
                gteIR1 = limB1(gteMAC1, 1);
                gteIR2 = limB2(gteMAC2, 1);
                gteIR3 = limB3(gteMAC3, 1);
-               gteMAC1 = (((gteR << 4) * gteIR1) + (gteIR0 * limB1(gteRFC - ((gteR * gteIR1) >> 8), 0))) >> 12;
-               gteMAC2 = (((gteG << 4) * gteIR2) + (gteIR0 * limB2(gteGFC - ((gteG * gteIR2) >> 8), 0))) >> 12;
-               gteMAC3 = (((gteB << 4) * gteIR3) + (gteIR0 * limB3(gteBFC - ((gteB * gteIR3) >> 8), 0))) >> 12;
+               gteMAC1 = (((gteR << 4) * gteIR1) + (gteIR0 * limB1(A1U((s64)gteRFC - ((gteR * gteIR1) >> 8)), 0))) >> 12;
+               gteMAC2 = (((gteG << 4) * gteIR2) + (gteIR0 * limB2(A2U((s64)gteGFC - ((gteG * gteIR2) >> 8)), 0))) >> 12;
+               gteMAC3 = (((gteB << 4) * gteIR3) + (gteIR0 * limB3(A3U((s64)gteBFC - ((gteB * gteIR3) >> 8)), 0))) >> 12;
 
                gteRGB0 = gteRGB1;
                gteRGB1 = gteRGB2;
@@ -698,9 +714,9 @@ void gteDCPL(psxCP2Regs *regs) {
 #endif
        gteFLAG = 0;
 
-       gteMAC1 = RIR1 + ((gteIR0 * limB1(gteRFC - RIR1, 0)) >> 12);
-       gteMAC2 = GIR2 + ((gteIR0 * limB1(gteGFC - GIR2, 0)) >> 12);
-       gteMAC3 = BIR3 + ((gteIR0 * limB1(gteBFC - BIR3, 0)) >> 12);
+       gteMAC1 = RIR1 + ((gteIR0 * limB1(A1U((s64)gteRFC - RIR1), 0)) >> 12);
+       gteMAC2 = GIR2 + ((gteIR0 * limB1(A2U((s64)gteGFC - GIR2), 0)) >> 12);
+       gteMAC3 = BIR3 + ((gteIR0 * limB1(A3U((s64)gteBFC - BIR3), 0)) >> 12);
 
        gteIR1 = limB1(gteMAC1, lm);
        gteIR2 = limB2(gteMAC2, lm);
@@ -768,9 +784,9 @@ void gteDPCS(psxCP2Regs *regs) {
 #endif
        gteFLAG = 0;
 
-       gteMAC1 = ((gteR << 16) + (gteIR0 * limB1(A1((s64)gteRFC - (gteR << 4)) << (12 - shift), 0))) >> 12;
-       gteMAC2 = ((gteG << 16) + (gteIR0 * limB2(A2((s64)gteGFC - (gteG << 4)) << (12 - shift), 0))) >> 12;
-       gteMAC3 = ((gteB << 16) + (gteIR0 * limB3(A3((s64)gteBFC - (gteB << 4)) << (12 - shift), 0))) >> 12;
+       gteMAC1 = ((gteR << 16) + (gteIR0 * limB1(A1U(((s64)gteRFC - (gteR << 4)) << (12 - shift)), 0))) >> 12;
+       gteMAC2 = ((gteG << 16) + (gteIR0 * limB2(A2U(((s64)gteGFC - (gteG << 4)) << (12 - shift)), 0))) >> 12;
+       gteMAC3 = ((gteB << 16) + (gteIR0 * limB3(A3U(((s64)gteBFC - (gteB << 4)) << (12 - shift)), 0))) >> 12;
 
        gteIR1 = limB1(gteMAC1, 0);
        gteIR2 = limB2(gteMAC2, 0);
@@ -792,9 +808,9 @@ void gteDPCT(psxCP2Regs *regs) {
        gteFLAG = 0;
 
        for (v = 0; v < 3; v++) {
-               gteMAC1 = ((gteR0 << 16) + (gteIR0 * limB1(gteRFC - (gteR0 << 4), 0))) >> 12;
-               gteMAC2 = ((gteG0 << 16) + (gteIR0 * limB1(gteGFC - (gteG0 << 4), 0))) >> 12;
-               gteMAC3 = ((gteB0 << 16) + (gteIR0 * limB1(gteBFC - (gteB0 << 4), 0))) >> 12;
+               gteMAC1 = ((gteR0 << 16) + (gteIR0 * limB1(A1U((s64)gteRFC - (gteR0 << 4)), 0))) >> 12;
+               gteMAC2 = ((gteG0 << 16) + (gteIR0 * limB1(A2U((s64)gteGFC - (gteG0 << 4)), 0))) >> 12;
+               gteMAC3 = ((gteB0 << 16) + (gteIR0 * limB1(A3U((s64)gteBFC - (gteB0 << 4)), 0))) >> 12;
 
                gteRGB0 = gteRGB1;
                gteRGB1 = gteRGB2;
@@ -905,9 +921,9 @@ void gteINTPL(psxCP2Regs *regs) {
 #endif
        gteFLAG = 0;
 
-       gteMAC1 = ((gteIR1 << 12) + (gteIR0 * limB1(gteRFC - gteIR1, 0))) >> shift;
-       gteMAC2 = ((gteIR2 << 12) + (gteIR0 * limB2(gteGFC - gteIR2, 0))) >> shift;
-       gteMAC3 = ((gteIR3 << 12) + (gteIR0 * limB3(gteBFC - gteIR3, 0))) >> shift;
+       gteMAC1 = ((gteIR1 << 12) + (gteIR0 * limB1(A1U((s64)gteRFC - gteIR1), 0))) >> shift;
+       gteMAC2 = ((gteIR2 << 12) + (gteIR0 * limB2(A2U((s64)gteGFC - gteIR2), 0))) >> shift;
+       gteMAC3 = ((gteIR3 << 12) + (gteIR0 * limB3(A3U((s64)gteBFC - gteIR3), 0))) >> shift;
        gteIR1 = limB1(gteMAC1, lm);
        gteIR2 = limB2(gteMAC2, lm);
        gteIR3 = limB3(gteMAC3, lm);
@@ -931,9 +947,9 @@ void gteCDP(psxCP2Regs *regs) {
        gteIR1 = limB1(gteMAC1, 1);
        gteIR2 = limB2(gteMAC2, 1);
        gteIR3 = limB3(gteMAC3, 1);
-       gteMAC1 = (((gteR << 4) * gteIR1) + (gteIR0 * limB1(gteRFC - ((gteR * gteIR1) >> 8), 0))) >> 12;
-       gteMAC2 = (((gteG << 4) * gteIR2) + (gteIR0 * limB2(gteGFC - ((gteG * gteIR2) >> 8), 0))) >> 12;
-       gteMAC3 = (((gteB << 4) * gteIR3) + (gteIR0 * limB3(gteBFC - ((gteB * gteIR3) >> 8), 0))) >> 12;
+       gteMAC1 = (((gteR << 4) * gteIR1) + (gteIR0 * limB1(A1U((s64)gteRFC - ((gteR * gteIR1) >> 8)), 0))) >> 12;
+       gteMAC2 = (((gteG << 4) * gteIR2) + (gteIR0 * limB2(A2U((s64)gteGFC - ((gteG * gteIR2) >> 8)), 0))) >> 12;
+       gteMAC3 = (((gteB << 4) * gteIR3) + (gteIR0 * limB3(A3U((s64)gteBFC - ((gteB * gteIR3) >> 8)), 0))) >> 12;
        gteIR1 = limB1(gteMAC1, 1);
        gteIR2 = limB2(gteMAC2, 1);
        gteIR3 = limB3(gteMAC3, 1);
@@ -945,3 +961,131 @@ void gteCDP(psxCP2Regs *regs) {
        gteG2 = limC2(gteMAC2 >> 4);
        gteB2 = limC3(gteMAC3 >> 4);
 }
+
+/* decomposed/parametrized versions for the recompiler */
+
+#ifndef FLAGLESS
+
+void gteSQR_part_noshift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = gteIR1 * gteIR1;
+       gteMAC2 = gteIR2 * gteIR2;
+       gteMAC3 = gteIR3 * gteIR3;
+}
+
+void gteSQR_part_shift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = (gteIR1 * gteIR1) >> 12;
+       gteMAC2 = (gteIR2 * gteIR2) >> 12;
+       gteMAC3 = (gteIR3 * gteIR3) >> 12;
+}
+
+void gteOP_part_noshift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = (gteR22 * gteIR3) - (gteR33 * gteIR2);
+       gteMAC2 = (gteR33 * gteIR1) - (gteR11 * gteIR3);
+       gteMAC3 = (gteR11 * gteIR2) - (gteR22 * gteIR1);
+}
+
+void gteOP_part_shift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = ((gteR22 * gteIR3) - (gteR33 * gteIR2)) >> 12;
+       gteMAC2 = ((gteR33 * gteIR1) - (gteR11 * gteIR3)) >> 12;
+       gteMAC3 = ((gteR11 * gteIR2) - (gteR22 * gteIR1)) >> 12;
+}
+
+void gteDCPL_part(psxCP2Regs *regs) {
+       s32 RIR1 = ((s32)gteR * gteIR1) >> 8;
+       s32 GIR2 = ((s32)gteG * gteIR2) >> 8;
+       s32 BIR3 = ((s32)gteB * gteIR3) >> 8;
+
+       gteFLAG = 0;
+
+       gteMAC1 = RIR1 + ((gteIR0 * limB1(A1U((s64)gteRFC - RIR1), 0)) >> 12);
+       gteMAC2 = GIR2 + ((gteIR0 * limB1(A2U((s64)gteGFC - GIR2), 0)) >> 12);
+       gteMAC3 = BIR3 + ((gteIR0 * limB1(A3U((s64)gteBFC - BIR3), 0)) >> 12);
+}
+
+void gteGPF_part_noshift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = gteIR0 * gteIR1;
+       gteMAC2 = gteIR0 * gteIR2;
+       gteMAC3 = gteIR0 * gteIR3;
+}
+
+void gteGPF_part_shift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = (gteIR0 * gteIR1) >> 12;
+       gteMAC2 = (gteIR0 * gteIR2) >> 12;
+       gteMAC3 = (gteIR0 * gteIR3) >> 12;
+}
+
+#endif // !FLAGLESS
+
+void gteGPL_part_noshift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = A1((s64)gteMAC1 + (gteIR0 * gteIR1));
+       gteMAC2 = A2((s64)gteMAC2 + (gteIR0 * gteIR2));
+       gteMAC3 = A3((s64)gteMAC3 + (gteIR0 * gteIR3));
+}
+
+void gteGPL_part_shift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = A1((s64)gteMAC1 + ((gteIR0 * gteIR1) >> 12));
+       gteMAC2 = A2((s64)gteMAC2 + ((gteIR0 * gteIR2) >> 12));
+       gteMAC3 = A3((s64)gteMAC3 + ((gteIR0 * gteIR3) >> 12));
+}
+
+void gteDPCS_part_noshift(psxCP2Regs *regs) {
+       int shift = 0;
+
+       gteFLAG = 0;
+
+       gteMAC1 = ((gteR << 16) + (gteIR0 * limB1(A1U((s64)gteRFC - (gteR << 4)) << (12 - shift), 0))) >> 12;
+       gteMAC2 = ((gteG << 16) + (gteIR0 * limB2(A2U((s64)gteGFC - (gteG << 4)) << (12 - shift), 0))) >> 12;
+       gteMAC3 = ((gteB << 16) + (gteIR0 * limB3(A3U((s64)gteBFC - (gteB << 4)) << (12 - shift), 0))) >> 12;
+}
+
+void gteDPCS_part_shift(psxCP2Regs *regs) {
+       int shift = 12;
+
+       gteFLAG = 0;
+
+       gteMAC1 = ((gteR << 16) + (gteIR0 * limB1(A1U((s64)gteRFC - (gteR << 4)) << (12 - shift), 0))) >> 12;
+       gteMAC2 = ((gteG << 16) + (gteIR0 * limB2(A2U((s64)gteGFC - (gteG << 4)) << (12 - shift), 0))) >> 12;
+       gteMAC3 = ((gteB << 16) + (gteIR0 * limB3(A3U((s64)gteBFC - (gteB << 4)) << (12 - shift), 0))) >> 12;
+}
+
+void gteINTPL_part_noshift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = ((gteIR1 << 12) + (gteIR0 * limB1(A1U((s64)gteRFC - gteIR1), 0)));
+       gteMAC2 = ((gteIR2 << 12) + (gteIR0 * limB2(A2U((s64)gteGFC - gteIR2), 0)));
+       gteMAC3 = ((gteIR3 << 12) + (gteIR0 * limB3(A3U((s64)gteBFC - gteIR3), 0)));
+}
+
+void gteINTPL_part_shift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = ((gteIR1 << 12) + (gteIR0 * limB1(A1U((s64)gteRFC - gteIR1), 0))) >> 12;
+       gteMAC2 = ((gteIR2 << 12) + (gteIR0 * limB2(A2U((s64)gteGFC - gteIR2), 0))) >> 12;
+       gteMAC3 = ((gteIR3 << 12) + (gteIR0 * limB3(A3U((s64)gteBFC - gteIR3), 0))) >> 12;
+}
+
+void gteMACtoRGB(psxCP2Regs *regs) {
+       gteRGB0 = gteRGB1;
+       gteRGB1 = gteRGB2;
+       gteCODE2 = gteCODE;
+       gteR2 = limC1(gteMAC1 >> 4);
+       gteG2 = limC2(gteMAC2 >> 4);
+       gteB2 = limC3(gteMAC3 >> 4);
+}
+