1 /*
2 Copyright (C) 2003 Rice1964
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 
18 */
19 
20 
21 #ifndef __RICE_RDP_GFX_H__
22 #define __RICE_RDP_GFX_H__
23 
24 #include "../../Graphics/GBI.h"
25 #include "../../Graphics/RSP/gSP_state.h"
26 
27 #define RSP_SPNOOP              0   // handle 0 gracefully
28 #define RSP_MTX                 1
29 #define RSP_RESERVED0           2   // unknown
30 #define RSP_MOVEMEM             3   // move a block of memory (up to 4 words) to dmem
31 #define RSP_VTX                 4
32 #define RSP_RESERVED1           5   // unknown
33 #define RSP_DL                  6
34 #define RSP_RESERVED2           7   // unknown
35 #define RSP_RESERVED3           8   // unknown
36 #define RSP_SPRITE2D            9   // sprite command
37 #define RSP_SPRITE2D_BASE       9   // sprite command
38 
39 
40 #define RSP_1ST                 0xBF
41 #define RSP_TRI1                (RSP_1ST-0)
42 #define RSP_CULLDL              (RSP_1ST-1)
43 #define RSP_POPMTX              (RSP_1ST-2)
44 #define RSP_MOVEWORD            (RSP_1ST-3)
45 #define RSP_TEXTURE             (RSP_1ST-4)
46 #define RSP_SETOTHERMODE_H      (RSP_1ST-5)
47 #define RSP_SETOTHERMODE_L      (RSP_1ST-6)
48 #define RSP_ENDDL               (RSP_1ST-7)
49 #define RSP_SETGEOMETRYMODE     (RSP_1ST-8)
50 #define RSP_CLEARGEOMETRYMODE   (RSP_1ST-9)
51 #define RSP_LINE3D              (RSP_1ST-10)
52 #define RSP_RDPHALF_1           (RSP_1ST-11)
53 #define RSP_RDPHALF_2           (RSP_1ST-12)
54 #define RSP_RDPHALF_CONT        (RSP_1ST-13)
55 
56 #define RSP_MODIFYVTX           (RSP_1ST-13)
57 #define RSP_TRI2                (RSP_1ST-14)
58 #define RSP_BRANCH_Z            (RSP_1ST-15)
59 #define RSP_LOAD_UCODE          (RSP_1ST-16)
60 
61 #define RSP_SPRITE2D_SCALEFLIP    (RSP_1ST-1)
62 #define RSP_SPRITE2D_DRAW         (RSP_1ST-2)
63 
64 #define RSP_ZELDAVTX                1
65 #define RSP_ZELDAMODIFYVTX          2
66 #define RSP_ZELDACULLDL             3
67 #define RSP_ZELDABRANCHZ            4
68 #define RSP_ZELDATRI1               5
69 #define RSP_ZELDATRI2               6
70 #define RSP_ZELDALINE3D             7
71 
72 // 4 is something like a conditional DL
73 #define RSP_DMATRI  0x05
74 #define G_DLINMEM   0x07
75 
76 // RSP_SETOTHERMODE_H gPipelineMode
77 #define RSP_PIPELINE_MODE_1PRIMITIVE        (1 << RSP_SETOTHERMODE_SHIFT_PIPELINE)
78 #define RSP_PIPELINE_MODE_NPRIMITIVE        (0 << RSP_SETOTHERMODE_SHIFT_PIPELINE)
79 
80 // RSP_SETOTHERMODE_H gSetTextureLUT
81 #define TLUT_FMT_NONE           (0 << G_MDSFT_TEXTLUT)
82 #define TLUT_FMT_UNKNOWN        (1 << G_MDSFT_TEXTLUT)
83 #define TLUT_FMT_RGBA16         (2 << G_MDSFT_TEXTLUT)
84 #define TLUT_FMT_IA16           (3 << G_MDSFT_TEXTLUT)
85 
86 // RSP_SETOTHERMODE_H gSetTextureFilter
87 #define RDP_TFILTER_POINT       (0 << G_MDSFT_TEXTFILT)
88 #define RDP_TFILTER_AVERAGE     (3 << G_MDSFT_TEXTFILT)
89 #define RDP_TFILTER_BILERP      (2 << G_MDSFT_TEXTFILT)
90 
91 // RSP_SETOTHERMODE_L gSetAlphaCompare
92 #define RDP_ALPHA_COMPARE_NONE          (0 << RSP_SETOTHERMODE_SHIFT_ALPHACOMPARE)
93 #define RDP_ALPHA_COMPARE_THRESHOLD     (1 << RSP_SETOTHERMODE_SHIFT_ALPHACOMPARE)
94 #define RDP_ALPHA_COMPARE_DITHER        (3 << RSP_SETOTHERMODE_SHIFT_ALPHACOMPARE)
95 
96 // RSP_SETOTHERMODE_L gSetRenderMode
97 #define Z_COMPARE           0x0010
98 #define Z_UPDATE            0x0020
99 
100 // Texturing macros
101 
102 #define RDP_TXT_LOADTILE    7
103 #define RDP_TXT_RENDERTILE  0
104 
105 #define RDP_TXT_NOMIRROR    0
106 #define RDP_TXT_WRAP        0
107 #define RDP_TXT_MIRROR      0x1
108 #define RDP_TXT_CLAMP       0x2
109 #define RDP_TXT_NOMASK      0
110 #define RDP_TXT_NOLOD       0
111 
112 // MOVEMEM indices
113 //
114 // Each of these indexes an entry in a dmem table
115 // which points to a 1-4 word block of dmem in
116 // which to store a 1-4 word DMA.
117 
118 # define RSP_GBI2_MV_MEM__VIEWPORT  8
119 # define RSP_GBI2_MV_MEM__LIGHT     10
120 # define RSP_GBI2_MV_MEM__POINT     12
121 # define RSP_GBI2_MV_MEM__MATRIX    14      // NOTE: this is in moveword table
122 
123 typedef struct
124 {
125     uint32_t  type;
126     uint32_t  flags;
127 
128     uint32_t  ucode_boot;
129     uint32_t  ucode_boot_size;
130 
131     uint32_t  ucode;
132     uint32_t  ucode_size;
133 
134     uint32_t  ucode_data;
135     uint32_t  ucode_data_size;
136 
137     uint32_t  dram_stack;
138     uint32_t  dram_stack_size;
139 
140     uint32_t  output_buff;
141     uint32_t  output_buff_size;
142 
143     uint32_t  data_ptr;
144     uint32_t  data_size;
145 
146     uint32_t  yield_data_ptr;
147     uint32_t  yield_data_size;
148 } OSTask_t;
149 
150 typedef union {
151     OSTask_t        t;
152     uint64_t  force_structure_alignment;
153 } OSTask;
154 
155 #define MAX_DL_COUNT        1000000
156 
157 typedef struct {
158     bool    used;
159     uint32_t  crc_size;
160     uint32_t  crc_800;
161     uint32_t  ucode;
162     uint32_t  minor_ver;
163     uint32_t  variant;
164     char    rspstr[200];
165 
166     uint32_t  ucStart;
167     uint32_t  ucSize;
168     uint32_t  ucDStart;
169     uint32_t  ucDSize;
170     uint32_t  ucCRC;
171     uint32_t  ucDWORD1;
172     uint32_t  ucDWORD2;
173     uint32_t  ucDWORD3;
174     uint32_t  ucDWORD4;
175 } UcodeInfo;
176 
177 
178 typedef struct
179 {
180     uint32_t      ucode;
181     uint32_t      crc_size;
182     uint32_t      crc_800;
183     const unsigned char * ucode_name;
184     bool        non_nearclip;
185     bool        reject;
186 } UcodeData;
187 
188 struct TileDescriptor
189 {
190     // Set by SetTile
191     unsigned int dwFormat   :3;     // e.g. RGBA, YUV etc
192     unsigned int dwSize     :2;     // e.g 4/8/16/32bpp
193     unsigned int dwLine     :9;     // Ummm...
194     unsigned int dwPalette  :4;     // 0..15 - a palette index?
195     uint32_t dwTMem;                  // Texture memory location
196 
197     unsigned int bClampS    :1;
198     unsigned int bClampT    :1;
199     unsigned int bMirrorS   :1;
200     unsigned int bMirrorT   :1;
201 
202     unsigned int dwMaskS    :4;
203     unsigned int dwMaskT    :4;
204     unsigned int dwShiftS   :4;
205     unsigned int dwShiftT   :4;
206 
207     // Set by SetTileSize
208     unsigned int sl     :10;    // Upper left S     - 8:3
209     unsigned int tl     :10;    // Upper Left T     - 8:3
210     unsigned int sh     :10;    // Lower Right S
211     unsigned int th     :10;    // Lower Right T
212 };
213 
214 enum LoadType
215 {
216     BY_NEVER_SET,
217     BY_LOAD_BLOCK,
218     BY_LOAD_TILE,
219     BY_LOAD_TLUT,
220 };
221 
222 struct LoadCmdInfo
223 {
224     LoadType    loadtype;
225     unsigned int sl     :10;    // Upper left S     - 8:3
226     unsigned int tl     :10;    // Upper Left T     - 8:3
227     unsigned int sh     :10;    // Lower Right S
228     unsigned int th     :10;    // Lower Right T
229     unsigned int dxt    :12;
230 };
231 
232 typedef struct {    // This is in Intel format
233     uint32_t SourceImagePointer;
234     uint32_t TlutPointer;
235 
236     short SubImageWidth;
237     short Stride;
238 
239     char  SourceImageBitSize;
240     char  SourceImageType;
241     short SubImageHeight;
242 
243     short SourceImageOffsetT;
244     short SourceImageOffsetS;
245 
246     char  dummy[4];
247 } SpriteStruct;         //Converted Sprint struct in Intel format
248 
249 typedef struct{
250     short px;
251     short py;
252     float scaleX;
253     float scaleY;
254     uint8_t  flipX;
255     uint8_t  flipY;
256     SpriteStruct *spritePtr;
257 } Sprite2DInfo;
258 
259 
260 typedef struct
261 {
262     unsigned int    c2_m2b:2;
263     unsigned int    c1_m2b:2;
264     unsigned int    c2_m2a:2;
265     unsigned int    c1_m2a:2;
266     unsigned int    c2_m1b:2;
267     unsigned int    c1_m1b:2;
268     unsigned int    c2_m1a:2;
269     unsigned int    c1_m1a:2;
270 } RDP_BlenderSetting;
271 
272 typedef struct
273 {
274     union
275     {
276         struct
277         {
278             // Low bits
279             unsigned int        alpha_compare : 2;          // 0..1
280             unsigned int        depth_source : 1;           // 2..2
281 
282         //  unsigned int        render_mode : 13;           // 3..15
283             unsigned int        aa_en : 1;                  // 3
284             unsigned int        z_cmp : 1;                  // 4
285             unsigned int        z_upd : 1;                  // 5
286             unsigned int        im_rd : 1;                  // 6
287             unsigned int        clr_on_cvg : 1;             // 7
288 
289             unsigned int        cvg_dst : 2;                // 8..9
290             unsigned int        zmode : 2;                  // 10..11
291 
292             unsigned int        cvg_x_alpha : 1;            // 12
293             unsigned int        alpha_cvg_sel : 1;          // 13
294             unsigned int        force_bl : 1;               // 14
295             unsigned int        tex_edge : 1;               // 15 - Not used
296 
297             unsigned int        blender : 16;               // 16..31
298 
299             // High bits
300             unsigned int        blend_mask : 4;             // 0..3 - not supported
301             unsigned int        alpha_dither : 2;           // 4..5
302             unsigned int        rgb_dither : 2;             // 6..7
303 
304             unsigned int        key_en : 1;                 // 8..8
305             unsigned int        text_conv : 3;              // 9..11
306             unsigned int        text_filt : 2;              // 12..13
307             unsigned int        text_tlut : 2;              // 14..15
308 
309             unsigned int        text_lod : 1;               // 16..16
310             unsigned int        text_sharpen : 1;           // 17..18
311             unsigned int        text_detail : 1;            // 17..18
312             unsigned int        text_persp : 1;             // 19..19
313             unsigned int        cycle_type : 2;             // 20..21
314             unsigned int        reserved : 1;               // 22..22 - not supported
315             unsigned int        atomic_prim : 1;            // 23..23
316 
317             unsigned int        pad : 8;                    // 24..31 - padding
318 
319         };
320         uint64_t          _u64;
321         uint32_t          _u32[2];
322     };
323 } RDP_OtherMode;
324 
325 
326 typedef enum
327 {
328     CMD_SETTILE,
329     CMD_SETTILE_SIZE,
330     CMD_LOADBLOCK,
331     CMD_LOADTILE,
332     CMD_LOADTLUT,
333     CMD_SET_TEXTURE,
334     CMD_LOAD_OBJ_TXTR,
335 } SetTileCmdType;
336 
337 
338 // The display list PC stack. Before this was an array of 10
339 // items, but this way we can nest as deeply as necessary.
340 
341 typedef struct
342 {
343     uint32_t pc;
344     int countdown;
345 } DListStack;
346 
347 typedef struct
348 {
349     int x0, y0, x1, y1, mode;
350     int left, top, right, bottom;
351 } ScissorType;
352 
353 // Mask down to 0x003FFFFF?
354 #define RSPSegmentAddr(seg) ( gSP.segment[((seg)>>24)&0x0F] + ((seg)&0x00FFFFFF) )
355 
356 extern uint16_t g_wRDPTlut[];
357 extern const char *textluttype[4];
358 
359 extern const char *pszImgFormat[8];
360 extern const char *pszImgSize[4];
361 extern uint8_t pnImgSize[4];
362 extern const char *textlutname[4];
363 
364 extern SetImgInfo g_CI;
365 extern SetImgInfo g_ZI;
366 extern SetImgInfo g_TI;
367 extern TmemType g_Tmem;
368 
369 void DLParser_Init();
370 void RDP_GFX_Reset();
371 void RDP_Cleanup();
372 void DLParser_Process(OSTask * pTask);
373 void RDP_DLParser_Process(void);
374 
375 void PrepareTextures();
376 void RDP_InitRenderState();
377 void DisplayVertexInfo(uint32_t dwAddr, uint32_t dwV0, uint32_t dwN);
378 
379 void ricegSPLight(uint32_t dwAddr, uint32_t dwLight);
380 void ricegSPViewport(uint32_t v);
381 
382 void RDP_NOIMPL_WARN(const char* op);
383 void RSP_GFX_Force_Matrix(uint32_t dwAddr);
384 void RSP_GFX_InitGeometryMode();
385 void RSP_SetUcode(int ucode, uint32_t ucStart, uint32_t ucDStart, uint32_t cdSize);
386 uint32_t CalcalateCRC(uint32_t* srcPtr, uint32_t srcSize);
387 void RDP_GFX_PopDL();
388 
389 extern Matrix matToLoad;
390 void LoadMatrix(uint32_t addr);
391 
392 unsigned int ComputeCRC32(unsigned int crc, const uint8_t *buf, unsigned int len);
393 
394 void TriggerDPInterrupt();
395 void TriggerSPInterrupt();
396 uint32_t DLParser_CheckUcode(uint32_t ucStart, uint32_t ucDStart, uint32_t ucSize, uint32_t ucDSize);
397 
398 bool IsUsedAsDI(uint32_t addr);
399 
400 #if defined(DEBUGGER)
401   void __cdecl LOG_UCODE(const char* szFormat, ...);
402 #else
LOG_UCODE(...)403   inline void LOG_UCODE(...) {}
404 #endif
405 
406 #endif  // __RICE_RDP_GFX_H__
407 
408