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
222 bDoVSyncUpdate=FALSE; // vsync done
225 ////////////////////////////////////////////////////////////////////////
226 // roughly emulated screen centering bits... not complete !!!
227 ////////////////////////////////////////////////////////////////////////
229 void ChangeDispOffsetsX(void) // X CENTER
233 if(!PSXDisplay.Range.x1) return;
235 l=PreviousPSXDisplay.DisplayMode.x;
237 l*=(long)PSXDisplay.Range.x1;
238 l/=2560;lx=l;l&=0xfffffff8;
240 if(l==PreviousPSXDisplay.Range.y1) return; // abusing range.y1 for
241 PreviousPSXDisplay.Range.y1=(short)l; // storing last x range and test
243 if(lx>=PreviousPSXDisplay.DisplayMode.x)
245 PreviousPSXDisplay.Range.x1=
246 (short)PreviousPSXDisplay.DisplayMode.x;
247 PreviousPSXDisplay.Range.x0=0;
251 PreviousPSXDisplay.Range.x1=(short)l;
253 PreviousPSXDisplay.Range.x0=
254 (PSXDisplay.Range.x0-500)/8;
256 if(PreviousPSXDisplay.Range.x0<0)
257 PreviousPSXDisplay.Range.x0=0;
259 if((PreviousPSXDisplay.Range.x0+lx)>
260 PreviousPSXDisplay.DisplayMode.x)
262 PreviousPSXDisplay.Range.x0=
263 (short)(PreviousPSXDisplay.DisplayMode.x-lx);
264 PreviousPSXDisplay.Range.x0+=2; //???
266 PreviousPSXDisplay.Range.x1+=(short)(lx-l);
268 PreviousPSXDisplay.Range.x1-=2; // makes linux stretching easier
272 // some linux alignment security
273 PreviousPSXDisplay.Range.x0=PreviousPSXDisplay.Range.x0>>1;
274 PreviousPSXDisplay.Range.x0=PreviousPSXDisplay.Range.x0<<1;
275 PreviousPSXDisplay.Range.x1=PreviousPSXDisplay.Range.x1>>1;
276 PreviousPSXDisplay.Range.x1=PreviousPSXDisplay.Range.x1<<1;
278 DoClearScreenBuffer();
284 ////////////////////////////////////////////////////////////////////////
286 void ChangeDispOffsetsY(void) // Y CENTER
288 int iT,iO=PreviousPSXDisplay.Range.y0;
289 int iOldYOffset=PreviousPSXDisplay.DisplayModeNew.y;
293 if((PreviousPSXDisplay.DisplayModeNew.x+PSXDisplay.DisplayModeNew.y)>512)
295 int dy1=512-PreviousPSXDisplay.DisplayModeNew.x;
296 int dy2=(PreviousPSXDisplay.DisplayModeNew.x+PSXDisplay.DisplayModeNew.y)-512;
300 PreviousPSXDisplay.DisplayModeNew.y=-dy2;
304 PSXDisplay.DisplayPosition.y=0;
305 PreviousPSXDisplay.DisplayModeNew.y=-dy1;
308 else PreviousPSXDisplay.DisplayModeNew.y=0;
312 if(PreviousPSXDisplay.DisplayModeNew.y!=iOldYOffset) // if old offset!=new offset: recalc height
314 PSXDisplay.Height = PSXDisplay.Range.y1 -
315 PSXDisplay.Range.y0 +
316 PreviousPSXDisplay.DisplayModeNew.y;
317 PSXDisplay.DisplayModeNew.y=PSXDisplay.Height*PSXDisplay.Double;
322 if(PSXDisplay.PAL) iT=48; else iT=28;
324 if(PSXDisplay.Range.y0>=iT)
326 PreviousPSXDisplay.Range.y0=
327 (short)((PSXDisplay.Range.y0-iT-4)*PSXDisplay.Double);
328 if(PreviousPSXDisplay.Range.y0<0)
329 PreviousPSXDisplay.Range.y0=0;
330 PSXDisplay.DisplayModeNew.y+=
331 PreviousPSXDisplay.Range.y0;
334 PreviousPSXDisplay.Range.y0=0;
336 if(iO!=PreviousPSXDisplay.Range.y0)
338 DoClearScreenBuffer();
342 ////////////////////////////////////////////////////////////////////////
343 // check if update needed
344 ////////////////////////////////////////////////////////////////////////
346 static void updateDisplayIfChanged(void) // UPDATE DISPLAY IF CHANGED
348 if ((PSXDisplay.DisplayMode.y == PSXDisplay.DisplayModeNew.y) &&
349 (PSXDisplay.DisplayMode.x == PSXDisplay.DisplayModeNew.x))
351 if((PSXDisplay.RGB24 == PSXDisplay.RGB24New) &&
352 (PSXDisplay.Interlaced == PSXDisplay.InterlacedNew)) return;
355 PSXDisplay.RGB24 = PSXDisplay.RGB24New; // get new infos
357 PSXDisplay.DisplayMode.y = PSXDisplay.DisplayModeNew.y;
358 PSXDisplay.DisplayMode.x = PSXDisplay.DisplayModeNew.x;
359 PreviousPSXDisplay.DisplayMode.x= // previous will hold
360 min(640,PSXDisplay.DisplayMode.x); // max 640x512... that's
361 PreviousPSXDisplay.DisplayMode.y= // the size of my
362 min(512,PSXDisplay.DisplayMode.y); // back buffer surface
363 PSXDisplay.Interlaced = PSXDisplay.InterlacedNew;
365 PSXDisplay.DisplayEnd.x= // calc end of display
366 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
367 PSXDisplay.DisplayEnd.y=
368 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
369 PreviousPSXDisplay.DisplayEnd.x=
370 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
371 PreviousPSXDisplay.DisplayEnd.y=
372 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
374 ChangeDispOffsetsX();
376 if(iFrameLimit==2) SetAutoFrameCap(); // -> set it
378 if(UseFrameSkip) updateDisplay(); // stupid stuff when frame skipping enabled
381 ////////////////////////////////////////////////////////////////////////
382 // update lace is called evry VSync
383 ////////////////////////////////////////////////////////////////////////
385 void CALLBACK GPUupdateLace(void) // VSYNC
388 lGPUstatusRet^=0x80000000; // odd/even bit
390 //pcsx-rearmed: removed, this is handled by core
391 //if(!(dwActFixes&32)) // std fps limitation?
394 if(PSXDisplay.Interlaced) // interlaced mode?
396 if(bDoVSyncUpdate && PSXDisplay.DisplayMode.x>0 && PSXDisplay.DisplayMode.y>0)
401 else // non-interlaced?
403 if(dwActFixes&64) // lazy screen update fix
405 if(bDoLazyUpdate && !UseFrameSkip)
411 if((bDoVSyncUpdate && !UseFrameSkip) // some primitives drawn?
412 || bDoVSyncUpdate >= 8) // not syned for a while
413 updateDisplay(); // -> update display
417 if(bDoVSyncUpdate) // if display not synced
418 bDoVSyncUpdate++; // count how many times
421 ////////////////////////////////////////////////////////////////////////
422 // process read request from GPU status register
423 ////////////////////////////////////////////////////////////////////////
426 uint32_t CALLBACK GPUreadStatus(void) // READ STATUS
430 static int iNumRead=0; // odd/even hack
434 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)
438 if(iFakePrimBusy) // 27.10.2007 - PETE : emulating some 'busy' while drawing... pfff
442 if(iFakePrimBusy&1) // we do a busy-idle-busy-idle sequence after/while drawing prims
445 GPUIsNotReadyForCommands;
450 GPUIsReadyForCommands;
453 return lGPUstatusRet;
456 ////////////////////////////////////////////////////////////////////////
457 // processes data send to GPU status register
458 // these are always single packet commands.
459 ////////////////////////////////////////////////////////////////////////
461 void CALLBACK GPUwriteStatus(uint32_t gdata) // WRITE STATUS
463 uint32_t lCommand=(gdata>>24)&0xff;
465 ulStatusControl[lCommand]=gdata; // store command for freezing
469 //--------------------------------------------------//
472 memset(lGPUInfoVals,0x00,16*sizeof(uint32_t));
473 lGPUstatusRet=0x14802000;
474 PSXDisplay.Disabled=1;
475 DataWriteMode=DataReadMode=DR_NORMAL;
476 PSXDisplay.DrawOffset.x=PSXDisplay.DrawOffset.y=0;
477 drawX=drawY=0;drawW=drawH=0;
478 sSetMask=0;lSetMask=0;bCheckMask=FALSE;
480 GlobalTextAddrX=0;GlobalTextAddrY=0;
481 GlobalTextTP=0;GlobalTextABR=0;
482 PSXDisplay.RGB24=FALSE;
483 PSXDisplay.Interlaced=FALSE;
486 //--------------------------------------------------//
487 // dis/enable display
490 PreviousPSXDisplay.Disabled = PSXDisplay.Disabled;
491 PSXDisplay.Disabled = (gdata & 1);
493 if(PSXDisplay.Disabled)
494 lGPUstatusRet|=GPUSTATUS_DISPLAYDISABLED;
495 else lGPUstatusRet&=~GPUSTATUS_DISPLAYDISABLED;
498 //--------------------------------------------------//
499 // setting transfer mode
501 gdata &= 0x03; // Only want the lower two bits
503 DataWriteMode=DataReadMode=DR_NORMAL;
504 if(gdata==0x02) DataWriteMode=DR_VRAMTRANSFER;
505 if(gdata==0x03) DataReadMode =DR_VRAMTRANSFER;
506 lGPUstatusRet&=~GPUSTATUS_DMABITS; // Clear the current settings of the DMA bits
507 lGPUstatusRet|=(gdata << 29); // Set the DMA bits according to the received data
510 //--------------------------------------------------//
511 // setting display position
514 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
515 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
518 PSXDisplay.DisplayPosition.y = (short)((gdata>>10)&0x1ff);
520 // store the same val in some helper var, we need it on later compares
521 PreviousPSXDisplay.DisplayModeNew.x=PSXDisplay.DisplayPosition.y;
523 if((PSXDisplay.DisplayPosition.y+PSXDisplay.DisplayMode.y)>512)
525 int dy1=512-PSXDisplay.DisplayPosition.y;
526 int dy2=(PSXDisplay.DisplayPosition.y+PSXDisplay.DisplayMode.y)-512;
530 PreviousPSXDisplay.DisplayModeNew.y=-dy2;
534 PSXDisplay.DisplayPosition.y=0;
535 PreviousPSXDisplay.DisplayModeNew.y=-dy1;
538 else PreviousPSXDisplay.DisplayModeNew.y=0;
541 PSXDisplay.DisplayPosition.x = (short)(gdata & 0x3ff);
542 PSXDisplay.DisplayEnd.x=
543 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
544 PSXDisplay.DisplayEnd.y=
545 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y + PreviousPSXDisplay.DisplayModeNew.y;
546 PreviousPSXDisplay.DisplayEnd.x=
547 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
548 PreviousPSXDisplay.DisplayEnd.y=
549 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y + PreviousPSXDisplay.DisplayModeNew.y;
553 if (!(PSXDisplay.Interlaced)) // stupid frame skipping option
555 if(UseFrameSkip) updateDisplay();
556 if(dwActFixes&64) bDoLazyUpdate=TRUE;
559 //--------------------------------------------------//
563 PSXDisplay.Range.x0=(short)(gdata & 0x7ff);
564 PSXDisplay.Range.x1=(short)((gdata>>12) & 0xfff);
566 PSXDisplay.Range.x1-=PSXDisplay.Range.x0;
568 ChangeDispOffsetsX();
571 //--------------------------------------------------//
576 PSXDisplay.Range.y0=(short)(gdata & 0x3ff);
577 PSXDisplay.Range.y1=(short)((gdata>>10) & 0x3ff);
579 PreviousPSXDisplay.Height = PSXDisplay.Height;
581 PSXDisplay.Height = PSXDisplay.Range.y1 -
582 PSXDisplay.Range.y0 +
583 PreviousPSXDisplay.DisplayModeNew.y;
585 if(PreviousPSXDisplay.Height!=PSXDisplay.Height)
587 PSXDisplay.DisplayModeNew.y=PSXDisplay.Height*PSXDisplay.Double;
589 ChangeDispOffsetsY();
591 updateDisplayIfChanged();
595 //--------------------------------------------------//
596 // setting display infos
599 PSXDisplay.DisplayModeNew.x =
600 sDispWidths[(gdata & 0x03) | ((gdata & 0x40) >> 4)];
602 if (gdata&0x04) PSXDisplay.Double=2;
603 else PSXDisplay.Double=1;
605 PSXDisplay.DisplayModeNew.y = PSXDisplay.Height*PSXDisplay.Double;
607 ChangeDispOffsetsY();
609 PSXDisplay.PAL = (gdata & 0x08)?TRUE:FALSE; // if 1 - PAL mode, else NTSC
610 PSXDisplay.RGB24New = (gdata & 0x10)?TRUE:FALSE; // if 1 - TrueColor
611 PSXDisplay.InterlacedNew = (gdata & 0x20)?TRUE:FALSE; // if 1 - Interlace
613 lGPUstatusRet&=~GPUSTATUS_WIDTHBITS; // Clear the width bits
615 (((gdata & 0x03) << 17) |
616 ((gdata & 0x40) << 10)); // Set the width bits
618 if(PSXDisplay.InterlacedNew)
620 if(!PSXDisplay.Interlaced)
622 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
623 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
625 lGPUstatusRet|=GPUSTATUS_INTERLACED;
627 else lGPUstatusRet&=~GPUSTATUS_INTERLACED;
630 lGPUstatusRet|=GPUSTATUS_PAL;
631 else lGPUstatusRet&=~GPUSTATUS_PAL;
633 if (PSXDisplay.Double==2)
634 lGPUstatusRet|=GPUSTATUS_DOUBLEHEIGHT;
635 else lGPUstatusRet&=~GPUSTATUS_DOUBLEHEIGHT;
637 if (PSXDisplay.RGB24New)
638 lGPUstatusRet|=GPUSTATUS_RGB24;
639 else lGPUstatusRet&=~GPUSTATUS_RGB24;
641 updateDisplayIfChanged();
644 //--------------------------------------------------//
645 // ask about GPU version and other stuff
653 lGPUdataRet=lGPUInfoVals[INFO_TW]; // tw infos
656 lGPUdataRet=lGPUInfoVals[INFO_DRAWSTART]; // draw start
659 lGPUdataRet=lGPUInfoVals[INFO_DRAWEND]; // draw end
663 lGPUdataRet=lGPUInfoVals[INFO_DRAWOFF]; // draw offset
666 lGPUdataRet=0x02; // gpu type
669 case 0x0F: // some bios addr?
670 lGPUdataRet=0xBFC03720;
674 //--------------------------------------------------//
678 ////////////////////////////////////////////////////////////////////////
679 // vram read/write helpers, needed by LEWPY's optimized vram read/write :)
680 ////////////////////////////////////////////////////////////////////////
682 static inline void FinishedVRAMWrite(void)
684 // Set register to NORMAL operation
685 DataWriteMode = DR_NORMAL;
686 // Reset transfer values, to prevent mis-transfer of data
690 VRAMWrite.Height = 0;
691 VRAMWrite.ColsRemaining = 0;
692 VRAMWrite.RowsRemaining = 0;
695 static inline void FinishedVRAMRead(void)
697 // Set register to NORMAL operation
698 DataReadMode = DR_NORMAL;
699 // Reset transfer values, to prevent mis-transfer of data
704 VRAMRead.ColsRemaining = 0;
705 VRAMRead.RowsRemaining = 0;
707 // Indicate GPU is no longer ready for VRAM data in the STATUS REGISTER
708 lGPUstatusRet&=~GPUSTATUS_READYFORVRAM;
711 ////////////////////////////////////////////////////////////////////////
712 // core read from vram
713 ////////////////////////////////////////////////////////////////////////
715 void CALLBACK GPUreadDataMem(uint32_t * pMem, int iSize)
719 if(DataReadMode!=DR_VRAMTRANSFER) return;
723 // adjust read ptr, if necessary
724 while(VRAMRead.ImagePtr>=psxVuw_eom)
725 VRAMRead.ImagePtr-=512*1024;
726 while(VRAMRead.ImagePtr<psxVuw)
727 VRAMRead.ImagePtr+=512*1024;
731 // do 2 seperate 16bit reads for compatibility (wrap issues)
732 if ((VRAMRead.ColsRemaining > 0) && (VRAMRead.RowsRemaining > 0))
735 lGPUdataRet=(uint32_t)GETLE16(VRAMRead.ImagePtr);
738 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
739 VRAMRead.RowsRemaining --;
741 if(VRAMRead.RowsRemaining<=0)
743 VRAMRead.RowsRemaining = VRAMRead.Width;
744 VRAMRead.ColsRemaining--;
745 VRAMRead.ImagePtr += 1024 - VRAMRead.Width;
746 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
749 // higher 16 bit (always, even if it's an odd width)
750 lGPUdataRet|=(uint32_t)GETLE16(VRAMRead.ImagePtr)<<16;
751 PUTLE32(pMem, lGPUdataRet); pMem++;
753 if(VRAMRead.ColsRemaining <= 0)
754 {FinishedVRAMRead();goto ENDREAD;}
757 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
758 VRAMRead.RowsRemaining--;
759 if(VRAMRead.RowsRemaining<=0)
761 VRAMRead.RowsRemaining = VRAMRead.Width;
762 VRAMRead.ColsRemaining--;
763 VRAMRead.ImagePtr += 1024 - VRAMRead.Width;
764 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
766 if(VRAMRead.ColsRemaining <= 0)
767 {FinishedVRAMRead();goto ENDREAD;}
769 else {FinishedVRAMRead();goto ENDREAD;}
777 ////////////////////////////////////////////////////////////////////////
779 uint32_t CALLBACK GPUreadData(void)
782 GPUreadDataMem(&l,1);
786 // Software drawing function
789 // PSX drawing primitives
792 ////////////////////////////////////////////////////////////////////////
793 // processes data send to GPU data register
794 // extra table entries for fixing polyline troubles
795 ////////////////////////////////////////////////////////////////////////
797 static const unsigned char primTableCX[256] =
818 // 5,5,5,5,6,6,6,6, // FLINE
819 254,254,254,254,254,254,254,254,
823 // 7,7,7,7,9,9,9,9, // GLINE
824 255,255,255,255,255,255,255,255,
828 2,2,2,2,3,3,3,3, // 3=SPRITE1???
867 void CALLBACK GPUwriteDataMem(uint32_t * pMem, int iSize)
869 unsigned char command;
873 GPUIsNotReadyForCommands;
877 if(DataWriteMode==DR_VRAMTRANSFER)
879 BOOL bFinished=FALSE;
881 // make sure we are in vram
882 while(VRAMWrite.ImagePtr>=psxVuw_eom)
883 VRAMWrite.ImagePtr-=512*1024;
884 while(VRAMWrite.ImagePtr<psxVuw)
885 VRAMWrite.ImagePtr+=512*1024;
888 while(VRAMWrite.ColsRemaining>0)
890 while(VRAMWrite.RowsRemaining>0)
892 if(i>=iSize) {goto ENDVRAM;}
895 gdata=GETLE32(pMem); pMem++;
897 PUTLE16(VRAMWrite.ImagePtr, (unsigned short)gdata); VRAMWrite.ImagePtr++;
898 if(VRAMWrite.ImagePtr>=psxVuw_eom) VRAMWrite.ImagePtr-=512*1024;
899 VRAMWrite.RowsRemaining --;
901 if(VRAMWrite.RowsRemaining <= 0)
903 VRAMWrite.ColsRemaining--;
904 if (VRAMWrite.ColsRemaining <= 0) // last pixel is odd width
906 gdata=(gdata&0xFFFF)|(((uint32_t)GETLE16(VRAMWrite.ImagePtr))<<16);
911 VRAMWrite.RowsRemaining = VRAMWrite.Width;
912 VRAMWrite.ImagePtr += 1024 - VRAMWrite.Width;
915 PUTLE16(VRAMWrite.ImagePtr, (unsigned short)(gdata>>16)); VRAMWrite.ImagePtr++;
916 if(VRAMWrite.ImagePtr>=psxVuw_eom) VRAMWrite.ImagePtr-=512*1024;
917 VRAMWrite.RowsRemaining --;
920 VRAMWrite.RowsRemaining = VRAMWrite.Width;
921 VRAMWrite.ColsRemaining--;
922 VRAMWrite.ImagePtr += 1024 - VRAMWrite.Width;
927 if(bFinished) bDoVSyncUpdate=TRUE;
932 if(DataWriteMode==DR_NORMAL)
934 void (* *primFunc)(unsigned char *);
935 if(bSkipNextFrame) primFunc=primTableSkip;
936 else primFunc=primTableJ;
940 if(DataWriteMode==DR_VRAMTRANSFER) goto STARTVRAM;
942 gdata=GETLE32(pMem); pMem++; i++;
946 command = (unsigned char)((gdata>>24) & 0xff);
948 //if(command>=0xb0 && command<0xc0) auxprintf("b0 %x!!!!!!!!!\n",command);
950 if(primTableCX[command])
952 gpuDataC = primTableCX[command];
953 gpuCommand = command;
954 PUTLE32_(&gpuDataM[0], gdata);
961 PUTLE32_(&gpuDataM[gpuDataP], gdata);
964 if((gpuDataC==254 && gpuDataP>=3) ||
965 (gpuDataC==255 && gpuDataP>=4 && !(gpuDataP&1)))
967 if((gpuDataM[gpuDataP] & 0xF000F000) == 0x50005000)
974 if(gpuDataP == gpuDataC)
977 primFunc[gpuCommand]((unsigned char *)gpuDataM);
978 if(dwActFixes&0x0400) // hack for emulating "gpu busy" in some games
986 GPUIsReadyForCommands;
990 ////////////////////////////////////////////////////////////////////////
992 void CALLBACK GPUwriteData(uint32_t gdata)
994 PUTLE32_(&gdata, gdata);
995 GPUwriteDataMem(&gdata,1);
998 ////////////////////////////////////////////////////////////////////////
999 // process gpu commands
1000 ////////////////////////////////////////////////////////////////////////
1002 unsigned long lUsedAddr[3];
1004 static inline BOOL CheckForEndlessLoop(unsigned long laddr)
1006 if(laddr==lUsedAddr[1]) return TRUE;
1007 if(laddr==lUsedAddr[2]) return TRUE;
1009 if(laddr<lUsedAddr[0]) lUsedAddr[1]=laddr;
1010 else lUsedAddr[2]=laddr;
1015 long CALLBACK GPUdmaChain(uint32_t * baseAddrL, uint32_t addr)
1018 unsigned char * baseAddrB;
1019 short count;unsigned int DMACommandCounter = 0;
1023 lUsedAddr[0]=lUsedAddr[1]=lUsedAddr[2]=0xffffff;
1025 baseAddrB = (unsigned char*) baseAddrL;
1030 if(DMACommandCounter++ > 2000000) break;
1031 if(CheckForEndlessLoop(addr)) break;
1033 count = baseAddrB[addr+3];
1037 if(count>0) GPUwriteDataMem(&baseAddrL[dmaMem>>2],count);
1039 addr = GETLE32(&baseAddrL[addr>>2])&0xffffff;
1041 while (addr != 0xffffff);
1048 ////////////////////////////////////////////////////////////////////////
1050 ////////////////////////////////////////////////////////////////////////
1052 typedef struct GPUFREEZETAG
1054 uint32_t ulFreezeVersion; // should be always 1 for now (set by main emu)
1055 uint32_t ulStatus; // current gpu status
1056 uint32_t ulControl[256]; // latest control register values
1057 unsigned char psxVRam[1024*1024*2]; // current VRam image (full 2 MB for ZN)
1060 ////////////////////////////////////////////////////////////////////////
1062 long CALLBACK GPUfreeze(uint32_t ulGetFreezeData,GPUFreeze_t * pF)
1064 //----------------------------------------------------//
1065 if(ulGetFreezeData==2) // 2: info, which save slot is selected? (just for display)
1067 long lSlotNum=*((long *)pF);
1068 if(lSlotNum<0) return 0;
1069 if(lSlotNum>8) return 0;
1070 lSelectedSlot=lSlotNum+1;
1073 //----------------------------------------------------//
1074 if(!pF) return 0; // some checks
1075 if(pF->ulFreezeVersion!=1) return 0;
1077 if(ulGetFreezeData==1) // 1: get data
1079 pF->ulStatus=lGPUstatusRet;
1080 memcpy(pF->ulControl,ulStatusControl,256*sizeof(uint32_t));
1081 memcpy(pF->psxVRam, psxVub, 1024*512*2);
1086 if(ulGetFreezeData!=0) return 0; // 0: set data
1088 lGPUstatusRet=pF->ulStatus;
1089 memcpy(ulStatusControl,pF->ulControl,256*sizeof(uint32_t));
1090 memcpy(psxVub, pF->psxVRam, 1024*512*2);
1092 // RESET TEXTURE STORE HERE, IF YOU USE SOMETHING LIKE THAT
1094 GPUwriteStatus(ulStatusControl[0]);
1095 GPUwriteStatus(ulStatusControl[1]);
1096 GPUwriteStatus(ulStatusControl[2]);
1097 GPUwriteStatus(ulStatusControl[3]);
1098 GPUwriteStatus(ulStatusControl[8]); // try to repair things
1099 GPUwriteStatus(ulStatusControl[6]);
1100 GPUwriteStatus(ulStatusControl[7]);
1101 GPUwriteStatus(ulStatusControl[5]);
1102 GPUwriteStatus(ulStatusControl[4]);