1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WVR - demos - with_mb
3   File:     common.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.h>
19 
20 #include "common.h"
21 #include "disp.h"
22 
23 
24 static void VBlankIntr(void);
25 
26 /*
27  * Common features used by this entire demo
28  */
29 static u16 padPress;
30 static u16 padTrig;
31 
32 /*---------------------------------------------------------------------------*
33   Name:         ReadKey
34 
35   Description:  Processing for reading keys.
36 
37   Arguments:    None.
38 
39   Returns:      None.
40  *---------------------------------------------------------------------------*/
ReadKey(void)41 void ReadKey(void)
42 {
43     u16     currData = PAD_Read();
44 
45     padTrig = (u16)(~padPress & currData);
46     padPress = currData;
47 }
48 
49 /*---------------------------------------------------------------------------*
50   Name:         GetPressKey
51 
52   Description:  Gets pressed key.
53 
54   Arguments:    None.
55 
56   Returns:      Bitmap of the pressed key.
57  *---------------------------------------------------------------------------*/
GetPressKey(void)58 u16 GetPressKey(void)
59 {
60     return padPress;
61 }
62 
63 
64 /*---------------------------------------------------------------------------*
65   Name:         GetTrigKey
66 
67   Description:  Gets key trigger.
68 
69   Arguments:    None.
70 
71   Returns:      A bitmap of key triggers.
72  *---------------------------------------------------------------------------*/
GetTrigKey(void)73 u16 GetTrigKey(void)
74 {
75     return padTrig;
76 }
77 
78 
79 /*---------------------------------------------------------------------------*
80   Name:         CommonInit
81 
82   Description:  Common initialization functions.
83 
84   Arguments:    None.
85 
86   Returns:      None.
87  *---------------------------------------------------------------------------*/
CommonInit(void)88 void CommonInit(void)
89 {
90     /* OS initialization */
91     OS_Init();
92     OS_InitTick();
93     OS_InitAlarm();
94     FX_Init();
95 
96     /* GX initialization */
97     GX_Init();
98     GX_DispOff();
99     GXS_DispOff();
100 
101     /* V-Blank interrupt configuration */
102     (void)OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
103     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
104     (void)OS_EnableIrqMask(OS_IE_FIFO_RECV);
105     (void)GX_VBlankIntr(TRUE);
106 
107     {                                  /* FS initialization */
108         static u32 fs_tablework[0x100 / 4];
109         FS_Init(FS_DMA_NOT_USE);
110         (void)FS_LoadTable(fs_tablework, sizeof(fs_tablework));
111     }
112 
113     // Read the key once as empty
114     ReadKey();
115 }
116 
117 
118 /*---------------------------------------------------------------------------*
119   Name:         InitAllocateSystem
120 
121   Description:  Initializes the memory allocation system within the main memory arena.
122 
123   Arguments:    None.
124 
125   Returns:      None.
126  *---------------------------------------------------------------------------*/
InitAllocateSystem(void)127 void InitAllocateSystem(void)
128 {
129     void   *tempLo;
130     OSHeapHandle hh;
131 
132     // Based on the premise that OS_Init has been already called
133     tempLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
134     OS_SetArenaLo(OS_ARENA_MAIN, tempLo);
135     hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
136     if (hh < 0)
137     {
138         OS_Panic("ARM9: Fail to create heap...\n");
139     }
140     hh = OS_SetCurrentHeap(OS_ARENA_MAIN, hh);
141 }
142 
143 
144 
145 
146 /*---------------------------------------------------------------------------*
147   Name:         VBlankIntr
148 
149   Description:  Gets key trigger.
150 
151   Arguments:    None.
152 
153   Returns:      None.
154  *---------------------------------------------------------------------------*/
VBlankIntr(void)155 static void VBlankIntr(void)
156 {
157     DispVBlankFunc();
158 
159     //---- Interrupt check flag
160     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
161 }
162