1 /*---------------------------------------------------------------------------*
2 Project: Revolution SDK Extension
3 File: demokpad.c
4
5 Copyright 2006 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 $Log: demokpad.c,v $
14 Revision 1.11 2008/06/02 02:24:01 seiki_masashi
15 Added support for KPADRead specification changes.
16
17 Revision 1.10 2008/05/30 02:27:03 seiki_masashi
18 Reverted code consequent upon reverted SDK code.
19
20 Revision 1.8 2007/10/23 13:42:06 seiki_masashi
21 Added REXDEMOGetAnyKPadHold.
22
23 Revision 1.7 2007/06/16 06:31:07 hirose_kazuki
24 Handled warnings.
25
26 Revision 1.6 2007/06/16 06:17:36 hirose_kazuki
27 Added GC Pad-related features.
28
29 Revision 1.5 2006/09/05 10:56:12 yosizaki
30 Added REXDEMOGetAnyKPadTrigger.
31
32 Revision 1.4 2006/08/29 07:19:20 adachi_hiroaki
33 Changed prefix. Cleaned up code elsewhere.
34
35 Revision 1.3 2006/08/11 10:41:15 yasu
36 Ignored the KPADRead return value.
37
38 Revision 1.2 2006/08/10 12:09:47 adachi_hiroaki
39 Changed the header file position.
40
41 Revision 1.1 2006/08/09 13:28:49 yasu
42 Added library for using KPAD.
43
44 $NoKeywords: $
45 *---------------------------------------------------------------------------*/
46 #include <revolution.h>
47 #include <revolution/kpad.h>
48 #include <revolution/mem.h>
49 #include "rexdemo/demokpad.h"
50
51 #define SAMPLE_PADHEAP_SIZE ( 128 * 1024 )
52
53 static MEMHeapHandle samplePadHeap;
54 static void* AllocFromPadHeap( u32 size );
55 static u8 FreeToPadHeap( void* ptr );
56
57 static u16 PadButtonPrev[PAD_MAX_CONTROLLERS];
58
59 KPADStatus REXDEMOKPadStatus[WPAD_MAX_CONTROLLERS];
60 PADStatus REXDEMOPadStatus[PAD_MAX_CONTROLLERS];
61
62
63 /*---------------------------------------------------------------------------*
64 Name: REXDEMOKPadInit
65
66 Description: Initialize PAD library and exported status
67
68 Arguments: None
69
70 Returns: None
71 *---------------------------------------------------------------------------*/
REXDEMOKPadInit(void)72 void REXDEMOKPadInit(void)
73 {
74 void* heapAddress;
75 int channel;
76
77 /* Initialize heap for WPAD sampling */
78 heapAddress = OSGetMEM2ArenaLo();
79 OSSetMEM2ArenaLo( (void*)OSRoundUp32B( (u32)heapAddress + SAMPLE_PADHEAP_SIZE ) );
80 samplePadHeap = MEMCreateExpHeap( heapAddress, SAMPLE_PADHEAP_SIZE );
81
82 if( samplePadHeap == NULL )
83 {
84 OSHalt( "Could not create heap.\n" );
85 }
86 WPADRegisterAllocator( AllocFromPadHeap, FreeToPadHeap );
87
88 KPADInit();
89 while( KPADRead( WPAD_CHAN0, &REXDEMOKPadStatus[0], 1 ) > 0 ) {}
90 while( KPADRead( WPAD_CHAN1, &REXDEMOKPadStatus[1], 1 ) > 0 ) {}
91 while( KPADRead( WPAD_CHAN2, &REXDEMOKPadStatus[2], 1 ) > 0 ) {}
92 while( KPADRead( WPAD_CHAN3, &REXDEMOKPadStatus[3], 1 ) > 0 ) {}
93
94 /* Initialize GameCube pads */
95 (void)PADInit();
96 for ( channel = 0 ; channel < PAD_MAX_CONTROLLERS ; ++channel )
97 {
98 PadButtonPrev[channel] = 0;
99 }
100 }
101
102 /*---------------------------------------------------------------------------*
103 Name: REXDEMOKPadRead
104
105 Description: Calls KPADRead().
106
107 Arguments: None
108
109 Returns: None
110 *---------------------------------------------------------------------------*/
REXDEMOKPadRead(void)111 void REXDEMOKPadRead( void )
112 {
113 int channel;
114
115 (void)KPADRead( WPAD_CHAN0, &REXDEMOKPadStatus[0], 1 );
116 (void)KPADRead( WPAD_CHAN1, &REXDEMOKPadStatus[1], 1 );
117 (void)KPADRead( WPAD_CHAN2, &REXDEMOKPadStatus[2], 1 );
118 (void)KPADRead( WPAD_CHAN3, &REXDEMOKPadStatus[3], 1 );
119
120 for ( channel = 0 ; channel < PAD_MAX_CONTROLLERS ; ++channel )
121 {
122 PadButtonPrev[channel] = REXDEMOPadStatus[channel].button;
123 }
124 (void)PADRead( REXDEMOPadStatus );
125 PADClamp( REXDEMOPadStatus );
126 }
127
128 /*---------------------------------------------------------------------------*
129 Name : REXDEMOGetAnyKPadTrigger
130 Description : Gets the logical sum of the trigger inputs from all pad channels.
131 Arguments : None.
132 Returns : Trigger input logical sum
133 *---------------------------------------------------------------------------*/
134 u32
REXDEMOGetAnyKPadTrigger(void)135 REXDEMOGetAnyKPadTrigger( void )
136 {
137 u32 retval = 0;
138 int channel;
139 for (channel = 0; channel < WPAD_MAX_CONTROLLERS; ++channel)
140 {
141 retval |= REXDEMOKPadStatus[channel].trig;
142 }
143
144 return retval;
145 }
146
147 /*---------------------------------------------------------------------------*
148 Name : REXDEMOGetAnyMixedPadTrigger
149 Description : Gets the logical sum of the trigger inputs from all pad channels.
150 (including the GameCube controller port)
151 Arguments : None.
152 Returns : Trigger input logical sum
153 *---------------------------------------------------------------------------*/
154 u32
REXDEMOGetAnyMixedPadTrigger(void)155 REXDEMOGetAnyMixedPadTrigger( void )
156 {
157 u32 retval = 0;
158 int channel;
159 for (channel = 0; channel < WPAD_MAX_CONTROLLERS; ++channel)
160 {
161 retval |= REXDEMOKPadStatus[channel].trig;
162 }
163
164 for (channel = 0; channel < PAD_MAX_CONTROLLERS; ++channel)
165 {
166 u16 padDown;
167 padDown = PADButtonDown(PadButtonPrev[channel],
168 REXDEMOPadStatus[channel].button);
169 retval |= ((u32)padDown << 16);
170 }
171
172 return retval;
173 }
174
175 /*---------------------------------------------------------------------------*
176 Name : REXDEMOGetAnyKPadHold
177 Description : Gets the logical sum of the inputs from all pad channels.
178 Arguments : None.
179 Returns : Trigger input logical sum
180 *---------------------------------------------------------------------------*/
181 u32
REXDEMOGetAnyKPadHold(void)182 REXDEMOGetAnyKPadHold( void )
183 {
184 u32 retval = 0;
185 int channel;
186 for (channel = 0; channel < WPAD_MAX_CONTROLLERS; ++channel)
187 {
188 retval |= REXDEMOKPadStatus[channel].hold;
189 }
190
191 return retval;
192 }
193
194 /*---------------------------------------------------------------------------*
195 Name : REXDEMOGetAnyMixedPadHold
196 Description : Gets the logical sum of the inputs from all pad channels.
197 (including the GameCube controller port)
198 Arguments : None.
199 Returns : Trigger input logical sum
200 *---------------------------------------------------------------------------*/
201 u32
REXDEMOGetAnyMixedPadHold(void)202 REXDEMOGetAnyMixedPadHold( void )
203 {
204 u32 retval = 0;
205 int channel;
206 for (channel = 0; channel < WPAD_MAX_CONTROLLERS; ++channel)
207 {
208 retval |= REXDEMOKPadStatus[channel].hold;
209 }
210
211 for (channel = 0; channel < PAD_MAX_CONTROLLERS; ++channel)
212 {
213 u16 padDown;
214 padDown = REXDEMOPadStatus[channel].button;
215 retval |= ((u32)padDown << 16);
216 }
217
218 return retval;
219 }
220
221 /*---------------------------------------------------------------------------*
222 Name : AllocFromPadHeap
223 Description : Dynamically allocates memory for the WPAD library.
224 Arguments : size - Specifies the size of memory to be allocated.
225 Returns : void* - Returns the starting address of the allocated memory.
226 *---------------------------------------------------------------------------*/
227 static void*
AllocFromPadHeap(u32 size)228 AllocFromPadHeap( u32 size )
229 {
230 return MEMAllocFromExpHeap( samplePadHeap, size );
231 }
232
233 /*---------------------------------------------------------------------------*
234 Name : FreeToPadHeap
235 Description : Deallocates memory dynamically allocated for the WPAD library.
236 Arguments : ptr - Specifies the start address of the memory to be deallocated.
237 Returns : u8 - Returns 0 if attempt to deallocate memory fails.
238 *---------------------------------------------------------------------------*/
239 static u8
FreeToPadHeap(void * ptr)240 FreeToPadHeap( void* ptr )
241 {
242 if( ptr == NULL )
243 {
244 return 0;
245 }
246 MEMFreeToExpHeap( samplePadHeap, ptr );
247 return 1;
248 }
249