1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - MB - demos - fake_child
3   File:     gmain.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 #ifdef SDK_TWL
19 #include	<twl.h>
20 #else
21 #include	<nitro.h>
22 #endif
23 
24 #include "gmain.h"
25 #include "common.h"
26 #include "disp.h"
27 #include "wh.h"
28 
29 
30 
31 //============================================================================
32 //  Variable Definitions
33 //============================================================================
34 
35 //-----------------------
36 // For data sharing
37 //-----------------------
38 static u8 sSendBuf[256] ATTRIBUTE_ALIGN(32);    // Send buffer (must be aligned to 32 bytes)
39 static u8 sRecvBuf[256] ATTRIBUTE_ALIGN(32);    // Receive buffer (must be aligned to 32 bytes)
40 static BOOL sRecvFlag[1 + WM_NUM_MAX_CHILD];    // Receive flag
41 static GShareData *sShareDataPtr;
42 
43 //-----------------------
44 // For key sharing
45 //-----------------------
46 static WMKeySet sKeySet;
47 
48 
49 //============================================================================
50 //  Function Definitions
51 //============================================================================
52 
53 /*---------------------------------------------------------------------------*
54   Name:         GInitDataShare
55 
56   Description:  Initializes settings for data-sharing buffer.
57 
58   Arguments:    None.
59 
60   Returns:      None.
61  *---------------------------------------------------------------------------*/
GInitDataShare(void)62 void GInitDataShare(void)
63 {
64     sShareDataPtr = (GShareData *) sSendBuf;
65 
66     DC_FlushRange(sSendBuf, 4);
67     DC_WaitWriteBufferEmpty();
68 }
69 
70 
71 /*---------------------------------------------------------------------------*
72   Name:         GStepDataShare
73 
74   Description:  Proceeds to the next synchronized data-sharing process.
75 
76   Arguments:    None.
77 
78   Returns:      None.
79  *---------------------------------------------------------------------------*/
GStepDataShare(s32 frame)80 void GStepDataShare(s32 frame)
81 {
82     sShareDataPtr->count = (u32)frame;
83     sShareDataPtr->key = GetPressKey();
84 
85     switch (WH_GetSystemState())
86     {
87         //------------------------------
88         // Data-sharing communication
89     case WH_SYSSTATE_DATASHARING:
90         {
91             u16     i;
92             u8     *adr;
93 
94             if (!WH_StepDS(sSendBuf))
95             {
96                 // Data-sharing communication failure
97                 return;
98             }
99 
100             for (i = 0; i < 16; ++i)
101             {
102                 adr = (u8 *)WH_GetSharedDataAdr(i);
103                 if (adr != NULL)
104                 {
105                     MI_CpuCopy8(adr, &(sRecvBuf[i * sizeof(GShareData)]), sizeof(GShareData));
106                     sRecvFlag[i] = TRUE;
107                 }
108                 else
109                 {
110                     sRecvFlag[i] = FALSE;
111                 }
112             }
113         }
114         break;
115         //------------------------------
116         // Key-sharing communications
117     case WH_SYSSTATE_KEYSHARING:
118         {
119             (void)WH_GetKeySet(&sKeySet);
120             if ((sKeySet.key[0] != 0) || (sKeySet.key[1] != 0))
121             {
122                 OS_TPrintf("0 -> %04x 1 -> %04x\n", sKeySet.key[0], sKeySet.key[1]);
123             }
124         }
125     }
126 }
127 
128 
129 
130 
131 
132 
133 /*---------------------------------------------------------------------------*
134   Name:         GMain
135 
136   Description:  Parent-child common main routine.
137 
138   Arguments:    None.
139 
140   Returns:      None.
141  *---------------------------------------------------------------------------*/
GMain(void)142 void GMain(void)
143 {
144 
145     BgPrintStr(4, 3, 0x4, "Send:     %04x-%04x", sShareDataPtr->key, sShareDataPtr->count);
146     BgPutString(4, 5, 0x4, "Receive:");
147     {
148         s32     i;
149 
150         for (i = 0; i < (WM_NUM_MAX_CHILD + 1); i++)
151         {
152             if (sRecvFlag[i])
153             {
154                 GShareData *sd;
155                 sd = (GShareData *) (&(sRecvBuf[i * sizeof(GShareData)]));
156 
157                 BgPrintStr(4, (s16)(6 + i), 0x4,
158                            "Player%02d: %04x-%04x", i, sd->key, sd->count & 0xffff);
159             }
160             else
161             {
162                 BgPutString(4, (s16)(6 + i), 0x7, "No child");
163             }
164         }
165     }
166 
167     if (IS_PAD_TRIGGER(PAD_BUTTON_START))
168     {
169         //********************************
170         (void)WH_Finalize();
171         //********************************
172     }
173 }
174