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