try to emulate borders properly
[pcsx_rearmed.git] / plugins / gpulib / gpu.c
index e9714e4..931583f 100644 (file)
@@ -9,6 +9,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include "gpu.h"
 
@@ -61,28 +62,90 @@ static noinline void do_reset(void)
   gpu.regs[3] = 1;
   gpu.screen.hres = gpu.screen.w = 256;
   gpu.screen.vres = gpu.screen.h = 240;
+  gpu.screen.x = gpu.screen.y = 0;
 }
 
 static noinline void update_width(void)
 {
+  static const short hres_all[8] = { 256, 368, 320, 368, 512, 368, 640, 368 };
+  static const uint8_t hdivs[8] = { 10, 7, 8, 7, 5, 7, 4, 7 };
+  uint8_t hdiv = hdivs[(gpu.status >> 16) & 7];
+  int hres = hres_all[(gpu.status >> 16) & 7];
+  int pal = gpu.status & PSX_GPU_STATUS_PAL;
   int sw = gpu.screen.x2 - gpu.screen.x1;
-  if (sw <= 0 || sw >= 2560)
-    // full width
-    gpu.screen.w = gpu.screen.hres;
-  else
-    gpu.screen.w = sw * gpu.screen.hres / 2560;
+  int x = 0, x_auto;
+  if (sw <= 0)
+    /* nothing displayed? */;
+  else {
+    int s = pal ? 656 : 608; // or 600? pal is just a guess
+    x = (gpu.screen.x1 - s) / hdiv;
+    x = (x + 1) & ~1;   // blitter limitation
+    sw /= hdiv;
+    sw = (sw + 2) & ~3; // according to nocash
+    switch (gpu.state.screen_centering_type) {
+    case 1:
+      break;
+    case 2:
+      x = gpu.state.screen_centering_x;
+      break;
+    default:
+      // correct if slightly miscentered
+      x_auto = (hres - sw) / 2 & ~3;
+      if ((uint32_t)x_auto <= 8u && abs(x) < 24)
+        x = x_auto;
+    }
+    if (x + sw > hres)
+      sw = hres - x;
+    // .x range check is done in vout_update()
+  }
+  // reduce the unpleasant right border that a few games have
+  if (gpu.state.screen_centering_type == 0
+      && x <= 4 && hres - (x + sw) >= 4)
+    hres -= 4;
+  gpu.screen.x = x;
+  gpu.screen.w = sw;
+  gpu.screen.hres = hres;
+  gpu.state.dims_changed = 1;
+  //printf("xx %d %d -> %2d, %d / %d\n",
+  //  gpu.screen.x1, gpu.screen.x2, x, sw, hres);
 }
 
 static noinline void update_height(void)
 {
-  // TODO: emulate this properly..
+  int pal = gpu.status & PSX_GPU_STATUS_PAL;
+  int dheight = gpu.status & PSX_GPU_STATUS_DHEIGHT;
+  int y = gpu.screen.y1 - (pal ? 39 : 16); // 39 for spyro
   int sh = gpu.screen.y2 - gpu.screen.y1;
-  if (gpu.status & PSX_GPU_STATUS_DHEIGHT)
-    sh *= 2;
-  if (sh <= 0 || sh > gpu.screen.vres)
-    sh = gpu.screen.vres;
-
+  int center_tol = 16;
+  int vres = 240;
+
+  if (pal && (sh > 240 || gpu.screen.vres == 256))
+    vres = 256;
+  if (dheight)
+    y *= 2, sh *= 2, vres *= 2, center_tol *= 2;
+  if (sh <= 0)
+    /* nothing displayed? */;
+  else {
+    switch (gpu.state.screen_centering_type) {
+    case 1:
+      break;
+    case 2:
+      y = gpu.state.screen_centering_y;
+      break;
+    default:
+      // correct if slightly miscentered
+      if ((uint32_t)(vres - sh) <= 1 && abs(y) <= center_tol)
+        y = 0;
+    }
+    if (y + sh > vres)
+      sh = vres - y;
+  }
+  gpu.screen.y = y;
   gpu.screen.h = sh;
+  gpu.screen.vres = vres;
+  gpu.state.dims_changed = 1;
+  //printf("yy %d %d -> %d, %d / %d\n",
+  //  gpu.screen.y1, gpu.screen.y2, y, sh, vres);
 }
 
 static noinline void decide_frameskip(void)
