mave plugin and core shared includes to single location
[pcsx_rearmed.git] / plugins / dfxvideo / gpu.c
index 200913c..3d20dfa 100644 (file)
@@ -23,7 +23,6 @@
 // memory image of the PSX vram 
 ////////////////////////////////////////////////////////////////////////
 
-unsigned char  *psxVSecure;
 unsigned char  *psxVub;
 signed   char  *psxVsb;
 unsigned short *psxVuw;
@@ -51,6 +50,8 @@ DATAREGISTERMODES DataWriteMode;
 DATAREGISTERMODES DataReadMode;
 
 BOOL              bSkipNextFrame = FALSE;
+BOOL              fskip_frameReady;
+DWORD             lace_count_since_flip;
 DWORD             dwLaceCnt=0;
 short             sDispWidths[8] = {256,320,512,640,368,384,512,640};
 PSXDisplay_t      PSXDisplay;
@@ -59,6 +60,7 @@ long              lSelectedSlot=0;
 BOOL              bDoLazyUpdate=FALSE;
 uint32_t          lGPUInfoVals[16];
 static int        iFakePrimBusy=0;
+static const int  *skip_advice;
 
 ////////////////////////////////////////////////////////////////////////
 // some misc external display funcs
@@ -84,16 +86,15 @@ static void SetFixes(void)
 // INIT, will be called after lib load... well, just do some var init...
 ////////////////////////////////////////////////////////////////////////
 
+// one extra MB for soft drawing funcs security
+static unsigned char vram[1024*512*2 + 1024*1024] __attribute__((aligned(2048)));
+
 long CALLBACK GPUinit(void)                                // GPU INIT
 {
  memset(ulStatusControl,0,256*sizeof(uint32_t));  // init save state scontrol field
 
- psxVSecure = (unsigned char *)malloc((512*2)*1024 + (1024*1024)); // always alloc one extra MB for soft drawing funcs security
- if (!psxVSecure)
-  return -1;
-
  //!!! ATTENTION !!!
- psxVub=psxVSecure + 512 * 1024;                           // security offset into double sized psx vram!
+ psxVub=vram + 512 * 1024;                           // security offset into double sized psx vram!
 
  psxVsb=(signed char *)psxVub;                         // different ways of accessing PSX VRAM
  psxVsw=(signed short *)psxVub;
@@ -103,7 +104,7 @@ long CALLBACK GPUinit(void)                                // GPU INIT
 
  psxVuw_eom=psxVuw+1024*512;                    // pre-calc of end of vram
 
- memset(psxVSecure,0x00,(512*2)*1024 + (1024*1024));
+ memset(vram,0x00,(512*2)*1024 + (1024*1024));
  memset(lGPUInfoVals,0x00,16*sizeof(uint32_t));
 
  PSXDisplay.RGB24        = FALSE;                      // init some stuff
@@ -181,7 +182,6 @@ long CALLBACK GPUclose()                               // GPU CLOSE
 long CALLBACK GPUshutdown(void)                            // GPU SHUTDOWN
 {
  CloseDisplay();                                       // shutdown direct draw
- free(psxVSecure);
  return 0;                                             // nothinh to do
 }
 
@@ -204,20 +204,37 @@ static void updateDisplay(void)                               // UPDATE DISPLAY
 
  if(UseFrameSkip)                                      // skip ?
   {
-   if(!bSkipNextFrame) DoBufferSwap();                 // -> to skip or not to skip
-   if(dwActFixes&0xa0)                                 // -> pc fps calculation fix/old skipping fix
+   if(fskip_frameReady)
     {
-     if((fps_skip < fFrameRateHz) && !(bSkipNextFrame))  // -> skip max one in a row
-         {bSkipNextFrame = TRUE; fps_skip=fFrameRateHz;}
-     else bSkipNextFrame = FALSE;
+     DoBufferSwap();                                   // -> to skip or not to skip
+     fskip_frameReady=FALSE;
+     bDoVSyncUpdate=FALSE;                             // vsync done
     }
-   else FrameSkip();
   }
  else                                                  // no skip ?
   {
    bSkipNextFrame = FALSE;
    DoBufferSwap();                                     // -> swap
+   bDoVSyncUpdate=FALSE;                               // vsync done
+  }
+}
+
+static void decideSkip(void)
+{
+ if(!bDoVSyncUpdate)
+   return;
+
+ lace_count_since_flip=0;
+ fskip_frameReady=!bSkipNextFrame;
+
+ if(dwActFixes&0xa0)                                   // -> pc fps calculation fix/old skipping fix
+  {
+   int skip = (skip_advice && *skip_advice) || UseFrameSkip == 1 || fps_skip < fFrameRateHz;
+   if(skip && !bSkipNextFrame)                         // -> skip max one in a row
+       {bSkipNextFrame = TRUE; fps_skip=fFrameRateHz;}
+   else bSkipNextFrame = FALSE;
   }
+ else FrameSkip();
 }
 
 ////////////////////////////////////////////////////////////////////////
