- s32 x0, x1;
- s32 y0, y1;
- s32 u0;
- s32 v0;
-
- x1 = x0 = GPU_EXPANDSIGN_SPRT(PacketBuffer.S2[2]) + DrawingOffset[0];
- y1 = y0 = GPU_EXPANDSIGN_SPRT(PacketBuffer.S2[3]) + DrawingOffset[1];
- x1+= PacketBuffer.S2[6];
- y1+= PacketBuffer.S2[7];
-
- {
- s32 xmin, xmax;
- s32 ymin, ymax;
- xmin = DrawingArea[0]; xmax = DrawingArea[2];
- ymin = DrawingArea[1]; ymax = DrawingArea[3];
-
- {
- int rx0 = Max2(xmin,Min2(x0,x1));
- int ry0 = Max2(ymin,Min2(y0,y1));
- int rx1 = Min2(xmax,Max2(x0,x1));
- int ry1 = Min2(ymax,Max2(y0,y1));
- if( rx0>=rx1 || ry0>=ry1) return;
- }
-
- u0 = PacketBuffer.U1[8];
- v0 = PacketBuffer.U1[9];
-
- r4 = s32(PacketBuffer.U1[0]);
- g4 = s32(PacketBuffer.U1[1]);
- b4 = s32(PacketBuffer.U1[2]);
-
- {
- s32 temp;
- temp = ymin - y0;
- if (temp > 0) { y0 = ymin; v0 += temp; }
- if (y1 > ymax) y1 = ymax;
- if (y1 <= y0) return;
-
- temp = xmin - x0;
- if (temp > 0) { x0 = xmin; u0 += temp; }
- if (x1 > xmax) x1 = xmax;
- x1 -= x0;
- if (x1 <= 0) return;
- }
- }
-
- {
- u16 *Pixel = &((u16*)GPU_FrameBuffer)[FRAME_OFFSET(x0, y0)];
- const int li=linesInterlace;
- const u32 masku=TextureWindow[2];
- const u32 maskv=TextureWindow[3];
-
- for (;y0<y1;++y0) {
- if( 0 == (y0&li) ) gpuSpriteSpanDriver(Pixel,x1,FRAME_OFFSET(u0,v0),masku);
- Pixel += FRAME_WIDTH;
- v0 = (v0+1)&maskv;
- }
+ s32 x0, x1, y0, y1;
+ u32 u0, v0;
+
+ //NOTE: Must 11-bit sign-extend the whole sum here, not just packet X/Y,
+ // or sprites in 1st level of SkullMonkeys disappear when walking right.
+ // This now matches behavior of Mednafen and PCSX Rearmed's gpu_neon:
+ x0 = GPU_EXPANDSIGN(le16_to_s16(packet.U2[2]) + gpu_unai.DrawingOffset[0]);
+ y0 = GPU_EXPANDSIGN(le16_to_s16(packet.U2[3]) + gpu_unai.DrawingOffset[1]);
+
+ u32 w = le16_to_u16(packet.U2[6]) & 0x3ff; // Max width is 1023
+ u32 h = le16_to_u16(packet.U2[7]) & 0x1ff; // Max height is 511
+ x1 = x0 + w;
+ y1 = y0 + h;
+
+ s32 xmin, xmax, ymin, ymax;
+ xmin = gpu_unai.DrawingArea[0]; xmax = gpu_unai.DrawingArea[2];
+ ymin = gpu_unai.DrawingArea[1]; ymax = gpu_unai.DrawingArea[3];
+
+ u0 = packet.U1[8];
+ v0 = packet.U1[9];
+
+ s32 temp;
+ temp = ymin - y0;
+ if (temp > 0) { y0 = ymin; v0 += temp; }
+ if (y1 > ymax) y1 = ymax;
+ if (y1 <= y0) return;
+
+ temp = xmin - x0;
+ if (temp > 0) { x0 = xmin; u0 += temp; }
+ if (x1 > xmax) x1 = xmax;
+ x1 -= x0;
+ if (x1 <= 0) return;
+ *w_out = x1;
+ *h_out = y1 - y0;
+
+ gpu_unai.r5 = packet.U1[0] >> 3;
+ gpu_unai.g5 = packet.U1[1] >> 3;
+ gpu_unai.b5 = packet.U1[2] >> 3;
+
+ le16_t *Pixel = &gpu_unai.vram[FRAME_OFFSET(x0, y0)];
+ const int li=gpu_unai.ilace_mask;
+ const int pi=(ProgressiveInterlaceEnabled()?(gpu_unai.ilace_mask+1):0);
+ const int pif=(ProgressiveInterlaceEnabled()?(gpu_unai.prog_ilace_flag?(gpu_unai.ilace_mask+1):0):1);
+ unsigned int tmode = gpu_unai.TEXT_MODE >> 5;
+ const u32 v0_mask = gpu_unai.TextureWindow[3];
+ u8* pTxt_base = (u8*)gpu_unai.TBA;
+
+ // Texture is accessed byte-wise, so adjust idx if 16bpp
+ if (tmode == 3) u0 <<= 1;
+
+ for (; y0<y1; ++y0) {
+ u8* pTxt = pTxt_base + ((v0 & v0_mask) * 2048);
+ if (!(y0&li) && (y0&pi)!=pif)
+ gpuSpriteSpanDriver(Pixel, x1, pTxt, u0);
+ Pixel += FRAME_WIDTH;
+ v0++;