gpulib: eliminate list scan-ahead
[pcsx_rearmed.git] / plugins / gpu_unai / gpulib_if.cpp
1 /***************************************************************************
2 *   Copyright (C) 2010 PCSX4ALL Team                                      *
3 *   Copyright (C) 2010 Unai                                               *
4 *   Copyright (C) 2011 notaz                                              *
5 *                                                                         *
6 *   This program is free software; you can redistribute it and/or modify  *
7 *   it under the terms of the GNU General Public License as published by  *
8 *   the Free Software Foundation; either version 2 of the License, or     *
9 *   (at your option) any later version.                                   *
10 *                                                                         *
11 *   This program is distributed in the hope that it will be useful,       *
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14 *   GNU General Public License for more details.                          *
15 *                                                                         *
16 *   You should have received a copy of the GNU General Public License     *
17 *   along with this program; if not, write to the                         *
18 *   Free Software Foundation, Inc.,                                       *
19 *   51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA.           *
20 ***************************************************************************/
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "../gpulib/gpu.h"
26
27 #define u8 uint8_t
28 #define s8 int8_t
29 #define u16 uint16_t
30 #define s16 int16_t
31 #define u32 uint32_t
32 #define s32 int32_t
33 #define s64 int64_t
34
35 #define INLINE static
36
37 #define FRAME_BUFFER_SIZE  (1024*512*2)
38 #define FRAME_WIDTH        1024
39 #define FRAME_HEIGHT       512
40 #define FRAME_OFFSET(x,y)  (((y)<<10)+(x))
41
42 #define isSkip 0 /* skip frame (info coming from GPU) */
43 #define alt_fps 0
44 static int linesInterlace;  /* internal lines interlace */
45 static int force_interlace;
46
47 static bool light = true; /* lighting */
48 static bool blend = true; /* blending */
49 static bool FrameToRead = false; /* load image in progress */
50 static bool FrameToWrite = false; /* store image in progress */
51
52 static bool enableAbbeyHack = false; /* Abe's Odyssey hack */
53
54 static u8 BLEND_MODE;
55 static u8 TEXT_MODE;
56 static u8 Masking;
57
58 static u16 PixelMSB;
59 static u16 PixelData;
60
61 ///////////////////////////////////////////////////////////////////////////////
62 //  GPU Global data
63 ///////////////////////////////////////////////////////////////////////////////
64
65 //  Dma Transfers info
66 static s32              px,py;
67 static s32              x_end,y_end;
68 static u16*  pvram;
69
70 static s32 PacketCount;
71 static s32 PacketIndex;
72
73 //  Rasterizer status
74 static u32 TextureWindow [4];
75 static u32 DrawingArea   [4];
76 static u32 DrawingOffset [2];
77
78 static u16* TBA;
79 static u16* CBA;
80
81 //  Inner Loops
82 static s32   u4, du4;
83 static s32   v4, dv4;
84 static s32   r4, dr4;
85 static s32   g4, dg4;
86 static s32   b4, db4;
87 static u32   lInc;
88 static u32   tInc, tMsk;
89
90 union GPUPacket
91 {
92         u32 U4[16];
93         s32 S4[16];
94         u16 U2[32];
95         s16 S2[32];
96         u8  U1[64];
97         s8  S1[64];
98 };
99
100 static GPUPacket PacketBuffer;
101 static u16  *GPU_FrameBuffer;
102 static u32   GPU_GP1;
103
104 ///////////////////////////////////////////////////////////////////////////////
105
106 #include "../gpu_unai/gpu_fixedpoint.h"
107
108 //  Inner loop driver instanciation file
109 #include "../gpu_unai/gpu_inner.h"
110
111 //  GPU Raster Macros
112 #define GPU_RGB16(rgb)        ((((rgb)&0xF80000)>>9)|(((rgb)&0xF800)>>6)|(((rgb)&0xF8)>>3))
113
114 #define GPU_EXPANDSIGN_POLY(x)  (((s32)(x)<<20)>>20)
115 //#define GPU_EXPANDSIGN_POLY(x)  (((s32)(x)<<21)>>21)
116 #define GPU_EXPANDSIGN_SPRT(x)  (((s32)(x)<<21)>>21)
117
118 //#define       GPU_TESTRANGE(x)      { if((u32)(x+1024) > 2047) return; }
119 #define GPU_TESTRANGE(x)      { if ((x<-1023) || (x>1023)) return; }
120
121 #define GPU_SWAP(a,b,t) {(t)=(a);(a)=(b);(b)=(t);}
122
123 // GPU internal image drawing functions
124 #include "../gpu_unai/gpu_raster_image.h"
125
126 // GPU internal line drawing functions
127 #include "../gpu_unai/gpu_raster_line.h"
128
129 // GPU internal polygon drawing functions
130 #include "../gpu_unai/gpu_raster_polygon.h"
131
132 // GPU internal sprite drawing functions
133 #include "../gpu_unai/gpu_raster_sprite.h"
134
135 // GPU command buffer execution/store
136 #include "../gpu_unai/gpu_command.h"
137
138 #define unai_do_prim(cmd, list, len) \
139   memcpy(PacketBuffer.U4, list, (len) * 4); \
140   gpuSendPacketFunction(cmd)
141
142 /////////////////////////////////////////////////////////////////////////////
143
144 int renderer_init(void)
145 {
146         GPU_FrameBuffer = (u16 *)gpu.vram;
147
148         // s_invTable
149         for(int i=1;i<=(1<<TABLE_BITS);++i)
150         {
151                 double v = 1.0 / double(i);
152                 #ifdef GPU_TABLE_10_BITS
153                 v *= double(0xffffffff>>1);
154                 #else
155                 v *= double(0x80000000);
156                 #endif
157                 s_invTable[i-1]=s32(v);
158         }
159
160         return 0;
161 }
162
163 extern const unsigned char cmd_lengths[256];
164
165 int do_cmd_list(unsigned int *list, int list_len, int *last_cmd)
166 {
167   unsigned int cmd = 0, len;
168   unsigned int *list_start = list;
169   unsigned int *list_end = list + list_len;
170
171   linesInterlace = force_interlace;
172 #ifndef __ARM_ARCH_7A__ /* XXX */
173   linesInterlace |= gpu.status.interlace;
174 #endif
175
176   for (; list < list_end; list += 1 + len)
177   {
178     cmd = *list >> 24;
179     len = cmd_lengths[cmd];
180     if (list + 1 + len > list_end) {
181       cmd = -1;
182       break;
183     }
184
185 #ifndef TEST
186     if (cmd == 0xa0 || cmd == 0xc0)
187       break; // image i/o, forward to upper layer
188     else if ((cmd & 0xf8) == 0xe0)
189       gpu.ex_regs[cmd & 7] = list[0];
190 #endif
191
192     switch(cmd)
193     {
194       case 0x48 ... 0x4F:
195       {
196         u32 num_vertexes = 1;
197         u32 *list_position = &(list[2]);
198         u32 PRIM = cmd;
199
200         memcpy(&PacketBuffer.U4[0], list, 4 * 3);
201         gpuDrawLF(gpuPixelDrivers [ (Blending_Mode | Masking | Blending | (PixelMSB>>3)) >> 1]);
202
203         while(1)
204         {
205           PacketBuffer.U4[1] = PacketBuffer.U4[2];
206           PacketBuffer.U4[2] = *list_position++;
207           gpuDrawLF(gpuPixelDrivers [ (Blending_Mode | Masking | Blending | (PixelMSB>>3)) >> 1]);
208
209           num_vertexes++;
210           if((*list_position & 0xf000f000) == 0x50005000 || list_position >= list_end)
211             break;
212         }
213
214         len += (num_vertexes - 2);
215         break;
216       }
217
218       case 0x58 ... 0x5F:
219       {
220         u32 num_vertexes = 1;
221         u32 *list_position = &(list[2]);
222         u32 PRIM = cmd;
223
224         memcpy(&PacketBuffer.U4[0], list, 4 * 4);
225         gpuDrawLG(gpuPixelDrivers [ (Blending_Mode | Masking | Blending | (PixelMSB>>3)) >> 1]);
226
227         while(1)
228         {
229           PacketBuffer.U4[0] = PacketBuffer.U4[2];
230           PacketBuffer.U4[1] = PacketBuffer.U4[3];
231           PacketBuffer.U4[2] = *list_position++;
232           PacketBuffer.U4[3] = *list_position++;
233           gpuDrawLG(gpuPixelDrivers [ (Blending_Mode | Masking | Blending | (PixelMSB>>3)) >> 1]);
234
235           num_vertexes++;
236           if((*list_position & 0xf000f000) == 0x50005000 || list_position >= list_end)
237             break;
238         }
239
240         len += (num_vertexes - 2) * 2;
241         break;
242       }
243
244 #ifdef TEST
245       case 0xA0:          //  sys -> vid
246       {
247         u32 load_width = list[2] & 0xffff;
248         u32 load_height = list[2] >> 16;
249         u32 load_size = load_width * load_height;
250
251         len += load_size / 2;
252         break;
253       }
254 #endif
255
256       default:
257         unai_do_prim(cmd, list, len + 1);
258         break;
259     }
260   }
261
262   gpu.ex_regs[1] &= ~0x1ff;
263   gpu.ex_regs[1] |= GPU_GP1 & 0x1ff;
264
265   *last_cmd = cmd;
266   return list - list_start;
267 }
268
269 void renderer_sync_ecmds(uint32_t *ecmds)
270 {
271   unai_do_prim(0xe1, &ecmds[1], 1);
272   unai_do_prim(0xe2, &ecmds[2], 1);
273   unai_do_prim(0xe3, &ecmds[3], 1);
274   unai_do_prim(0xe4, &ecmds[4], 1);
275   unai_do_prim(0xe5, &ecmds[5], 1);
276   unai_do_prim(0xe6, &ecmds[6], 1);
277 }
278
279 void renderer_update_caches(int x, int y, int w, int h)
280 {
281 }
282
283 void renderer_flush_queues(void)
284 {
285 }
286
287 void renderer_set_interlace(int enable, int is_odd)
288 {
289 }
290
291 #ifndef TEST
292
293 #include "../../frontend/plugin_lib.h"
294
295 void renderer_set_config(const struct rearmed_cbs *cbs)
296 {
297   force_interlace = cbs->gpu_unai.lineskip;
298   enableAbbeyHack = cbs->gpu_unai.abe_hack;
299   light = !cbs->gpu_unai.no_light;
300   blend = !cbs->gpu_unai.no_blend;
301 }
302
303 #endif