1 /*---------------------------------------------------------------------------*
2
3 Copyright (C) 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.h
14 //
15 // -----------------------------------------------------------------------------
16
17 #ifndef __DEMO_SYSTEM_H__
18 #define __DEMO_SYSTEM_H__
19
20 #define DEMO_INLINE static inline
21
22 #define DEMOAssert(expr) ASSERT(expr)
23
24 #define DEMORoundUp256B(x) (((u32)(x) + 256 - 1) & ~(256 - 1))
25 #define DEMORoundUp32B(x) (((u32)(x) + 32 - 1) & ~(32 - 1))
26 #define DEMORoundUp4B(x) (((u32)(x) + 4 - 1) & ~(4 - 1))
27
28 #define DEMORoundUpForIO(x) (((u32)(x) + PPC_IO_BUFFER_ALIGN - 1) & ~(PPC_IO_BUFFER_ALIGN - 1))
29
30 #define DEMO_RAND_MAX ((u32)0xFFFF)
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /// @addtogroup demoSystem
37 /// @{
38
39 /// \brief Initialize System
40 ///
41 /// The DEMO library provides a common application framework
42 /// that is used in many of the example demos distributed with SDK.
43 /// The source code for the DEMO library is also distributed with the SDK.
44 ///
45 /// This function initializes various system components including:
46 /// - OS: Base operation system
47 /// - Memory: All of the main memory is then allocated into a
48 /// heap that can be managed with \ref DEMOAlloc
49 void DEMOInit(void);
50
51 /// \brief Have \ref DEMOIsRunning() return false the next time it is called
52 ///
53 /// This allows a controlled shutdown by letting the current loop finish
54 void DEMOStopRunning(void);
55
56 /// \brief Shutdown System
57 ///
58 /// Shuts down all system components initialized by \ref DEMOInit
59 void DEMOShutdown(void);
60
61 /// \brief Set the main core
62 ///
63 /// Sets the main core for DEMOIsRunning
64 void DEMOSetMainCore(int core);
65
66 /// \brief Set the random seed
67 ///
68 /// Seeds the random number generator.
69 void DEMOSRand(u32 seed);
70
71 /// \brief Random float number generator
72 ///
73 /// Returns a random number: 0 <= n <= 1.
74 /// \retval The next random number.
75 f32 DEMOFRand(void);
76
77 /// \brief Random integer number generator
78 ///
79 /// Returns a random number: 0 <= n <= DEMO_RAND_MAX.
80 /// \retval The next random number.
81 u32 DEMORand(void);
82
83 /// \brief Type used for default DEMO memory allocator
84 typedef void* (*DEMODefaultAllocateFunc)(u32 byteCount, int alignment);
85
86 /// \brief Type used for default DEMO memory free function
87 typedef void (*DEMODefaultFreeFunc)(void* pMem);
88
89 /// \brief Set default functions to use for memory allocation/freeing.
90 ///
91 /// These will be the functions called by DEMOAlloc/DEMOAllocEx/DEMOFree.
92 /// Those entry points are used by the DEMO libs when they need to allocate memory.
93 /// (Except for when non-regular-MEM2 arenas are needed.)
94 ///
95 /// If not set by the user, these will just call MEMAllocFromDefaultHeap/MEMFreeToDefaultHeap.
96 ///
97 /// \param pfnAlloc pointer to allocator function
98 /// \param pfnFree pointer to free function
99 void DEMOSetDefaultAllocator(DEMODefaultAllocateFunc pfnAlloc, DEMODefaultFreeFunc pfnFree);
100
101 /// \brief Get default functions to use for memory allocation/freeing.
102 /// \param ppfnAlloc pointer to get pointer to allocator function
103 /// \param ppfnFree pointer to get pointer to free function
104 void DEMOGetDefaultAllocator(DEMODefaultAllocateFunc *ppfnAlloc, DEMODefaultFreeFunc *ppfnFree);
105
106 /// \brief Allocate memory
107 ///
108 /// \param size Size to allocate
109 /// \retval Pointer to the allocated buffer if allocation succeeded
110 void* DEMOAlloc(u32 size);
111
112 /// \brief Allocate memory with specific alignment
113 ///
114 /// \param size Size to allocate
115 /// \param align Alignment to use for allocation
116 /// \retval Pointer to the allocated buffer if allocation succeeded
117 void* DEMOAllocEx(u32 size, u32 align);
118
119 /// \brief Free memory
120 ///
121 /// \param ptr Pointer to the buffer to be deallocated
122 void DEMOFree(void* ptr);
123
124 /// \brief Get demo running state
125 ///
126 /// \note This function also calls the DEMO Test functions, and therefore
127 /// it is expected that this function is only called once prior to each
128 /// main loop iteration.
129 ///
130 /// \retval TRUE if \ref DEMOInit() has been called and DEMOStopRunning() has not been called; false otherwise.
131 BOOL DEMOIsRunning(void);
132
133 typedef void (*DEMOReleaseCallbackFunc)(void);
134
135 /// \brief Sets the callback for when a release occurs
136 ///
137 /// \param func Function to call upon release message
138 void DEMOSetReleaseCallback(DEMOReleaseCallbackFunc func);
139
140 /// \brief Get current time
141 ///
142 /// \retval The value of time in seconds since 1970-01-01 00:00:00
143 DEMO_INLINE
DEMOGetTime(void)144 s64 DEMOGetTime(void)
145 {
146 s64 diffSinceEpoch = 946627200;
147 return OSTicksToSeconds(OSGetSystemTime()) + diffSinceEpoch;
148 }
149
150 /// \brief Prints formatted message to debug output
151 ///
152 /// \param msg Pointer to a null-terminated string including format specification
153 /// (equivalent to C's standard output function).
154 /// \param ... Optional argument
155 void DEMOPrintf (const char* msg, ...);
156
157 /// \brief Perform a "fast" CPU copy
158 ///
159 /// This copy makes no assumptions about source & dest alignment.
160 /// Both source & dest buffers are flushed out of the CPU cache.
161 /// The buffers must not overlap.
162 ///
163 /// \note This function does not use locked-cache DMA or any non-CPU hardware.
164 /// It is therefore not the fastest way to copy, but it is usually faster than
165 /// just calling memcpy. Better copy functions may become available in the future.
166 ///
167 void DEMOFastCopy(void *dst, void *src, u32 size);
168
169 /// @}
170
171 #ifdef __cplusplus
172 }
173 #endif
174
175 #ifdef __cplusplus
176 #include <xmemory>
177
178 template<class T>
179 struct DEMOAllocator : public std::allocator<T>
180 {
181 template<class _Other>
182 struct rebind
183 { // convert an allocator<void> to an allocator <_Other>
184 typedef DEMOAllocator<_Other> other;
185 };
186
DEMOAllocatorDEMOAllocator187 DEMOAllocator() {}
188
DEMOAllocatorDEMOAllocator189 DEMOAllocator(const DEMOAllocator<T>&) {}
190
191 template<class _Other>
DEMOAllocatorDEMOAllocator192 DEMOAllocator(const DEMOAllocator<_Other>&) {}
193
194 template<class _Other>
195 DEMOAllocator<T>& operator=(const DEMOAllocator<_Other>&) { return (*this); }
196
allocateDEMOAllocator197 T* allocate(std::size_t n)
198 {
199 return (T*)DEMOAlloc(n * sizeof(T));
200 }
201
deallocateDEMOAllocator202 void deallocate(T* p, std::size_t n)
203 {
204 DEMOFree(p);
205 }
206 };
207 #endif
208
209 #endif /// __DEMO_SYSTEM_H__
210