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 *psxVSecure;
27 unsigned char *psxVub;
29 unsigned short *psxVuw;
30 unsigned short *psxVuw_eom;
35 ////////////////////////////////////////////////////////////////////////
37 ////////////////////////////////////////////////////////////////////////
39 static long lGPUdataRet;
41 uint32_t ulStatusControl[256];
43 static uint32_t gpuDataM[256];
44 static unsigned char gpuCommand = 0;
45 static long gpuDataC = 0;
46 static long gpuDataP = 0;
50 DATAREGISTERMODES DataWriteMode;
51 DATAREGISTERMODES DataReadMode;
53 BOOL bSkipNextFrame = FALSE;
55 short sDispWidths[8] = {256,320,512,640,368,384,512,640};
56 PSXDisplay_t PSXDisplay;
57 PSXDisplay_t PreviousPSXDisplay;
59 BOOL bDoLazyUpdate=FALSE;
60 uint32_t lGPUInfoVals[16];
61 static int iFakePrimBusy=0;
63 ////////////////////////////////////////////////////////////////////////
64 // some misc external display funcs
65 ////////////////////////////////////////////////////////////////////////
73 ////////////////////////////////////////////////////////////////////////
74 // sets all kind of act fixes
75 ////////////////////////////////////////////////////////////////////////
77 static void SetFixes(void)
79 if(dwActFixes&0x02) sDispWidths[4]=384;
80 else sDispWidths[4]=368;
83 ////////////////////////////////////////////////////////////////////////
84 // INIT, will be called after lib load... well, just do some var init...
85 ////////////////////////////////////////////////////////////////////////
87 long CALLBACK GPUinit(void) // GPU INIT
89 memset(ulStatusControl,0,256*sizeof(uint32_t)); // init save state scontrol field
91 psxVSecure = (unsigned char *)malloc((512*2)*1024 + (1024*1024)); // always alloc one extra MB for soft drawing funcs security
96 psxVub=psxVSecure + 512 * 1024; // security offset into double sized psx vram!
98 psxVsb=(signed char *)psxVub; // different ways of accessing PSX VRAM
99 psxVsw=(signed short *)psxVub;
100 psxVsl=(int32_t *)psxVub;
101 psxVuw=(unsigned short *)psxVub;
102 psxVul=(uint32_t *)psxVub;
104 psxVuw_eom=psxVuw+1024*512; // pre-calc of end of vram
106 memset(psxVSecure,0x00,(512*2)*1024 + (1024*1024));
107 memset(lGPUInfoVals,0x00,16*sizeof(uint32_t));
109 PSXDisplay.RGB24 = FALSE; // init some stuff
110 PSXDisplay.Interlaced = FALSE;
111 PSXDisplay.DrawOffset.x = 0;
112 PSXDisplay.DrawOffset.y = 0;
113 PSXDisplay.DisplayMode.x= 320;
114 PSXDisplay.DisplayMode.y= 240;
115 PreviousPSXDisplay.DisplayMode.x= 320;
116 PreviousPSXDisplay.DisplayMode.y= 240;
117 PSXDisplay.Disabled = FALSE;
118 PreviousPSXDisplay.Range.x0 =0;
119 PreviousPSXDisplay.Range.y0 =0;
120 PSXDisplay.Range.x0=0;
121 PSXDisplay.Range.x1=0;
122 PreviousPSXDisplay.DisplayModeNew.y=0;
123 PSXDisplay.Double = 1;
126 DataWriteMode = DR_NORMAL;
128 // Reset transfer values, to prevent mis-transfer of data
129 memset(&VRAMWrite, 0, sizeof(VRAMLoad_t));
130 memset(&VRAMRead, 0, sizeof(VRAMLoad_t));
132 // device initialised already !
133 lGPUstatusRet = 0x14802000;
135 GPUIsReadyForCommands;
136 bDoVSyncUpdate = TRUE;
141 ////////////////////////////////////////////////////////////////////////
142 // Here starts all...
143 ////////////////////////////////////////////////////////////////////////
146 long GPUopen(unsigned long * disp,char * CapText,char * CfgFile)
154 bDoVSyncUpdate = TRUE;
156 d=ulInitDisplay(); // setup x
159 *disp=d; // wanna x pointer? ok
166 ////////////////////////////////////////////////////////////////////////
168 ////////////////////////////////////////////////////////////////////////
170 long CALLBACK GPUclose() // GPU CLOSE
172 CloseDisplay(); // shutdown direct draw
177 ////////////////////////////////////////////////////////////////////////
178 // I shot the sheriff
179 ////////////////////////////////////////////////////////////////////////
181 long CALLBACK GPUshutdown(void) // GPU SHUTDOWN
183 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 ?
207 if(!bSkipNextFrame) DoBufferSwap(); // -> to skip or not to skip
208 if(dwActFixes&0xa0) // -> pc fps calculation fix/old skipping fix
210 if((fps_skip < fFrameRateHz) && !(bSkipNextFrame)) // -> skip max one in a row
211 {bSkipNextFrame = TRUE; fps_skip=fFrameRateHz;}
212 else bSkipNextFrame = FALSE;
218 bSkipNextFrame = FALSE;
219 DoBufferSwap(); // -> swap
223 ////////////////////////////////////////////////////////////////////////
224 // roughly emulated screen centering bits... not complete !!!
225 ////////////////////////////////////////////////////////////////////////
227 void ChangeDispOffsetsX(void) // X CENTER
231 if(!PSXDisplay.Range.x1) return;
233 l=PreviousPSXDisplay.DisplayMode.x;
235 l*=(long)PSXDisplay.Range.x1;
236 l/=2560;lx=l;l&=0xfffffff8;
238 if(l==PreviousPSXDisplay.Range.y1) return; // abusing range.y1 for
239 PreviousPSXDisplay.Range.y1=(short)l; // storing last x range and test
241 if(lx>=PreviousPSXDisplay.DisplayMode.x)
243 PreviousPSXDisplay.Range.x1=
244 (short)PreviousPSXDisplay.DisplayMode.x;
245 PreviousPSXDisplay.Range.x0=0;
249 PreviousPSXDisplay.Range.x1=(short)l;
251 PreviousPSXDisplay.Range.x0=
252 (PSXDisplay.Range.x0-500)/8;
254 if(PreviousPSXDisplay.Range.x0<0)
255 PreviousPSXDisplay.Range.x0=0;
257 if((PreviousPSXDisplay.Range.x0+lx)>
258 PreviousPSXDisplay.DisplayMode.x)
260 PreviousPSXDisplay.Range.x0=
261 (short)(PreviousPSXDisplay.DisplayMode.x-lx);
262 PreviousPSXDisplay.Range.x0+=2; //???
264 PreviousPSXDisplay.Range.x1+=(short)(lx-l);
266 PreviousPSXDisplay.Range.x1-=2; // makes linux stretching easier
270 // some linux alignment security
271 PreviousPSXDisplay.Range.x0=PreviousPSXDisplay.Range.x0>>1;
272 PreviousPSXDisplay.Range.x0=PreviousPSXDisplay.Range.x0<<1;
273 PreviousPSXDisplay.Range.x1=PreviousPSXDisplay.Range.x1>>1;
274 PreviousPSXDisplay.Range.x1=PreviousPSXDisplay.Range.x1<<1;
276 DoClearScreenBuffer();
282 ////////////////////////////////////////////////////////////////////////
284 void ChangeDispOffsetsY(void) // Y CENTER
286 int iT,iO=PreviousPSXDisplay.Range.y0;
287 int iOldYOffset=PreviousPSXDisplay.DisplayModeNew.y;
291 if((PreviousPSXDisplay.DisplayModeNew.x+PSXDisplay.DisplayModeNew.y)>512)
293 int dy1=512-PreviousPSXDisplay.DisplayModeNew.x;
294 int dy2=(PreviousPSXDisplay.DisplayModeNew.x+PSXDisplay.DisplayModeNew.y)-512;
298 PreviousPSXDisplay.DisplayModeNew.y=-dy2;
302 PSXDisplay.DisplayPosition.y=0;
303 PreviousPSXDisplay.DisplayModeNew.y=-dy1;
306 else PreviousPSXDisplay.DisplayModeNew.y=0;
310 if(PreviousPSXDisplay.DisplayModeNew.y!=iOldYOffset) // if old offset!=new offset: recalc height
312 PSXDisplay.Height = PSXDisplay.Range.y1 -
313 PSXDisplay.Range.y0 +
314 PreviousPSXDisplay.DisplayModeNew.y;
315 PSXDisplay.DisplayModeNew.y=PSXDisplay.Height*PSXDisplay.Double;
320 if(PSXDisplay.PAL) iT=48; else iT=28;
322 if(PSXDisplay.Range.y0>=iT)
324 PreviousPSXDisplay.Range.y0=
325 (short)((PSXDisplay.Range.y0-iT-4)*PSXDisplay.Double);
326 if(PreviousPSXDisplay.Range.y0<0)
327 PreviousPSXDisplay.Range.y0=0;
328 PSXDisplay.DisplayModeNew.y+=
329 PreviousPSXDisplay.Range.y0;
332 PreviousPSXDisplay.Range.y0=0;
334 if(iO!=PreviousPSXDisplay.Range.y0)
336 DoClearScreenBuffer();
340 ////////////////////////////////////////////////////////////////////////
341 // check if update needed
342 ////////////////////////////////////////////////////////////////////////
344 static void updateDisplayIfChanged(void) // UPDATE DISPLAY IF CHANGED
346 if ((PSXDisplay.DisplayMode.y == PSXDisplay.DisplayModeNew.y) &&
347 (PSXDisplay.DisplayMode.x == PSXDisplay.DisplayModeNew.x))
349 if((PSXDisplay.RGB24 == PSXDisplay.RGB24New) &&
350 (PSXDisplay.Interlaced == PSXDisplay.InterlacedNew)) return;
353 PSXDisplay.RGB24 = PSXDisplay.RGB24New; // get new infos
355 PSXDisplay.DisplayMode.y = PSXDisplay.DisplayModeNew.y;
356 PSXDisplay.DisplayMode.x = PSXDisplay.DisplayModeNew.x;
357 PreviousPSXDisplay.DisplayMode.x= // previous will hold
358 min(640,PSXDisplay.DisplayMode.x); // max 640x512... that's
359 PreviousPSXDisplay.DisplayMode.y= // the size of my
360 min(512,PSXDisplay.DisplayMode.y); // back buffer surface
361 PSXDisplay.Interlaced = PSXDisplay.InterlacedNew;
363 PSXDisplay.DisplayEnd.x= // calc end of display
364 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
365 PSXDisplay.DisplayEnd.y=
366 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
367 PreviousPSXDisplay.DisplayEnd.x=
368 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
369 PreviousPSXDisplay.DisplayEnd.y=
370 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
372 ChangeDispOffsetsX();
374 if(iFrameLimit==2) SetAutoFrameCap(); // -> set it
376 if(UseFrameSkip) updateDisplay(); // stupid stuff when frame skipping enabled
379 ////////////////////////////////////////////////////////////////////////
380 // update lace is called evry VSync
381 ////////////////////////////////////////////////////////////////////////
383 void CALLBACK GPUupdateLace(void) // VSYNC
386 lGPUstatusRet^=0x80000000; // odd/even bit
388 //pcsx-rearmed: removed, this is handled by core
389 //if(!(dwActFixes&32)) // std fps limitation?
392 if(PSXDisplay.Interlaced) // interlaced mode?
394 if(bDoVSyncUpdate && PSXDisplay.DisplayMode.x>0 && PSXDisplay.DisplayMode.y>0)
399 else // non-interlaced?
401 if(dwActFixes&64) // lazy screen update fix
403 if(bDoLazyUpdate && !UseFrameSkip)
409 if(bDoVSyncUpdate && !UseFrameSkip) // some primitives drawn?
410 updateDisplay(); // -> update display
413 bDoVSyncUpdate=FALSE; // vsync done
416 ////////////////////////////////////////////////////////////////////////
417 // process read request from GPU status register
418 ////////////////////////////////////////////////////////////////////////
421 uint32_t CALLBACK GPUreadStatus(void) // READ STATUS
425 static int iNumRead=0; // odd/even hack
429 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)
433 if(iFakePrimBusy) // 27.10.2007 - PETE : emulating some 'busy' while drawing... pfff
437 if(iFakePrimBusy&1) // we do a busy-idle-busy-idle sequence after/while drawing prims
440 GPUIsNotReadyForCommands;
445 GPUIsReadyForCommands;
448 return lGPUstatusRet;
451 ////////////////////////////////////////////////////////////////////////
452 // processes data send to GPU status register
453 // these are always single packet commands.
454 ////////////////////////////////////////////////////////////////////////
456 void CALLBACK GPUwriteStatus(uint32_t gdata) // WRITE STATUS
458 uint32_t lCommand=(gdata>>24)&0xff;
460 ulStatusControl[lCommand]=gdata; // store command for freezing
464 //--------------------------------------------------//
467 memset(lGPUInfoVals,0x00,16*sizeof(uint32_t));
468 lGPUstatusRet=0x14802000;
469 PSXDisplay.Disabled=1;
470 DataWriteMode=DataReadMode=DR_NORMAL;
471 PSXDisplay.DrawOffset.x=PSXDisplay.DrawOffset.y=0;
472 drawX=drawY=0;drawW=drawH=0;
473 sSetMask=0;lSetMask=0;bCheckMask=FALSE;
475 GlobalTextAddrX=0;GlobalTextAddrY=0;
476 GlobalTextTP=0;GlobalTextABR=0;
477 PSXDisplay.RGB24=FALSE;
478 PSXDisplay.Interlaced=FALSE;
481 //--------------------------------------------------//
482 // dis/enable display
485 PreviousPSXDisplay.Disabled = PSXDisplay.Disabled;
486 PSXDisplay.Disabled = (gdata & 1);
488 if(PSXDisplay.Disabled)
489 lGPUstatusRet|=GPUSTATUS_DISPLAYDISABLED;
490 else lGPUstatusRet&=~GPUSTATUS_DISPLAYDISABLED;
493 //--------------------------------------------------//
494 // setting transfer mode
496 gdata &= 0x03; // Only want the lower two bits
498 DataWriteMode=DataReadMode=DR_NORMAL;
499 if(gdata==0x02) DataWriteMode=DR_VRAMTRANSFER;
500 if(gdata==0x03) DataReadMode =DR_VRAMTRANSFER;
501 lGPUstatusRet&=~GPUSTATUS_DMABITS; // Clear the current settings of the DMA bits
502 lGPUstatusRet|=(gdata << 29); // Set the DMA bits according to the received data
505 //--------------------------------------------------//
506 // setting display position
509 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
510 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
513 PSXDisplay.DisplayPosition.y = (short)((gdata>>10)&0x1ff);
515 // store the same val in some helper var, we need it on later compares
516 PreviousPSXDisplay.DisplayModeNew.x=PSXDisplay.DisplayPosition.y;
518 if((PSXDisplay.DisplayPosition.y+PSXDisplay.DisplayMode.y)>512)
520 int dy1=512-PSXDisplay.DisplayPosition.y;
521 int dy2=(PSXDisplay.DisplayPosition.y+PSXDisplay.DisplayMode.y)-512;
525 PreviousPSXDisplay.DisplayModeNew.y=-dy2;
529 PSXDisplay.DisplayPosition.y=0;
530 PreviousPSXDisplay.DisplayModeNew.y=-dy1;
533 else PreviousPSXDisplay.DisplayModeNew.y=0;
536 PSXDisplay.DisplayPosition.x = (short)(gdata & 0x3ff);
537 PSXDisplay.DisplayEnd.x=
538 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
539 PSXDisplay.DisplayEnd.y=
540 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y + PreviousPSXDisplay.DisplayModeNew.y;
541 PreviousPSXDisplay.DisplayEnd.x=
542 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
543 PreviousPSXDisplay.DisplayEnd.y=
544 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y + PreviousPSXDisplay.DisplayModeNew.y;
548 if (!(PSXDisplay.Interlaced)) // stupid frame skipping option
550 if(UseFrameSkip) updateDisplay();
551 if(dwActFixes&64) bDoLazyUpdate=TRUE;
554 //--------------------------------------------------//
558 PSXDisplay.Range.x0=(short)(gdata & 0x7ff);
559 PSXDisplay.Range.x1=(short)((gdata>>12) & 0xfff);
561 PSXDisplay.Range.x1-=PSXDisplay.Range.x0;
563 ChangeDispOffsetsX();
566 //--------------------------------------------------//
571 PSXDisplay.Range.y0=(short)(gdata & 0x3ff);
572 PSXDisplay.Range.y1=(short)((gdata>>10) & 0x3ff);
574 PreviousPSXDisplay.Height = PSXDisplay.Height;
576 PSXDisplay.Height = PSXDisplay.Range.y1 -
577 PSXDisplay.Range.y0 +
578 PreviousPSXDisplay.DisplayModeNew.y;
580 if(PreviousPSXDisplay.Height!=PSXDisplay.Height)
582 PSXDisplay.DisplayModeNew.y=PSXDisplay.Height*PSXDisplay.Double;
584 ChangeDispOffsetsY();
586 updateDisplayIfChanged();
590 //--------------------------------------------------//
591 // setting display infos
594 PSXDisplay.DisplayModeNew.x =
595 sDispWidths[(gdata & 0x03) | ((gdata & 0x40) >> 4)];
597 if (gdata&0x04) PSXDisplay.Double=2;
598 else PSXDisplay.Double=1;
600 PSXDisplay.DisplayModeNew.y = PSXDisplay.Height*PSXDisplay.Double;
602 ChangeDispOffsetsY();
604 PSXDisplay.PAL = (gdata & 0x08)?TRUE:FALSE; // if 1 - PAL mode, else NTSC
605 PSXDisplay.RGB24New = (gdata & 0x10)?TRUE:FALSE; // if 1 - TrueColor
606 PSXDisplay.InterlacedNew = (gdata & 0x20)?TRUE:FALSE; // if 1 - Interlace
608 lGPUstatusRet&=~GPUSTATUS_WIDTHBITS; // Clear the width bits
610 (((gdata & 0x03) << 17) |
611 ((gdata & 0x40) << 10)); // Set the width bits
613 if(PSXDisplay.InterlacedNew)
615 if(!PSXDisplay.Interlaced)
617 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
618 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
620 lGPUstatusRet|=GPUSTATUS_INTERLACED;
622 else lGPUstatusRet&=~GPUSTATUS_INTERLACED;
625 lGPUstatusRet|=GPUSTATUS_PAL;
626 else lGPUstatusRet&=~GPUSTATUS_PAL;
628 if (PSXDisplay.Double==2)
629 lGPUstatusRet|=GPUSTATUS_DOUBLEHEIGHT;
630 else lGPUstatusRet&=~GPUSTATUS_DOUBLEHEIGHT;
632 if (PSXDisplay.RGB24New)
633 lGPUstatusRet|=GPUSTATUS_RGB24;
634 else lGPUstatusRet&=~GPUSTATUS_RGB24;
636 updateDisplayIfChanged();
639 //--------------------------------------------------//
640 // ask about GPU version and other stuff
648 lGPUdataRet=lGPUInfoVals[INFO_TW]; // tw infos
651 lGPUdataRet=lGPUInfoVals[INFO_DRAWSTART]; // draw start
654 lGPUdataRet=lGPUInfoVals[INFO_DRAWEND]; // draw end
658 lGPUdataRet=lGPUInfoVals[INFO_DRAWOFF]; // draw offset
661 lGPUdataRet=0x02; // gpu type
664 case 0x0F: // some bios addr?
665 lGPUdataRet=0xBFC03720;
669 //--------------------------------------------------//
673 ////////////////////////////////////////////////////////////////////////
674 // vram read/write helpers, needed by LEWPY's optimized vram read/write :)
675 ////////////////////////////////////////////////////////////////////////
677 static inline void FinishedVRAMWrite(void)
679 // Set register to NORMAL operation
680 DataWriteMode = DR_NORMAL;
681 // Reset transfer values, to prevent mis-transfer of data
685 VRAMWrite.Height = 0;
686 VRAMWrite.ColsRemaining = 0;
687 VRAMWrite.RowsRemaining = 0;
690 static inline void FinishedVRAMRead(void)
692 // Set register to NORMAL operation
693 DataReadMode = DR_NORMAL;
694 // Reset transfer values, to prevent mis-transfer of data
699 VRAMRead.ColsRemaining = 0;
700 VRAMRead.RowsRemaining = 0;
702 // Indicate GPU is no longer ready for VRAM data in the STATUS REGISTER
703 lGPUstatusRet&=~GPUSTATUS_READYFORVRAM;
706 ////////////////////////////////////////////////////////////////////////
707 // core read from vram
708 ////////////////////////////////////////////////////////////////////////
710 void CALLBACK GPUreadDataMem(uint32_t * pMem, int iSize)
714 if(DataReadMode!=DR_VRAMTRANSFER) return;
718 // adjust read ptr, if necessary
719 while(VRAMRead.ImagePtr>=psxVuw_eom)
720 VRAMRead.ImagePtr-=512*1024;
721 while(VRAMRead.ImagePtr<psxVuw)
722 VRAMRead.ImagePtr+=512*1024;
726 // do 2 seperate 16bit reads for compatibility (wrap issues)
727 if ((VRAMRead.ColsRemaining > 0) && (VRAMRead.RowsRemaining > 0))
730 lGPUdataRet=(uint32_t)GETLE16(VRAMRead.ImagePtr);
733 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
734 VRAMRead.RowsRemaining --;
736 if(VRAMRead.RowsRemaining<=0)
738 VRAMRead.RowsRemaining = VRAMRead.Width;
739 VRAMRead.ColsRemaining--;
740 VRAMRead.ImagePtr += 1024 - VRAMRead.Width;
741 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
744 // higher 16 bit (always, even if it's an odd width)
745 lGPUdataRet|=(uint32_t)GETLE16(VRAMRead.ImagePtr)<<16;
746 PUTLE32(pMem, lGPUdataRet); pMem++;
748 if(VRAMRead.ColsRemaining <= 0)
749 {FinishedVRAMRead();goto ENDREAD;}
752 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
753 VRAMRead.RowsRemaining--;
754 if(VRAMRead.RowsRemaining<=0)
756 VRAMRead.RowsRemaining = VRAMRead.Width;
757 VRAMRead.ColsRemaining--;
758 VRAMRead.ImagePtr += 1024 - VRAMRead.Width;
759 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
761 if(VRAMRead.ColsRemaining <= 0)
762 {FinishedVRAMRead();goto ENDREAD;}
764 else {FinishedVRAMRead();goto ENDREAD;}
772 ////////////////////////////////////////////////////////////////////////
774 uint32_t CALLBACK GPUreadData(void)
777 GPUreadDataMem(&l,1);
781 // Software drawing function
784 // PSX drawing primitives
787 ////////////////////////////////////////////////////////////////////////
788 // processes data send to GPU data register
789 // extra table entries for fixing polyline troubles
790 ////////////////////////////////////////////////////////////////////////
792 static const unsigned char primTableCX[256] =
813 // 5,5,5,5,6,6,6,6, // FLINE
814 254,254,254,254,254,254,254,254,
818 // 7,7,7,7,9,9,9,9, // GLINE
819 255,255,255,255,255,255,255,255,
823 2,2,2,2,3,3,3,3, // 3=SPRITE1???
862 void CALLBACK GPUwriteDataMem(uint32_t * pMem, int iSize)
864 unsigned char command;
868 GPUIsNotReadyForCommands;
872 if(DataWriteMode==DR_VRAMTRANSFER)
874 BOOL bFinished=FALSE;
876 // make sure we are in vram
877 while(VRAMWrite.ImagePtr>=psxVuw_eom)
878 VRAMWrite.ImagePtr-=512*1024;
879 while(VRAMWrite.ImagePtr<psxVuw)
880 VRAMWrite.ImagePtr+=512*1024;
883 while(VRAMWrite.ColsRemaining>0)
885 while(VRAMWrite.RowsRemaining>0)
887 if(i>=iSize) {goto ENDVRAM;}
890 gdata=GETLE32(pMem); pMem++;
892 PUTLE16(VRAMWrite.ImagePtr, (unsigned short)gdata); VRAMWrite.ImagePtr++;
893 if(VRAMWrite.ImagePtr>=psxVuw_eom) VRAMWrite.ImagePtr-=512*1024;
894 VRAMWrite.RowsRemaining --;
896 if(VRAMWrite.RowsRemaining <= 0)
898 VRAMWrite.ColsRemaining--;
899 if (VRAMWrite.ColsRemaining <= 0) // last pixel is odd width
901 gdata=(gdata&0xFFFF)|(((uint32_t)GETLE16(VRAMWrite.ImagePtr))<<16);
906 VRAMWrite.RowsRemaining = VRAMWrite.Width;
907 VRAMWrite.ImagePtr += 1024 - VRAMWrite.Width;
910 PUTLE16(VRAMWrite.ImagePtr, (unsigned short)(gdata>>16)); VRAMWrite.ImagePtr++;
911 if(VRAMWrite.ImagePtr>=psxVuw_eom) VRAMWrite.ImagePtr-=512*1024;
912 VRAMWrite.RowsRemaining --;
915 VRAMWrite.RowsRemaining = VRAMWrite.Width;
916 VRAMWrite.ColsRemaining--;
917 VRAMWrite.ImagePtr += 1024 - VRAMWrite.Width;
922 if(bFinished) bDoVSyncUpdate=TRUE;
927 if(DataWriteMode==DR_NORMAL)
929 void (* *primFunc)(unsigned char *);
930 if(bSkipNextFrame) primFunc=primTableSkip;
931 else primFunc=primTableJ;
935 if(DataWriteMode==DR_VRAMTRANSFER) goto STARTVRAM;
937 gdata=GETLE32(pMem); pMem++; i++;
941 command = (unsigned char)((gdata>>24) & 0xff);
943 //if(command>=0xb0 && command<0xc0) auxprintf("b0 %x!!!!!!!!!\n",command);
945 if(primTableCX[command])
947 gpuDataC = primTableCX[command];
948 gpuCommand = command;
949 PUTLE32_(&gpuDataM[0], gdata);
956 PUTLE32_(&gpuDataM[gpuDataP], gdata);
959 if((gpuDataC==254 && gpuDataP>=3) ||
960 (gpuDataC==255 && gpuDataP>=4 && !(gpuDataP&1)))
962 if((gpuDataM[gpuDataP] & 0xF000F000) == 0x50005000)
969 if(gpuDataP == gpuDataC)
972 primFunc[gpuCommand]((unsigned char *)gpuDataM);
973 if(dwActFixes&0x0400) // hack for emulating "gpu busy" in some games
981 GPUIsReadyForCommands;
985 ////////////////////////////////////////////////////////////////////////
987 void CALLBACK GPUwriteData(uint32_t gdata)
989 PUTLE32_(&gdata, gdata);
990 GPUwriteDataMem(&gdata,1);
993 ////////////////////////////////////////////////////////////////////////
994 // process gpu commands
995 ////////////////////////////////////////////////////////////////////////
997 unsigned long lUsedAddr[3];
999 static inline BOOL CheckForEndlessLoop(unsigned long laddr)
1001 if(laddr==lUsedAddr[1]) return TRUE;
1002 if(laddr==lUsedAddr[2]) return TRUE;
1004 if(laddr<lUsedAddr[0]) lUsedAddr[1]=laddr;
1005 else lUsedAddr[2]=laddr;
1010 long CALLBACK GPUdmaChain(uint32_t * baseAddrL, uint32_t addr)
1013 unsigned char * baseAddrB;
1014 short count;unsigned int DMACommandCounter = 0;
1018 lUsedAddr[0]=lUsedAddr[1]=lUsedAddr[2]=0xffffff;
1020 baseAddrB = (unsigned char*) baseAddrL;
1025 if(DMACommandCounter++ > 2000000) break;
1026 if(CheckForEndlessLoop(addr)) break;
1028 count = baseAddrB[addr+3];
1032 if(count>0) GPUwriteDataMem(&baseAddrL[dmaMem>>2],count);
1034 addr = GETLE32(&baseAddrL[addr>>2])&0xffffff;
1036 while (addr != 0xffffff);
1043 ////////////////////////////////////////////////////////////////////////
1045 ////////////////////////////////////////////////////////////////////////
1047 typedef struct GPUFREEZETAG
1049 uint32_t ulFreezeVersion; // should be always 1 for now (set by main emu)
1050 uint32_t ulStatus; // current gpu status
1051 uint32_t ulControl[256]; // latest control register values
1052 unsigned char psxVRam[1024*1024*2]; // current VRam image (full 2 MB for ZN)
1055 ////////////////////////////////////////////////////////////////////////
1057 long CALLBACK GPUfreeze(uint32_t ulGetFreezeData,GPUFreeze_t * pF)
1059 //----------------------------------------------------//
1060 if(ulGetFreezeData==2) // 2: info, which save slot is selected? (just for display)
1062 long lSlotNum=*((long *)pF);
1063 if(lSlotNum<0) return 0;
1064 if(lSlotNum>8) return 0;
1065 lSelectedSlot=lSlotNum+1;
1068 //----------------------------------------------------//
1069 if(!pF) return 0; // some checks
1070 if(pF->ulFreezeVersion!=1) return 0;
1072 if(ulGetFreezeData==1) // 1: get data
1074 pF->ulStatus=lGPUstatusRet;
1075 memcpy(pF->ulControl,ulStatusControl,256*sizeof(uint32_t));
1076 memcpy(pF->psxVRam, psxVub, 1024*512*2);
1081 if(ulGetFreezeData!=0) return 0; // 0: set data
1083 lGPUstatusRet=pF->ulStatus;
1084 memcpy(ulStatusControl,pF->ulControl,256*sizeof(uint32_t));
1085 memcpy(psxVub, pF->psxVRam, 1024*512*2);
1087 // RESET TEXTURE STORE HERE, IF YOU USE SOMETHING LIKE THAT
1089 GPUwriteStatus(ulStatusControl[0]);
1090 GPUwriteStatus(ulStatusControl[1]);
1091 GPUwriteStatus(ulStatusControl[2]);
1092 GPUwriteStatus(ulStatusControl[3]);
1093 GPUwriteStatus(ulStatusControl[8]); // try to repair things
1094 GPUwriteStatus(ulStatusControl[6]);
1095 GPUwriteStatus(ulStatusControl[7]);
1096 GPUwriteStatus(ulStatusControl[5]);
1097 GPUwriteStatus(ulStatusControl[4]);