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