1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX -
3   File:     g3x.h
4 
5   Copyright 2003-2009 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2009-06-04#$
14   $Rev: 10698 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NITRO_G3X_H_
19 #define NITRO_G3X_H_
20 
21 #include <nitro/gx/gxcommon.h>
22 
23 #ifndef SDK_TWL
24 #include <nitro/hw/ARM9/ioreg_G3X.h>
25 #include <nitro/hw/ARM9/mmap_global.h>
26 #else
27 #include <twl/hw/ARM9/ioreg_G3X.h>
28 #include <twl/hw/ARM9/mmap_global.h>
29 #endif
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 //----------------------------------------------------------------------------
36 // Type definition
37 //----------------------------------------------------------------------------
38 
39 //----------------------------------------------------------------------------
40 //    Parameters for G3X_SetShading
41 //----------------------------------------------------------------------------
42 typedef enum
43 {
44     GX_SHADING_TOON = 0,
45     GX_SHADING_HIGHLIGHT = 1
46 }
47 GXShading;
48 
49 #define GX_SHADING_ASSERT(x)              \
50     SDK_MINMAX_ASSERT(x, GX_SHADING_TOON, GX_SHADING_HIGHLIGHT)
51 
52 //----------------------------------------------------------------------------
53 //    Parameters for G3X_SetFog
54 //----------------------------------------------------------------------------
55 typedef enum
56 {
57     GX_FOGBLEND_COLOR_ALPHA = 0,
58     GX_FOGBLEND_ALPHA = 1
59 }
60 GXFogBlend;
61 
62 #define GX_FOGBLEND_ASSERT(x)                          \
63     SDK_MINMAX_ASSERT(x, GX_FOGBLEND_COLOR_ALPHA, GX_FOGBLEND_ALPHA)
64 
65 typedef enum
66 {
67     GX_FOGSLOPE_0x8000 = 0,
68     GX_FOGSLOPE_0x4000 = 1,
69     GX_FOGSLOPE_0x2000 = 2,
70     GX_FOGSLOPE_0x1000 = 3,
71     GX_FOGSLOPE_0x0800 = 4,
72     GX_FOGSLOPE_0x0400 = 5,
73     GX_FOGSLOPE_0x0200 = 6,
74     GX_FOGSLOPE_0x0100 = 7,
75     GX_FOGSLOPE_0x0080 = 8,
76     GX_FOGSLOPE_0x0040 = 9,
77     GX_FOGSLOPE_0x0020 = 10
78 }
79 GXFogSlope;
80 
81 #define GX_FOGSLOPE_ASSERT(x)                \
82     SDK_MINMAX_ASSERT(x, GX_FOGSLOPE_0x8000, GX_FOGSLOPE_0x0020)
83 
84 //----------------------------------------------------------------------------
85 //    Parameters for G3X_SetFifoIntrCond
86 //----------------------------------------------------------------------------
87 typedef enum
88 {
89     GX_FIFOINTR_COND_DISABLE = 0,
90     GX_FIFOINTR_COND_UNDERHALF = 1,
91     GX_FIFOINTR_COND_EMPTY = 2
92 }
93 GXFifoIntrCond;
94 
95 #define GX_FIFOINTR_COND_ASSERT(x)                        \
96     SDK_MINMAX_ASSERT(x, GX_FIFOINTR_COND_DISABLE, GX_FIFOINTR_COND_EMPTY)
97 
98 //----------------------------------------------------------------------------
99 //    Return type of G3X_GetCommandFifoStatus
100 //----------------------------------------------------------------------------
101 typedef enum
102 {
103     GX_FIFOSTAT_EMPTY = 6,
104     GX_FIFOSTAT_UNDERHALF = 2,
105     GX_FIFOSTAT_OVERHALF = 0,
106     GX_FIFOSTAT_FULL = 1
107 }
108 GXFifoStat;
109 
110 #define GX_RDLINES_COUNT_MAX   46
111 #define GX_LISTRAM_COUNT_MAX   2048    // 0x800
112 #define GX_VTXRAM_COUNT_MAX    6144    // 0x1800
113 
114 
115 /* if include from Other Environment for exsample VC or BCB, */
116 /* please define SDK_FROM_TOOL                               */
117 #if !(defined(SDK_WIN32) || defined(SDK_FROM_TOOL))
118 
119 //----------------------------------------------------------------------------
120 // Declaration of function
121 //----------------------------------------------------------------------------
122 
123 // initialization
124 void    G3X_Init(void);
125 void    G3X_InitMtxStack(void);
126 void    G3X_ResetMtxStack(void);
127 void    G3X_InitTable(void);
128 void    G3X_Reset(void);
129 
130 // for reg_G3X_DISP3DCNT
131 static void G3X_SetShading(GXShading shading);
132 static void G3X_AlphaTest(BOOL enable, int ref);
133 static void G3X_AlphaBlend(BOOL enable);
134 static void G3X_AntiAlias(BOOL enable);
135 static void G3X_EdgeMarking(BOOL enable);
136 void    G3X_SetFog(BOOL enable, GXFogBlend fogMode, GXFogSlope fogSlope, int fogOffset);
137 
138 // for reg_G3X_GXSTAT
139 int     G3X_GetMtxStackLevelPV(s32 *level);     // read: GXSTAT(8-12), GXSTAT(14)
140 int     G3X_GetMtxStackLevelPJ(s32 *level);     // read: GXSTAT(13),   GXSTAT(14)
141 static void G3X_SetFifoIntrCond(GXFifoIntrCond cond);
142 
143 // for reg_G3X_DISP_1DOT_DEPTH
144 void    G3X_SetDisp1DotDepth(fx32 w);  // write: DISP_1DOT_DEPTH
145 
146 int     G3X_GetBoxTestResult(s32 *in); // read: GXSTAT(0-1)
147 int     G3X_GetPositionTestResult(VecFx32 *vec, fx32 *w);       // read: GXSTAT(0), POS_RESULT_X, POS_RESULT_Y, POS_RESULT_Z, POS_RESULT_W
148 int     G3X_GetVectorTestResult(fx16 *vec);     // read: GXSTAT(0), VEC_RESULT_X, VEC_RESULT_Y, VEC_RESULT_Z
149 
150 int     G3X_GetClipMtx(MtxFx44 *m);    // read: CLIPMTX_RESULT_0-15
151 int     G3X_GetVectorMtx(MtxFx33 *m);
152 
153 // for reg_G2_BG0OFS
154 void    G3X_SetHOffset(int hOffset);
155 
156 void    G3X_SetEdgeColorTable(const GXRgb *rgb_8);
157 void    G3X_SetFogTable(const u32 *fogTable);
158 void    G3X_SetToonTable(const GXRgb *rgb_32);
159 void    G3X_SetClearColor(GXRgb rgb, int alpha, int depth, int polygonID, BOOL fog);
160 static void G3X_SetClearImageOffset(int xOffset, int yOffset);
161 static void G3X_SetFogColor(GXRgb rgb, int alpha);
162 
163 static s32 G3X_IsMtxStackOverflow(s32 *overflow);
164 static s32 G3X_IsLineBufferUnderflow(void);
165 static s32 G3X_IsListRamOverflow(void);
166 static s32 G3X_IsGeometryBusy(void);
167 
168 static void G3X_ResetMtxStackOverflow(void);
169 static void G3X_ResetLineBufferUnderflow(void);
170 static void G3X_ResetListRamOverflow(void);
171 void    G3X_ClearFifo(void);
172 
173 static s32 G3X_GetPolygonListRamCount(void);
174 static s32 G3X_GetVtxListRamCount(void);
175 static s32 G3X_GetRenderedLineCount(void);
176 static s32 G3X_GetCommandFifoCount(void);
177 static GXFifoStat G3X_GetCommandFifoStatus(void);
178 
179 //----------------------------------------------------------------------------
180 // Implementation of inline function
181 //----------------------------------------------------------------------------
182 
183 
184 // DISP3DCNT(1)
185 /*---------------------------------------------------------------------------*
186   Name:         G3X_SetShading
187 
188   Description:  Select Toon or Highlight shading
189 
190   Arguments:    shading      select toon/highlight shading
191 
192   Returns:      none
193  *---------------------------------------------------------------------------*/
G3X_SetShading(GXShading shading)194 static inline void G3X_SetShading(GXShading shading)
195 {
196     reg_G3X_DISP3DCNT = (u16)((reg_G3X_DISP3DCNT & ~(REG_G3X_DISP3DCNT_THS_MASK |
197                                                      REG_G3X_DISP3DCNT_RO_MASK |
198                                                      REG_G3X_DISP3DCNT_GO_MASK)) |
199                               (shading << REG_G3X_DISP3DCNT_THS_SHIFT));
200 }
201 
202 
203 // DISP3DCNT(2) and ALPHA_TEST_REF
204 /*---------------------------------------------------------------------------*
205   Name:         G3X_AlphaTest
206 
207   Description:  Set the condition for Alpha Testing
208 
209   Arguments:    enable     if FALSE, alpha test is disabled.
210                            otherwise, alpha test is enabled.
211                 ref        if alpha is less than or equal to 'ref',
212                            polygons are not drawn.
213 
214   Returns:      none
215  *---------------------------------------------------------------------------*/
G3X_AlphaTest(BOOL enable,int ref)216 static inline void G3X_AlphaTest(BOOL enable, int ref)
217 {
218     if (enable)
219     {
220         GX_ALPHA_ASSERT(ref);
221         reg_G3X_DISP3DCNT = (u16)(reg_G3X_DISP3DCNT &
222                                   ~(REG_G3X_DISP3DCNT_RO_MASK | REG_G3X_DISP3DCNT_GO_MASK) |
223                                   REG_G3X_DISP3DCNT_ATE_MASK);
224         reg_G3X_ALPHA_TEST_REF = (u16)ref;
225     }
226     else
227     {
228         reg_G3X_DISP3DCNT &= (u16)~(REG_G3X_DISP3DCNT_ATE_MASK |
229                                     REG_G3X_DISP3DCNT_RO_MASK | REG_G3X_DISP3DCNT_GO_MASK);
230     }
231 }
232 
233 
234 // DISP3DCNT(3)
235 /*---------------------------------------------------------------------------*
236   Name:         G3X_AlphaBlend
237 
238   Description:  Enable/Disable alpha blending.
239                 Blends color buffer's color and fragment color by the fragment's alpha
240 
241   Arguments:    enable     if FALSE, alpha blending is disabled.
242                            otherwise, alpha blending is enabled.
243 
244   Returns:      none
245  *---------------------------------------------------------------------------*/
G3X_AlphaBlend(BOOL enable)246 static inline void G3X_AlphaBlend(BOOL enable)
247 {
248     if (enable)
249     {
250         reg_G3X_DISP3DCNT = (u16)(reg_G3X_DISP3DCNT &
251                                   ~(REG_G3X_DISP3DCNT_RO_MASK | REG_G3X_DISP3DCNT_GO_MASK) |
252                                   REG_G3X_DISP3DCNT_ABE_MASK);
253     }
254     else
255     {
256         reg_G3X_DISP3DCNT &= (u16)~(REG_G3X_DISP3DCNT_ABE_MASK |
257                                     REG_G3X_DISP3DCNT_RO_MASK | REG_G3X_DISP3DCNT_GO_MASK);
258     }
259 }
260 
261 
262 // DISP3DCNT(4)
263 /*---------------------------------------------------------------------------*
264   Name:         G3X_AntiAlias
265 
266   Description:  Enable/Disable anti-aliasing
267 
268   Arguments:    enable     if FALSE, anti-aliasing is disabled.
269                            otherwise, anti-aliasing is enabled.
270 
271   Returns:      none
272  *---------------------------------------------------------------------------*/
G3X_AntiAlias(BOOL enable)273 static inline void G3X_AntiAlias(BOOL enable)
274 {
275     if (enable)
276     {
277         reg_G3X_DISP3DCNT = (u16)(reg_G3X_DISP3DCNT &
278                                   ~(REG_G3X_DISP3DCNT_RO_MASK | REG_G3X_DISP3DCNT_GO_MASK) |
279                                   REG_G3X_DISP3DCNT_AAE_MASK);
280     }
281     else
282     {
283         reg_G3X_DISP3DCNT &= (u16)~(REG_G3X_DISP3DCNT_AAE_MASK |
284                                     REG_G3X_DISP3DCNT_RO_MASK | REG_G3X_DISP3DCNT_GO_MASK);
285     }
286 }
287 
288 
289 // DISP3DCNT(5)
290 /*---------------------------------------------------------------------------*
291   Name:         G3X_EdgeMarking
292 
293   Description:  Enable/Disable edge marking
294 
295   Arguments:    enable     if FALSE, edge marking is disabled.
296                            otherwise, edge marking is enabled.
297 
298   Returns:      none
299  *---------------------------------------------------------------------------*/
G3X_EdgeMarking(BOOL enable)300 static inline void G3X_EdgeMarking(BOOL enable)
301 {
302     if (enable)
303     {
304         reg_G3X_DISP3DCNT = (u16)(reg_G3X_DISP3DCNT &
305                                   ~(REG_G3X_DISP3DCNT_RO_MASK | REG_G3X_DISP3DCNT_GO_MASK) |
306                                   REG_G3X_DISP3DCNT_EME_MASK);
307     }
308     else
309     {
310         reg_G3X_DISP3DCNT &= (u16)~(REG_G3X_DISP3DCNT_EME_MASK |
311                                     REG_G3X_DISP3DCNT_RO_MASK | REG_G3X_DISP3DCNT_GO_MASK);
312     }
313 }
314 
315 
316 /*---------------------------------------------------------------------------*
317   Name:         G3X_SetFifoIntrCond
318 
319   Description:  Specifies the condition for the geometry command FIFO interrupt
320                 request generation.
321 
322   Arguments:    cond       specify disable/underhalf/empty
323 
324   Returns:      none
325  *---------------------------------------------------------------------------*/
G3X_SetFifoIntrCond(GXFifoIntrCond cond)326 static inline void G3X_SetFifoIntrCond(GXFifoIntrCond cond)
327 {
328     GX_FIFOINTR_COND_ASSERT(cond);
329 
330     reg_G3X_GXSTAT = ((reg_G3X_GXSTAT & ~REG_G3X_GXSTAT_FI_MASK) |
331                       (cond << REG_G3X_GXSTAT_FI_SHIFT));
332 }
333 
334 
335 /*---------------------------------------------------------------------------*
336   Name:         G3X_SetClearImageOffset
337 
338   Description:  The offset of clearimage can be specified if enabled.
339 
340   Arguments:    xOffset    X offset (in dot)
341                 yOffset    Y offset (in dot)
342 
343   Returns:      none
344  *---------------------------------------------------------------------------*/
G3X_SetClearImageOffset(int xOffset,int yOffset)345 static inline void G3X_SetClearImageOffset(int xOffset, int yOffset)
346 {
347     reg_G3X_CLRIMAGE_OFFSET = (u16)(((xOffset << REG_G3X_CLRIMAGE_OFFSET_OFFSETX_SHIFT) &
348                                      REG_G3X_CLRIMAGE_OFFSET_OFFSETX_MASK) | ((yOffset <<
349                                                                                REG_G3X_CLRIMAGE_OFFSET_OFFSETY_SHIFT)
350                                                                               &
351                                                                               REG_G3X_CLRIMAGE_OFFSET_OFFSETY_MASK));
352 }
353 
354 
355 /*---------------------------------------------------------------------------*
356   Name:         G3X_SetFogColor
357 
358   Description:  Specifies the color of Fog.
359 
360   Arguments:    rgb       RGB of fog
361                 alpha     alpha of fog
362 
363   Returns:      none
364  *---------------------------------------------------------------------------*/
G3X_SetFogColor(GXRgb rgb,int alpha)365 static inline void G3X_SetFogColor(GXRgb rgb, int alpha)
366 {
367     GXRGB_ASSERT(rgb);
368     GX_ALPHA_ASSERT(alpha);
369 
370     reg_G3X_FOG_COLOR = (u32)((rgb << REG_G3X_FOG_COLOR_FOG_RED_SHIFT) |
371                               (alpha << REG_G3X_FOG_COLOR_FOG_ALPHA_SHIFT));
372 }
373 
374 
375 /*---------------------------------------------------------------------------*
376   Name:         G3X_IsMtxStackOverflow
377 
378   Description:  Test if the matrix stack overflows
379 
380   Arguments:    overflow    a pointer to overflow flag of matrix stack.
381                             0 if the stack isn't overflow.
382                             others the stack is overflow.
383 
384   Returns:      0 if *overflow has the result (not busy)
385                 others the matrix stack is busy.
386  *---------------------------------------------------------------------------*/
G3X_IsMtxStackOverflow(s32 * overflow)387 static inline s32 G3X_IsMtxStackOverflow(s32 *overflow)
388 {
389     SDK_NULL_ASSERT(overflow);
390     if (reg_G3X_GXSTAT & REG_G3X_GXSTAT_SB_MASK)
391     {
392         return -1;
393     }
394 
395     *overflow = (s32)(reg_G3X_GXSTAT & REG_G3X_GXSTAT_SE_MASK);
396     return 0;
397 }
398 
399 
400 /*---------------------------------------------------------------------------*
401   Name:         G3X_IsLineBufferUnderflow
402 
403   Description:  Test if the line buffer underflow
404 
405   Arguments:    none
406 
407   Returns:      0 if the buffer does not overflow
408                 others if the buffer overflows
409  *---------------------------------------------------------------------------*/
410 #define G3X_IsLineBufferOverflow    G3X_IsLineBufferUnderflow
411 
G3X_IsLineBufferUnderflow(void)412 static inline s32 G3X_IsLineBufferUnderflow(void)
413 {
414     return (reg_G3X_DISP3DCNT & REG_G3X_DISP3DCNT_RO_MASK);
415 }
416 
417 
418 /*---------------------------------------------------------------------------*
419   Name:         G3X_IsListRamOverflow
420 
421   Description:  Test if the polygon list RAM or the vertex RAM overflow
422 
423   Arguments:    none
424 
425   Returns:      0 if the list does not overflow
426                 others if the list overflows
427  *---------------------------------------------------------------------------*/
G3X_IsListRamOverflow(void)428 static inline s32 G3X_IsListRamOverflow(void)
429 {
430     return (reg_G3X_DISP3DCNT & REG_G3X_DISP3DCNT_GO_MASK);
431 }
432 
433 
434 /*---------------------------------------------------------------------------*
435   Name:         G3X_IsGeometryBusy
436 
437   Description:  Test if geometry engine is busy
438 
439   Arguments:    none
440 
441   Returns:      0 if geometry engine is not busy
442                 others if geometry engine is busy
443  *---------------------------------------------------------------------------*/
G3X_IsGeometryBusy(void)444 static inline s32 G3X_IsGeometryBusy(void)
445 {
446     return (s32)(reg_G3X_GXSTAT & REG_G3X_GXSTAT_B_MASK);
447 }
448 
449 
450 /*---------------------------------------------------------------------------*
451   Name:         G3X_ResetMtxStackOverflow
452 
453   Description:  Reset overflow state of the matrix stack
454 
455   Arguments:    none
456 
457   Returns:      none
458  *---------------------------------------------------------------------------*/
G3X_ResetMtxStackOverflow(void)459 static inline void G3X_ResetMtxStackOverflow(void)
460 {
461     reg_G3X_GXSTAT |= REG_G3X_GXSTAT_SE_MASK;
462 }
463 
464 
465 /*---------------------------------------------------------------------------*
466   Name:         G3X_ResetLineBufferUnderflow
467 
468   Description:  Reset overflow state of the line buffer
469 
470   Arguments:    none
471 
472   Returns:      none
473  *---------------------------------------------------------------------------*/
474 #define G3X_ResetLineBufferOverflow     G3X_ResetLineBufferUnderflow
475 
G3X_ResetLineBufferUnderflow(void)476 static inline void G3X_ResetLineBufferUnderflow(void)
477 {
478     reg_G3X_DISP3DCNT |= REG_G3X_DISP3DCNT_RO_MASK;
479 }
480 
481 
482 /*---------------------------------------------------------------------------*
483   Name:         G3X_ResetListRamOverflow
484 
485   Description:  Reset overflow state of the polygon list RAM and the vertex RAM
486 
487   Arguments:    none
488 
489   Returns:      none
490  *---------------------------------------------------------------------------*/
G3X_ResetListRamOverflow(void)491 static inline void G3X_ResetListRamOverflow(void)
492 {
493     reg_G3X_DISP3DCNT |= REG_G3X_DISP3DCNT_GO_MASK;
494 }
495 
496 
497 /*---------------------------------------------------------------------------*
498   Name:         G3X_GetPolygonListRamCount
499 
500   Description:  Returns the number of polygons in the polygon list RAM
501 
502   Arguments:    none
503 
504   Returns:      the number of polygons
505  *---------------------------------------------------------------------------*/
G3X_GetPolygonListRamCount(void)506 static inline s32 G3X_GetPolygonListRamCount(void)
507 {
508     return reg_G3X_LISTRAM_COUNT;
509 }
510 
511 
512 /*---------------------------------------------------------------------------*
513   Name:         G3X_GetVtxListRamCount
514 
515   Description:  Returns the number of vertexes in the vertex RAM
516 
517   Arguments:    none
518 
519   Returns:      the number of vertexes
520  *---------------------------------------------------------------------------*/
G3X_GetVtxListRamCount(void)521 static inline s32 G3X_GetVtxListRamCount(void)
522 {
523     return reg_G3X_VTXRAM_COUNT;
524 }
525 
526 
527 /*---------------------------------------------------------------------------*
528   Name:         G3X_GetRenderedLineCount
529 
530   Description:  Returns the minimum number of the rendered lines in the frame just before
531 
532   Arguments:    none
533 
534   Returns:      the minimum number of the rendered lines
535  *---------------------------------------------------------------------------*/
G3X_GetRenderedLineCount(void)536 static inline s32 G3X_GetRenderedLineCount(void)
537 {
538     return reg_G3X_RDLINES_COUNT;
539 }
540 
541 
542 /*---------------------------------------------------------------------------*
543   Name:         G3X_GetCommandFifoCount
544 
545   Description:  Returns the number of the geometry command/data in the command FIFO
546 
547   Arguments:    none
548 
549   Returns:      the number of the geometry command/data in the command FIFO
550  *---------------------------------------------------------------------------*/
G3X_GetCommandFifoCount(void)551 static inline s32 G3X_GetCommandFifoCount(void)
552 {
553     return (s32)((reg_G3X_GXSTAT & REG_G3X_GXSTAT_FIFOCNT_MASK) >> REG_G3X_GXSTAT_FIFOCNT_SHIFT);
554 }
555 
556 
557 /*---------------------------------------------------------------------------*
558   Name:         G3X_GetCommandFifoStatus
559 
560   Description:  Return the status of the command FIFO
561 
562   Arguments:    none
563 
564   Returns:      the status of the command FIFO
565  *---------------------------------------------------------------------------*/
G3X_GetCommandFifoStatus(void)566 static inline GXFifoStat G3X_GetCommandFifoStatus(void)
567 {
568     return (GXFifoStat)((reg_G3X_GXSTAT & (REG_G3X_GXSTAT_F_MASK |
569                                            REG_G3X_GXSTAT_H_MASK |
570                                            REG_G3X_GXSTAT_E_MASK)) >> REG_G3X_GXSTAT_F_SHIFT);
571 }
572 
573 
574 
575 #endif // SDK_FROM_TOOL
576 
577 #ifdef __cplusplus
578 }/* extern "C" */
579 #endif
580 
581 /* NITRO_G3X_H_ */
582 #endif
583