1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - OS
3   File:     os_attention.c
4 
5   Copyright 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 #include <os_attention.h>
19 #include <nitro/os/common/interrupt.h>
20 
21 #ifdef SDK_TWLLTD
22 
23 
24 /*---------------------------------------------------------------------------*
25  * Function Declarations
26  *---------------------------------------------------------------------------*/
27 static u8*  OSi_LoadImage(SPEC_DEST dest, IMAGE_OBJ_INDEX index, u32 *p_size);
28 void OSi_WaitVBlank(void);
29 void OSi_VBlankIntr(void);
30 void OSi_PrepareAttention(void);
31 void OSi_ShowAttention(void);
32 
33 
34 
35 /*---------------------------------------------------------------------------*
36   Name:         OSi_LoadImage
37 
38   Description:  Loads an image file (provisional). Note: This function actually just returns a pointer.
39 
40   Arguments:    dest: A particular market
41                 index: Index to the file to load
42                 p_size: Pointer to u32 that stores the file size
43                             If not needed, specify NULL, and it will be ignored.
44 
45   Returns:      Returns the starting address of the image data.
46  *---------------------------------------------------------------------------*/
OSi_LoadImage(SPEC_DEST dest,IMAGE_OBJ_INDEX index,u32 * p_size)47 static u8 *OSi_LoadImage(SPEC_DEST dest, IMAGE_OBJ_INDEX index, u32 *p_size)
48 {
49     extern u8   _binary_attention_limited_01nbfc_bin[];
50     extern u8   _binary_attention_limited_01nbfc_bin_end[];
51     extern u8   _binary_attention_limited_01nbfs_bin[];
52     extern u8   _binary_attention_limited_01nbfs_bin_end[];
53     extern u8   _binary_attention_limited_02nbfc_bin[];
54     extern u8   _binary_attention_limited_02nbfc_bin_end[];
55     extern u8   _binary_attention_limited_02nbfs_bin[];
56     extern u8   _binary_attention_limited_02nbfs_bin_end[];
57     extern u8   _binary_attention_limited_korea_01nbfc_bin[];
58     extern u8   _binary_attention_limited_korea_01nbfc_bin_end[];
59     extern u8   _binary_attention_limited_korea_01nbfs_bin[];
60     extern u8   _binary_attention_limited_korea_01nbfs_bin_end[];
61     extern u8   _binary_attention_limited_korea_02nbfc_bin[];
62     extern u8   _binary_attention_limited_korea_02nbfc_bin_end[];
63     extern u8   _binary_attention_limited_korea_02nbfs_bin[];
64     extern u8   _binary_attention_limited_korea_02nbfs_bin_end[];
65     extern u8   _binary_attention_limited_nbfp_bin[];
66     extern u8   _binary_attention_limited_nbfp_bin_end[];
67 
68     static u8 *ptr_table[SPEC_DEST_NUM][IMAGE_OBJ_NUM] =
69     {
70         {
71             _binary_attention_limited_01nbfc_bin,
72             _binary_attention_limited_01nbfs_bin,
73             _binary_attention_limited_02nbfc_bin,
74             _binary_attention_limited_02nbfs_bin,
75             _binary_attention_limited_nbfp_bin
76         },
77         {
78             _binary_attention_limited_korea_01nbfc_bin,
79             _binary_attention_limited_korea_01nbfs_bin,
80             _binary_attention_limited_korea_02nbfc_bin,
81             _binary_attention_limited_korea_02nbfs_bin,
82             _binary_attention_limited_nbfp_bin
83         }
84     };
85 
86     static u8 *ptr_end_table[SPEC_DEST_NUM][IMAGE_OBJ_NUM] =
87     {
88         {
89             _binary_attention_limited_01nbfc_bin_end,
90             _binary_attention_limited_01nbfs_bin_end,
91             _binary_attention_limited_02nbfc_bin_end,
92             _binary_attention_limited_02nbfs_bin_end,
93             _binary_attention_limited_nbfp_bin_end
94         },
95         {
96             _binary_attention_limited_korea_01nbfc_bin_end,
97             _binary_attention_limited_korea_01nbfs_bin_end,
98             _binary_attention_limited_korea_02nbfc_bin_end,
99             _binary_attention_limited_korea_02nbfs_bin_end,
100             _binary_attention_limited_nbfp_bin_end
101         }
102     };
103 
104     if(p_size)
105     {
106         *p_size = (u32)(ptr_end_table[dest][index] - ptr_table[dest][index]);
107     }
108 
109     return (u8 *)ptr_table[dest][index];
110 }
111 
112 /*---------------------------------------------------------------------------*
113   Name:         OSi_WaitVBlank
114 
115   Description:  Waits for a V-Blank interrupt.
116 
117   Arguments:    None.
118 
119   Returns:      None.
120  *---------------------------------------------------------------------------*/
OSi_WaitVBlank(void)121 void OSi_WaitVBlank(void)
122 {
123 #if 0
124     OS_WaitIrq(TRUE, OS_IE_V_BLANK);
125 #else
126     // Loop and wait because threads have not been initialized
127     while(1)
128     {
129         if(OS_GetIrqCheckFlag() & OS_IE_V_BLANK) break;
130     }
131     OS_ClearIrqCheckFlag(OS_IE_V_BLANK);
132 #endif
133 }
134 
135 /*---------------------------------------------------------------------------*
136   Name:         OSi_VBlankIntr
137 
138   Description:  V-Blank interrupt vector.
139 
140   Arguments:    None.
141 
142   Returns:      None.
143  *---------------------------------------------------------------------------*/
OSi_VBlankIntr(void)144 void OSi_VBlankIntr(void)
145 {
146     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
147 }
148 
149 /*---------------------------------------------------------------------------*
150   Name:         OS_ShowAttentionOfLimitedRom
151 
152   Description:  Display notice for running in limited mode in NITRO.
153 
154   Arguments:    None.
155 
156   Returns:      None.
157  *---------------------------------------------------------------------------*/
OS_ShowAttentionOfLimitedRom(void)158 SDK_WEAK_SYMBOL void OS_ShowAttentionOfLimitedRom(void)
159 {
160     /* Preparation */
161     OSi_PrepareAttention();
162 
163     /* Load image data */
164     {
165         SPEC_DEST   dest = (*(u8*)(HW_ROM_HEADER_BUF + 0x1d) & 0x40) ? SPEC_DEST_KOREA : SPEC_DEST_NONE;
166 
167         u32     plt_size;
168         void    const *data_plt     = OSi_LoadImage(dest, IMAGE_OBJ_PAL, &plt_size);
169         void    const *data_01_chr  = OSi_LoadImage(dest, IMAGE_OBJ_01_CHR, NULL);
170         void    const *data_01_scr  = OSi_LoadImage(dest, IMAGE_OBJ_01_SCR, NULL);
171         void    const *data_02_chr  = OSi_LoadImage(dest, IMAGE_OBJ_02_CHR, NULL);
172         void    const *data_02_scr  = OSi_LoadImage(dest, IMAGE_OBJ_02_SCR, NULL);
173 
174         /* Load data for the upper screen into VRAM-A */
175         MI_UncompressLZ16(data_01_chr, (u32 *)HW_BG_VRAM);
176         MI_UncompressLZ16(data_01_scr, (u32 *)(HW_BG_VRAM + 0xf000));
177 
178         /* Load data for the lower screen into VRAM-C */
179         MI_UncompressLZ16(data_02_chr, (u32 *)HW_DB_BG_VRAM);
180         MI_UncompressLZ16(data_02_scr, (u32 *)(HW_DB_BG_VRAM + 0xf000));
181 
182         /* Load palette data into standard palette ROM */
183         SVC_CpuCopyFast(data_plt, (u32 *)(HW_BG_PLTT),    plt_size);
184         SVC_CpuCopyFast(data_plt, (u32 *)(HW_DB_BG_PLTT), plt_size);
185     }
186 
187     /* Display (Note: This will loop) */
188     OSi_ShowAttention();
189 }
190 
191 /*---------------------------------------------------------------------------*
192   Name:         OSi_PrepareAttention
193 
194   Description:  Prepares to display the warning screen.
195 
196   Arguments:
197 
198   Returns:
199  *---------------------------------------------------------------------------*/
OSi_PrepareAttention(void)200 void OSi_PrepareAttention(void)
201 {
202     u16 gx_powcnt = reg_GX_POWCNT;
203 
204     /* Stop display */
205     reg_GX_DISPCNT      = 0;
206     reg_GXS_DB_DISPCNT  = 0;
207 
208     /* Initialize power control */
209     if(!(gx_powcnt & REG_GX_POWCNT_LCD_MASK))
210     {
211         /* When changing LCD enable from OFF to ON, wait 100 ms */
212         SVC_WaitByLoop(HW_CPU_CLOCK_ARM9 / 40);
213     }
214 
215     reg_GX_POWCNT = (u16)(REG_GX_POWCNT_DSEL_MASK | REG_GX_POWCNT_E2DG_MASK |
216                             REG_GX_POWCNT_E2DGB_MASK | REG_GX_POWCNT_LCD_MASK);
217 
218     /* Initialization of master brightness */
219     reg_GX_MASTER_BRIGHT        = (u16)(1 << REG_GX_MASTER_BRIGHT_E_MOD_SHIFT);
220     reg_GXS_DB_MASTER_BRIGHT    = reg_GX_MASTER_BRIGHT;
221 
222     /* Prepare to display the upper screen */
223     reg_GX_VRAMCNT_A = (u8)((1 << REG_GX_VRAMCNT_A_MST_SHIFT) | (1 << REG_GX_VRAMCNT_A_E_SHIFT));
224     reg_G2_BG0CNT    = (u16)((GX_BG_SCRSIZE_TEXT_256x256 << REG_G2_BG0CNT_SCREENSIZE_SHIFT) |
225                              (GX_BG_COLORMODE_16 << REG_G2_BG0CNT_COLORMODE_SHIFT) |
226                              (GX_BG_SCRBASE_0xf000 << REG_G2_BG0CNT_SCREENBASE_SHIFT) |
227                              (GX_BG_CHARBASE_0x00000 << REG_G2_BG0CNT_CHARBASE_SHIFT) |
228                              (0 << REG_G2_BG0CNT_PRIORITY_SHIFT));
229     reg_G2_BG0OFS    = 0;
230     reg_GX_DISPCNT  |= ((GX_BGMODE_0 << REG_GX_DISPCNT_BGMODE_SHIFT) |
231                         (GX_PLANEMASK_BG0 << REG_GX_DISPCNT_DISPLAY_SHIFT));
232 
233     /* Prepare to display the lower screen */
234     reg_GX_VRAMCNT_C    = (u8)((4 << REG_GX_VRAMCNT_C_MST_SHIFT) | (1 << REG_GX_VRAMCNT_C_E_SHIFT));
235     reg_G2S_DB_BG0CNT   = (u16)((GX_BG_SCRSIZE_TEXT_256x256 << REG_G2S_DB_BG0CNT_SCREENSIZE_SHIFT) |
236                                 (GX_BG_COLORMODE_16 << REG_G2S_DB_BG0CNT_COLORMODE_SHIFT) |
237                                 (GX_BG_SCRBASE_0xf000 << REG_G2S_DB_BG0CNT_SCREENBASE_SHIFT) |
238                                 (GX_BG_CHARBASE_0x00000 << REG_G2S_DB_BG0CNT_CHARBASE_SHIFT) |
239                                 (0 << REG_G2S_DB_BG0CNT_PRIORITY_SHIFT));
240     reg_G2S_DB_BG0OFS   = 0;
241     reg_GXS_DB_DISPCNT |= ((GX_BGMODE_0 << REG_GXS_DB_DISPCNT_BGMODE_SHIFT) |
242                           ((GX_PLANEMASK_BG0 | GX_PLANEMASK_OBJ) << REG_GXS_DB_DISPCNT_DISPLAY_SHIFT));
243 }
244 
245 /*---------------------------------------------------------------------------*
246   Name:         OSi_ShowAttention
247 
248   Description:  Displays the warning screen.
249 
250   Arguments:
251 
252   Returns:
253  *---------------------------------------------------------------------------*/
OSi_ShowAttention(void)254 void OSi_ShowAttention(void)
255 {
256 
257     /* Start display */
258     reg_GX_DISPCNT      |= (u32)(GX_DISPMODE_GRAPHICS << REG_GX_DISPCNT_MODE_SHIFT);
259     reg_GXS_DB_DISPCNT  |= (u32)(REG_GXS_DB_DISPCNT_MODE_MASK);
260 
261     /* Interrupt settings */
262     reg_GX_DISPSTAT     |= REG_GX_DISPSTAT_VBI_MASK;
263     OS_SetIrqFunction(OS_IE_V_BLANK, OSi_VBlankIntr);
264 
265     /* Loop */
266     while(1)
267     {
268         OSi_WaitVBlank();
269     }
270 }
271 
272 
273 /*---------------------------------------------------------------------------*
274   Name:         OS_IsLimitedRomRunningOnTwl
275 
276   Description:  Check running platform (only for NITRO-TWL limited mode).
277 
278   Arguments:    None.
279 
280   Returns:      TRUE: running on TWL.
281                 FALSE: running on NITRO.
282  *---------------------------------------------------------------------------*/
OS_IsLimitedRomRunningOnTwl(void)283 BOOL OS_IsLimitedRomRunningOnTwl(void)
284 {
285 #ifdef SDK_ARM9
286     u8 rom9 = reg_SCFG_A9ROM;
287 #else // SDK_ARM7
288     u8 rom9 = (u8)(*(u8*)(HW_PRV_WRAM_SYSRV + HWi_WSYS08_WRAMOFFSET) >> HWi_WSYS08_ROM_ARM9SEC_SHIFT);
289 #endif
290 
291     return (rom9 & (REG_SCFG_A9ROM_SEC_MASK | REG_SCFG_A9ROM_RSEL_MASK)) == REG_SCFG_A9ROM_SEC_MASK;
292 }
293 
294 
295 #endif  // #ifdef SDK_TWLLTD
296 /*---------------------------------------------------------------------------*
297     End of file
298  *---------------------------------------------------------------------------*/
299