1 /*---------------------------------------------------------------------------*
2
3 Copyright 2010-2012 Nintendo. All rights reserved.
4
5 These coded instructions, statements, and computer programs contain
6 proprietary information of Nintendo of America Inc. and/or Nintendo
7 Company Ltd., and are protected by Federal copyright law. They may
8 not be disclosed to third parties or copied or duplicated in any form,
9 in whole or in part, without the prior written consent of Nintendo.
10
11 *---------------------------------------------------------------------------*/
12 ////===========================================================================
13 /// demoSystem.c
14 ///
15 /// This is system code for the demo library.
16 ///
17 ////===========================================================================
18 #include <cafe/demo.h>
19 #include <cafe/procui.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24
25 void (*DEMOReleaseCallback)(void);
26
27 #define MAX_BUFSIZ 512
28 extern unsigned int TCoreMEM2Limit(void);
TCoreMEM2Limit(void)29 extern unsigned int TCoreMEM2Limit(void)
30 {
31 // Limit TCore MEM2 Usage to: 12MB
32 // TOD0: Pick a less randmon TCore MEM limit
33 return 0x00C00000;
34 }
35
DEMOSaveCallback(void * unused)36 u32 DEMOSaveCallback(void* unused)
37 {
38 // Finish any saving before calling OSSavesDone_ReadyToRelease
39
40 OSSavesDone_ReadyToRelease();
41
42 return 0;
43 }
44
DEMOEmptyReleaseCallback(void)45 void DEMOEmptyReleaseCallback(void)
46 {
47 // If fading or something else is desired, this call can be postponed
48 ProcUIDrawDoneRelease();
49 }
50
DEMOSetReleaseCallback(DEMOReleaseCallbackFunc func)51 void DEMOSetReleaseCallback(DEMOReleaseCallbackFunc func)
52 {
53 DEMOReleaseCallback = func;
54 }
55
56 static BOOL gDemoRunningFlag;
57 static int sMainCore;
58
59 static DEMODefaultAllocateFunc gpAllocFunc = NULL;
60 static DEMODefaultFreeFunc gpFreeFunc = NULL;
61
DEMOInit()62 void DEMOInit()
63 {
64 OSReport("DEMO: Build date - %s %s\n", __DATE__, __TIME__);
65 gDemoRunningFlag = TRUE;
66 sMainCore = OSGetCoreId();
67
68 DEMOPadInit();
69
70 ProcUIInitEx(&DEMOSaveCallback, NULL);
71
72 DEMOReleaseCallback = &DEMOEmptyReleaseCallback;
73 }
74
DEMOSetMainCore(int core)75 void DEMOSetMainCore(int core)
76 {
77 sMainCore = core;
78 }
79
DEMOStopRunning()80 void DEMOStopRunning()
81 {
82 gDemoRunningFlag = FALSE;
83 }
84
DEMOShutdown()85 void DEMOShutdown()
86 {
87 DEMOStopRunning();
88 DEMOPadShutdown();
89
90 gpAllocFunc = NULL;
91 gpFreeFunc = NULL;
92
93 OSReport("\nEnd of demo\n");
94 }
95
DEMOIsRunning()96 BOOL DEMOIsRunning()
97 {
98 // Only do extra on the proper core
99 if (OSGetCoreId() != sMainCore)
100 {
101 ProcUISubProcessMessages(TRUE);
102 return gDemoRunningFlag;
103 }
104
105 // See if test action needs to be taken
106 DEMOTestCheck();
107
108 // See if any process switching or exiting is going on
109 ProcUIStatus status;
110
111 // Block if we're in the background
112 status = ProcUIProcessMessages(TRUE);
113
114 // Handle exiting or release
115 if (status == PROCUI_STATUS_EXIT)
116 DEMOStopRunning();
117 else if (status == PROCUI_STATUS_RELEASING)
118 DEMOReleaseCallback();
119
120 if (!gDemoRunningFlag)
121 {
122 // This function can only be called when in the foreground
123 // This must be called prior to freeing any shaders or other memory the GPU might be using
124 DEMOGfxDrawDone();
125
126 ProcUIShutdown();
127 }
128
129 return gDemoRunningFlag;
130 }
131
DEMOSetDefaultAllocator(DEMODefaultAllocateFunc pfnAlloc,DEMODefaultFreeFunc pfnFree)132 void DEMOSetDefaultAllocator(DEMODefaultAllocateFunc pfnAlloc, DEMODefaultFreeFunc pfnFree)
133 {
134 gpAllocFunc = pfnAlloc;
135 gpFreeFunc = pfnFree;
136 }
137
DEMOGetDefaultAllocator(DEMODefaultAllocateFunc * ppfnAlloc,DEMODefaultFreeFunc * ppfnFree)138 void DEMOGetDefaultAllocator(DEMODefaultAllocateFunc *ppfnAlloc, DEMODefaultFreeFunc *ppfnFree)
139 {
140 if (ppfnAlloc) *ppfnAlloc = gpAllocFunc;
141 if (ppfnFree) *ppfnFree = gpFreeFunc;
142 }
143
DEMOAlloc(u32 size)144 void* DEMOAlloc(u32 size)
145 {
146 if (!gpAllocFunc) {
147 return MEMAllocFromDefaultHeapEx(size, PPC_IO_BUFFER_ALIGN);
148 }
149 return gpAllocFunc(size, PPC_IO_BUFFER_ALIGN);
150 }
151
DEMOAllocEx(u32 size,u32 align)152 void* DEMOAllocEx(u32 size, u32 align)
153 {
154 if (!gpAllocFunc) {
155 return MEMAllocFromDefaultHeapEx(size, align);
156 }
157 return gpAllocFunc(size, align);
158 }
159
DEMOFree(void * ptr)160 void DEMOFree(void* ptr)
161 {
162 if (!gpFreeFunc) {
163 MEMFreeToDefaultHeap(ptr);
164 } else {
165 gpFreeFunc(ptr);
166 }
167 }
168
169 // ----------------------------------------------------------------------------
170 // Print
171 // ----------------------------------------------------------------------------
172
DEMOPrintf(const char * msg,...)173 void DEMOPrintf(const char* msg, ...)
174 {
175 va_list args;
176 va_start(args, msg);
177 vprintf(msg, args);
178 va_end(args);
179 }
180