1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX -
3   File:     g2.c
4 
5   Copyright 2003-2008 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:: 2008-09-17#$
14   $Rev: 8556 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #include <nitro/gx/g2.h>
19 #include <nitro/fx/fx_const.h>
20 
21 
22 //----------------------------------------------------------------------------
23 // G2x_SetBGyAffine_:
24 //
25 // Called from G2_SetBG2Affine, G2_SetBG3Affine,
26 // G2S_SetBG2Affine, and G2S_SetBG3Affine. Internal use only.
27 //----------------------------------------------------------------------------
28 #include <nitro/code32.h>
29 // In this case, ARM binary is equal or better than THUMB binary even in size.
G2x_SetBGyAffine_(u32 addr,const MtxFx22 * mtx,int centerX,int centerY,int x1,int y1)30 void G2x_SetBGyAffine_(u32 addr, const MtxFx22 *mtx, int centerX, int centerY, int x1, int y1)
31 {
32     s32     dx, dy;
33     fx32    x2, y2;
34 
35     SDK_NULL_ASSERT(mtx);
36     SDK_MINMAX_ASSERT(mtx->_00, -128 * FX32_ONE, 128 * FX32_ONE - 1);
37     SDK_MINMAX_ASSERT(mtx->_01, -128 * FX32_ONE, 128 * FX32_ONE - 1);
38     SDK_MINMAX_ASSERT(mtx->_10, -128 * FX32_ONE, 128 * FX32_ONE - 1);
39     SDK_MINMAX_ASSERT(mtx->_11, -128 * FX32_ONE, 128 * FX32_ONE - 1);
40 
41     // BGxPA, BGxPB are in s7.8 format
42     *((vu32 *)addr + 0) = (u32)((u16)(s16)(mtx->_00 >> 4) | (u16)(s16)(mtx->_01 >> 4) << 16);
43 
44     // BGxPC, BGxPC are in s7.8 format
45     *((vu32 *)addr + 1) = (u32)((u16)(s16)(mtx->_10 >> 4) | (u16)(s16)(mtx->_11 >> 4) << 16);
46 
47     dx = x1 - centerX;
48     dy = y1 - centerY;
49 
50     // mtx * d + center
51     // Note that x2, y2 are in fx32 format since dx, dy is integer.
52     x2 = mtx->_00 * dx + mtx->_01 * dy + (centerX << FX32_SHIFT);
53     y2 = mtx->_10 * dx + mtx->_11 * dy + (centerY << FX32_SHIFT);
54 
55     // reg_G2m_BGnXY has s19.8 format
56     *((vu32 *)addr + 2) = (u32)(x2 >> 4);       // BGxX
57     *((vu32 *)addr + 3) = (u32)(y2 >> 4);       // BGxY
58 }
59 
60 #include <nitro/codereset.h>
61 
62 
63 typedef enum
64 {
65     G2_BLENDTYPE_NONE = 0x0000,
66     G2_BLENDTYPE_ALPHA = 0x0040,
67     G2_BLENDTYPE_FADEIN = 0x0080,
68     G2_BLENDTYPE_FADEOUT = 0x00c0
69 }
70 G2_BLENDTYPE;
71 
72 
73 //----------------------------------------------------------------------------
74 // G2x_SetBlendAlpha_:
75 //
76 // Called from G2_SetBlendAlpha and G2S_SetBlendAlpha.
77 // Internal use only.
78 //----------------------------------------------------------------------------
G2x_SetBlendAlpha_(u32 addr,int plane1,int plane2,int ev1,int ev2)79 void G2x_SetBlendAlpha_(u32 addr, int plane1, int plane2, int ev1, int ev2)
80 {
81     SDK_MINMAX_ASSERT(plane1, 0, 0x20 - 1);
82     SDK_MINMAX_ASSERT(plane2, 0, 0x40 - 1);
83     SDK_MINMAX_ASSERT(ev1, 0, 31);
84     SDK_MINMAX_ASSERT(ev2, 0, 31);
85 
86     // BLDCNT & BLDALPHA
87     *((vu32 *)addr + 0) =
88         (u32)((G2_BLENDTYPE_ALPHA | plane1 | plane2 << 8) | ((ev1 | ev2 << 8) << 16));
89 }
90 
91 
92 //----------------------------------------------------------------------------
93 // G2x_SetBlendBrightness_:
94 //
95 // Called from G2_SetBlendBrightness and G2S_SetBlendBrightness.
96 // Internal use only.
97 //----------------------------------------------------------------------------
G2x_SetBlendBrightness_(u32 addr,int plane,int brightness)98 void G2x_SetBlendBrightness_(u32 addr, int plane, int brightness)
99 {
100     SDK_MINMAX_ASSERT(brightness, -16, 16);
101     SDK_MINMAX_ASSERT(plane, 0, 0x40 - 1);
102 
103     if (brightness < 0)
104     {
105         // BLDCNT
106         *((vu16 *)addr + 0) = (u16)(G2_BLENDTYPE_FADEOUT | plane);
107         // BLDY
108         *((vu16 *)addr + 2) = (u16)-brightness;
109     }
110     else
111     {
112         // BLDCNT
113         *((vu16 *)addr + 0) = (u16)(G2_BLENDTYPE_FADEIN | plane);
114         // BLDY
115         *((vu16 *)addr + 2) = (u16)brightness;
116     }
117 }
118 
119 
120 //----------------------------------------------------------------------------
121 // G2x_SetBlendBrightnessExt_:
122 //
123 // Called from G2_SetBlendBrightnessExt and G2S_SetBlendBrightnessExt.
124 // Internal use only.
125 //----------------------------------------------------------------------------
G2x_SetBlendBrightnessExt_(u32 addr,int plane1,int plane2,int ev1,int ev2,int brightness)126 void G2x_SetBlendBrightnessExt_(u32 addr, int plane1, int plane2, int ev1, int ev2, int brightness)
127 {
128     SDK_MINMAX_ASSERT(brightness, -16, 16);
129     SDK_MINMAX_ASSERT(plane1, 0, 0x40 - 1);
130     SDK_MINMAX_ASSERT(plane2, 0, 0x40 - 1);
131     SDK_MINMAX_ASSERT(ev1, 0, 31);
132     SDK_MINMAX_ASSERT(ev2, 0, 31);
133 
134     // BLDALPHA
135     *((vu16 *)addr + 1) = (u16)(ev1 | (ev2 << 8));
136 
137     if (brightness < 0)
138     {
139         // BLDCNT
140         *((vu16 *)addr + 0) = (u16)(G2_BLENDTYPE_FADEOUT | plane1 | (plane2 << 8));
141 
142         // BLDY
143         *((vu16 *)addr + 2) = (u16)-brightness;
144     }
145     else
146     {
147         // BLDCNT
148         *((vu16 *)addr + 0) = (u16)(G2_BLENDTYPE_FADEIN | plane1 | (plane2 << 8));
149 
150         // BLDY
151         *((vu16 *)addr + 2) = (u16)brightness;
152     }
153 }
154 
155 
156 //----------------------------------------------------------------------------
157 // G2x_ChangeBlendBrightness_:
158 //
159 // Called from G2_ChangeBlendBrightness and G2S_ChangeBlendBrightness.
160 // Internal use only.
161 //----------------------------------------------------------------------------
G2x_ChangeBlendBrightness_(u32 addr,int brightness)162 void G2x_ChangeBlendBrightness_(u32 addr, int brightness)
163 {
164     u16     tmp;
165     SDK_MINMAX_ASSERT(brightness, -16, 16);
166 
167     // read BLDCNT
168     tmp = *((vu16 *)addr + 0);
169 
170     if (brightness < 0)
171     {
172         if (G2_BLENDTYPE_FADEIN == (tmp & REG_G2_BLDCNT_EFFECT_MASK))
173         {
174             // BLDCNT
175             *((vu16 *)addr + 0) = (u16)((tmp & ~REG_G2_BLDCNT_EFFECT_MASK) | G2_BLENDTYPE_FADEOUT);
176         }
177         // BLDY
178         *((vu16 *)addr + 2) = (u16)-brightness;
179     }
180     else
181     {
182         if (G2_BLENDTYPE_FADEOUT == (tmp & REG_G2_BLDCNT_EFFECT_MASK))
183         {
184             // BLDCNT
185             *((vu16 *)addr + 0) = (u16)((tmp & ~REG_G2_BLDCNT_EFFECT_MASK) | G2_BLENDTYPE_FADEIN);
186         }
187         // BLDY
188         *((vu16 *)addr + 2) = (u16)brightness;
189     }
190 }
191