#define gteBFC (((s32 *)regs->CP2C.r)[23])
#define gteOFX (((s32 *)regs->CP2C.r)[24])
#define gteOFY (((s32 *)regs->CP2C.r)[25])
-#define gteH (regs->CP2C.p[26].sw.l)
+// senquack - gteH register is u16, not s16, and used in GTE that way.
+// HOWEVER when read back by CPU using CFC2, it will be incorrectly
+// sign-extended by bug in original hardware, according to Nocash docs
+// GTE section 'Screen Offset and Distance'. The emulator does this
+// sign extension when it is loaded to GTE by CTC2.
+//#define gteH (psxRegs.CP2C.p[26].sw.l)
+#define gteH (psxRegs.CP2C.p[26].w.l)
#define gteDQA (regs->CP2C.p[27].sw.l)
#define gteDQB (((s32 *)regs->CP2C.r)[28])
#define gteZSF3 (regs->CP2C.p[29].sw.l)
#ifndef FLAGLESS
-static inline s32 BOUNDS_(psxCP2Regs *regs, s64 n_value, s64 n_max, int n_maxflag, s64 n_min, int n_minflag) {
+static inline s64 BOUNDS_(psxCP2Regs *regs, s64 n_value, s64 n_max, int n_maxflag, s64 n_min, int n_minflag) {
if (n_value > n_max) {
gteFLAG |= n_maxflag;
} else if (n_value < n_min) {
#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
+
+
+//senquack - n param should be unsigned (will be 'gteH' reg which is u16)
+#ifdef GTE_USE_NATIVE_DIVIDE
+INLINE u32 DIVIDE(u16 n, u16 d) {
+ if (n < d * 2) {
+ return ((u32)n << 16) / d;
+ }
+ return 0xffffffff;
+}
+#else
#include "gte_divider.h"
+#endif // GTE_USE_NATIVE_DIVIDE
#ifndef FLAGLESS
static inline u32 MFC2(int reg) {
- psxCP2Regs *regs = (psxCP2Regs *)&psxRegs.CP2D;
+ psxCP2Regs *regs = &psxRegs.CP2;
switch (reg) {
case 1:
case 3:
}
static inline void MTC2(u32 value, int reg) {
- psxCP2Regs *regs = (psxCP2Regs *)&psxRegs.CP2D;
+ psxCP2Regs *regs = &psxRegs.CP2;
switch (reg) {
case 15:
gteSXY0 = gteSXY1;
case 28:
gteIRGB = value;
- gteIR1 = (value & 0x1f) << 7;
- gteIR2 = (value & 0x3e0) << 2;
- gteIR3 = (value & 0x7c00) >> 3;
+ // not gteIR1 etc. just to be consistent with dynarec
+ regs->CP2D.n.ir1 = (value & 0x1f) << 7;
+ regs->CP2D.n.ir2 = (value & 0x3e0) << 2;
+ regs->CP2D.n.ir3 = (value & 0x7c00) >> 3;
break;
case 30:
void gteRTPS(psxCP2Regs *regs) {
int quotient;
+ s64 tmp;
#ifdef GTE_LOG
GTE_LOG("GTE RTPS\n");
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");
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) {
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);
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;
#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);
#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);
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;
#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);
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);
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);
+}
+