@@ -373,7 +390,7 @@ static void updateDisplayIfChanged(void)                      // UPDATE DISPLAY
 
  if(iFrameLimit==2) SetAutoFrameCap();                 // -> set it
 
- if(UseFrameSkip) updateDisplay();                     // stupid stuff when frame skipping enabled
+ if(UseFrameSkip) decideSkip();                        // stupid stuff when frame skipping enabled
 }
 
 ////////////////////////////////////////////////////////////////////////
@@ -382,8 +399,8 @@ static void updateDisplayIfChanged(void)                      // UPDATE DISPLAY
 
 void CALLBACK GPUupdateLace(void)                      // VSYNC
 {
- if(!(dwActFixes&1))
-  lGPUstatusRet^=0x80000000;                           // odd/even bit
//if(!(dwActFixes&1))
// lGPUstatusRet^=0x80000000;                           // odd/even bit
 
  //pcsx-rearmed: removed, this is handled by core
  //if(!(dwActFixes&32))                                  // std fps limitation?
@@ -391,6 +408,8 @@ void CALLBACK GPUupdateLace(void)                      // VSYNC
 
  if(PSXDisplay.Interlaced)                             // interlaced mode?
   {
+   lGPUstatusRet^=0x80000000;                          // odd/even bit?
+
    if(bDoVSyncUpdate && PSXDisplay.DisplayMode.x>0 && PSXDisplay.DisplayMode.y>0)
     {
      updateDisplay();
@@ -400,17 +419,24 @@ void CALLBACK GPUupdateLace(void)                      // VSYNC
   {
    if(dwActFixes&64)                                   // lazy screen update fix
     {
-     if(bDoLazyUpdate && !UseFrameSkip) 
+     if(bDoLazyUpdate)
       updateDisplay(); 
      bDoLazyUpdate=FALSE;
     }
    else
     {
-     if(bDoVSyncUpdate && !UseFrameSkip)               // some primitives drawn?
-      updateDisplay();                                 // -> update display
+     if(bDoVSyncUpdate)                                // some primitives drawn?
+       updateDisplay();                                // -> update display
     }
   }
- bDoVSyncUpdate=FALSE;                                 // vsync done
+
+ if(UseFrameSkip) {                                    // frame over-skip guard
+  lace_count_since_flip++;
+  if(lace_count_since_flip > 8) {
+   bSkipNextFrame=FALSE;
+   fskip_frameReady=TRUE;
+  }
+ }
 }
 
 ////////////////////////////////////////////////////////////////////////
@@ -547,9 +573,9 @@ void CALLBACK GPUwriteStatus(uint32_t gdata)      // WRITE STATUS
 
      if (!(PSXDisplay.Interlaced))                      // stupid frame skipping option
       {
-       if(UseFrameSkip)  updateDisplay();
        if(dwActFixes&64) bDoLazyUpdate=TRUE;
       }
+     if(UseFrameSkip)  decideSkip();
     }return;
    //--------------------------------------------------//
    // setting width
@@ -619,7 +645,7 @@ void CALLBACK GPUwriteStatus(uint32_t gdata)      // WRITE STATUS
        }
       lGPUstatusRet|=GPUSTATUS_INTERLACED;
      }
