GLES2N64 (from mupen64plus-ae) plugin. Compile and run on the OpenPandora
[mupen64plus-pandora.git] / source / gles2n64 / src / GBI.h
1 #ifndef GBI_H
2 #define GBI_H
3 #include "Hash.h"
4 #include "Types.h"
5 #include <stdio.h>
6
7 // Microcode Types
8 #define F3D         0
9 #define F3DEX       1
10 #define F3DEX2      2
11 #define L3D         3
12 #define L3DEX       4
13 #define L3DEX2      5
14 #define S2DEX       6
15 #define S2DEX2      7
16 #define F3DPD       8
17 #define F3DDKR      9
18 #define F3DWRUS     10
19 #define F3DCBFD     11
20 #define NONE        12
21
22 #ifdef MAINDEF
23 const char *MicrocodeTypes[] =
24 {
25     "Fast3D",
26     "F3DEX",
27     "F3DEX2",
28     "Line3D",
29     "L3DEX",
30     "L3DEX2",
31     "S2DEX",
32     "S2DEX2",
33     "Perfect Dark",
34     "DKR/JFG",
35     "Waverace US",
36     "Conker's Bad Fur Day",
37     "None",
38 };
39 #else
40 extern const char *MicrocodeTypes[];
41 #endif
42
43 static const int numMicrocodeTypes = 11;
44
45 // Fixed point conversion factors
46 #define FIXED2FLOATRECIP1   0.5f
47 #define FIXED2FLOATRECIP2   0.25f
48 #define FIXED2FLOATRECIP3   0.125f
49 #define FIXED2FLOATRECIP4   0.0625f
50 #define FIXED2FLOATRECIP5   0.03125f
51 #define FIXED2FLOATRECIP6   0.015625f
52 #define FIXED2FLOATRECIP7   0.0078125f
53 #define FIXED2FLOATRECIP8   0.00390625f
54 #define FIXED2FLOATRECIP9   0.001953125f
55 #define FIXED2FLOATRECIP10  0.0009765625f
56 #define FIXED2FLOATRECIP11  0.00048828125f
57 #define FIXED2FLOATRECIP12  0.00024414063f
58 #define FIXED2FLOATRECIP13  0.00012207031f
59 #define FIXED2FLOATRECIP14  6.1035156e-05f
60 #define FIXED2FLOATRECIP15  3.0517578e-05f
61 #define FIXED2FLOATRECIP16  1.5258789e-05f
62
63 #define _FIXED2FLOAT( v, b ) \
64     ((f32)v * FIXED2FLOATRECIP##b)
65
66 // Useful macros for decoding GBI command's parameters
67 #define _SHIFTL( v, s, w )  \
68     (((u32)v & ((0x01 << w) - 1)) << s)
69 #define _SHIFTR( v, s, w )  \
70     (((u32)v >> s) & ((0x01 << w) - 1))
71
72 // BG flags
73 #define G_BGLT_LOADBLOCK    0x0033
74 #define G_BGLT_LOADTILE     0xfff4
75
76 #define G_BG_FLAG_FLIPS     0x01
77 #define G_BG_FLAG_FLIPT     0x10
78
79 // Sprite object render modes
80 #define G_OBJRM_NOTXCLAMP       0x01
81 #define G_OBJRM_XLU             0x02    /* Ignored */
82 #define G_OBJRM_ANTIALIAS       0x04    /* Ignored */
83 #define G_OBJRM_BILERP          0x08
84 #define G_OBJRM_SHRINKSIZE_1    0x10
85 #define G_OBJRM_SHRINKSIZE_2    0x20
86 #define G_OBJRM_WIDEN           0x40
87
88 // Sprite texture loading types
89 #define G_OBJLT_TXTRBLOCK   0x00001033
90 #define G_OBJLT_TXTRTILE    0x00fc1034
91 #define G_OBJLT_TLUT        0x00000030
92
93
94 // These are all the constant flags
95 #define G_ZBUFFER               0x00000001
96 #define G_SHADE                 0x00000004
97 #define G_FOG                   0x00010000
98 #define G_LIGHTING              0x00020000
99 #define G_TEXTURE_GEN           0x00040000
100 #define G_TEXTURE_GEN_LINEAR    0x00080000
101 #define G_LOD                   0x00100000
102
103 #define G_MV_MMTX       2
104 #define G_MV_PMTX       6
105 #define G_MV_LIGHT      10
106 #define G_MV_POINT      12
107 #define G_MV_MATRIX     14
108
109 #define G_MVO_LOOKATX   0
110 #define G_MVO_LOOKATY   24
111 #define G_MVO_L0        48
112 #define G_MVO_L1        72
113 #define G_MVO_L2        96
114 #define G_MVO_L3        120
115 #define G_MVO_L4        144
116 #define G_MVO_L5        168
117 #define G_MVO_L6        192
118 #define G_MVO_L7        216
119
120 #define G_MV_LOOKATY    0x82
121 #define G_MV_LOOKATX    0x84
122 #define G_MV_L0         0x86
123 #define G_MV_L1         0x88
124 #define G_MV_L2         0x8a
125 #define G_MV_L3         0x8c
126 #define G_MV_L4         0x8e
127 #define G_MV_L5         0x90
128 #define G_MV_L6         0x92
129 #define G_MV_L7         0x94
130 #define G_MV_TXTATT     0x96
131 #define G_MV_MATRIX_1   0x9E
132 #define G_MV_MATRIX_2   0x98
133 #define G_MV_MATRIX_3   0x9A
134 #define G_MV_MATRIX_4   0x9C
135
136 #define G_MW_MATRIX         0x00
137 #define G_MW_NUMLIGHT       0x02
138 #define G_MW_CLIP           0x04
139 #define G_MW_SEGMENT        0x06
140 #define G_MW_FOG            0x08
141 #define G_MW_LIGHTCOL       0x0A
142 #define G_MW_FORCEMTX       0x0C
143 #define G_MW_POINTS         0x0C
144 #define G_MW_PERSPNORM      0x0E
145 #define G_MV_COORDMOD       0x10    //Conker Bad Fur Day
146
147 #define G_MWO_NUMLIGHT      0x00
148 #define G_MWO_CLIP_RNX      0x04
149 #define G_MWO_CLIP_RNY      0x0c
150 #define G_MWO_CLIP_RPX      0x14
151 #define G_MWO_CLIP_RPY      0x1c
152 #define G_MWO_SEGMENT_0     0x00
153 #define G_MWO_SEGMENT_1     0x01
154 #define G_MWO_SEGMENT_2     0x02
155 #define G_MWO_SEGMENT_3     0x03
156 #define G_MWO_SEGMENT_4     0x04
157 #define G_MWO_SEGMENT_5     0x05
158 #define G_MWO_SEGMENT_6     0x06
159 #define G_MWO_SEGMENT_7     0x07
160 #define G_MWO_SEGMENT_8     0x08
161 #define G_MWO_SEGMENT_9     0x09
162 #define G_MWO_SEGMENT_A     0x0a
163 #define G_MWO_SEGMENT_B     0x0b
164 #define G_MWO_SEGMENT_C     0x0c
165 #define G_MWO_SEGMENT_D     0x0d
166 #define G_MWO_SEGMENT_E     0x0e
167 #define G_MWO_SEGMENT_F     0x0f
168 #define G_MWO_FOG           0x00
169
170 #define G_MWO_MATRIX_XX_XY_I    0x00
171 #define G_MWO_MATRIX_XZ_XW_I    0x04
172 #define G_MWO_MATRIX_YX_YY_I    0x08
173 #define G_MWO_MATRIX_YZ_YW_I    0x0C
174 #define G_MWO_MATRIX_ZX_ZY_I    0x10
175 #define G_MWO_MATRIX_ZZ_ZW_I    0x14
176 #define G_MWO_MATRIX_WX_WY_I    0x18
177 #define G_MWO_MATRIX_WZ_WW_I    0x1C
178 #define G_MWO_MATRIX_XX_XY_F    0x20
179 #define G_MWO_MATRIX_XZ_XW_F    0x24
180 #define G_MWO_MATRIX_YX_YY_F    0x28
181 #define G_MWO_MATRIX_YZ_YW_F    0x2C
182 #define G_MWO_MATRIX_ZX_ZY_F    0x30
183 #define G_MWO_MATRIX_ZZ_ZW_F    0x34
184 #define G_MWO_MATRIX_WX_WY_F    0x38
185 #define G_MWO_MATRIX_WZ_WW_F    0x3C
186 #define G_MWO_POINT_RGBA        0x10
187 #define G_MWO_POINT_ST          0x14
188 #define G_MWO_POINT_XYSCREEN    0x18
189 #define G_MWO_POINT_ZSCREEN     0x1C
190
191 #ifdef DEBUG
192 static const char *MWOPointText[] =
193 {
194     "G_MWO_POINT_RGBA",
195     "G_MWO_POINT_ST",
196     "G_MWO_POINT_XYSCREEN",
197     "G_MWO_POINT_ZSCREEN"
198 };
199
200 static const char *MWOMatrixText[] =
201 {
202     "G_MWO_MATRIX_XX_XY_I", "G_MWO_MATRIX_XZ_XW_I", "G_MWO_MATRIX_YX_YY_I", "G_MWO_MATRIX_YZ_YW_I",
203     "G_MWO_MATRIX_ZX_ZY_I", "G_MWO_MATRIX_ZZ_ZW_I", "G_MWO_MATRIX_WX_WY_I", "G_MWO_MATRIX_WZ_WW_I",
204     "G_MWO_MATRIX_XX_XY_F", "G_MWO_MATRIX_XZ_XW_F", "G_MWO_MATRIX_YX_YY_F", "G_MWO_MATRIX_YZ_YW_F",
205     "G_MWO_MATRIX_ZX_ZY_F", "G_MWO_MATRIX_ZZ_ZW_F", "G_MWO_MATRIX_WX_WY_F", "G_MWO_MATRIX_WZ_WW_F"
206 };
207 #endif
208
209 // These flags change between ucodes
210 extern u32 G_MTX_STACKSIZE;
211
212 extern u32 G_MTX_MODELVIEW;
213 extern u32 G_MTX_PROJECTION;
214 extern u32 G_MTX_MUL;
215 extern u32 G_MTX_LOAD;
216 extern u32 G_MTX_NOPUSH;
217 extern u32 G_MTX_PUSH;
218
219 extern u32 G_TEXTURE_ENABLE;
220 extern u32 G_SHADING_SMOOTH;
221 extern u32 G_CULL_FRONT;
222 extern u32 G_CULL_BACK;
223 extern u32 G_CULL_BOTH;
224 extern u32 G_CLIPPING;
225
226 extern u32 G_MV_VIEWPORT;
227
228 extern u32 G_MWO_aLIGHT_1, G_MWO_bLIGHT_1;
229 extern u32 G_MWO_aLIGHT_2, G_MWO_bLIGHT_2;
230 extern u32 G_MWO_aLIGHT_3, G_MWO_bLIGHT_3;
231 extern u32 G_MWO_aLIGHT_4, G_MWO_bLIGHT_4;
232 extern u32 G_MWO_aLIGHT_5, G_MWO_bLIGHT_5;
233 extern u32 G_MWO_aLIGHT_6, G_MWO_bLIGHT_6;
234 extern u32 G_MWO_aLIGHT_7, G_MWO_bLIGHT_7;
235 extern u32 G_MWO_aLIGHT_8, G_MWO_bLIGHT_8;
236
237 // Image formats
238 #define G_IM_FMT_RGBA   0
239 #define G_IM_FMT_YUV    1
240 #define G_IM_FMT_CI     2
241 #define G_IM_FMT_IA     3
242 #define G_IM_FMT_I      4
243 #define G_IM_FMT_CI_IA  5   //not real
244
245 // Image sizes
246 #define G_IM_SIZ_4b     0
247 #define G_IM_SIZ_8b     1
248 #define G_IM_SIZ_16b    2
249 #define G_IM_SIZ_32b    3
250 #define G_IM_SIZ_DD     5
251
252 #define G_TX_MIRROR     0x1
253 #define G_TX_CLAMP      0x2
254
255 #ifdef DEBUG
256 static const char *ImageFormatText[] =
257 {
258     "G_IM_FMT_RGBA",
259     "G_IM_FMT_YUV",
260     "G_IM_FMT_CI",
261     "G_IM_FMT_IA",
262     "G_IM_FMT_I",
263     "G_IM_FMT_INVALID",
264     "G_IM_FMT_INVALID",
265     "G_IM_FMT_INVALID"
266 };
267
268 static const char *ImageSizeText[] =
269 {
270     "G_IM_SIZ_4b",
271     "G_IM_SIZ_8b",
272     "G_IM_SIZ_16b",
273     "G_IM_SIZ_32b"
274 };
275
276 static const char *SegmentText[] =
277 {
278     "G_MWO_SEGMENT_0", "G_MWO_SEGMENT_1", "G_MWO_SEGMENT_2", "G_MWO_SEGMENT_3",
279     "G_MWO_SEGMENT_4", "G_MWO_SEGMENT_5", "G_MWO_SEGMENT_6", "G_MWO_SEGMENT_7",
280     "G_MWO_SEGMENT_8", "G_MWO_SEGMENT_9", "G_MWO_SEGMENT_A", "G_MWO_SEGMENT_B",
281     "G_MWO_SEGMENT_C", "G_MWO_SEGMENT_D", "G_MWO_SEGMENT_E", "G_MWO_SEGMENT_F"
282 };
283 #endif
284
285 #define G_NOOP                  0x00
286
287 #define G_IMMFIRST              -65
288
289 // These GBI commands are common to all ucodes
290 #define G_SETCIMG               0xFF    /*  -1 */
291 #define G_SETZIMG               0xFE    /*  -2 */
292 #define G_SETTIMG               0xFD    /*  -3 */
293 #define G_SETCOMBINE            0xFC    /*  -4 */
294 #define G_SETENVCOLOR           0xFB    /*  -5 */
295 #define G_SETPRIMCOLOR          0xFA    /*  -6 */
296 #define G_SETBLENDCOLOR         0xF9    /*  -7 */
297 #define G_SETFOGCOLOR           0xF8    /*  -8 */
298 #define G_SETFILLCOLOR          0xF7    /*  -9 */
299 #define G_FILLRECT              0xF6    /* -10 */
300 #define G_SETTILE               0xF5    /* -11 */
301 #define G_LOADTILE              0xF4    /* -12 */
302 #define G_LOADBLOCK             0xF3    /* -13 */
303 #define G_SETTILESIZE           0xF2    /* -14 */
304 #define G_LOADTLUT              0xF0    /* -16 */
305 #define G_RDPSETOTHERMODE       0xEF    /* -17 */
306 #define G_SETPRIMDEPTH          0xEE    /* -18 */
307 #define G_SETSCISSOR            0xED    /* -19 */
308 #define G_SETCONVERT            0xEC    /* -20 */
309 #define G_SETKEYR               0xEB    /* -21 */
310 #define G_SETKEYGB              0xEA    /* -22 */
311 #define G_RDPFULLSYNC           0xE9    /* -23 */
312 #define G_RDPTILESYNC           0xE8    /* -24 */
313 #define G_RDPPIPESYNC           0xE7    /* -25 */
314 #define G_RDPLOADSYNC           0xE6    /* -26 */
315 #define G_TEXRECTFLIP           0xE5    /* -27 */
316 #define G_TEXRECT               0xE4    /* -28 */
317
318 #define G_RDPNOOP               0xC0
319
320 #define G_TRI_FILL              0xC8    /* fill triangle:            11001000 */
321 #define G_TRI_FILL_ZBUFF        0xC9    /* fill, zbuff triangle:     11001001 */
322 #define G_TRI_TXTR              0xCA    /* texture triangle:         11001010 */
323 #define G_TRI_TXTR_ZBUFF        0xCB    /* texture, zbuff triangle:  11001011 */
324 #define G_TRI_SHADE             0xCC    /* shade triangle:           11001100 */
325 #define G_TRI_SHADE_ZBUFF       0xCD    /* shade, zbuff triangle:    11001101 */
326 #define G_TRI_SHADE_TXTR        0xCE    /* shade, texture triangle:  11001110 */
327 #define G_TRI_SHADE_TXTR_ZBUFF  0xCF    /* shade, txtr, zbuff trngl: 11001111 */
328
329 /*
330  * G_SETOTHERMODE_L sft: shift count
331  */
332 #define G_MDSFT_ALPHACOMPARE    0
333 #define G_MDSFT_ZSRCSEL         2
334 #define G_MDSFT_RENDERMODE      3
335 #define G_MDSFT_BLENDER         16
336
337 /*
338  * G_SETOTHERMODE_H sft: shift count
339  */
340 #define G_MDSFT_BLENDMASK       0   /* unsupported */
341 #define G_MDSFT_ALPHADITHER     4
342 #define G_MDSFT_RGBDITHER       6
343
344 #define G_MDSFT_COMBKEY         8
345 #define G_MDSFT_TEXTCONV        9
346 #define G_MDSFT_TEXTFILT        12
347 #define G_MDSFT_TEXTLUT         14
348 #define G_MDSFT_TEXTLOD         16
349 #define G_MDSFT_TEXTDETAIL      17
350 #define G_MDSFT_TEXTPERSP       19
351 #define G_MDSFT_CYCLETYPE       20
352 #define G_MDSFT_COLORDITHER     22  /* unsupported in HW 2.0 */
353 #define G_MDSFT_PIPELINE        23
354
355 /* G_SETOTHERMODE_H gPipelineMode */
356 #define G_PM_1PRIMITIVE         1
357 #define G_PM_NPRIMITIVE         0
358
359 /* G_SETOTHERMODE_H gSetCycleType */
360 #define G_CYC_1CYCLE            0
361 #define G_CYC_2CYCLE            1
362 #define G_CYC_COPY              2
363 #define G_CYC_FILL              3
364
365 /* G_SETOTHERMODE_H gSetTexturePersp */
366 #define G_TP_NONE               0
367 #define G_TP_PERSP              1
368
369 /* G_SETOTHERMODE_H gSetTextureDetail */
370 #define G_TD_CLAMP              0
371 #define G_TD_SHARPEN            1
372 #define G_TD_DETAIL             2
373
374 /* G_SETOTHERMODE_H gSetTextureLOD */
375 #define G_TL_TILE               0
376 #define G_TL_LOD                1
377
378 /* G_SETOTHERMODE_H gSetTextureLUT */
379 #define G_TT_NONE               0
380 #define G_TT_RGBA16             2
381 #define G_TT_IA16               3
382
383 /* G_SETOTHERMODE_H gSetTextureFilter */
384 #define G_TF_POINT              0
385 #define G_TF_AVERAGE            3
386 #define G_TF_BILERP             2
387
388 /* G_SETOTHERMODE_H gSetTextureConvert */
389 #define G_TC_CONV               0
390 #define G_TC_FILTCONV           5
391 #define G_TC_FILT               6
392
393 /* G_SETOTHERMODE_H gSetCombineKey */
394 #define G_CK_NONE               0
395 #define G_CK_KEY                1
396
397 /* G_SETOTHERMODE_H gSetColorDither */
398 #define G_CD_MAGICSQ            0
399 #define G_CD_BAYER              1
400 #define G_CD_NOISE              2
401
402 #define G_CD_DISABLE            3
403 #define G_CD_ENABLE             G_CD_NOISE  /* HW 1.0 compatibility mode */
404
405 /* G_SETOTHERMODE_H gSetAlphaDither */
406 #define G_AD_PATTERN            0
407 #define G_AD_NOTPATTERN         1
408 #define G_AD_NOISE              2
409 #define G_AD_DISABLE            3
410
411 /* G_SETOTHERMODE_L gSetAlphaCompare */
412 #define G_AC_NONE               0
413 #define G_AC_THRESHOLD          1
414 #define G_AC_DITHER             3
415
416 /* G_SETOTHERMODE_L gSetDepthSource */
417 #define G_ZS_PIXEL              0
418 #define G_ZS_PRIM               1
419
420 /* G_SETOTHERMODE_L gSetRenderMode */
421 #define AA_EN                   1
422 #define Z_CMP                   1
423 #define Z_UPD                   1
424 #define IM_RD                   1
425 #define CLR_ON_CVG              1
426 #define CVG_DST_CLAMP           0
427 #define CVG_DST_WRAP            1
428 #define CVG_DST_FULL            2
429 #define CVG_DST_SAVE            3
430 #define ZMODE_OPA               0
431 #define ZMODE_INTER             1
432 #define ZMODE_XLU               2
433 #define ZMODE_DEC               3
434 #define CVG_X_ALPHA             1
435 #define ALPHA_CVG_SEL           1
436 #define FORCE_BL                1
437 #define TEX_EDGE                0 // not used
438
439 #define G_SC_NON_INTERLACE      0
440 #define G_SC_EVEN_INTERLACE     2
441 #define G_SC_ODD_INTERLACE      3
442
443 #ifdef DEBUG
444 static const char *AAEnableText = "AA_EN";
445 static const char *DepthCompareText = "Z_CMP";
446 static const char *DepthUpdateText = "Z_UPD";
447 static const char *ClearOnCvgText = "CLR_ON_CVG";
448 static const char *CvgXAlphaText = "CVG_X_ALPHA";
449 static const char *AlphaCvgSelText = "ALPHA_CVG_SEL";
450 static const char *ForceBlenderText = "FORCE_BL";
451
452 static const char *AlphaCompareText[] =
453 {
454     "G_AC_NONE", "G_AC_THRESHOLD", "G_AC_INVALID", "G_AC_DITHER"
455 };
456
457 static const char *DepthSourceText[] =
458 {
459     "G_ZS_PIXEL", "G_ZS_PRIM"
460 };
461
462 static const char *AlphaDitherText[] =
463 {
464     "G_AD_PATTERN", "G_AD_NOTPATTERN", "G_AD_NOISE", "G_AD_DISABLE"
465 };
466
467 static const char *ColorDitherText[] =
468 {
469     "G_CD_MAGICSQ", "G_CD_BAYER", "G_CD_NOISE", "G_CD_DISABLE"
470 };
471
472 static const char *CombineKeyText[] =
473 {
474     "G_CK_NONE", "G_CK_KEY"
475 };
476
477 static const char *TextureConvertText[] =
478 {
479     "G_TC_CONV", "G_TC_INVALID", "G_TC_INVALID", "G_TC_INVALID", "G_TC_INVALID", "G_TC_FILTCONV", "G_TC_FILT", "G_TC_INVALID"
480 };
481
482 static const char *TextureFilterText[] =
483 {
484     "G_TF_POINT", "G_TF_INVALID", "G_TF_BILERP", "G_TF_AVERAGE"
485 };
486
487 static const char *TextureLUTText[] =
488 {
489     "G_TT_NONE", "G_TT_INVALID", "G_TT_RGBA16", "G_TT_IA16"
490 };
491
492 static const char *TextureLODText[] =
493 {
494     "G_TL_TILE", "G_TL_LOD"
495 };
496
497 static const char *TextureDetailText[] =
498 {
499     "G_TD_CLAMP", "G_TD_SHARPEN", "G_TD_DETAIL"
500 };
501
502 static const char *TexturePerspText[] =
503 {
504     "G_TP_NONE", "G_TP_PERSP"
505 };
506
507 static const char *CycleTypeText[] =
508 {
509     "G_CYC_1CYCLE", "G_CYC_2CYCLE", "G_CYC_COPY", "G_CYC_FILL"
510 };
511
512 static const char *PipelineModeText[] =
513 {
514     "G_PM_NPRIMITIVE", "G_PM_1PRIMITIVE"
515 };
516
517 static const char *CvgDestText[] =
518 {
519     "CVG_DST_CLAMP", "CVG_DST_WRAP", "CVG_DST_FULL", "CVG_DST_SAVE"
520 };
521
522 static const char *DepthModeText[] =
523 {
524     "ZMODE_OPA", "ZMODE_INTER", "ZMODE_XLU", "ZMODE_DEC"
525 };
526
527 static const char *ScissorModeText[] =
528 {
529     "G_SC_NON_INTERLACE", "G_SC_INVALID", "G_SC_EVEN_INTERLACE", "G_SC_ODD_INTERLACE"
530 };
531 #endif
532
533 /* Color combiner constants: */
534 #define G_CCMUX_COMBINED        0
535 #define G_CCMUX_TEXEL0          1
536 #define G_CCMUX_TEXEL1          2
537 #define G_CCMUX_PRIMITIVE       3
538 #define G_CCMUX_SHADE           4
539 #define G_CCMUX_ENVIRONMENT     5
540 #define G_CCMUX_CENTER          6
541 #define G_CCMUX_SCALE           6
542 #define G_CCMUX_COMBINED_ALPHA  7
543 #define G_CCMUX_TEXEL0_ALPHA    8
544 #define G_CCMUX_TEXEL1_ALPHA    9
545 #define G_CCMUX_PRIMITIVE_ALPHA 10
546 #define G_CCMUX_SHADE_ALPHA     11
547 #define G_CCMUX_ENV_ALPHA       12
548 #define G_CCMUX_LOD_FRACTION    13
549 #define G_CCMUX_PRIM_LOD_FRAC   14
550 #define G_CCMUX_NOISE           7
551 #define G_CCMUX_K4              7
552 #define G_CCMUX_K5              15
553 #define G_CCMUX_1               6
554 #define G_CCMUX_0               31
555
556 /* Alpha combiner constants: */
557 #define G_ACMUX_COMBINED        0
558 #define G_ACMUX_TEXEL0          1
559 #define G_ACMUX_TEXEL1          2
560 #define G_ACMUX_PRIMITIVE       3
561 #define G_ACMUX_SHADE           4
562 #define G_ACMUX_ENVIRONMENT     5
563 #define G_ACMUX_LOD_FRACTION    0
564 #define G_ACMUX_PRIM_LOD_FRAC   6
565 #define G_ACMUX_1               6
566 #define G_ACMUX_0               7
567
568 #ifdef DEBUG
569 static const char *saRGBText[] =
570 {
571     "COMBINED",         "TEXEL0",           "TEXEL1",           "PRIMITIVE",
572     "SHADE",            "ENVIRONMENT",      "NOISE",            "1",
573     "0",                "0",                "0",                "0",
574     "0",                "0",                "0",                "0"
575 };
576
577 static const char *sbRGBText[] =
578 {
579     "COMBINED",         "TEXEL0",           "TEXEL1",           "PRIMITIVE",
580     "SHADE",            "ENVIRONMENT",      "CENTER",           "K4",
581     "0",                "0",                "0",                "0",
582     "0",                "0",                "0",                "0"
583 };
584
585 static const char *mRGBText[] =
586 {
587     "COMBINED",         "TEXEL0",           "TEXEL1",           "PRIMITIVE",
588     "SHADE",            "ENVIRONMENT",      "SCALE",            "COMBINED_ALPHA",
589     "TEXEL0_ALPHA",     "TEXEL1_ALPHA",     "PRIMITIVE_ALPHA",  "SHADE_ALPHA",
590     "ENV_ALPHA",        "LOD_FRACTION",     "PRIM_LOD_FRAC",    "K5",
591     "0",                "0",                "0",                "0",
592     "0",                "0",                "0",                "0",
593     "0",                "0",                "0",                "0",
594     "0",                "0",                "0",                "0"
595 };
596
597 static const char *aRGBText[] =
598 {
599     "COMBINED",         "TEXEL0",           "TEXEL1",           "PRIMITIVE",
600     "SHADE",            "ENVIRONMENT",      "1",                "0",
601 };
602
603 static const char *saAText[] =
604 {
605     "COMBINED",         "TEXEL0",           "TEXEL1",           "PRIMITIVE",
606     "SHADE",            "ENVIRONMENT",      "1",                "0",
607 };
608
609 static const char *sbAText[] =
610 {
611     "COMBINED",         "TEXEL0",           "TEXEL1",           "PRIMITIVE",
612     "SHADE",            "ENVIRONMENT",      "1",                "0",
613 };
614
615 static const char *mAText[] =
616 {
617     "LOD_FRACTION",     "TEXEL0",           "TEXEL1",           "PRIMITIVE",
618     "SHADE",            "ENVIRONMENT",      "PRIM_LOD_FRAC",    "0",
619 };
620
621 static const char *aAText[] =
622 {
623     "COMBINED",         "TEXEL0",           "TEXEL1",           "PRIMITIVE",
624     "SHADE",            "ENVIRONMENT",      "1",                "0",
625 };
626 #endif
627
628 extern u32 G_RDPHALF_1, G_RDPHALF_2, G_RDPHALF_CONT;
629 extern u32 G_SPNOOP;
630 extern u32 G_SETOTHERMODE_H, G_SETOTHERMODE_L;
631 extern u32 G_DL, G_ENDDL, G_CULLDL, G_BRANCH_Z;
632 extern u32 G_LOAD_UCODE;
633 extern u32 G_MOVEMEM, G_MOVEWORD;
634 extern u32 G_MTX, G_POPMTX;
635 extern u32 G_GEOMETRYMODE, G_SETGEOMETRYMODE, G_CLEARGEOMETRYMODE;
636 extern u32 G_TEXTURE;
637 extern u32 G_DMA_IO, G_DMA_DL, G_DMA_TRI, G_DMA_MTX, G_DMA_VTX, G_DMA_OFFSETS;
638 extern u32 G_SPECIAL_1, G_SPECIAL_2, G_SPECIAL_3;
639 extern u32 G_VTX, G_MODIFYVTX, G_VTXCOLORBASE;
640 extern u32 G_TRI1, G_TRI2, G_TRI4;
641 extern u32 G_QUAD, G_LINE3D;
642 extern u32 G_RESERVED0, G_RESERVED1, G_RESERVED2, G_RESERVED3;
643 extern u32 G_SPRITE2D_BASE;
644 extern u32 G_BG_1CYC, G_BG_COPY;
645 extern u32 G_OBJ_RECTANGLE, G_OBJ_SPRITE, G_OBJ_MOVEMEM;
646 extern u32 G_SELECT_DL, G_OBJ_RENDERMODE, G_OBJ_RECTANGLE_R;
647 extern u32 G_OBJ_LOADTXTR, G_OBJ_LDTX_SPRITE, G_OBJ_LDTX_RECT, G_OBJ_LDTX_RECT_R;
648 extern u32 G_RDPHALF_0, G_TRI_UNKNOWN;
649
650 #define LIGHT_1 1
651 #define LIGHT_2 2
652 #define LIGHT_3 3
653 #define LIGHT_4 4
654 #define LIGHT_5 5
655 #define LIGHT_6 6
656 #define LIGHT_7 7
657 #define LIGHT_8 8
658
659 #define G_DL_PUSH       0x00
660 #define G_DL_NOPUSH     0x01
661
662 typedef struct
663 {
664     s16 y;
665     s16 x;
666
667     u16 flag;
668     s16 z;
669
670     s16 t;
671     s16 s;
672
673     union {
674         struct
675         {
676             u8 a;
677             u8 b;
678             u8 g;
679             u8 r;
680         } color;
681         struct
682         {
683             s8 a;
684             s8 z;   // b
685             s8 y;   //g
686             s8 x;   //r
687         } normal;
688     };
689 } Vertex;
690
691 typedef struct
692 {
693     s16 y, x;
694     u16 ci;
695     s16 z;
696     s16 t, s;
697 } PDVertex;
698
699
700 typedef struct
701 {
702     u8      v2, v1, v0, flag;
703     s16     t0, s0;
704     s16     t1, s1;
705     s16     t2, s2;
706 } DKRTriangle;
707
708 struct Light
709 {
710     u8 pad0, b, g, r;
711     u8 pad1, b2, g2, r2;
712     s8 pad2, z, y, x;
713 };
714
715 struct LightMM
716 {
717     u8 pad0, b, g, r;
718     u8 pad1, b2, g2, r2;
719     s16 y, x, range, z;
720 };
721
722
723 // GBI commands
724 typedef void (*GBIFunc)( u32 w0, u32 w1 );
725 //extern GBIFunc GBICmd[256];
726
727 struct SpecialMicrocodeInfo
728 {
729     u32 type;
730     u32 NoN;
731     u32 crc;
732     const char *text;
733 };
734
735 struct MicrocodeInfo
736 {
737     u32 address, dataAddress;
738     u16 dataSize;
739     u32 type;
740     u32 NoN;
741     u32 crc;
742     u32 *text;
743
744     MicrocodeInfo *higher, *lower;
745 };
746
747 struct GBIInfo
748 {
749     GBIFunc cmd[256];
750
751     u32 PCStackSize, numMicrocodes;
752     MicrocodeInfo *current, *top, *bottom;
753
754 #ifdef PROFILE_GBI
755     unsigned int profileTimer[256 * 12];
756     unsigned int profileNum[256 * 12];
757     unsigned int profileTmp;
758 #endif
759 };
760
761 extern GBIInfo GBI;
762
763 #ifdef PROFILE_GBI
764 void GBI_ProfileReset();
765 void GBI_ProfileInit();
766 void GBI_ProfileBegin(u32 cmd);
767 void GBI_ProfileEnd(u32 cmd);
768 u32  GBI_ProfilePrint(FILE *file);
769 const char* GBI_GetFuncName(u32 ucode, u32 cmd);
770 u32  GBI_GetFuncTime(u32 ucode, u32 cmd);
771 #endif
772
773 void GBI_MakeCurrent( MicrocodeInfo *current );
774 MicrocodeInfo *GBI_DetectMicrocode( u32 uc_start, u32 uc_dstart, u16 uc_dsize );
775 extern u32 last_good_ucode;
776 void GBI_Init();
777 void GBI_Destroy();
778
779 // Allows easier setting of GBI commands
780 #define GBI_SetGBI( command, value, function ) \
781     command = value; \
782     GBI.cmd[command] = function
783
784 #define GBI_InitFlags( ucode ) \
785     G_MTX_STACKSIZE     = ucode##_MTX_STACKSIZE; \
786     G_MTX_MODELVIEW     = ucode##_MTX_MODELVIEW; \
787     G_MTX_PROJECTION    = ucode##_MTX_PROJECTION; \
788     G_MTX_MUL           = ucode##_MTX_MUL; \
789     G_MTX_LOAD          = ucode##_MTX_LOAD; \
790     G_MTX_NOPUSH        = ucode##_MTX_NOPUSH; \
791     G_MTX_PUSH          = ucode##_MTX_PUSH; \
792 \
793     G_TEXTURE_ENABLE    = ucode##_TEXTURE_ENABLE; \
794     G_SHADING_SMOOTH    = ucode##_SHADING_SMOOTH; \
795     G_CULL_FRONT        = ucode##_CULL_FRONT; \
796     G_CULL_BACK         = ucode##_CULL_BACK; \
797     G_CULL_BOTH         = ucode##_CULL_BOTH; \
798     G_CLIPPING          = ucode##_CLIPPING; \
799 \
800     G_MV_VIEWPORT       = ucode##_MV_VIEWPORT; \
801 \
802     G_MWO_aLIGHT_1      = ucode##_MWO_aLIGHT_1; \
803     G_MWO_bLIGHT_1      = ucode##_MWO_bLIGHT_1; \
804     G_MWO_aLIGHT_2      = ucode##_MWO_aLIGHT_2; \
805     G_MWO_bLIGHT_2      = ucode##_MWO_bLIGHT_2; \
806     G_MWO_aLIGHT_3      = ucode##_MWO_aLIGHT_3; \
807     G_MWO_bLIGHT_3      = ucode##_MWO_bLIGHT_3; \
808     G_MWO_aLIGHT_4      = ucode##_MWO_aLIGHT_4; \
809     G_MWO_bLIGHT_4      = ucode##_MWO_bLIGHT_4; \
810     G_MWO_aLIGHT_5      = ucode##_MWO_aLIGHT_5; \
811     G_MWO_bLIGHT_5      = ucode##_MWO_bLIGHT_5; \
812     G_MWO_aLIGHT_6      = ucode##_MWO_aLIGHT_6; \
813     G_MWO_bLIGHT_6      = ucode##_MWO_bLIGHT_6; \
814     G_MWO_aLIGHT_7      = ucode##_MWO_aLIGHT_7; \
815     G_MWO_bLIGHT_7      = ucode##_MWO_bLIGHT_7; \
816     G_MWO_aLIGHT_8      = ucode##_MWO_aLIGHT_8; \
817     G_MWO_bLIGHT_8      = ucode##_MWO_bLIGHT_8;
818
819 #endif
820