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