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;
62 static uint32_t vBlank=0;
64 ////////////////////////////////////////////////////////////////////////
65 // some misc external display funcs
66 ////////////////////////////////////////////////////////////////////////
74 ////////////////////////////////////////////////////////////////////////
75 // sets all kind of act fixes
76 ////////////////////////////////////////////////////////////////////////
78 static void SetFixes(void)
80 if(dwActFixes&0x02) sDispWidths[4]=384;
81 else sDispWidths[4]=368;
84 ////////////////////////////////////////////////////////////////////////
85 // INIT, will be called after lib load... well, just do some var init...
86 ////////////////////////////////////////////////////////////////////////
88 long CALLBACK GPUinit(void) // GPU INIT
90 memset(ulStatusControl,0,256*sizeof(uint32_t)); // init save state scontrol field
92 psxVSecure = (unsigned char *)malloc((512*2)*1024 + (1024*1024)); // always alloc one extra MB for soft drawing funcs security
97 psxVub=psxVSecure + 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(psxVSecure,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
186 return 0; // nothinh to do
189 ////////////////////////////////////////////////////////////////////////
190 // Update display (swap buffers)
191 ////////////////////////////////////////////////////////////////////////
193 static void updateDisplay(void) // UPDATE DISPLAY
195 if(PSXDisplay.Disabled) // disable?
197 return; // -> and bye
200 if(dwActFixes&32) // pc fps calculation fix
202 if(UseFrameLimit) PCFrameCap(); // -> brake
203 if(UseFrameSkip) PCcalcfps();
206 if(UseFrameSkip) // skip ?
208 if(!bSkipNextFrame) DoBufferSwap(); // -> to skip or not to skip
209 if(dwActFixes&0xa0) // -> pc fps calculation fix/old skipping fix
211 if((fps_skip < fFrameRateHz) && !(bSkipNextFrame)) // -> skip max one in a row
212 {bSkipNextFrame = TRUE; fps_skip=fFrameRateHz;}
213 else bSkipNextFrame = FALSE;
219 bSkipNextFrame = FALSE;
220 DoBufferSwap(); // -> swap
223 bDoVSyncUpdate=FALSE; // vsync done
226 ////////////////////////////////////////////////////////////////////////
227 // roughly emulated screen centering bits... not complete !!!
228 ////////////////////////////////////////////////////////////////////////
230 void ChangeDispOffsetsX(void) // X CENTER
234 if(!PSXDisplay.Range.x1) return;
236 l=PreviousPSXDisplay.DisplayMode.x;
238 l*=(long)PSXDisplay.Range.x1;
239 l/=2560;lx=l;l&=0xfffffff8;
241 if(l==PreviousPSXDisplay.Range.y1) return; // abusing range.y1 for
242 PreviousPSXDisplay.Range.y1=(short)l; // storing last x range and test
244 if(lx>=PreviousPSXDisplay.DisplayMode.x)
246 PreviousPSXDisplay.Range.x1=
247 (short)PreviousPSXDisplay.DisplayMode.x;
248 PreviousPSXDisplay.Range.x0=0;
252 PreviousPSXDisplay.Range.x1=(short)l;
254 PreviousPSXDisplay.Range.x0=
255 (PSXDisplay.Range.x0-500)/8;
257 if(PreviousPSXDisplay.Range.x0<0)
258 PreviousPSXDisplay.Range.x0=0;
260 if((PreviousPSXDisplay.Range.x0+lx)>
261 PreviousPSXDisplay.DisplayMode.x)
263 PreviousPSXDisplay.Range.x0=
264 (short)(PreviousPSXDisplay.DisplayMode.x-lx);
265 PreviousPSXDisplay.Range.x0+=2; //???
267 PreviousPSXDisplay.Range.x1+=(short)(lx-l);
269 PreviousPSXDisplay.Range.x1-=2; // makes linux stretching easier
273 // some linux alignment security
274 PreviousPSXDisplay.Range.x0=PreviousPSXDisplay.Range.x0>>1;
275 PreviousPSXDisplay.Range.x0=PreviousPSXDisplay.Range.x0<<1;
276 PreviousPSXDisplay.Range.x1=PreviousPSXDisplay.Range.x1>>1;
277 PreviousPSXDisplay.Range.x1=PreviousPSXDisplay.Range.x1<<1;
279 DoClearScreenBuffer();
285 ////////////////////////////////////////////////////////////////////////
287 void ChangeDispOffsetsY(void) // Y CENTER
289 int iT,iO=PreviousPSXDisplay.Range.y0;
290 int iOldYOffset=PreviousPSXDisplay.DisplayModeNew.y;
294 if((PreviousPSXDisplay.DisplayModeNew.x+PSXDisplay.DisplayModeNew.y)>512)
296 int dy1=512-PreviousPSXDisplay.DisplayModeNew.x;
297 int dy2=(PreviousPSXDisplay.DisplayModeNew.x+PSXDisplay.DisplayModeNew.y)-512;
301 PreviousPSXDisplay.DisplayModeNew.y=-dy2;
305 PSXDisplay.DisplayPosition.y=0;
306 PreviousPSXDisplay.DisplayModeNew.y=-dy1;
309 else PreviousPSXDisplay.DisplayModeNew.y=0;
313 if(PreviousPSXDisplay.DisplayModeNew.y!=iOldYOffset) // if old offset!=new offset: recalc height
315 PSXDisplay.Height = PSXDisplay.Range.y1 -
316 PSXDisplay.Range.y0 +
317 PreviousPSXDisplay.DisplayModeNew.y;
318 PSXDisplay.DisplayModeNew.y=PSXDisplay.Height*PSXDisplay.Double;
323 if(PSXDisplay.PAL) iT=48; else iT=28;
325 if(PSXDisplay.Range.y0>=iT)
327 PreviousPSXDisplay.Range.y0=
328 (short)((PSXDisplay.Range.y0-iT-4)*PSXDisplay.Double);
329 if(PreviousPSXDisplay.Range.y0<0)
330 PreviousPSXDisplay.Range.y0=0;
331 PSXDisplay.DisplayModeNew.y+=
332 PreviousPSXDisplay.Range.y0;
335 PreviousPSXDisplay.Range.y0=0;
337 if(iO!=PreviousPSXDisplay.Range.y0)
339 DoClearScreenBuffer();
343 ////////////////////////////////////////////////////////////////////////
344 // check if update needed
345 ////////////////////////////////////////////////////////////////////////
347 static void updateDisplayIfChanged(void) // UPDATE DISPLAY IF CHANGED
349 if ((PSXDisplay.DisplayMode.y == PSXDisplay.DisplayModeNew.y) &&
350 (PSXDisplay.DisplayMode.x == PSXDisplay.DisplayModeNew.x))
352 if((PSXDisplay.RGB24 == PSXDisplay.RGB24New) &&
353 (PSXDisplay.Interlaced == PSXDisplay.InterlacedNew)) return;
356 PSXDisplay.RGB24 = PSXDisplay.RGB24New; // get new infos
358 PSXDisplay.DisplayMode.y = PSXDisplay.DisplayModeNew.y;
359 PSXDisplay.DisplayMode.x = PSXDisplay.DisplayModeNew.x;
360 PreviousPSXDisplay.DisplayMode.x= // previous will hold
361 min(640,PSXDisplay.DisplayMode.x); // max 640x512... that's
362 PreviousPSXDisplay.DisplayMode.y= // the size of my
363 min(512,PSXDisplay.DisplayMode.y); // back buffer surface
364 PSXDisplay.Interlaced = PSXDisplay.InterlacedNew;
366 PSXDisplay.DisplayEnd.x= // calc end of display
367 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
368 PSXDisplay.DisplayEnd.y=
369 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
370 PreviousPSXDisplay.DisplayEnd.x=
371 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
372 PreviousPSXDisplay.DisplayEnd.y=
373 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
375 ChangeDispOffsetsX();
377 if(iFrameLimit==2) SetAutoFrameCap(); // -> set it
379 if(UseFrameSkip) updateDisplay(); // stupid stuff when frame skipping enabled
382 ////////////////////////////////////////////////////////////////////////
383 // update lace is called evry VSync
384 ////////////////////////////////////////////////////////////////////////
386 void CALLBACK GPUupdateLace(void) // VSYNC
388 //if(!(dwActFixes&1))
389 // lGPUstatusRet^=0x80000000; // odd/even bit
391 //pcsx-rearmed: removed, this is handled by core
392 //if(!(dwActFixes&32)) // std fps limitation?
395 if(PSXDisplay.Interlaced) // interlaced mode?
397 lGPUstatusRet^=0x80000000; // odd/even bit?
399 if(bDoVSyncUpdate && PSXDisplay.DisplayMode.x>0 && PSXDisplay.DisplayMode.y>0)
404 else // non-interlaced?
406 if(dwActFixes&64) // lazy screen update fix
408 if(bDoLazyUpdate && !UseFrameSkip)
414 if((bDoVSyncUpdate && !UseFrameSkip) // some primitives drawn?
415 || bDoVSyncUpdate >= 8) // not syned for a while
416 updateDisplay(); // -> update display
420 if(bDoVSyncUpdate) // if display not synced
421 bDoVSyncUpdate++; // count how many times
424 ////////////////////////////////////////////////////////////////////////
425 // process read request from GPU status register
426 ////////////////////////////////////////////////////////////////////////
429 uint32_t CALLBACK GPUreadStatus(void) // READ STATUS
433 static int iNumRead=0; // odd/even hack
437 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)
441 if(iFakePrimBusy) // 27.10.2007 - PETE : emulating some 'busy' while drawing... pfff
445 if(iFakePrimBusy&1) // we do a busy-idle-busy-idle sequence after/while drawing prims
448 GPUIsNotReadyForCommands;
453 GPUIsReadyForCommands;
456 return lGPUstatusRet | vBlank;
459 ////////////////////////////////////////////////////////////////////////
460 // processes data send to GPU status register
461 // these are always single packet commands.
462 ////////////////////////////////////////////////////////////////////////
464 void CALLBACK GPUwriteStatus(uint32_t gdata) // WRITE STATUS
466 uint32_t lCommand=(gdata>>24)&0xff;
468 ulStatusControl[lCommand]=gdata; // store command for freezing
472 //--------------------------------------------------//
475 memset(lGPUInfoVals,0x00,16*sizeof(uint32_t));
476 lGPUstatusRet=0x14802000;
477 PSXDisplay.Disabled=1;
478 DataWriteMode=DataReadMode=DR_NORMAL;
479 PSXDisplay.DrawOffset.x=PSXDisplay.DrawOffset.y=0;
480 drawX=drawY=0;drawW=drawH=0;
481 sSetMask=0;lSetMask=0;bCheckMask=FALSE;
483 GlobalTextAddrX=0;GlobalTextAddrY=0;
484 GlobalTextTP=0;GlobalTextABR=0;
485 PSXDisplay.RGB24=FALSE;
486 PSXDisplay.Interlaced=FALSE;
489 //--------------------------------------------------//
490 // dis/enable display
493 PreviousPSXDisplay.Disabled = PSXDisplay.Disabled;
494 PSXDisplay.Disabled = (gdata & 1);
496 if(PSXDisplay.Disabled)
497 lGPUstatusRet|=GPUSTATUS_DISPLAYDISABLED;
498 else lGPUstatusRet&=~GPUSTATUS_DISPLAYDISABLED;
501 //--------------------------------------------------//
502 // setting transfer mode
504 gdata &= 0x03; // Only want the lower two bits
506 DataWriteMode=DataReadMode=DR_NORMAL;
507 if(gdata==0x02) DataWriteMode=DR_VRAMTRANSFER;
508 if(gdata==0x03) DataReadMode =DR_VRAMTRANSFER;
509 lGPUstatusRet&=~GPUSTATUS_DMABITS; // Clear the current settings of the DMA bits
510 lGPUstatusRet|=(gdata << 29); // Set the DMA bits according to the received data
513 //--------------------------------------------------//
514 // setting display position
517 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
518 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
521 PSXDisplay.DisplayPosition.y = (short)((gdata>>10)&0x1ff);
523 // store the same val in some helper var, we need it on later compares
524 PreviousPSXDisplay.DisplayModeNew.x=PSXDisplay.DisplayPosition.y;
526 if((PSXDisplay.DisplayPosition.y+PSXDisplay.DisplayMode.y)>512)
528 int dy1=512-PSXDisplay.DisplayPosition.y;
529 int dy2=(PSXDisplay.DisplayPosition.y+PSXDisplay.DisplayMode.y)-512;
533 PreviousPSXDisplay.DisplayModeNew.y=-dy2;
537 PSXDisplay.DisplayPosition.y=0;
538 PreviousPSXDisplay.DisplayModeNew.y=-dy1;
541 else PreviousPSXDisplay.DisplayModeNew.y=0;
544 PSXDisplay.DisplayPosition.x = (short)(gdata & 0x3ff);
545 PSXDisplay.DisplayEnd.x=
546 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
547 PSXDisplay.DisplayEnd.y=
548 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y + PreviousPSXDisplay.DisplayModeNew.y;
549 PreviousPSXDisplay.DisplayEnd.x=
550 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
551 PreviousPSXDisplay.DisplayEnd.y=
552 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y + PreviousPSXDisplay.DisplayModeNew.y;
556 if (!(PSXDisplay.Interlaced)) // stupid frame skipping option
558 if(UseFrameSkip) updateDisplay();
559 if(dwActFixes&64) bDoLazyUpdate=TRUE;
562 //--------------------------------------------------//
566 PSXDisplay.Range.x0=(short)(gdata & 0x7ff);
567 PSXDisplay.Range.x1=(short)((gdata>>12) & 0xfff);
569 PSXDisplay.Range.x1-=PSXDisplay.Range.x0;
571 ChangeDispOffsetsX();
574 //--------------------------------------------------//
579 PSXDisplay.Range.y0=(short)(gdata & 0x3ff);
580 PSXDisplay.Range.y1=(short)((gdata>>10) & 0x3ff);
582 PreviousPSXDisplay.Height = PSXDisplay.Height;
584 PSXDisplay.Height = PSXDisplay.Range.y1 -
585 PSXDisplay.Range.y0 +
586 PreviousPSXDisplay.DisplayModeNew.y;
588 if(PreviousPSXDisplay.Height!=PSXDisplay.Height)
590 PSXDisplay.DisplayModeNew.y=PSXDisplay.Height*PSXDisplay.Double;
592 ChangeDispOffsetsY();
594 updateDisplayIfChanged();
598 //--------------------------------------------------//
599 // setting display infos
602 PSXDisplay.DisplayModeNew.x =
603 sDispWidths[(gdata & 0x03) | ((gdata & 0x40) >> 4)];
605 if (gdata&0x04) PSXDisplay.Double=2;
606 else PSXDisplay.Double=1;
608 PSXDisplay.DisplayModeNew.y = PSXDisplay.Height*PSXDisplay.Double;
610 ChangeDispOffsetsY();
612 PSXDisplay.PAL = (gdata & 0x08)?TRUE:FALSE; // if 1 - PAL mode, else NTSC
613 PSXDisplay.RGB24New = (gdata & 0x10)?TRUE:FALSE; // if 1 - TrueColor
614 PSXDisplay.InterlacedNew = (gdata & 0x20)?TRUE:FALSE; // if 1 - Interlace
616 lGPUstatusRet&=~GPUSTATUS_WIDTHBITS; // Clear the width bits
618 (((gdata & 0x03) << 17) |
619 ((gdata & 0x40) << 10)); // Set the width bits
621 if(PSXDisplay.InterlacedNew)
623 if(!PSXDisplay.Interlaced)
625 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
626 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
628 lGPUstatusRet|=GPUSTATUS_INTERLACED;
630 else lGPUstatusRet&=~GPUSTATUS_INTERLACED;
633 lGPUstatusRet|=GPUSTATUS_PAL;
634 else lGPUstatusRet&=~GPUSTATUS_PAL;
636 if (PSXDisplay.Double==2)
637 lGPUstatusRet|=GPUSTATUS_DOUBLEHEIGHT;
638 else lGPUstatusRet&=~GPUSTATUS_DOUBLEHEIGHT;
640 if (PSXDisplay.RGB24New)
641 lGPUstatusRet|=GPUSTATUS_RGB24;
642 else lGPUstatusRet&=~GPUSTATUS_RGB24;
644 updateDisplayIfChanged();
647 //--------------------------------------------------//
648 // ask about GPU version and other stuff
656 lGPUdataRet=lGPUInfoVals[INFO_TW]; // tw infos
659 lGPUdataRet=lGPUInfoVals[INFO_DRAWSTART]; // draw start
662 lGPUdataRet=lGPUInfoVals[INFO_DRAWEND]; // draw end
666 lGPUdataRet=lGPUInfoVals[INFO_DRAWOFF]; // draw offset
669 lGPUdataRet=0x02; // gpu type
672 case 0x0F: // some bios addr?
673 lGPUdataRet=0xBFC03720;
677 //--------------------------------------------------//
681 ////////////////////////////////////////////////////////////////////////
682 // vram read/write helpers, needed by LEWPY's optimized vram read/write :)
683 ////////////////////////////////////////////////////////////////////////
685 static inline void FinishedVRAMWrite(void)
687 // Set register to NORMAL operation
688 DataWriteMode = DR_NORMAL;
689 // Reset transfer values, to prevent mis-transfer of data
693 VRAMWrite.Height = 0;
694 VRAMWrite.ColsRemaining = 0;
695 VRAMWrite.RowsRemaining = 0;
698 static inline void FinishedVRAMRead(void)
700 // Set register to NORMAL operation
701 DataReadMode = DR_NORMAL;
702 // Reset transfer values, to prevent mis-transfer of data
707 VRAMRead.ColsRemaining = 0;
708 VRAMRead.RowsRemaining = 0;
710 // Indicate GPU is no longer ready for VRAM data in the STATUS REGISTER
711 lGPUstatusRet&=~GPUSTATUS_READYFORVRAM;
714 ////////////////////////////////////////////////////////////////////////
715 // core read from vram
716 ////////////////////////////////////////////////////////////////////////
718 void CALLBACK GPUreadDataMem(uint32_t * pMem, int iSize)
722 if(DataReadMode!=DR_VRAMTRANSFER) return;
726 // adjust read ptr, if necessary
727 while(VRAMRead.ImagePtr>=psxVuw_eom)
728 VRAMRead.ImagePtr-=512*1024;
729 while(VRAMRead.ImagePtr<psxVuw)
730 VRAMRead.ImagePtr+=512*1024;
734 // do 2 seperate 16bit reads for compatibility (wrap issues)
735 if ((VRAMRead.ColsRemaining > 0) && (VRAMRead.RowsRemaining > 0))
738 lGPUdataRet=(uint32_t)GETLE16(VRAMRead.ImagePtr);
741 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
742 VRAMRead.RowsRemaining --;
744 if(VRAMRead.RowsRemaining<=0)
746 VRAMRead.RowsRemaining = VRAMRead.Width;
747 VRAMRead.ColsRemaining--;
748 VRAMRead.ImagePtr += 1024 - VRAMRead.Width;
749 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
752 // higher 16 bit (always, even if it's an odd width)
753 lGPUdataRet|=(uint32_t)GETLE16(VRAMRead.ImagePtr)<<16;
754 PUTLE32(pMem, lGPUdataRet); pMem++;
756 if(VRAMRead.ColsRemaining <= 0)
757 {FinishedVRAMRead();goto ENDREAD;}
760 if(VRAMRead.ImagePtr>=psxVuw_eom) VRAMRead.ImagePtr-=512*1024;
761 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;
769 if(VRAMRead.ColsRemaining <= 0)
770 {FinishedVRAMRead();goto ENDREAD;}
772 else {FinishedVRAMRead();goto ENDREAD;}
780 ////////////////////////////////////////////////////////////////////////
782 uint32_t CALLBACK GPUreadData(void)
785 GPUreadDataMem(&l,1);
789 // Software drawing function
792 // PSX drawing primitives
795 ////////////////////////////////////////////////////////////////////////
796 // processes data send to GPU data register
797 // extra table entries for fixing polyline troubles
798 ////////////////////////////////////////////////////////////////////////
800 static const unsigned char primTableCX[256] =
821 // 5,5,5,5,6,6,6,6, // FLINE
822 254,254,254,254,254,254,254,254,
826 // 7,7,7,7,9,9,9,9, // GLINE
827 255,255,255,255,255,255,255,255,
831 2,2,2,2,3,3,3,3, // 3=SPRITE1???
870 void CALLBACK GPUwriteDataMem(uint32_t * pMem, int iSize)
872 unsigned char command;
876 GPUIsNotReadyForCommands;
880 if(DataWriteMode==DR_VRAMTRANSFER)
882 BOOL bFinished=FALSE;
884 // make sure we are in vram
885 while(VRAMWrite.ImagePtr>=psxVuw_eom)
886 VRAMWrite.ImagePtr-=512*1024;
887 while(VRAMWrite.ImagePtr<psxVuw)
888 VRAMWrite.ImagePtr+=512*1024;
891 while(VRAMWrite.ColsRemaining>0)
893 while(VRAMWrite.RowsRemaining>0)
895 if(i>=iSize) {goto ENDVRAM;}
898 gdata=GETLE32(pMem); pMem++;
900 PUTLE16(VRAMWrite.ImagePtr, (unsigned short)gdata); VRAMWrite.ImagePtr++;
901 if(VRAMWrite.ImagePtr>=psxVuw_eom) VRAMWrite.ImagePtr-=512*1024;
902 VRAMWrite.RowsRemaining --;
904 if(VRAMWrite.RowsRemaining <= 0)
906 VRAMWrite.ColsRemaining--;
907 if (VRAMWrite.ColsRemaining <= 0) // last pixel is odd width
909 gdata=(gdata&0xFFFF)|(((uint32_t)GETLE16(VRAMWrite.ImagePtr))<<16);
914 VRAMWrite.RowsRemaining = VRAMWrite.Width;
915 VRAMWrite.ImagePtr += 1024 - VRAMWrite.Width;
918 PUTLE16(VRAMWrite.ImagePtr, (unsigned short)(gdata>>16)); VRAMWrite.ImagePtr++;
919 if(VRAMWrite.ImagePtr>=psxVuw_eom) VRAMWrite.ImagePtr-=512*1024;
920 VRAMWrite.RowsRemaining --;
923 VRAMWrite.RowsRemaining = VRAMWrite.Width;
924 VRAMWrite.ColsRemaining--;
925 VRAMWrite.ImagePtr += 1024 - VRAMWrite.Width;
930 if(bFinished) bDoVSyncUpdate=TRUE;
935 if(DataWriteMode==DR_NORMAL)
937 void (* *primFunc)(unsigned char *);
938 if(bSkipNextFrame) primFunc=primTableSkip;
939 else primFunc=primTableJ;
943 if(DataWriteMode==DR_VRAMTRANSFER) goto STARTVRAM;
945 gdata=GETLE32(pMem); pMem++; i++;
949 command = (unsigned char)((gdata>>24) & 0xff);
951 //if(command>=0xb0 && command<0xc0) auxprintf("b0 %x!!!!!!!!!\n",command);
953 if(primTableCX[command])
955 gpuDataC = primTableCX[command];
956 gpuCommand = command;
957 PUTLE32_(&gpuDataM[0], gdata);
964 PUTLE32_(&gpuDataM[gpuDataP], gdata);
967 if((gpuDataC==254 && gpuDataP>=3) ||
968 (gpuDataC==255 && gpuDataP>=4 && !(gpuDataP&1)))
970 if((gpuDataM[gpuDataP] & 0xF000F000) == 0x50005000)
977 if(gpuDataP == gpuDataC)
980 primFunc[gpuCommand]((unsigned char *)gpuDataM);
981 if(dwActFixes&0x0400) // hack for emulating "gpu busy" in some games
989 GPUIsReadyForCommands;
993 ////////////////////////////////////////////////////////////////////////
995 void CALLBACK GPUwriteData(uint32_t gdata)
997 PUTLE32_(&gdata, gdata);
998 GPUwriteDataMem(&gdata,1);
1001 ////////////////////////////////////////////////////////////////////////
1002 // process gpu commands
1003 ////////////////////////////////////////////////////////////////////////
1005 unsigned long lUsedAddr[3];
1007 static inline BOOL CheckForEndlessLoop(unsigned long laddr)
1009 if(laddr==lUsedAddr[1]) return TRUE;
1010 if(laddr==lUsedAddr[2]) return TRUE;
1012 if(laddr<lUsedAddr[0]) lUsedAddr[1]=laddr;
1013 else lUsedAddr[2]=laddr;
1018 long CALLBACK GPUdmaChain(uint32_t * baseAddrL, uint32_t addr)
1021 unsigned char * baseAddrB;
1022 short count;unsigned int DMACommandCounter = 0;
1026 lUsedAddr[0]=lUsedAddr[1]=lUsedAddr[2]=0xffffff;
1028 baseAddrB = (unsigned char*) baseAddrL;
1033 if(DMACommandCounter++ > 2000000) break;
1034 if(CheckForEndlessLoop(addr)) break;
1036 count = baseAddrB[addr+3];
1040 if(count>0) GPUwriteDataMem(&baseAddrL[dmaMem>>2],count);
1042 addr = GETLE32(&baseAddrL[addr>>2])&0xffffff;
1044 while (addr != 0xffffff);
1051 ////////////////////////////////////////////////////////////////////////
1053 ////////////////////////////////////////////////////////////////////////
1055 typedef struct GPUFREEZETAG
1057 uint32_t ulFreezeVersion; // should be always 1 for now (set by main emu)
1058 uint32_t ulStatus; // current gpu status
1059 uint32_t ulControl[256]; // latest control register values
1060 unsigned char psxVRam[1024*1024*2]; // current VRam image (full 2 MB for ZN)
1063 ////////////////////////////////////////////////////////////////////////
1065 long CALLBACK GPUfreeze(uint32_t ulGetFreezeData,GPUFreeze_t * pF)
1067 //----------------------------------------------------//
1068 if(ulGetFreezeData==2) // 2: info, which save slot is selected? (just for display)
1070 long lSlotNum=*((long *)pF);
1071 if(lSlotNum<0) return 0;
1072 if(lSlotNum>8) return 0;
1073 lSelectedSlot=lSlotNum+1;
1076 //----------------------------------------------------//
1077 if(!pF) return 0; // some checks
1078 if(pF->ulFreezeVersion!=1) return 0;
1080 if(ulGetFreezeData==1) // 1: get data
1082 pF->ulStatus=lGPUstatusRet;
1083 memcpy(pF->ulControl,ulStatusControl,256*sizeof(uint32_t));
1084 memcpy(pF->psxVRam, psxVub, 1024*512*2);
1089 if(ulGetFreezeData!=0) return 0; // 0: set data
1091 lGPUstatusRet=pF->ulStatus;
1092 memcpy(ulStatusControl,pF->ulControl,256*sizeof(uint32_t));
1093 memcpy(psxVub, pF->psxVRam, 1024*512*2);
1095 // RESET TEXTURE STORE HERE, IF YOU USE SOMETHING LIKE THAT
1097 GPUwriteStatus(ulStatusControl[0]);
1098 GPUwriteStatus(ulStatusControl[1]);
1099 GPUwriteStatus(ulStatusControl[2]);
1100 GPUwriteStatus(ulStatusControl[3]);
1101 GPUwriteStatus(ulStatusControl[8]); // try to repair things
1102 GPUwriteStatus(ulStatusControl[6]);
1103 GPUwriteStatus(ulStatusControl[7]);
1104 GPUwriteStatus(ulStatusControl[5]);
1105 GPUwriteStatus(ulStatusControl[4]);
1110 void CALLBACK GPUvBlank(int val)
1112 vBlank=val?0x80000000:0;