3e69aaa91d7a97bf02753e3735c8417e6bb1f182
[pcsx_rearmed.git] / plugins / gpu_unai / gpu.cpp
1 /***************************************************************************
2 *   Copyright (C) 2010 PCSX4ALL Team                                      *
3 *   Copyright (C) 2010 Unai                                               *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) any later version.                                   *
9 *                                                                         *
10 *   This program is distributed in the hope that it will be useful,       *
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13 *   GNU General Public License for more details.                          *
14 *                                                                         *
15 *   You should have received a copy of the GNU General Public License     *
16 *   along with this program; if not, write to the                         *
17 *   Free Software Foundation, Inc.,                                       *
18 *   51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA.           *
19 ***************************************************************************/
20
21 #include "port.h"
22 #include "gpu.h"
23 #include "profiler.h"
24 #include "debug.h"
25
26 int skipCount = 2; /* frame skip (0,1,2,3...) */
27 int skCount=0; /* internal frame skip */
28 int linesInterlace = 0;  /* internal lines interlace */
29 int linesInterlace_user = 0; /* Lines interlace */
30
31 bool isSkip=false; /* skip frame (info coming from GPU) */
32 bool skipFrame=false; /* skip frame (according to frame skip) */
33 bool alt_fps = false; /* Alternative FPS algorithm */
34 bool show_fps = false; /* Show FPS statistics */
35
36 bool isPAL=false; /* PAL video timing */
37 bool progressInterlace_flag = false; /* Progressive interlace flag */
38 bool progressInterlace = false; /* Progressive interlace option*/
39 bool frameLimit = false; /* frames to wait */
40 bool light = true; /* lighting */
41 bool blend = true; /* blending */
42
43 bool fb_dirty = false;
44
45 bool enableAbbeyHack = false; /* Abe's Odyssey hack */
46 u8 BLEND_MODE;
47 u8 TEXT_MODE;
48 u8 Masking;
49
50 u16 PixelMSB;
51 u16 PixelData;
52
53 ///////////////////////////////////////////////////////////////////////////////
54 //  GPU Global data
55 ///////////////////////////////////////////////////////////////////////////////
56
57 ///////////////////////////////////////////////////////////////////////////////
58 //  Dma Transfers info
59 s32             px,py;
60 s32             x_end,y_end;
61 u16*  pvram;
62
63 u32 FrameToRead;
64 u32 FrameToWrite;
65
66 u32 GP0;
67 s32 PacketCount;
68 s32 PacketIndex;
69
70 ///////////////////////////////////////////////////////////////////////////////
71 //  Display status
72 u32 DisplayArea   [6];
73
74 ///////////////////////////////////////////////////////////////////////////////
75 //  Rasterizer status
76 u32 TextureWindow [4];
77 u32 DrawingArea   [4];
78 u32 DrawingOffset [2];
79
80 ///////////////////////////////////////////////////////////////////////////////
81 //  Rasterizer status
82
83 u16* TBA;
84 u16* CBA;
85
86 ///////////////////////////////////////////////////////////////////////////////
87 //  Inner Loops
88 s32   u4, du4;
89 s32   v4, dv4;
90 s32   r4, dr4;
91 s32   g4, dg4;
92 s32   b4, db4;
93 u32   lInc;
94 u32   tInc, tMsk;
95
96 GPUPacket PacketBuffer;
97 u16   GPU_FrameBuffer[FRAME_BUFFER_SIZE/2] __attribute__((aligned(16)));    // FRAME_BUFFER_SIZE is defined in bytes
98 u32   GPU_GP1;
99
100 ///////////////////////////////////////////////////////////////////////////////
101 //  Inner loop driver instanciation file
102 #include "gpu_inner.h"
103
104 ///////////////////////////////////////////////////////////////////////////////
105 //  GPU Raster Macros
106 #define GPU_RGB16(rgb)        ((((rgb)&0xF80000)>>9)|(((rgb)&0xF800)>>6)|(((rgb)&0xF8)>>3))
107
108 #define GPU_EXPANDSIGN_POLY(x)  (((s32)(x)<<20)>>20)
109 //#define GPU_EXPANDSIGN_POLY(x)  (((s32)(x)<<21)>>21)
110 #define GPU_EXPANDSIGN_SPRT(x)  (((s32)(x)<<21)>>21)
111
112 //#define       GPU_TESTRANGE(x)      { if((u32)(x+1024) > 2047) return; }
113 #define GPU_TESTRANGE(x)      { if ((x<-1023) || (x>1023)) return; }
114
115 #define GPU_SWAP(a,b,t) {(t)=(a);(a)=(b);(b)=(t);}
116
117 ///////////////////////////////////////////////////////////////////////////////
118 // GPU internal image drawing functions
119 #include "gpu_raster_image.h"
120
121 ///////////////////////////////////////////////////////////////////////////////
122 // GPU internal line drawing functions
123 #include "gpu_raster_line.h"
124
125 ///////////////////////////////////////////////////////////////////////////////
126 // GPU internal polygon drawing functions
127 #include "gpu_raster_polygon.h"
128
129 ///////////////////////////////////////////////////////////////////////////////
130 // GPU internal sprite drawing functions
131 #include "gpu_raster_sprite.h"
132
133 ///////////////////////////////////////////////////////////////////////////////
134 // GPU command buffer execution/store
135 #include "gpu_command.h"
136
137 ///////////////////////////////////////////////////////////////////////////////
138 INLINE void gpuReset(void)
139 {
140         GPU_GP1 = 0x14802000;
141         TextureWindow[0] = 0;
142         TextureWindow[1] = 0;
143         TextureWindow[2] = 255;
144         TextureWindow[3] = 255;
145         DrawingArea[2] = 256;
146         DrawingArea[3] = 240;
147         DisplayArea[2] = 256;
148         DisplayArea[3] = 240;
149         DisplayArea[5] = 240;
150 }
151
152 ///////////////////////////////////////////////////////////////////////////////
153 bool  GPU_init(void)
154 {
155         gpuReset();
156         
157         // s_invTable
158         for(int i=1;i<=(1<<TABLE_BITS);++i)
159         {
160                 double v = 1.0 / double(i);
161                 #ifdef GPU_TABLE_10_BITS
162                 v *= double(0xffffffff>>1);
163                 #else
164                 v *= double(0x80000000);
165                 #endif
166                 s_invTable[i-1]=s32(v);
167         }
168         return (0);
169 }
170
171 ///////////////////////////////////////////////////////////////////////////////
172 void  GPU_shutdown(void)
173 {
174 }
175
176 ///////////////////////////////////////////////////////////////////////////////
177 long  GPU_freeze(unsigned int bWrite, GPUFreeze_t* p2)
178 {
179         if (!p2) return (0);
180         if (p2->Version != 1) return (0);
181
182         if (bWrite)
183         {
184                 p2->GPU_gp1 = GPU_GP1;
185                 memcpy(p2->FrameBuffer, (u16*)GPU_FrameBuffer, FRAME_BUFFER_SIZE);
186                 return (1);
187         }
188         else
189         {
190                 GPU_GP1 = p2->GPU_gp1;
191                 memcpy((u16*)GPU_FrameBuffer, p2->FrameBuffer, FRAME_BUFFER_SIZE);
192                 return (1);
193         }
194         return (0);
195 }
196
197 ///////////////////////////////////////////////////////////////////////////////
198 //  GPU DMA comunication
199
200 ///////////////////////////////////////////////////////////////////////////////
201 u8 PacketSize[256] =
202 {
203         0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //              0-15
204         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //              16-31
205         3, 3, 3, 3, 6, 6, 6, 6, 4, 4, 4, 4, 8, 8, 8, 8, //              32-47
206         5, 5, 5, 5, 8, 8, 8, 8, 7, 7, 7, 7, 11, 11, 11, 11,     //      48-63
207         2, 2, 2, 2, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, //              64-79
208         3, 3, 3, 3, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, //              80-95
209         2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 2, 2, 2, 2, //              96-111
210         1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, //              112-127
211         3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //              128-
212         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //              144
213         2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //              160
214         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
215         2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
216         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
217         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
218         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  //
219 };
220
221 ///////////////////////////////////////////////////////////////////////////////
222 INLINE void gpuSendPacket()
223 {
224 #ifdef DEBUG_ANALYSIS
225         dbg_anacnt_GPU_sendPacket++;
226 #endif
227         gpuSendPacketFunction(PacketBuffer.U4[0]>>24);
228 }
229
230 ///////////////////////////////////////////////////////////////////////////////
231 INLINE void gpuCheckPacket(u32 uData)
232 {
233         if (PacketCount)
234         {
235                 PacketBuffer.U4[PacketIndex++] = uData;
236                 --PacketCount;
237         }
238         else
239         {
240                 PacketBuffer.U4[0] = uData;
241                 PacketCount = PacketSize[uData >> 24];
242                 PacketIndex = 1;
243         }
244         if (!PacketCount) gpuSendPacket();
245 }
246
247 ///////////////////////////////////////////////////////////////////////////////
248 void  GPU_writeDataMem(u32* dmaAddress, s32 dmaCount)
249 {
250 #ifdef DEBUG_ANALYSIS
251         dbg_anacnt_GPU_writeDataMem++;
252 #endif
253         pcsx4all_prof_pause(PCSX4ALL_PROF_CPU);
254         pcsx4all_prof_start_with_pause(PCSX4ALL_PROF_GPU,PCSX4ALL_PROF_HW_WRITE);
255         u32 data;
256         const u16 *VIDEO_END=(GPU_FrameBuffer+(FRAME_BUFFER_SIZE/2)-1);
257         GPU_GP1 &= ~0x14000000;
258
259         while (dmaCount) 
260         {
261                 if (FrameToWrite) 
262                 {
263                         while (dmaCount--) 
264                         {
265                                 data = *dmaAddress++;
266                                 if ((&pvram[px])>(VIDEO_END)) pvram-=512*1024;
267                                 pvram[px] = data;
268                                 if (++px>=x_end) 
269                                 {
270                                         px = 0;
271                                         pvram += 1024;
272                                         if (++py>=y_end) 
273                                         {
274                                                 FrameToWrite = 0;
275                                                 GPU_GP1 &= ~0x08000000;
276                                                 break;
277                                         }
278                                 }
279                                 if ((&pvram[px])>(VIDEO_END)) pvram-=512*1024;
280                                 pvram[px] = data>>16;
281                                 if (++px>=x_end) 
282                                 {
283                                         px = 0;
284                                         pvram += 1024;
285                                         if (++py>=y_end) 
286                                         {
287                                                 FrameToWrite = 0;
288                                                 GPU_GP1 &= ~0x08000000;
289                                                 break;
290                                         }
291                                 }
292                         }
293                 }
294                 else
295                 {
296                         data = *dmaAddress++;
297                         dmaCount--;
298                         gpuCheckPacket(data);
299                 }
300         }
301
302         GPU_GP1 = (GPU_GP1 | 0x14000000) & ~0x60000000;
303         fb_dirty = true;
304         pcsx4all_prof_end_with_resume(PCSX4ALL_PROF_GPU,PCSX4ALL_PROF_HW_WRITE);
305         pcsx4all_prof_resume(PCSX4ALL_PROF_CPU);
306 }
307
308 u32 *lUsedAddr[3];
309 INLINE int CheckForEndlessLoop(u32 *laddr)
310 {
311         if(laddr==lUsedAddr[1]) return 1;
312         if(laddr==lUsedAddr[2]) return 1;
313
314         if(laddr<lUsedAddr[0]) lUsedAddr[1]=laddr;
315         else                   lUsedAddr[2]=laddr;
316         lUsedAddr[0]=laddr;
317         return 0;
318 }
319
320 ///////////////////////////////////////////////////////////////////////////////
321 void  GPU_dmaChain(u32* baseAddr, u32 dmaVAddr)
322 {
323 #ifdef DEBUG_ANALYSIS
324         dbg_anacnt_GPU_dmaChain++;
325 #endif
326         pcsx4all_prof_start_with_pause(PCSX4ALL_PROF_GPU,PCSX4ALL_PROF_HW_WRITE);
327         u32 data, *address, count, offset;
328         unsigned int DMACommandCounter = 0;
329
330         GPU_GP1 &= ~0x14000000;
331         lUsedAddr[0]=lUsedAddr[1]=lUsedAddr[2]=(u32*)0x1fffff;
332         dmaVAddr &= 0x001FFFFF;
333         while (dmaVAddr != 0x1FFFFF)
334         {
335                 address = (baseAddr + (dmaVAddr >> 2));
336                 if(DMACommandCounter++ > 2000000) break;
337                 if(CheckForEndlessLoop(address)) break;
338                 data = *address++;
339                 count = (data >> 24);
340                 offset = data & 0x001FFFFF;
341                 if (dmaVAddr != offset) dmaVAddr = offset;
342                 else dmaVAddr = 0x1FFFFF;
343
344                 if(count>0) GPU_writeDataMem(address,count);
345         }
346         GPU_GP1 = (GPU_GP1 | 0x14000000) & ~0x60000000;
347         pcsx4all_prof_end_with_resume(PCSX4ALL_PROF_GPU,PCSX4ALL_PROF_HW_WRITE);
348 }
349
350 ///////////////////////////////////////////////////////////////////////////////
351 void  GPU_writeData(u32 data)
352 {
353         const u16 *VIDEO_END=(GPU_FrameBuffer+(FRAME_BUFFER_SIZE/2)-1);
354 #ifdef DEBUG_ANALYSIS
355         dbg_anacnt_GPU_writeData++;
356 #endif
357         pcsx4all_prof_pause(PCSX4ALL_PROF_CPU);
358         pcsx4all_prof_start_with_pause(PCSX4ALL_PROF_GPU,PCSX4ALL_PROF_HW_WRITE);
359         GPU_GP1 &= ~0x14000000;
360
361         if (FrameToWrite)
362         {
363                 if ((&pvram[px])>(VIDEO_END)) pvram-=512*1024;
364                 pvram[px]=(u16)data;
365                 if (++px>=x_end)
366                 {
367                         px = 0;
368                         pvram += 1024;
369                         if (++py>=y_end) 
370                         {
371                                 FrameToWrite = 0;
372                                 GPU_GP1 &= ~0x08000000;
373                         }
374                 }
375                 if (FrameToWrite)
376                 {
377                         if ((&pvram[px])>(VIDEO_END)) pvram-=512*1024;
378                         pvram[px]=data>>16;
379                         if (++px>=x_end)
380                         {
381                                 px = 0;
382                                 pvram += 1024;
383                                 if (++py>=y_end) 
384                                 {
385                                         FrameToWrite = 0;
386                                         GPU_GP1 &= ~0x08000000;
387                                 }
388                         }
389                 }
390         }
391         else
392         {
393                 gpuCheckPacket(data);
394         }
395         GPU_GP1 |= 0x14000000;
396         fb_dirty = true;
397         pcsx4all_prof_end_with_resume(PCSX4ALL_PROF_GPU,PCSX4ALL_PROF_HW_WRITE);
398         pcsx4all_prof_resume(PCSX4ALL_PROF_CPU);
399
400 }
401
402
403 ///////////////////////////////////////////////////////////////////////////////
404 void  GPU_readDataMem(u32* dmaAddress, s32 dmaCount)
405 {
406         const u16 *VIDEO_END=(GPU_FrameBuffer+(FRAME_BUFFER_SIZE/2)-1);
407 #ifdef DEBUG_ANALYSIS
408         dbg_anacnt_GPU_readDataMem++;
409 #endif
410         if(!FrameToRead) return;
411
412         pcsx4all_prof_start_with_pause(PCSX4ALL_PROF_GPU,PCSX4ALL_PROF_HW_WRITE);
413         GPU_GP1 &= ~0x14000000;
414         do 
415         {
416                 if ((&pvram[px])>(VIDEO_END)) pvram-=512*1024;
417                 // lower 16 bit
418                 u32 data = (unsigned long)pvram[px];
419
420                 if (++px>=x_end) 
421                 {
422                         px = 0;
423                         pvram += 1024;
424                 }
425
426                 if ((&pvram[px])>(VIDEO_END)) pvram-=512*1024;
427                 // higher 16 bit (always, even if it's an odd width)
428                 data |= (unsigned long)(pvram[px])<<16;
429                 
430                 *dmaAddress++ = data;
431
432                 if (++px>=x_end) 
433                 {
434                         px = 0;
435                         pvram += 1024;
436                         if (++py>=y_end) 
437                         {
438                                 FrameToRead = 0;
439                                 GPU_GP1 &= ~0x08000000;
440                                 break;
441                         }
442                 }
443         } while (--dmaCount);
444
445         GPU_GP1 = (GPU_GP1 | 0x14000000) & ~0x60000000;
446         pcsx4all_prof_end_with_resume(PCSX4ALL_PROF_GPU,PCSX4ALL_PROF_HW_WRITE);
447 }
448
449
450
451 ///////////////////////////////////////////////////////////////////////////////
452 u32  GPU_readData(void)
453 {
454         const u16 *VIDEO_END=(GPU_FrameBuffer+(FRAME_BUFFER_SIZE/2)-1);
455 #ifdef DEBUG_ANALYSIS
456         dbg_anacnt_GPU_readData++;
457 #endif
458         pcsx4all_prof_pause(PCSX4ALL_PROF_CPU);
459         pcsx4all_prof_start_with_pause(PCSX4ALL_PROF_GPU,PCSX4ALL_PROF_HW_READ);
460         GPU_GP1 &= ~0x14000000;
461         if (FrameToRead)
462         {
463                 if ((&pvram[px])>(VIDEO_END)) pvram-=512*1024;
464                 GP0 = pvram[px];
465                 if (++px>=x_end)
466                 {
467                         px = 0;
468                         pvram += 1024;
469                         if (++py>=y_end) 
470                         {
471                                 FrameToRead = 0;
472                                 GPU_GP1 &= ~0x08000000;
473                         }
474                 }
475                 if ((&pvram[px])>(VIDEO_END)) pvram-=512*1024;
476                 GP0 |= pvram[px]<<16;
477                 if (++px>=x_end)
478                 {
479                         px = 0;
480                         pvram +=1024;
481                         if (++py>=y_end) 
482                         {
483                                 FrameToRead = 0;
484                                 GPU_GP1 &= ~0x08000000;
485                         }
486                 }
487
488         }
489         GPU_GP1 |= 0x14000000;
490
491         pcsx4all_prof_end_with_resume(PCSX4ALL_PROF_GPU,PCSX4ALL_PROF_HW_READ);
492         pcsx4all_prof_resume(PCSX4ALL_PROF_CPU);
493         return (GP0);
494 }
495
496 ///////////////////////////////////////////////////////////////////////////////
497 u32     GPU_readStatus(void)
498 {
499 #ifdef DEBUG_ANALYSIS
500         dbg_anacnt_GPU_readStatus++;
501 #endif
502         return GPU_GP1;
503 }
504
505 ///////////////////////////////////////////////////////////////////////////////
506 void  GPU_writeStatus(u32 data)
507 {
508 #ifdef DEBUG_ANALYSIS
509         dbg_anacnt_GPU_writeStatus++;
510 #endif
511         pcsx4all_prof_pause(PCSX4ALL_PROF_CPU);
512         pcsx4all_prof_start_with_pause(PCSX4ALL_PROF_GPU,PCSX4ALL_PROF_HW_WRITE);
513         switch (data >> 24) {
514         case 0x00:
515                 gpuReset();
516                 break;
517         case 0x01:
518                 GPU_GP1 &= ~0x08000000;
519                 PacketCount = FrameToRead = FrameToWrite = 0;
520                 break;
521         case 0x02:
522                 GPU_GP1 &= ~0x08000000;
523                 PacketCount = FrameToRead = FrameToWrite = 0;
524                 break;
525         case 0x03:
526                 GPU_GP1 = (GPU_GP1 & ~0x00800000) | ((data & 1) << 23);
527                 break;
528         case 0x04:
529                 if (data == 0x04000000)
530                 PacketCount = 0;
531                 GPU_GP1 = (GPU_GP1 & ~0x60000000) | ((data & 3) << 29);
532                 break;
533         case 0x05:
534                 DisplayArea[0] = (data & 0x000003FF); //(short)(data & 0x3ff);
535                 DisplayArea[1] = ((data & 0x0007FC00)>>10); //(data & 0x000FFC00) >> 10; //(short)((data>>10)&0x1ff);
536                 fb_dirty = true;
537                 break;
538         case 0x07:
539                 DisplayArea[4] = data & 0x000003FF; //(short)(data & 0x3ff);
540                 DisplayArea[5] = (data & 0x000FFC00) >> 10; //(short)((data>>10) & 0x3ff);
541                 fb_dirty = true;
542                 break;
543         case 0x08:
544                 {
545                         GPU_GP1 = (GPU_GP1 & ~0x007F0000) | ((data & 0x3F) << 17) | ((data & 0x40) << 10);
546                         static u32 HorizontalResolution[8] = { 256, 368, 320, 384, 512, 512, 640, 640 };
547                         DisplayArea[2] = HorizontalResolution[(GPU_GP1 >> 16) & 7];
548                         static u32 VerticalResolution[4] = { 240, 480, 256, 480 };
549                         DisplayArea[3] = VerticalResolution[(GPU_GP1 >> 19) & 3];
550                         isPAL = (data & 0x08) ? true : false; // if 1 - PAL mode, else NTSC
551                 }
552                 fb_dirty = true;
553                 break;
554         case 0x10:
555                 switch (data & 0xffff) {
556                 case 0:
557                 case 1:
558                 case 3:
559                         GP0 = (DrawingArea[1] << 10) | DrawingArea[0];
560                         break;
561                 case 4:
562                         GP0 = ((DrawingArea[3]-1) << 10) | (DrawingArea[2]-1);
563                         break;
564                 case 6:
565                 case 5:
566                         GP0 = (DrawingOffset[1] << 11) | DrawingOffset[0];
567                         break;
568                 case 7:
569                         GP0 = 2;
570                         break;
571                 default:
572                         GP0 = 0;
573                 }
574                 break;
575         }
576         pcsx4all_prof_end_with_resume(PCSX4ALL_PROF_GPU,PCSX4ALL_PROF_HW_WRITE);
577         pcsx4all_prof_resume(PCSX4ALL_PROF_CPU);
578 }
579
580 #ifndef REARMED
581
582 // Blitting functions
583 #include "gpu_blit.h"
584
585 INLINE void gpuVideoOutput(void)
586 {
587         static s16 old_res_horz, old_res_vert, old_rgb24;
588         s16 h0, x0, y0, w0, h1;
589
590         x0 = DisplayArea[0];
591         y0 = DisplayArea[1];
592
593         w0 = DisplayArea[2];
594         h0 = DisplayArea[3];  // video mode
595
596         h1 = DisplayArea[5] - DisplayArea[4]; // display needed
597         if (h0 == 480) h1 = Min2(h1*2,480);
598
599         u16* dest_screen16 = SCREEN;
600         u16* src_screen16  = &((u16*)GPU_FrameBuffer)[FRAME_OFFSET(x0,y0)];
601         u32 isRGB24 = (GPU_GP1 & 0x00200000 ? 32 : 0);
602
603         /* Clear the screen if resolution changed to prevent interlacing and clipping to clash */
604         if( (w0 != old_res_horz || h1 != old_res_vert || (s16)isRGB24 != old_rgb24) )
605         {
606                 // Update old resolution
607                 old_res_horz = w0;
608                 old_res_vert = h1;
609                 old_rgb24 = (s16)isRGB24;
610                 // Finally, clear the screen for this special case
611                 video_clear();
612         }
613
614         //  Height centering
615         int sizeShift = 1;
616         if(h0==256) h0 = 240; else if(h0==480) sizeShift = 2;
617         if(h1>h0) { src_screen16 += ((h1-h0)>>sizeShift)*1024; h1 = h0; }
618         else if(h1<h0) dest_screen16 += ((h0-h1)>>sizeShift)*VIDEO_WIDTH;
619
620         /* Main blitter */
621         int incY = (h0==480) ? 2 : 1;
622         h0=(h0==480 ? 2048 : 1024);
623
624         if (!progressInterlace)
625         {
626                 const int li=linesInterlace;
627                 switch ( w0 )
628                 {
629                         case 256:
630                                 for(int y1=y0+h1; y0<y1; y0+=incY)
631                                 {
632                                         if( 0 == (y0&li) ) GPU_BlitWWDWW(       src_screen16,   dest_screen16, isRGB24);
633                                         dest_screen16 += VIDEO_WIDTH;
634                                         src_screen16  += h0;
635                                 }
636                                 break;
637                         case 368:
638                                 for(int y1=y0+h1; y0<y1; y0+=incY)
639                                 {
640                                         if( 0 == (y0&li) ) GPU_BlitWWWWWWWWS(   src_screen16,   dest_screen16, isRGB24, 4);
641                                         dest_screen16 += VIDEO_WIDTH;
642                                         src_screen16  += h0;
643                                 }
644                                 break;
645                         case 320:
646                                 for(int y1=y0+h1; y0<y1; y0+=incY)
647                                 {
648                                         if( 0 == (y0&li) ) GPU_BlitWW(  src_screen16,   dest_screen16, isRGB24);
649                                         dest_screen16 += VIDEO_WIDTH;
650                                         src_screen16  += h0;
651                                 }
652                                 break;
653                         case 384:
654                                 for(int y1=y0+h1; y0<y1; y0+=incY)
655                                 {
656                                         if( 0 == (y0&li) ) GPU_BlitWWWWWS(      src_screen16,   dest_screen16, isRGB24);
657                                         dest_screen16 += VIDEO_WIDTH;
658                                         src_screen16  += h0;
659                                 }
660                                 break;
661                         case 512:
662                                 for(int y1=y0+h1; y0<y1; y0+=incY)
663                                 {
664                                         if( 0 == (y0&li) ) GPU_BlitWWSWWSWS(    src_screen16, dest_screen16, isRGB24);
665                                         dest_screen16 += VIDEO_WIDTH;
666                                         src_screen16  += h0;
667                                 }
668                                 break;
669                         case 640:
670                                 for(int y1=y0+h1; y0<y1; y0+=incY)
671                                 {
672                                         if( 0 == (y0&li) ) GPU_BlitWS(  src_screen16, dest_screen16, isRGB24);
673                                         dest_screen16 += VIDEO_WIDTH;
674                                         src_screen16  += h0;
675                                 }
676                                 break;
677                 }
678         }
679         else
680         {
681                 const int li=linesInterlace;
682                 bool flag=progressInterlace_flag; /* progressive interlace */
683                 switch ( w0 )
684                 {
685                         case 256:
686                                 for(int y1=y0+h1; y0<y1; y0+=incY)
687                                 {
688                                         if(( 0 == (y0&li) ) && (flag=!flag)) GPU_BlitWWDWW(     src_screen16,   dest_screen16, isRGB24);
689                                         dest_screen16 += VIDEO_WIDTH;
690                                         src_screen16  += h0;
691                                 }
692                                 break;
693                         case 368:
694                                 for(int y1=y0+h1; y0<y1; y0+=incY)
695                                 {
696                                         if(( 0 == (y0&li) ) && (flag=!flag)) GPU_BlitWWWWWWWWS( src_screen16,   dest_screen16, isRGB24, 4);
697                                         dest_screen16 += VIDEO_WIDTH;
698                                         src_screen16  += h0;
699                                 }
700                                 break;
701                         case 320:
702                                 for(int y1=y0+h1; y0<y1; y0+=incY)
703                                 {
704                                         if(( 0 == (y0&li) ) && (flag=!flag)) GPU_BlitWW(        src_screen16,   dest_screen16, isRGB24);
705                                         dest_screen16 += VIDEO_WIDTH;
706                                         src_screen16  += h0;
707                                 }
708                                 break;
709                         case 384:
710                                 for(int y1=y0+h1; y0<y1; y0+=incY)
711                                 {
712                                         if(( 0 == (y0&li) ) && (flag=!flag)) GPU_BlitWWWWWS(    src_screen16,   dest_screen16, isRGB24);
713                                         dest_screen16 += VIDEO_WIDTH;
714                                         src_screen16  += h0;
715                                 }
716                                 break;
717                         case 512:
718                                 for(int y1=y0+h1; y0<y1; y0+=incY)
719                                 {
720                                         if(( 0 == (y0&li) ) && (flag=!flag)) GPU_BlitWWSWWSWS(  src_screen16, dest_screen16, isRGB24);
721                                         dest_screen16 += VIDEO_WIDTH;
722                                         src_screen16  += h0;
723                                 }
724                                 break;
725                         case 640:
726                                 for(int y1=y0+h1; y0<y1; y0+=incY)
727                                 {
728                                         if(( 0 == (y0&li) ) && (flag=!flag)) GPU_BlitWS(        src_screen16, dest_screen16, isRGB24);
729                                         dest_screen16 += VIDEO_WIDTH;
730                                         src_screen16  += h0;
731                                 }
732                                 break;
733                 }
734                 progressInterlace_flag=!flag;
735         }
736         video_flip();
737 }
738
739 ///////////////////////////////////////////////////////////////////////////////
740 void  GPU_updateLace(void)
741 {
742 #ifdef  ENABLE_GPU_LOG_SUPPORT
743         fprintf(stdout,"GPU_updateLace()\n");
744 #endif
745 #ifdef DEBUG_ANALYSIS
746         dbg_anacnt_GPU_updateLace++;
747 #endif
748         pcsx4all_prof_start_with_pause(PCSX4ALL_PROF_GPU,PCSX4ALL_PROF_COUNTERS);
749 #ifdef PROFILER_PCSX4ALL
750         pcsx4all_prof_frames++;
751 #endif
752 #ifdef DEBUG_FRAME
753         if(isdbg_frame())
754         {
755                 static int passed=0;
756                 if (!passed) dbg_enable();
757                 else pcsx4all_exit();
758                 passed++;
759         }
760 #endif
761
762         // Frame skip table
763         static const unsigned char skipTable[12][12] =
764         {
765                 { 0,0,0,0,0,0,0,0,0,0,0,0 },
766                 { 0,0,0,0,0,0,0,0,0,0,0,1 },
767                 { 0,0,0,0,0,1,0,0,0,0,0,1 },
768                 { 0,0,0,1,0,0,0,1,0,0,0,1 },
769                 { 0,0,1,0,0,1,0,0,1,0,0,1 },
770                 { 0,1,0,0,1,0,1,0,0,1,0,1 },
771                 { 0,1,0,1,0,1,0,1,0,1,0,1 },
772                 { 0,1,0,1,1,0,1,0,1,1,0,1 },
773                 { 0,1,1,0,1,1,0,1,1,0,1,1 },
774                 { 0,1,1,1,0,1,1,1,0,1,1,1 },
775                 { 0,1,1,1,1,1,0,1,1,1,1,1 },
776                 { 0,1,1,1,1,1,1,1,1,1,1,1 }
777         };
778         
779         // Interlace bit toggle
780         GPU_GP1 ^= 0x80000000;
781
782         // Update display
783         if ((!skipFrame) && (!isSkip) && (!(((GPU_GP1&0x08000000))||((GPU_GP1&0x00800000)))))
784         {
785                 gpuVideoOutput(); // Display updated
786
787                 if (DisplayArea[3] == 480)
788                 {
789                         if (linesInterlace_user) linesInterlace = 3; // 1/4 of lines
790                         else linesInterlace = 1; // if 480 we only need half of lines
791                 }
792                 else if (linesInterlace != linesInterlace_user)
793                 {
794                         linesInterlace = linesInterlace_user; // resolution changed from 480 to lower one
795                         video_clear();
796                 }
797
798                 // Limit FPS
799                 if (frameLimit)
800                 {
801                         static unsigned next=get_ticks();
802                         unsigned now=get_ticks();
803                         if (!skipFrame)
804                         {
805                                 if (now<next) wait_ticks(next-now);
806                         }
807                         next+=(isPAL?(1000000/50):((unsigned)(1000000.0/59.94)));
808                 }
809         }
810
811         // Show FPS statistics
812         if (show_fps)
813         {
814                 static u32 real_fps=0;
815                 static u32 prev=get_ticks();
816                 static char msg[24]="FPS=000/00 SPD=000%";
817                 u32 now=get_ticks();
818                 real_fps++;
819                 if ((now-prev)>=1000000)
820                 {
821                         u32 expected_fps=(isPAL?50:60);
822                         sprintf(msg,"FPS=%3d/%2d SPD=%3d%%",((real_fps*(12-skipCount))/12),((expected_fps*(12-skipCount))/12),((real_fps*100)/expected_fps));
823                         prev=now;
824                         real_fps=0;
825                 }
826                 port_printf(5,5,msg);
827         }
828
829         // Update frame-skip
830         if (!alt_fps)
831         {
832                 // Video frame-skip
833                 skipFrame=skipTable[skipCount][skCount];
834                 skCount--; if (skCount<0) skCount=11;
835                 isSkip=skipFrame;
836         }
837         else
838         {
839                 // Game frame-skip
840                 if (!isSkip)
841                 {
842                         skipFrame=skipTable[skipCount][skCount];
843                         skCount--; if (skCount<0) skCount=11;
844                         isSkip=true;
845                 }
846         }
847
848         pcsx4all_prof_end_with_resume(PCSX4ALL_PROF_GPU,PCSX4ALL_PROF_COUNTERS);
849 }
850
851 #else
852
853 #include "../../frontend/plugin_lib.h"
854
855 extern "C" {
856
857 extern void bgr555_to_rgb565(void *dst, void *src, int bytes);
858 extern void bgr888_to_rgb888(void *dst, void *src, int bytes);
859 static const struct rearmed_cbs *cbs;
860 static void *screen_buf;
861
862 static void blit(void)
863 {
864         static s16 old_res_horz, old_res_vert, old_rgb24;
865         s16 isRGB24 = (GPU_GP1 & 0x00200000) ? 1 : 0;
866         s16 h0, x0, y0, w0, h1;
867         u16 *srcs;
868         u8  *dest;
869
870         x0 = DisplayArea[0] & ~3; // alignment needed by blitter
871         y0 = DisplayArea[1];
872         srcs = &((u16*)GPU_FrameBuffer)[FRAME_OFFSET(x0,y0)];
873
874         w0 = DisplayArea[2];
875         h0 = DisplayArea[3];  // video mode
876
877         h1 = DisplayArea[5] - DisplayArea[4]; // display needed
878         if (h0 == 480) h1 = Min2(h1*2,480);
879
880         if (h1 <= 0)
881                 return;
882
883         if (w0 != old_res_horz || h1 != old_res_vert || isRGB24 != old_rgb24)
884         {
885                 old_res_horz = w0;
886                 old_res_vert = h1;
887                 old_rgb24 = (s16)isRGB24;
888                 screen_buf = cbs->pl_fbdev_set_mode(w0, h1, isRGB24 ? 24 : 16);
889         }
890         dest = (u8 *)screen_buf;
891
892         if (isRGB24)
893         {
894                 for (; h1-- > 0; dest += w0 * 3, srcs += 1024)
895                 {
896                         bgr888_to_rgb888(dest, srcs, w0 * 3);
897                 }
898         }
899         else
900         {
901                 for (; h1-- > 0; dest += w0 * 2, srcs += 1024)
902                 {
903                         bgr555_to_rgb565(dest, srcs, w0 * 2);
904                 }
905         }
906
907         screen_buf = cbs->pl_fbdev_flip();
908 }
909
910 void GPU_updateLace(void)
911 {
912         // Interlace bit toggle
913         GPU_GP1 ^= 0x80000000;
914
915         if (!fb_dirty || (GPU_GP1&0x08800000))
916                 return;
917
918         if (!isSkip) {
919                 blit();
920
921                 fb_dirty = false;
922                 if (*cbs->fskip_option)
923                         isSkip = true;
924         }
925         else
926                 isSkip = false;
927 }
928
929 long GPUopen(unsigned long *, char *, char *)
930 {
931         cbs->pl_fbdev_open();
932         screen_buf = cbs->pl_fbdev_flip();
933         return 0;
934 }
935
936 long GPUclose(void)
937 {
938         cbs->pl_fbdev_close();
939         return 0;
940 }
941
942 long GPUfreeze(unsigned int ulGetFreezeData, GPUFreeze_t* p2)
943 {
944         if (ulGetFreezeData > 1)
945                 return 0;
946
947         return GPU_freeze(ulGetFreezeData, p2);
948 }
949
950 void GPUrearmedCallbacks(const struct rearmed_cbs *cbs_)
951 {
952         cbs = cbs_;
953 }
954
955 } /* extern "C" */
956
957 #endif