-    else lGPUstatusRet&=~GPUSTATUS_INTERLACED;
+    else lGPUstatusRet&=~(GPUSTATUS_INTERLACED|0x80000000);
 
     if (PSXDisplay.PAL)
          lGPUstatusRet|=GPUSTATUS_PAL;
@@ -946,14 +972,14 @@ ENDVRAM:
         {
          gpuDataC = primTableCX[command];
          gpuCommand = command;
-         PUTLE32(&gpuDataM[0], gdata);
+         PUTLE32_(&gpuDataM[0], gdata);
          gpuDataP = 1;
         }
        else continue;
       }
      else
       {
-       PUTLE32(&gpuDataM[gpuDataP], gdata);
+       PUTLE32_(&gpuDataM[gpuDataP], gdata);
        if(gpuDataC>128)
         {
          if((gpuDataC==254 && gpuDataP>=3) ||
@@ -986,7 +1012,7 @@ ENDVRAM:
 
 void CALLBACK GPUwriteData(uint32_t gdata)
 {
- PUTLE32(&gdata, gdata);
+ PUTLE32_(&gdata, gdata);
  GPUwriteDataMem(&gdata,1);
 }
 
@@ -1012,6 +1038,7 @@ long CALLBACK GPUdmaChain(uint32_t * baseAddrL, uint32_t addr)
  uint32_t dmaMem;
  unsigned char * baseAddrB;
  short count;unsigned int DMACommandCounter = 0;
+ long dmaWords = 0;
 
  GPUIsBusy;
 
@@ -1026,6 +1053,7 @@ long CALLBACK GPUdmaChain(uint32_t * baseAddrL, uint32_t addr)
    if(CheckForEndlessLoop(addr)) break;
 
    count = baseAddrB[addr+3];
+   dmaWords += 1 + count;
 
    dmaMem=addr+4;
 
@@ -1037,7 +1065,7 @@ long CALLBACK GPUdmaChain(uint32_t * baseAddrL, uint32_t addr)
 
  GPUIsIdle;
 
- return 0;
+ return dmaWords;
 }
 
 ////////////////////////////////////////////////////////////////////////
@@ -1086,6 +1114,7 @@ long CALLBACK GPUfreeze(uint32_t ulGetFreezeData,GPUFreeze_t * pF)
 
 // RESET TEXTURE STORE HERE, IF YOU USE SOMETHING LIKE THAT
 
+ PreviousPSXDisplay.Height = 0;
  GPUwriteStatus(ulStatusControl[0]);
  GPUwriteStatus(ulStatusControl[1]);
  GPUwriteStatus(ulStatusControl[2]);
@@ -1098,3 +1127,26 @@ long CALLBACK GPUfreeze(uint32_t ulGetFreezeData,GPUFreeze_t * pF)
 
  return 1;
 }
+
+// rearmed thing
+#include "../../frontend/plugin_lib.h"
+
+const struct rearmed_cbs *rcbs;
+
+void GPUrearmedCallbacks(const struct rearmed_cbs *cbs)
+{
+ // sync config
+ UseFrameSkip = cbs->frameskip;
+ iUseDither = cbs->gpu_peops.iUseDither;
+ dwActFixes = cbs->gpu_peops.dwActFixes;
+ fFrameRateHz = cbs->gpu_peops.fFrameRateHz;
+ dwFrameRateTicks = cbs->gpu_peops.dwFrameRateTicks;
+ if (cbs->pl_vout_set_raw_vram)
+  cbs->pl_vout_set_raw_vram(psxVub);
+ if (cbs->pl_set_gpu_caps)
+  cbs->pl_set_gpu_caps(0);
+
+ skip_advice = &cbs->fskip_advice;
+ fps_skip = 100.0f;
+ rcbs = cbs;
+}