@@ -115,8 +178,8 @@ static noinline int decide_frameskip_allow(uint32_t cmd_e3)
   uint32_t x = cmd_e3 & 0x3ff;
   uint32_t y = (cmd_e3 >> 10) & 0x3ff;
   gpu.frameskip.allow = (gpu.status & PSX_GPU_STATUS_INTERLACE) ||
-    (uint32_t)(x - gpu.screen.x) >= (uint32_t)gpu.screen.w ||
-    (uint32_t)(y - gpu.screen.y) >= (uint32_t)gpu.screen.h;
+    (uint32_t)(x - gpu.screen.src_x) >= (uint32_t)gpu.screen.w ||
+    (uint32_t)(y - gpu.screen.src_y) >= (uint32_t)gpu.screen.h;
   return gpu.frameskip.allow;
 }
 
@@ -192,8 +255,6 @@ long GPUshutdown(void)
 
 void GPUwriteStatus(uint32_t data)
 {
-  static const short hres[8] = { 256, 368, 320, 384, 512, 512, 640, 640 };
-  static const short vres[4] = { 240, 480, 256, 480 };
   uint32_t cmd = data >> 24;
 
   if (cmd < ARRAY_SIZE(gpu.regs)) {
@@ -212,8 +273,10 @@ void GPUwriteStatus(uint32_t data)
       do_cmd_reset();
       break;
     case 0x03:
-      if (data & 1)
+      if (data & 1) {
         gpu.status |= PSX_GPU_STATUS_BLANKING;
+        gpu.state.dims_changed = 1; // for hud clearing
+      }
       else
         gpu.status &= ~PSX_GPU_STATUS_BLANKING;
       break;
@@ -222,8 +285,8 @@ void GPUwriteStatus(uint32_t data)
       gpu.status |= PSX_GPU_STATUS_DMA(data & 3);
       break;
     case 0x05:
-      gpu.screen.x = data & 0x3ff;
-      gpu.screen.y = (data >> 10) & 0x1ff;
+      gpu.screen.src_x = data & 0x3ff;
+      gpu.screen.src_y = (data >> 10) & 0x1ff;
       if (gpu.frameskip.set) {
         decide_frameskip_allow(gpu.ex_regs[3]);
         if (gpu.frameskip.last_flip_frame != *gpu.state.frame_count) {
@@ -244,8 +307,6 @@ void GPUwriteStatus(uint32_t data)
       break;
     case 0x08:
       gpu.status = (gpu.status & ~0x7f0000) | ((data & 0x3F) << 17) | ((data & 0x40) << 10);
-      gpu.screen.hres = hres[(gpu.status >> 16) & 7];
-      gpu.screen.vres = vres[(gpu.status >> 19) & 3];
       update_width();
       update_height();
       renderer_notify_res_change();
@@ -752,6 +813,15 @@ void GPUrearmedCallbacks(const struct rearmed_cbs *cbs)
   gpu.state.frame_count = cbs->gpu_frame_count;
   gpu.state.allow_interlace = cbs->gpu_neon.allow_interlace;
   gpu.state.enhancement_enable = cbs->gpu_neon.enhancement_enable;
+  if (gpu.state.screen_centering_type != cbs->screen_centering_type
+      || gpu.state.screen_centering_x != cbs->screen_centering_x
+      || gpu.state.screen_centering_y != cbs->screen_centering_y) {
+    gpu.state.screen_centering_type = cbs->screen_centering_type;
+    gpu.state.screen_centering_x = cbs->screen_centering_x;
+    gpu.state.screen_centering_y = cbs->screen_centering_y;
+    update_width();
+    update_height();
+  }
 
   gpu.mmap = cbs->mmap;
   gpu.munmap = cbs->munmap;