1 /*---------------------------------------------------------------------------*
2 
3   Copyright (C) 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.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 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /// @addtogroup demoSystem
35 /// @{
36 
37 /// \brief Initialize System
38 ///
39 /// The DEMO library provides a common application framework
40 /// that is used in many of the example demos distributed with SDK.
41 /// The source code for the DEMO library is also distributed with the SDK.
42 ///
43 /// This function initializes various system components including:
44 /// - OS: Base operation system
45 /// - Memory: All of the main memory is then allocated into a
46 ///   heap that can be managed with \ref DEMOAlloc
47 void DEMOInit(void);
48 
49 /// \brief Have \ref DEMOIsRunning() return false the next time it is called
50 ///
51 /// This allows a controlled shutdown by letting the current loop finish
52 void DEMOStopRunning(void);
53 
54 /// \brief Shutdown System
55 ///
56 /// Shuts down all system components initialized by \ref DEMOInit
57 void DEMOShutdown(void);
58 
59 /// \brief Set the main core
60 ///
61 /// Sets the main core for DEMOIsRunning
62 void DEMOSetMainCore(int core);
63 
64 /// \brief Type used for default DEMO memory allocator
65 typedef void* (*DEMODefaultAllocateFunc)(u32 byteCount, int alignment);
66 
67 /// \brief Type used for default DEMO memory free function
68 typedef void (*DEMODefaultFreeFunc)(void* pMem);
69 
70 /// \brief Set default functions to use for memory allocation/freeing.
71 ///
72 /// These will be the functions called by DEMOAlloc/DEMOAllocEx/DEMOFree.
73 /// Those entry points are used by the DEMO libs when they allocate memory.
74 /// (Except for when non-regular-MEM2 arenas are needed.)
75 ///
76 /// If not set by the user, these will just call MEMAllocFromDefaultHeap/MEMFreeToDefaultHeap.
77 ///
78 /// \param pfnAlloc pointer to allocator function
79 /// \param pfnFree  pointer to free function
80 void DEMOSetDefaultAllocator(DEMODefaultAllocateFunc pfnAlloc, DEMODefaultFreeFunc pfnFree);
81 
82 /// \brief Get default functions to use for memory allocation/freeing.
83 /// \param ppfnAlloc pointer to get pointer to allocator function
84 /// \param ppfnFree  pointer to get pointer to free function
85 void DEMOGetDefaultAllocator(DEMODefaultAllocateFunc *ppfnAlloc, DEMODefaultFreeFunc *ppfnFree);
86 
87 /// \brief Allocate memory
88 ///
89 /// \param size Size to allocate
90 /// \retval Pointer to the allocated buffer if allocation succeeded
91 void* DEMOAlloc(u32 size);
92 
93 /// \brief Allocate memory with specific alignment
94 ///
95 /// \param size Size to allocate
96 /// \param align Alignment to use for allocation
97 /// \retval Pointer to the allocated buffer if allocation succeeded
98 void* DEMOAllocEx(u32 size, u32 align);
99 
100 /// \brief Free memory
101 ///
102 /// \param ptr Pointer to the buffer to be deallocated
103 void DEMOFree(void* ptr);
104 
105 /// \brief Get demo running state
106 ///
107 /// \note This function also calls the DEMO Test functions, and therefore
108 ///       it is expected that this function is only called once prior to each
109 ///       main loop iteration.
110 ///
111 /// \retval TRUE if \ref DEMOInit() has been called and DEMOStopRunning() has not been called; false otherwise.
112 BOOL DEMOIsRunning(void);
113 
114 typedef void (*DEMOReleaseCallbackFunc)(void);
115 
116 /// \brief Sets the callback for when a release occurs
117 ///
118 /// \param func Function to call upon release message
119 void DEMOSetReleaseCallback(DEMOReleaseCallbackFunc func);
120 
121 /// \brief Get current time
122 ///
123 /// \retval The value of time in seconds since 1970-01-01 00:00:00
124 DEMO_INLINE
DEMOGetTime(void)125 s64 DEMOGetTime(void)
126 {
127     s64 diffSinceEpoch = 946627200;
128     return OSTicksToSeconds(OSGetSystemTime()) + diffSinceEpoch;
129 }
130 
131 /// \brief Prints formatted message to debug output
132 ///
133 /// \param msg Pointer to a null-terminated string including format specification
134 /// (equivalent to C's standard output function).
135 /// \param ... Optional argument
136 void DEMOPrintf (const char* msg, ...);
137 
138 /// \brief Perform a "fast" CPU copy
139 ///
140 /// This copy makes no assumptions about source & dest alignment.
141 /// Both source & dest buffers are flushed out of the CPU cache.
142 /// The buffers must not overlap.
143 ///
144 /// \note This function does not use locked-cache DMA or any non-CPU hardware.
145 /// It is therefore not the fastest way to copy, but it is usually faster than
146 /// just calling memcpy.  Better copy functions may become available in the future.
147 ///
148 void DEMOFastCopy(void *dst, void *src, u32 size);
149 
150 /// @}
151 
152 #ifdef __cplusplus
153 }
154 #endif
155 
156 #endif /// __DEMO_SYSTEM_H__
157