1 /***************************************************************************
4 begin : Sun Oct 28 2001
5 copyright : (C) 2001 by Pete Bernert
6 email : BlackDove@addcom.de
7 ***************************************************************************/
8 /***************************************************************************
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. See also the license.txt file for *
14 * additional informations. *
16 ***************************************************************************/
20 #include "psemu_plugin_defs.h"
22 ////////////////////////////////////////////////////////////////////////
23 // memory image of the PSX vram
24 ////////////////////////////////////////////////////////////////////////
26 unsigned char *psxVub;
28 unsigned short *psxVuw;
29 unsigned short *psxVuw_eom;
34 ////////////////////////////////////////////////////////////////////////
36 ////////////////////////////////////////////////////////////////////////
38 static long lGPUdataRet;
40 uint32_t ulStatusControl[256];
42 static uint32_t gpuDataM[256];
43 static unsigned char gpuCommand = 0;
44 static long gpuDataC = 0;
45 static long gpuDataP = 0;
49 DATAREGISTERMODES DataWriteMode;
50 DATAREGISTERMODES DataReadMode;
52 BOOL bSkipNextFrame = FALSE;
53 BOOL fskip_frameReady;
54 DWORD lace_count_since_flip;
56 short sDispWidths[8] = {256,320,512,640,368,384,512,640};
57 PSXDisplay_t PSXDisplay;
58 PSXDisplay_t PreviousPSXDisplay;
60 BOOL bDoLazyUpdate=FALSE;
61 uint32_t lGPUInfoVals[16];
62 static int iFakePrimBusy=0;
63 static const int *skip_advice;
65 ////////////////////////////////////////////////////////////////////////
66 // some misc external display funcs
67 ////////////////////////////////////////////////////////////////////////
75 ////////////////////////////////////////////////////////////////////////
76 // sets all kind of act fixes
77 ////////////////////////////////////////////////////////////////////////
79 static void SetFixes(void)
81 if(dwActFixes&0x02) sDispWidths[4]=384;
82 else sDispWidths[4]=368;
85 ////////////////////////////////////////////////////////////////////////
86 // INIT, will be called after lib load... well, just do some var init...
87 ////////////////////////////////////////////////////////////////////////
89 // one extra MB for soft drawing funcs security
90 static unsigned char vram[1024*512*2 + 1024*1024] __attribute__((aligned(2048)));
92 long CALLBACK GPUinit(void) // GPU INIT
94 memset(ulStatusControl,0,256*sizeof(uint32_t)); // init save state scontrol field
97 psxVub=vram + 512 * 1024; // security offset into double sized psx vram!
99 psxVsb=(signed char *)psxVub; // different ways of accessing PSX VRAM
100 psxVsw=(signed short *)psxVub;
101 psxVsl=(int32_t *)psxVub;
102 psxVuw=(unsigned short *)psxVub;
103 psxVul=(uint32_t *)psxVub;
105 psxVuw_eom=psxVuw+1024*512; // pre-calc of end of vram
107 memset(vram,0x00,(512*2)*1024 + (1024*1024));
108 memset(lGPUInfoVals,0x00,16*sizeof(uint32_t));
110 PSXDisplay.RGB24 = FALSE; // init some stuff
111 PSXDisplay.Interlaced = FALSE;
112 PSXDisplay.DrawOffset.x = 0;
113 PSXDisplay.DrawOffset.y = 0;
114 PSXDisplay.DisplayMode.x= 320;
115 PSXDisplay.DisplayMode.y= 240;
116 PreviousPSXDisplay.DisplayMode.x= 320;
117 PreviousPSXDisplay.DisplayMode.y= 240;
118 PSXDisplay.Disabled = FALSE;
119 PreviousPSXDisplay.Range.x0 =0;
120 PreviousPSXDisplay.Range.y0 =0;
121 PSXDisplay.Range.x0=0;
122 PSXDisplay.Range.x1=0;
123 PreviousPSXDisplay.DisplayModeNew.y=0;
124 PSXDisplay.Double = 1;
127 DataWriteMode = DR_NORMAL;
129 // Reset transfer values, to prevent mis-transfer of data
130 memset(&VRAMWrite, 0, sizeof(VRAMLoad_t));
131 memset(&VRAMRead, 0, sizeof(VRAMLoad_t));
133 // device initialised already !
134 lGPUstatusRet = 0x14802000;
136 GPUIsReadyForCommands;
137 bDoVSyncUpdate = TRUE;
142 ////////////////////////////////////////////////////////////////////////
143 // Here starts all...
144 ////////////////////////////////////////////////////////////////////////
147 long GPUopen(unsigned long * disp,char * CapText,char * CfgFile)
155 bDoVSyncUpdate = TRUE;
157 d=ulInitDisplay(); // setup x
160 *disp=d; // wanna x pointer? ok
167 ////////////////////////////////////////////////////////////////////////
169 ////////////////////////////////////////////////////////////////////////
171 long CALLBACK GPUclose() // GPU CLOSE
173 CloseDisplay(); // shutdown direct draw
178 ////////////////////////////////////////////////////////////////////////
179 // I shot the sheriff
180 ////////////////////////////////////////////////////////////////////////
182 long CALLBACK GPUshutdown(void) // GPU SHUTDOWN
184 CloseDisplay(); // shutdown direct draw
185 return 0; // nothinh to do
188 ////////////////////////////////////////////////////////////////////////
189 // Update display (swap buffers)
190 ////////////////////////////////////////////////////////////////////////
192 static void updateDisplay(void) // UPDATE DISPLAY
194 if(PSXDisplay.Disabled) // disable?
196 return; // -> and bye
199 if(dwActFixes&32) // pc fps calculation fix
201 if(UseFrameLimit) PCFrameCap(); // -> brake
202 if(UseFrameSkip) PCcalcfps();
205 if(UseFrameSkip) // skip ?
209 DoBufferSwap(); // -> to skip or not to skip
210 fskip_frameReady=FALSE;
211 bDoVSyncUpdate=FALSE; // vsync done
216 bSkipNextFrame = FALSE;
217 DoBufferSwap(); // -> swap
218 bDoVSyncUpdate=FALSE; // vsync done
222 static void decideSkip(void)
227 lace_count_since_flip=0;
228 fskip_frameReady=!bSkipNextFrame;
230 if(dwActFixes&0xa0) // -> pc fps calculation fix/old skipping fix
232 int skip = (skip_advice && *skip_advice) || UseFrameSkip == 1 || fps_skip < fFrameRateHz;
233 if(skip && !bSkipNextFrame) // -> skip max one in a row
234 {bSkipNextFrame = TRUE; fps_skip=fFrameRateHz;}
235 else bSkipNextFrame = FALSE;
240 ////////////////////////////////////////////////////////////////////////
241 // roughly emulated screen centering bits... not complete !!!
242 ////////////////////////////////////////////////////////////////////////
244 void ChangeDispOffsetsX(void) // X CENTER
248 if(!PSXDisplay.Range.x1) return;
250 l=PreviousPSXDisplay.DisplayMode.x;
252 l*=(long)PSXDisplay.Range.x1;
253 l/=2560;lx=l;l&=0xfffffff8;
255 if(l==PreviousPSXDisplay.Range.y1) return; // abusing range.y1 for
256 PreviousPSXDisplay.Range.y1=(short)l; // storing last x range and test
258 if(lx>=PreviousPSXDisplay.DisplayMode.x)
260 PreviousPSXDisplay.Range.x1=
261 (short)PreviousPSXDisplay.DisplayMode.x;
262 PreviousPSXDisplay.Range.x0=0;
266 PreviousPSXDisplay.Range.x1=(short)l;
268 PreviousPSXDisplay.Range.x0=
269 (PSXDisplay.Range.x0-500)/8;
271 if(PreviousPSXDisplay.Range.x0<0)
272 PreviousPSXDisplay.Range.x0=0;
274 if((PreviousPSXDisplay.Range.x0+lx)>
275 PreviousPSXDisplay.DisplayMode.x)
277 PreviousPSXDisplay.Range.x0=
278 (short)(PreviousPSXDisplay.DisplayMode.x-lx);
279 PreviousPSXDisplay.Range.x0+=2; //???
281 PreviousPSXDisplay.Range.x1+=(short)(lx-l);
283 PreviousPSXDisplay.Range.x1-=2; // makes linux stretching easier
287 // some linux alignment security
288 PreviousPSXDisplay.Range.x0=PreviousPSXDisplay.Range.x0>>1;
289 PreviousPSXDisplay.Range.x0=PreviousPSXDisplay.Range.x0<<1;
290 PreviousPSXDisplay.Range.x1=PreviousPSXDisplay.Range.x1>>1;
291 PreviousPSXDisplay.Range.x1=PreviousPSXDisplay.Range.x1<<1;
293 DoClearScreenBuffer();
299 ////////////////////////////////////////////////////////////////////////
301 void ChangeDispOffsetsY(void) // Y CENTER
303 int iT,iO=PreviousPSXDisplay.Range.y0;
304 int iOldYOffset=PreviousPSXDisplay.DisplayModeNew.y;
308 if((PreviousPSXDisplay.DisplayModeNew.x+PSXDisplay.DisplayModeNew.y)>512)
310 int dy1=512-PreviousPSXDisplay.DisplayModeNew.x;
311 int dy2=(PreviousPSXDisplay.DisplayModeNew.x+PSXDisplay.DisplayModeNew.y)-512;
315 PreviousPSXDisplay.DisplayModeNew.y=-dy2;
319 PSXDisplay.DisplayPosition.y=0;
320 PreviousPSXDisplay.DisplayModeNew.y=-dy1;
323 else PreviousPSXDisplay.DisplayModeNew.y=0;
327 if(PreviousPSXDisplay.DisplayModeNew.y!=iOldYOffset) // if old offset!=new offset: recalc height
329 PSXDisplay.Height = PSXDisplay.Range.y1 -
330 PSXDisplay.Range.y0 +
331 PreviousPSXDisplay.DisplayModeNew.y;
332 PSXDisplay.DisplayModeNew.y=PSXDisplay.Height*PSXDisplay.Double;
337 if(PSXDisplay.PAL) iT=48; else iT=28;
339 if(PSXDisplay.Range.y0>=iT)
341 PreviousPSXDisplay.Range.y0=
342 (short)((PSXDisplay.Range.y0-iT-4)*PSXDisplay.Double);
343 if(PreviousPSXDisplay.Range.y0<0)
344 PreviousPSXDisplay.Range.y0=0;
345 PSXDisplay.DisplayModeNew.y+=
346 PreviousPSXDisplay.Range.y0;
349 PreviousPSXDisplay.Range.y0=0;
351 if(iO!=PreviousPSXDisplay.Range.y0)
353 DoClearScreenBuffer();
357 ////////////////////////////////////////////////////////////////////////
358 // check if update needed
359 ////////////////////////////////////////////////////////////////////////
361 static void updateDisplayIfChanged(void) // UPDATE DISPLAY IF CHANGED
363 if ((PSXDisplay.DisplayMode.y == PSXDisplay.DisplayModeNew.y) &&
364 (PSXDisplay.DisplayMode.x == PSXDisplay.DisplayModeNew.x))
366 if((PSXDisplay.RGB24 == PSXDisplay.RGB24New) &&
367 (PSXDisplay.Interlaced == PSXDisplay.InterlacedNew)) return;
370 PSXDisplay.RGB24 = PSXDisplay.RGB24New; // get new infos
372 PSXDisplay.DisplayMode.y = PSXDisplay.DisplayModeNew.y;
373 PSXDisplay.DisplayMode.x = PSXDisplay.DisplayModeNew.x;
374 PreviousPSXDisplay.DisplayMode.x= // previous will hold
375 min(640,PSXDisplay.DisplayMode.x); // max 640x512... that's
376 PreviousPSXDisplay.DisplayMode.y= // the size of my
377 min(512,PSXDisplay.DisplayMode.y); // back buffer surface
378 PSXDisplay.Interlaced = PSXDisplay.InterlacedNew;
380 PSXDisplay.DisplayEnd.x= // calc end of display
381 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
382 PSXDisplay.DisplayEnd.y=
383 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
384 PreviousPSXDisplay.DisplayEnd.x=
385 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
386 PreviousPSXDisplay.DisplayEnd.y=
387 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
389 ChangeDispOffsetsX();
391 if(iFrameLimit==2) SetAutoFrameCap(); // -> set it
393 if(UseFrameSkip) decideSkip(); // stupid stuff when frame skipping enabled
396 ////////////////////////////////////////////////////////////////////////
397 // update lace is called evry VSync
398 ////////////////////////////////////////////////////////////////////////
400 void CALLBACK GPUupdateLace(void) // VSYNC
402 //if(!(dwActFixes&1))
403 // lGPUstatusRet^=0x80000000; // odd/even bit
405 //pcsx-rearmed: removed, this is handled by core
406 //if(!(dwActFixes&32)) // std fps limitation?
409 if(PSXDisplay.Interlaced) // interlaced mode?
411 lGPUstatusRet^=0x80000000; // odd/even bit?
413 if(bDoVSyncUpdate && PSXDisplay.DisplayMode.x>0 && PSXDisplay.DisplayMode.y>0)
418 else // non-interlaced?
420 if(dwActFixes&64) // lazy screen update fix
428 if(bDoVSyncUpdate) // some primitives drawn?
429 updateDisplay(); // -> update display
433 if(UseFrameSkip) { // frame over-skip guard
434 lace_count_since_flip++;
435 if(lace_count_since_flip > 8) {
436 bSkipNextFrame=FALSE;
437 fskip_frameReady=TRUE;
442 ////////////////////////////////////////////////////////////////////////
443 // process read request from GPU status register
444 ////////////////////////////////////////////////////////////////////////
447 uint32_t CALLBACK GPUreadStatus(void) // READ STATUS
451 static int iNumRead=0; // odd/even hack
455 lGPUstatusRet^=0x80000000; // interlaced bit toggle... we do it on every 3 read status... needed by some games (like ChronoCross) with old epsxe versions (1.5.2 and older)
459 if(iFakePrimBusy) // 27.10.2007 - PETE : emulating some 'busy' while drawing... pfff
463 if(iFakePrimBusy&1) // we do a busy-idle-busy-idle sequence after/while drawing prims
466 GPUIsNotReadyForCommands;
471 GPUIsReadyForCommands;
474 return lGPUstatusRet;
477 ////////////////////////////////////////////////////////////////////////
478 // processes data send to GPU status register
479 // these are always single packet commands.
480 ////////////////////////////////////////////////////////////////////////
482 void CALLBACK GPUwriteStatus(uint32_t gdata) // WRITE STATUS
484 uint32_t lCommand=(gdata>>24)&0xff;
486 ulStatusControl[lCommand]=gdata; // store command for freezing
490 //--------------------------------------------------//
493 memset(lGPUInfoVals,0x00,16*sizeof(uint32_t));
494 lGPUstatusRet=0x14802000;
495 PSXDisplay.Disabled=1;
496 DataWriteMode=DataReadMode=DR_NORMAL;
497 PSXDisplay.DrawOffset.x=PSXDisplay.DrawOffset.y=0;
498 drawX=drawY=0;drawW=drawH=0;
499 sSetMask=0;lSetMask=0;bCheckMask=FALSE;
501 GlobalTextAddrX=0;GlobalTextAddrY=0;
502 GlobalTextTP=0;GlobalTextABR=0;
503 PSXDisplay.RGB24=FALSE;
504 PSXDisplay.Interlaced=FALSE;
507 //--------------------------------------------------//
508 // dis/enable display
511 PreviousPSXDisplay.Disabled = PSXDisplay.Disabled;
512 PSXDisplay.Disabled = (gdata & 1);
514 if(PSXDisplay.Disabled)
515 lGPUstatusRet|=GPUSTATUS_DISPLAYDISABLED;
516 else lGPUstatusRet&=~GPUSTATUS_DISPLAYDISABLED;
519 //--------------------------------------------------//
520 // setting transfer mode
522 gdata &= 0x03; // Only want the lower two bits
524 DataWriteMode=DataReadMode=DR_NORMAL;
525 if(gdata==0x02) DataWriteMode=DR_VRAMTRANSFER;
526 if(gdata==0x03) DataReadMode =DR_VRAMTRANSFER;
527 lGPUstatusRet&=~GPUSTATUS_DMABITS; // Clear the current settings of the DMA bits
528 lGPUstatusRet|=(gdata << 29); // Set the DMA bits according to the received data
531 //--------------------------------------------------//
532 // setting display position
535 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
536 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
539 PSXDisplay.DisplayPosition.y = (short)((gdata>>10)&0x1ff);
541 // store the same val in some helper var, we need it on later compares
542 PreviousPSXDisplay.DisplayModeNew.x=PSXDisplay.DisplayPosition.y;
544 if((PSXDisplay.DisplayPosition.y+PSXDisplay.DisplayMode.y)>512)
546 int dy1=512-PSXDisplay.DisplayPosition.y;
547 int dy2=(PSXDisplay.DisplayPosition.y+PSXDisplay.DisplayMode.y)-512;
551 PreviousPSXDisplay.DisplayModeNew.y=-dy2;
555 PSXDisplay.DisplayPosition.y=0;
556 PreviousPSXDisplay.DisplayModeNew.y=-dy1;
559 else PreviousPSXDisplay.DisplayModeNew.y=0;
562 PSXDisplay.DisplayPosition.x = (short)(gdata & 0x3ff);
563 PSXDisplay.DisplayEnd.x=
564 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
565 PSXDisplay.DisplayEnd.y=
566 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y + PreviousPSXDisplay.DisplayModeNew.y;
567 PreviousPSXDisplay.DisplayEnd.x=
568 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
569 PreviousPSXDisplay.DisplayEnd.y=
570 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y + PreviousPSXDisplay.DisplayModeNew.y;
574 if (!(PSXDisplay.Interlaced)) // stupid frame skipping option
576 if(dwActFixes&64) bDoLazyUpdate=TRUE;
578 if(UseFrameSkip) decideSkip();
580 //--------------------------------------------------//
584 PSXDisplay.Range.x0=(short)(gdata & 0x7ff);
585 PSXDisplay.Range.x1=(short)((gdata>>12) & 0xfff);
587 PSXDisplay.Range.x1-=PSXDisplay.Range.x0;
589 ChangeDispOffsetsX();
592 //--------------------------------------------------//
597 PSXDisplay.Range.y0=(short)(gdata & 0x3ff);
598 PSXDisplay.Range.y1=(short)((gdata>>10) & 0x3ff);
600 PreviousPSXDisplay.Height = PSXDisplay.Height;
602 PSXDisplay.Height = PSXDisplay.Range.y1 -
603 PSXDisplay.Range.y0 +
604 PreviousPSXDisplay.DisplayModeNew.y;
606 if(PreviousPSXDisplay.Height!=PSXDisplay.Height)
608 PSXDisplay.DisplayModeNew.y=PSXDisplay.Height*PSXDisplay.Double;
610 ChangeDispOffsetsY();
612 updateDisplayIfChanged();
616 //--------------------------------------------------//
617 // setting display infos
620 PSXDisplay.DisplayModeNew.x =
621 sDispWidths[(gdata & 0x03) | ((gdata & 0x40) >> 4)];
623 if (gdata&0x04) PSXDisplay.Double=2;
624 else PSXDisplay.Double=1;
626 PSXDisplay.DisplayModeNew.y = PSXDisplay.Height*PSXDisplay.Double;
628 ChangeDispOffsetsY();
630 PSXDisplay.PAL = (gdata & 0x08)?TRUE:FALSE; // if 1 - PAL mode, else NTSC
631 PSXDisplay.RGB24New = (gdata & 0x10)?TRUE:FALSE; // if 1 - TrueColor
632 PSXDisplay.InterlacedNew = (gdata & 0x20)?TRUE:FALSE; // if 1 - Interlace
634 lGPUstatusRet&=~GPUSTATUS_WIDTHBITS; // Clear the width bits
636 (((gdata & 0x03) << 17) |
637 ((gdata & 0x40) << 10)); // Set the width bits
639 if(PSXDisplay.InterlacedNew)
641 if(!PSXDisplay.Interlaced)
643 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
644 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
646 lGPUstatusRet|=GPUSTATUS_INTERLACED;
648 else lGPUstatusRet&=~(GPUSTATUS_INTERLACED|0x80000000);
651 lGPUstatusRet|=GPUSTATUS_PAL;
652 else lGPUstatusRet&=~GPUSTATUS_PAL;
654 if (PSXDisplay.Double==2)
655 lGPUstatusRet|=GPUSTATUS_DOUBLEHEIGHT;
656 else lGPUstatusRet&=~GPUSTATUS_DOUBLEHEIGHT;
658 if (PSXDisplay.RGB24New)
659 lGPUstatusRet|=GPUSTATUS_RGB24;
660 else lGPUstatusRet&=~GPUSTATUS_RGB24;
662 updateDisplayIfChanged();
665 //--------------------------------------------------//
666 // ask about GPU version and other stuff
674 lGPUdataRet=lGPUInfoVals[INFO_TW]; // tw infos
677 lGPUdataRet=lGPUInfoVals[INFO_DRAWSTART]; // draw start
680 lGPUdataRet=lGPUInfoVals[INFO_DRAWEND]; // draw end
684 lGPUdataRet=lGPUInfoVals[INFO_DRAWOFF]; // draw offset
687 lGPUdataRet=0x02; // gpu type
690 case 0x0F: // some bios addr?
691 lGPUdataRet=0xBFC03720;
695 //--------------------------------------------------//
699 ////////////////////////////////////////////////////////////////////////
700 // vram read/write helpers, needed by LEWPY's optimized vram read/write :)
701 ////////////////////////////////////////////////////////////////////////
703 static inline void FinishedVRAMWrite(void)
705 // Set register to NORMAL operation
706 DataWriteMode = DR_NORMAL;
707 // Reset transfer values, to prevent mis-transfer of data
711 VRAMWrite.Height = 0;
712 VRAMWrite.ColsRemaining = 0;
713 VRAMWrite.RowsRemaining = 0;
716 static inline void FinishedVRAMRead(void)
718 // Set register to NORMAL operation
719 DataReadMode = DR_NORMAL;
720 // Reset transfer values, to prevent mis-transfer of data
725 VRAMRead.ColsRemaining = 0;
726 VRAMRead.RowsRemaining = 0;
728 // Indicate GPU is no longer ready for VRAM data in the STATUS REGISTER
729 lGPUstatusRet&=~GPUSTATUS_READYFORVRAM;
732 ////////////////////////////////////////////////////////////////////////
733 // core read from vram
734 ////////////////////////////////////////////////////////////////////////
736 void CALLBACK GPUreadDataMem(uint32_t * pMem, int iSize)
740 if(DataReadMode!=DR_VRAMTRANSFER) return;
744 // adjust read ptr, if necessary
745 while(VRAMRead.ImagePtr>=psxVuw_eom)
746 VRAMRead.ImagePtr-=512*1024;
747 while(VRAMRead.ImagePtr<psxVuw)
748 VRAMRead.ImagePtr+=512*1024;
752 // do 2 seperate 16bit reads for compatibility (wrap issues)
753 if ((VRAMRead.ColsRemaining > 0) && (VRAMRead.RowsRemaining > 0))
756 lGPUdataRet=(uint32_t)GETLE16(VRAMRead.ImagePtr);
759 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
760 VRAMRead.RowsRemaining --;
762 if(VRAMRead.RowsRemaining<=0)
764 VRAMRead.RowsRemaining = VRAMRead.Width;
765 VRAMRead.ColsRemaining--;
766 VRAMRead.ImagePtr += 1024 - VRAMRead.Width;
767 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
770 // higher 16 bit (always, even if it's an odd width)
771 lGPUdataRet|=(uint32_t)GETLE16(VRAMRead.ImagePtr)<<16;
772 PUTLE32(pMem, lGPUdataRet); pMem++;
774 if(VRAMRead.ColsRemaining <= 0)
775 {FinishedVRAMRead();goto ENDREAD;}
778 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
779 VRAMRead.RowsRemaining--;
780 if(VRAMRead.RowsRemaining<=0)
782 VRAMRead.RowsRemaining = VRAMRead.Width;
783 VRAMRead.ColsRemaining--;
784 VRAMRead.ImagePtr += 1024 - VRAMRead.Width;
785 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
787 if(VRAMRead.ColsRemaining <= 0)
788 {FinishedVRAMRead();goto ENDREAD;}
790 else {FinishedVRAMRead();goto ENDREAD;}
798 ////////////////////////////////////////////////////////////////////////
800 uint32_t CALLBACK GPUreadData(void)
803 GPUreadDataMem(&l,1);
807 // Software drawing function
810 // PSX drawing primitives
813 ////////////////////////////////////////////////////////////////////////
814 // processes data send to GPU data register
815 // extra table entries for fixing polyline troubles
816 ////////////////////////////////////////////////////////////////////////
818 static const unsigned char primTableCX[256] =
839 // 5,5,5,5,6,6,6,6, // FLINE
840 254,254,254,254,254,254,254,254,
844 // 7,7,7,7,9,9,9,9, // GLINE
845 255,255,255,255,255,255,255,255,
849 2,2,2,2,3,3,3,3, // 3=SPRITE1???
888 void CALLBACK GPUwriteDataMem(uint32_t * pMem, int iSize)
890 unsigned char command;
894 GPUIsNotReadyForCommands;
898 if(DataWriteMode==DR_VRAMTRANSFER)
900 BOOL bFinished=FALSE;
902 // make sure we are in vram
903 while(VRAMWrite.ImagePtr>=psxVuw_eom)
904 VRAMWrite.ImagePtr-=512*1024;
905 while(VRAMWrite.ImagePtr<psxVuw)
906 VRAMWrite.ImagePtr+=512*1024;
909 while(VRAMWrite.ColsRemaining>0)
911 while(VRAMWrite.RowsRemaining>0)
913 if(i>=iSize) {goto ENDVRAM;}
916 gdata=GETLE32(pMem); pMem++;
918 PUTLE16(VRAMWrite.ImagePtr, (unsigned short)gdata); VRAMWrite.ImagePtr++;
919 if(VRAMWrite.ImagePtr>=psxVuw_eom) VRAMWrite.ImagePtr-=512*1024;
920 VRAMWrite.RowsRemaining --;
922 if(VRAMWrite.RowsRemaining <= 0)
924 VRAMWrite.ColsRemaining--;
925 if (VRAMWrite.ColsRemaining <= 0) // last pixel is odd width
927 gdata=(gdata&0xFFFF)|(((uint32_t)GETLE16(VRAMWrite.ImagePtr))<<16);
932 VRAMWrite.RowsRemaining = VRAMWrite.Width;
933 VRAMWrite.ImagePtr += 1024 - VRAMWrite.Width;
936 PUTLE16(VRAMWrite.ImagePtr, (unsigned short)(gdata>>16)); VRAMWrite.ImagePtr++;
937 if(VRAMWrite.ImagePtr>=psxVuw_eom) VRAMWrite.ImagePtr-=512*1024;
938 VRAMWrite.RowsRemaining --;
941 VRAMWrite.RowsRemaining = VRAMWrite.Width;
942 VRAMWrite.ColsRemaining--;
943 VRAMWrite.ImagePtr += 1024 - VRAMWrite.Width;
948 if(bFinished) bDoVSyncUpdate=TRUE;
953 if(DataWriteMode==DR_NORMAL)
955 void (* *primFunc)(unsigned char *);
956 if(bSkipNextFrame) primFunc=primTableSkip;
957 else primFunc=primTableJ;
961 if(DataWriteMode==DR_VRAMTRANSFER) goto STARTVRAM;
963 gdata=GETLE32(pMem); pMem++; i++;
967 command = (unsigned char)((gdata>>24) & 0xff);
969 //if(command>=0xb0 && command<0xc0) auxprintf("b0 %x!!!!!!!!!\n",command);
971 if(primTableCX[command])
973 gpuDataC = primTableCX[command];
974 gpuCommand = command;
975 PUTLE32_(&gpuDataM[0], gdata);
982 PUTLE32_(&gpuDataM[gpuDataP], gdata);
985 if((gpuDataC==254 && gpuDataP>=3) ||
986 (gpuDataC==255 && gpuDataP>=4 && !(gpuDataP&1)))
988 if((gpuDataM[gpuDataP] & 0xF000F000) == 0x50005000)
995 if(gpuDataP == gpuDataC)
998 primFunc[gpuCommand]((unsigned char *)gpuDataM);
999 if(dwActFixes&0x0400) // hack for emulating "gpu busy" in some games
1007 GPUIsReadyForCommands;
1011 ////////////////////////////////////////////////////////////////////////
1013 void CALLBACK GPUwriteData(uint32_t gdata)
1015 PUTLE32_(&gdata, gdata);
1016 GPUwriteDataMem(&gdata,1);
1019 ////////////////////////////////////////////////////////////////////////
1020 // process gpu commands
1021 ////////////////////////////////////////////////////////////////////////
1023 unsigned long lUsedAddr[3];
1025 static inline BOOL CheckForEndlessLoop(unsigned long laddr)
1027 if(laddr==lUsedAddr[1]) return TRUE;
1028 if(laddr==lUsedAddr[2]) return TRUE;
1030 if(laddr<lUsedAddr[0]) lUsedAddr[1]=laddr;
1031 else lUsedAddr[2]=laddr;
1036 long CALLBACK GPUdmaChain(uint32_t * baseAddrL, uint32_t addr)
1039 unsigned char * baseAddrB;
1040 short count;unsigned int DMACommandCounter = 0;
1045 lUsedAddr[0]=lUsedAddr[1]=lUsedAddr[2]=0xffffff;
1047 baseAddrB = (unsigned char*) baseAddrL;
1052 if(DMACommandCounter++ > 2000000) break;
1053 if(CheckForEndlessLoop(addr)) break;
1055 count = baseAddrB[addr+3];
1056 dmaWords += 1 + count;
1060 if(count>0) GPUwriteDataMem(&baseAddrL[dmaMem>>2],count);
1062 addr = GETLE32(&baseAddrL[addr>>2])&0xffffff;
1064 while (addr != 0xffffff);
1071 ////////////////////////////////////////////////////////////////////////
1073 ////////////////////////////////////////////////////////////////////////
1075 typedef struct GPUFREEZETAG
1077 uint32_t ulFreezeVersion; // should be always 1 for now (set by main emu)
1078 uint32_t ulStatus; // current gpu status
1079 uint32_t ulControl[256]; // latest control register values
1080 unsigned char psxVRam[1024*1024*2]; // current VRam image (full 2 MB for ZN)
1083 ////////////////////////////////////////////////////////////////////////
1085 long CALLBACK GPUfreeze(uint32_t ulGetFreezeData,GPUFreeze_t * pF)
1087 //----------------------------------------------------//
1088 if(ulGetFreezeData==2) // 2: info, which save slot is selected? (just for display)
1090 long lSlotNum=*((long *)pF);
1091 if(lSlotNum<0) return 0;
1092 if(lSlotNum>8) return 0;
1093 lSelectedSlot=lSlotNum+1;
1096 //----------------------------------------------------//
1097 if(!pF) return 0; // some checks
1098 if(pF->ulFreezeVersion!=1) return 0;
1100 if(ulGetFreezeData==1) // 1: get data
1102 pF->ulStatus=lGPUstatusRet;
1103 memcpy(pF->ulControl,ulStatusControl,256*sizeof(uint32_t));
1104 memcpy(pF->psxVRam, psxVub, 1024*512*2);
1109 if(ulGetFreezeData!=0) return 0; // 0: set data
1111 lGPUstatusRet=pF->ulStatus;
1112 memcpy(ulStatusControl,pF->ulControl,256*sizeof(uint32_t));
1113 memcpy(psxVub, pF->psxVRam, 1024*512*2);
1115 // RESET TEXTURE STORE HERE, IF YOU USE SOMETHING LIKE THAT
1117 PreviousPSXDisplay.Height = 0;
1118 GPUwriteStatus(ulStatusControl[0]);
1119 GPUwriteStatus(ulStatusControl[1]);
1120 GPUwriteStatus(ulStatusControl[2]);
1121 GPUwriteStatus(ulStatusControl[3]);
1122 GPUwriteStatus(ulStatusControl[8]); // try to repair things
1123 GPUwriteStatus(ulStatusControl[6]);
1124 GPUwriteStatus(ulStatusControl[7]);
1125 GPUwriteStatus(ulStatusControl[5]);
1126 GPUwriteStatus(ulStatusControl[4]);
1132 #include "../../frontend/plugin_lib.h"
1134 const struct rearmed_cbs *rcbs;
1136 void GPUrearmedCallbacks(const struct rearmed_cbs *cbs)
1139 UseFrameSkip = cbs->frameskip;
1140 iUseDither = cbs->gpu_peops.iUseDither;
1141 dwActFixes = cbs->gpu_peops.dwActFixes;
1142 fFrameRateHz = cbs->gpu_peops.fFrameRateHz;
1143 dwFrameRateTicks = cbs->gpu_peops.dwFrameRateTicks;
1144 if (cbs->pl_vout_set_raw_vram)
1145 cbs->pl_vout_set_raw_vram(psxVub);
1146 if (cbs->pl_set_gpu_caps)
1147 cbs->pl_set_gpu_caps(0);
1149 skip_advice = &cbs->fskip_advice;