dfxvideo: patch up some unsafe macros
[pcsx_rearmed.git] / plugins / dfxvideo / gpulib_if.c
1 /***************************************************************************
2     copyright            : (C) 2001 by Pete Bernert, 2011 notaz
3
4  ***************************************************************************/
5 /***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version. See also the license.txt file for *
11  *   additional informations.                                              *
12  *                                                                         *
13  ***************************************************************************/
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "../gpulib/gpu.h"
19 #include "../../include/arm_features.h"
20
21 #ifdef THREAD_RENDERING
22 #include "../gpulib/gpulib_thread_if.h"
23 #define do_cmd_list real_do_cmd_list
24 #define renderer_init real_renderer_init
25 #define renderer_finish real_renderer_finish
26 #define renderer_sync_ecmds real_renderer_sync_ecmds
27 #define renderer_update_caches real_renderer_update_caches
28 #define renderer_flush_queues real_renderer_flush_queues
29 #define renderer_set_interlace real_renderer_set_interlace
30 #define renderer_set_config real_renderer_set_config
31 #define renderer_notify_res_change real_renderer_notify_res_change
32 #define renderer_notify_update_lace real_renderer_notify_update_lace
33 #define renderer_sync real_renderer_sync
34 #define ex_regs scratch_ex_regs
35 #endif
36
37 #define u32 uint32_t
38
39 #define INFO_TW        0
40 #define INFO_DRAWSTART 1
41 #define INFO_DRAWEND   2
42 #define INFO_DRAWOFF   3
43
44 #define SHADETEXBIT(x) ((x>>24) & 0x1)
45 #define SEMITRANSBIT(x) ((x>>25) & 0x1)
46 #define PSXRGB(r,g,b) ((g<<10)|(b<<5)|r)
47
48 #define DATAREGISTERMODES unsigned short
49
50 #define DR_NORMAL        0
51 #define DR_VRAMTRANSFER  1
52
53 #define GPUSTATUS_READYFORVRAM        0x08000000
54
55 // byteswappings
56
57 #define SWAP16(x) __builtin_bswap16(x)
58 #define SWAP32(x) __builtin_bswap32(x)
59
60 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
61
62 // big endian config
63 #define HOST2LE32(x) SWAP32(x)
64 #define HOST2BE32(x) (x)
65 #define LE2HOST32(x) SWAP32(x)
66 #define BE2HOST32(x) (x)
67
68 #define HOST2LE16(x) SWAP16(x)
69 #define HOST2BE16(x) (x)
70 #define LE2HOST16(x) SWAP16(x)
71 #define BE2HOST16(x) (x)
72
73 #else
74
75 // little endian config
76 #define HOST2LE32(x) (x)
77 #define HOST2BE32(x) SWAP32(x)
78 #define LE2HOST32(x) (x)
79 #define BE2HOST32(x) SWAP32(x)
80
81 #define HOST2LE16(x) (x)
82 #define HOST2BE16(x) SWAP16(x)
83 #define LE2HOST16(x) (x)
84 #define BE2HOST16(x) SWAP16(x)
85
86 #endif
87
88 #define GETLEs16(X) ((int16_t)GETLE16((uint16_t *)(X)))
89
90 #define GETLE16(X) LE2HOST16(*(uint16_t *)(X))
91 #define GETLE32_(X) LE2HOST32(*(uint32_t *)(X))
92 #define PUTLE16(X, Y) do{*((uint16_t *)(X))=HOST2LE16((uint16_t)(Y));}while(0)
93 #define PUTLE32_(X, Y) do{*((uint32_t *)(X))=HOST2LE32((uint32_t)(Y));}while(0)
94 #if defined(__arm__) && !defined(HAVE_ARMV6)
95 // for (very) old ARMs with no unaligned loads?
96 #define GETLE32(X) (*(uint16_t *)(X)|((uint32_t)((uint16_t *)(X))[1]<<16))
97 #define PUTLE32(X, Y) do{uint16_t *p_=(uint16_t *)(X);uint32_t y_=Y;p_[0]=y_;p_[1]=y_>>16;}while(0)
98 #else
99 #define GETLE32 GETLE32_
100 #define PUTLE32 PUTLE32_
101 #endif
102
103 /////////////////////////////////////////////////////////////////////////////
104
105 typedef struct VRAMLOADTTAG
106 {
107  short x;
108  short y;
109  short Width;
110  short Height;
111  short RowsRemaining;
112  short ColsRemaining;
113  unsigned short *ImagePtr;
114 } VRAMLoad_t;
115
116 /////////////////////////////////////////////////////////////////////////////
117
118 typedef struct PSXPOINTTAG
119 {
120  int32_t x;
121  int32_t y;
122 } PSXPoint_t;
123
124 typedef struct PSXSPOINTTAG
125 {
126  short x;
127  short y;
128 } PSXSPoint_t;
129
130 typedef struct PSXRECTTAG
131 {
132  short x0;
133  short x1;
134  short y0;
135  short y1;
136 } PSXRect_t;
137
138 // linux defines for some windows stuff
139
140 #define FALSE 0
141 #define TRUE 1
142 #define BOOL unsigned short
143 #define LOWORD(l)           ((unsigned short)(l))
144 #define HIWORD(l)           ((unsigned short)(((uint32_t)(l) >> 16) & 0xFFFF))
145 #define max(a,b)            (((a) > (b)) ? (a) : (b))
146 #define min(a,b)            (((a) < (b)) ? (a) : (b))
147 #define DWORD uint32_t
148 #ifndef __int64
149 #define __int64 long long int
150 #endif
151
152 typedef struct RECTTAG
153 {
154  int left;
155  int top;
156  int right;
157  int bottom;
158 }RECT;
159
160 /////////////////////////////////////////////////////////////////////////////
161
162 typedef struct TWINTAG
163 {
164  PSXRect_t  Position;
165  int xmask, ymask;
166 } TWin_t;
167
168 /////////////////////////////////////////////////////////////////////////////
169
170 typedef struct PSXDISPLAYTAG
171 {
172  PSXPoint_t  DisplayModeNew;
173  PSXPoint_t  DisplayMode;
174  PSXPoint_t  DisplayPosition;
175  PSXPoint_t  DisplayEnd;
176
177  int32_t        Double;
178  int32_t        Height;
179  int32_t        PAL;
180  int32_t        InterlacedNew;
181  int32_t        Interlaced;
182  int32_t        RGB24New;
183  int32_t        RGB24;
184  PSXSPoint_t DrawOffset;
185  int32_t        Disabled;
186  PSXRect_t   Range;
187
188 } PSXDisplay_t;
189
190 /////////////////////////////////////////////////////////////////////////////
191
192 // draw.c
193
194 extern int32_t           GlobalTextAddrX,GlobalTextAddrY,GlobalTextTP;
195 extern int32_t           GlobalTextABR,GlobalTextPAGE;
196 extern short          ly0,lx0,ly1,lx1,ly2,lx2,ly3,lx3;
197 extern long           lLowerpart;
198 extern BOOL           bCheckMask;
199 extern unsigned short sSetMask;
200 extern unsigned long  lSetMask;
201 extern short          g_m1;
202 extern short          g_m2;
203 extern short          g_m3;
204 extern short          DrawSemiTrans;
205
206 // prim.c
207
208 extern BOOL           bUsingTWin;
209 extern TWin_t         TWin;
210 extern void (*primTableJ[256])(unsigned char *);
211 extern void (*primTableSkip[256])(unsigned char *);
212 extern unsigned short  usMirror;
213 extern int            iDither;
214 extern uint32_t  dwCfgFixes;
215 extern uint32_t  dwActFixes;
216 extern int            iUseFixes;
217 extern int            iUseDither;
218 extern BOOL           bDoVSyncUpdate;
219 extern int32_t           drawX;
220 extern int32_t           drawY;
221 extern int32_t           drawW;
222 extern int32_t           drawH;
223
224 // gpu.h
225
226 #define OPAQUEON   10
227 #define OPAQUEOFF  11
228
229 #define KEY_RESETTEXSTORE 1
230 #define KEY_SHOWFPS       2
231 #define KEY_RESETOPAQUE   4
232 #define KEY_RESETDITHER   8
233 #define KEY_RESETFILTER   16
234 #define KEY_RESETADVBLEND 32
235 #define KEY_BADTEXTURES   128
236 #define KEY_CHECKTHISOUT  256
237
238 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
239 #define RED(x) (x & 0xff)
240 #define BLUE(x) ((x>>16) & 0xff)
241 #define GREEN(x) ((x>>8) & 0xff)
242 #define COLOR(x) (x & 0xffffff)
243 #else
244 #define RED(x) ((x>>24) & 0xff)
245 #define BLUE(x) ((x>>8) & 0xff)
246 #define GREEN(x) ((x>>16) & 0xff)
247 #define COLOR(x) SWAP32(x & 0xffffff)
248 #endif
249
250 PSXDisplay_t      PSXDisplay;
251 unsigned char  *psxVub;
252 signed   char  *psxVsb;
253 unsigned short *psxVuw;
254 unsigned short *psxVuw_eom;
255 signed   short *psxVsw;
256 uint32_t *psxVul;
257 int32_t  *psxVsl;
258
259 long              lGPUstatusRet;
260 uint32_t          lGPUInfoVals[16];
261
262 VRAMLoad_t        VRAMWrite;
263 VRAMLoad_t        VRAMRead;
264
265 DATAREGISTERMODES DataWriteMode;
266 DATAREGISTERMODES DataReadMode;
267
268 BOOL           bCheckMask = FALSE;
269 unsigned short sSetMask = 0;
270 unsigned long  lSetMask = 0;
271 long           lLowerpart;
272
273 #include "soft.c"
274 #include "prim.c"
275
276 /////////////////////////////////////////////////////////////////////////////
277
278 static void set_vram(void *vram)
279 {
280  psxVub=vram;
281
282  psxVsb=(signed char *)psxVub;                         // different ways of accessing PSX VRAM
283  psxVsw=(signed short *)psxVub;
284  psxVsl=(int32_t *)psxVub;
285  psxVuw=(unsigned short *)psxVub;
286  psxVul=(uint32_t *)psxVub;
287
288  psxVuw_eom=psxVuw+1024*512;                           // pre-calc of end of vram
289 }
290
291 int renderer_init(void)
292 {
293  set_vram(gpu.vram);
294
295  PSXDisplay.RGB24        = FALSE;                      // init some stuff
296  PSXDisplay.Interlaced   = FALSE;
297  PSXDisplay.DrawOffset.x = 0;
298  PSXDisplay.DrawOffset.y = 0;
299  PSXDisplay.DisplayMode.x= 320;
300  PSXDisplay.DisplayMode.y= 240;
301  PSXDisplay.Disabled     = FALSE;
302  PSXDisplay.Range.x0=0;
303  PSXDisplay.Range.x1=0;
304  PSXDisplay.Double = 1;
305
306  DataWriteMode = DR_NORMAL;
307  lGPUstatusRet = 0x14802000;
308
309  return 0;
310 }
311
312 void renderer_finish(void)
313 {
314 }
315
316 void renderer_notify_res_change(void)
317 {
318 }
319
320 extern const unsigned char cmd_lengths[256];
321
322 int do_cmd_list(uint32_t *list, int list_len, int *last_cmd)
323 {
324   unsigned int cmd = 0, len;
325   uint32_t *list_start = list;
326   uint32_t *list_end = list + list_len;
327
328   for (; list < list_end; list += 1 + len)
329   {
330     cmd = GETLE32(list) >> 24;
331     len = cmd_lengths[cmd];
332     if (list + 1 + len > list_end) {
333       cmd = -1;
334       break;
335     }
336
337 #ifndef TEST
338     if (cmd == 0xa0 || cmd == 0xc0)
339       break; // image i/o, forward to upper layer
340     else if ((cmd & 0xf8) == 0xe0)
341       gpu.ex_regs[cmd & 7] = GETLE32(list);
342 #endif
343
344     primTableJ[cmd]((void *)list);
345
346     switch(cmd)
347     {
348       case 0x48 ... 0x4F:
349       {
350         u32 num_vertexes = 2;
351         u32 *list_position = &(list[3]);
352
353         while(1)
354         {
355           if(list_position >= list_end) {
356             cmd = -1;
357             goto breakloop;
358           }
359
360           if((*list_position & HOST2LE32(0xf000f000)) == HOST2LE32(0x50005000))
361             break;
362
363           list_position++;
364           num_vertexes++;
365         }
366
367         len += (num_vertexes - 2);
368         break;
369       }
370
371       case 0x58 ... 0x5F:
372       {
373         u32 num_vertexes = 2;
374         u32 *list_position = &(list[4]);
375
376         while(1)
377         {
378           if(list_position >= list_end) {
379             cmd = -1;
380             goto breakloop;
381           }
382
383           if((*list_position & HOST2LE32(0xf000f000)) == HOST2LE32(0x50005000))
384             break;
385
386           list_position += 2;
387           num_vertexes++;
388         }
389
390         len += (num_vertexes - 2) * 2;
391         break;
392       }
393
394 #ifdef TEST
395       case 0xA0:          //  sys -> vid
396       {
397         short *slist = (void *)list;
398         u32 load_width = LE2HOST32(slist[4]);
399         u32 load_height = LE2HOST32(slist[5]);
400         u32 load_size = load_width * load_height;
401
402         len += load_size / 2;
403         break;
404       }
405 #endif
406     }
407   }
408
409 breakloop:
410   gpu.ex_regs[1] &= ~0x1ff;
411   gpu.ex_regs[1] |= lGPUstatusRet & 0x1ff;
412
413   *last_cmd = cmd;
414   return list - list_start;
415 }
416
417 void renderer_sync_ecmds(uint32_t *ecmds)
418 {
419   cmdTexturePage((unsigned char *)&ecmds[1]);
420   cmdTextureWindow((unsigned char *)&ecmds[2]);
421   cmdDrawAreaStart((unsigned char *)&ecmds[3]);
422   cmdDrawAreaEnd((unsigned char *)&ecmds[4]);
423   cmdDrawOffset((unsigned char *)&ecmds[5]);
424   cmdSTP((unsigned char *)&ecmds[6]);
425 }
426
427 void renderer_update_caches(int x, int y, int w, int h)
428 {
429 }
430
431 void renderer_flush_queues(void)
432 {
433 }
434
435 void renderer_set_interlace(int enable, int is_odd)
436 {
437 }
438
439 void renderer_sync(void)
440 {
441 }
442
443 void renderer_notify_update_lace(int updated)
444 {
445 }
446
447 #include "../../frontend/plugin_lib.h"
448
449 void renderer_set_config(const struct rearmed_cbs *cbs)
450 {
451  iUseDither = cbs->gpu_peops.iUseDither;
452  dwActFixes = cbs->gpu_peops.dwActFixes;
453  if (cbs->pl_set_gpu_caps)
454   cbs->pl_set_gpu_caps(0);
455  set_vram(gpu.vram);
456 }