1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - include -
3   File:     misc.h
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-11-04#$
14   $Rev: 9197 $
15   $Author: yosizaki $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NITRO_MISC_H_
19 #define NITRO_MISC_H_
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #ifndef SDK_WIN32
26 #include <nitro/os/common/printf.h>
27 #endif
28 
29 //--------------------------------------------------------------------------------
30 //  C++ Macros
31 //
32 #ifdef __cplusplus
33 #define SDK_IFDEFCPP extern "C" {
34 #define SDK_ENDIFCPP }
35 #else
36 #define SDK_IFDEFCPP
37 #define SDK_ENDIFCPP
38 #endif
39 
40 //--------------------------------------------------------------------------------
41 //      Asserts
42 //
43 #ifdef  SDK_DEBUG
44 #ifndef SDK_ASSERT
45 #define SDK_ASSERT(exp) \
46              (void) ((exp) || (OSi_Panic(__FILE__, __LINE__, "Failed assertion " #exp), 0))
47 #endif
48 #else  // SDK_DEBUG
49 #ifndef SDK_ASSERT
50 #define SDK_ASSERT(exp)           ((void) 0)
51 #endif
52 #endif // SDK_DEBUG
53 
54 /* Check done when compiling */
55 #define SDK_COMPILER_ASSERT(expr) \
56     extern void sdk_compiler_assert ## __LINE__ ( char is[(expr) ? +1 : -1] )
57 
58 //--------------------------------------------------------------------------------
59 //      Assert Messages
60 //
61 #ifdef  SDK_DEBUG
62 #ifndef SDK_ASSERTMSG
63 #define SDK_ASSERTMSG(exp, ...) \
64     (void) ((exp) || (OSi_Panic(__FILE__, __LINE__, __VA_ARGS__), 0))
65 #endif
66 #ifndef SDK_TASSERTMSG
67 #define SDK_TASSERTMSG(exp, ...) \
68     (void) ((exp) || (OSi_TPanic(__FILE__, __LINE__, __VA_ARGS__), 0))
69 #endif
70 #else  // SDK_DEBUG
71 #ifndef SDK_ASSERTMSG
72 #define SDK_ASSERTMSG(exp, ...)     ((void) 0)
73 #endif
74 #ifndef SDK_TASSERTMSG
75 #define SDK_TASSERTMSG(exp, ...)    ((void) 0)
76 #endif
77 #endif // SDK_DEBUG
78 
79 //--------------------------------------------------------------------------------
80 //      Assert Values
81 //
82 #ifdef  SDK_DEBUG
83 #ifndef SDK_ASSERT_INT
84 #define SDK_ASSERT_INT(exp, value) \
85              (void) ((exp) || (OSi_Panic(__FILE__, __LINE__, "Failed assertion %s : %s = %p",  #exp, #value, value), 0))
86 #endif
87 #else  // SDK_DEBUG
88 #ifndef SDK_ASSERT_INT
89 #define SDK_ASSERT_INT(exp, value)  ((void) 0)
90 #endif
91 #endif // SDK_DEBUG
92 
93 //--------------------------------------------------------------------------------
94 //      Warnings
95 //
96 #ifdef  SDK_DEBUG
97 #ifndef SDK_WARNING
98 #define SDK_WARNING(exp, ...) \
99     (void) ((exp) || (OSi_Warning(__FILE__, __LINE__, __VA_ARGS__), 0))
100 #endif
101 #ifndef SDK_TWARNING
102 #define SDK_TWARNING(exp, ...) \
103     (void) ((exp) || (OSi_TWarning(__FILE__, __LINE__, __VA_ARGS__), 0))
104 #endif
105 #else  // SDK_DEBUG
106 #ifndef SDK_WARNING
107 #define SDK_WARNING(exp, ...)     ((void) 0)
108 #endif
109 #ifndef SDK_TWARNING
110 #define SDK_TWARNING(exp, ...)    ((void) 0)
111 #endif
112 #endif // SDK_DEBUG
113 
114 //--------------------------------------------------------------------------------
115 //      NULL Asserts
116 //
117 #ifdef  SDK_DEBUG
118 #ifndef SDK_NULL_ASSERT
119 #define SDK_NULL_ASSERT(exp) \
120              (void) (((exp) != NULL) || (OSi_Panic(__FILE__, __LINE__, "Pointer must not be NULL ("#exp")"), 0))
121 #endif
122 #else  // SDK_DEBUG
123 #ifndef SDK_NULL_ASSERT
124 #define SDK_NULL_ASSERT(exp)           ((void) 0)
125 #endif
126 #endif // SDK_DEBUG
127 
128 //--------------------------------------------------------------------------------
129 //      Pointer Checks
130 //
131 #ifdef  SDK_DEBUG
132 #ifndef SDK_IS_VALID_POINTER
133 #define SDK_IS_VALID_POINTER(exp)   ( (0x01000000 <= ((u32)(exp))) && (((u32)(exp)) < 0x10000000) )
134 //#define SDK_IS_VALID_POINTER(exp)   ((exp) != NULL)
135 #endif
136 #else  // SDK_DEBUG
137 #ifndef SDK_IS_VALID_POINTER
138 #define SDK_IS_VALID_POINTER(exp)           (TRUE)
139 #endif
140 #endif // SDK_DEBUG
141 
142 //--------------------------------------------------------------------------------
143 //      Pointer Asserts
144 //
145 #ifdef  SDK_DEBUG
146 #ifndef SDK_POINTER_ASSERT
147 #define SDK_POINTER_ASSERT(exp) \
148              (void) (SDK_IS_VALID_POINTER(exp) || (OSi_Panic(__FILE__, __LINE__, "%s(=%p) is not valid pointer", #exp, (exp)), 0))
149 #endif
150 #else  // SDK_DEBUG
151 #ifndef SDK_POINTER_ASSERT
152 #define SDK_POINTER_ASSERT(exp)             ((void) 0)
153 #endif
154 #endif // SDK_DEBUG
155 
156 //--------------------------------------------------------------------------------
157 //      Pointer or NULL Asserts
158 //
159 #ifdef  SDK_DEBUG
160 #ifndef SDK_POINTER_OR_NULL_ASSERT
161 #define SDK_POINTER_OR_NULL_ASSERT(exp) \
162              (void) (((exp) == NULL) || SDK_IS_VALID_POINTER(exp) || (OSi_Panic(__FILE__, __LINE__, "%s(=%p) must be NULL or valid pointer", #exp, (exp)), 0))
163 #endif
164 #else  // SDK_DEBUG
165 #ifndef SDK_POINTER_OR_NULL_ASSERT
166 #define SDK_POINTER_OR_NULL_ASSERT(exp)     ((void) 0)
167 #endif
168 #endif // SDK_DEBUG
169 
170 //--------------------------------------------------------------------------------
171 //      Min Asserts
172 //
173 #ifdef  SDK_DEBUG
174 #ifndef SDK_MIN_ASSERT
175 #define SDK_MIN_ASSERT(exp, min) \
176              (void) (((exp) >= (min)) || \
177                      (OSi_Panic(__FILE__, __LINE__, #exp " is out of bounds(%d)\n%d <= "#exp" not satisfied.", exp, min), 0))
178 #endif
179 #else  // SDK_DEBUG
180 #ifndef SDK_MIN_ASSERT
181 #define SDK_MIN_ASSERT(exp, min)           ((void) 0)
182 #endif
183 #endif // SDK_DEBUG
184 
185 //--------------------------------------------------------------------------------
186 //      Max Asserts
187 //
188 #ifdef  SDK_DEBUG
189 #ifndef SDK_MAX_ASSERT
190 #define SDK_MAX_ASSERT(exp, max) \
191              (void) (((exp) <= (max)) || \
192                      (OSi_Panic(__FILE__, __LINE__, #exp " is out of bounds(%d)\n"#exp" <= %d not satisfied.", exp, max), 0))
193 #endif
194 #else  // SDK_DEBUG
195 #ifndef SDK_MAX_ASSERT
196 #define SDK_MAX_ASSERT(exp, max)           ((void) 0)
197 #endif
198 #endif // SDK_DEBUG
199 
200 //--------------------------------------------------------------------------------
201 //      Min Max Asserts
202 //
203 #ifdef  SDK_DEBUG
204 #ifndef SDK_MINMAX_ASSERT
205 #define SDK_MINMAX_ASSERT(exp, min, max) \
206              (void) (((exp) >= (min) && (exp) <= (max)) || \
207                      (OSi_Panic(__FILE__, __LINE__, #exp " is out of bounds(%d)\n%d <= "#exp" <= %d not satisfied.", exp, min, max), 0))
208 #endif
209 #else  // SDK_DEBUG
210 #ifndef SDK_MINMAX_ASSERT
211 #define SDK_MINMAX_ASSERT(exp, min, max)           ((void) 0)
212 #endif
213 #endif // SDK_DEBUG
214 
215 //--------------------------------------------------------------------------------
216 //      Fatal Errors
217 //
218 #ifdef  SDK_DEBUG
219 #ifndef SDK_FATAL_ERROR
220 #define SDK_FATAL_ERROR(...) \
221     (void) (OSi_Panic(__FILE__, __LINE__, "Fatal Error\n"__VA_ARGS__), 0)
222 #endif
223 #ifndef SDK_TFATAL_ERROR
224 #define SDK_TFATAL_ERROR(...) \
225     (void) (OSi_TPanic(__FILE__, __LINE__, "Fatal Error\n"__VA_ARGS__), 0)
226 #endif
227 #else  // SDK_DEBUG
228 #ifndef SDK_FATAL_ERROR
229 #define SDK_FATAL_ERROR(...)     ((void) 0)
230 #endif
231 #ifndef SDK_TFATAL_ERROR
232 #define SDK_TFATAL_ERROR(...)    ((void) 0)
233 #endif
234 #endif // SDK_DEBUG
235 
236 //--------------------------------------------------------------------------------
237 //      Internal Errors
238 //
239 #ifdef  SDK_DEBUG
240 #ifndef SDK_INTERNAL_ERROR
241 #define SDK_INTERNAL_ERROR(...) \
242     (void) (OSi_Panic(__FILE__, __LINE__, "SDK Internal error\nPlease e-mail to nintendo\n" __VA_ARGS__), 0)
243 #endif
244 #ifndef SDK_TINTERNAL_ERROR
245 #define SDK_TINTERNAL_ERROR(...) \
246     (void) (OSi_TPanic(__FILE__, __LINE__, "SDK Internal error\nPlease e-mail to nintendo\n" __VA_ARGS__), 0)
247 #endif
248 #else  // SDK_DEBUG
249 #ifndef SDK_INTERNAL_ERROR
250 #define SDK_INTERNAL_ERROR(...)     ((void) 0)
251 #endif
252 #ifndef SDK_TINTERNAL_ERROR
253 #define SDK_TINTERNAL_ERROR(...)    ((void) 0)
254 #endif
255 #endif // SDK_DEBUG
256 
257 //--------------------------------------------------------------------------------
258 //      Alignment Errors (4-byte)
259 //
260 #ifdef  SDK_DEBUG
261 #ifndef SDK_ALIGN4_ASSERT
262 #define SDK_ALIGN4_ASSERT(exp) \
263              (void) ((((u32)(exp) & 3) == 0) || (OSi_Panic(__FILE__, __LINE__, "Alignment Error(0x%08x)\n"#exp" must be aligned to 4 bytes boundary.", exp), 0))
264 #endif
265 #else  // SDK_DEBUG
266 #ifndef SDK_ALIGN4_ASSERT
267 #define SDK_ALIGN4_ASSERT(exp)           ((void) 0)
268 #endif
269 #endif // SDK_DEBUG
270 
271 //--------------------------------------------------------------------------------
272 //      Alignment Errors (2-byte)
273 //
274 #ifdef  SDK_DEBUG
275 #ifndef SDK_ALIGN2_ASSERT
276 #define SDK_ALIGN2_ASSERT(exp) \
277              (void) ((((u32)(exp) & 1) == 0) || (OSi_Panic(__FILE__, __LINE__, "Alignment Error(0x%08x)\n"#exp" must be aligned to 2 bytes boundary.", exp), 0))
278 #endif
279 #else  // SDK_DEBUG
280 #ifndef SDK_ALIGN2_ASSERT
281 #define SDK_ALIGN2_ASSERT(exp)           ((void) 0)
282 #endif
283 #endif // SDK_DEBUG
284 
285 #if 0
286 // ** This block has been moved to section.h **
287 //
288 //--------------------------------------------------------------------------------
289 //       Section Definitions for LCF
290 #if     defined(SDK_CW) || defined(SDK_RX) || defined(__MWERKS__)
291 #ifdef SDK_ARM9
292 #pragma define_section ITCM ".itcm" abs32 RWX
293 #pragma define_section DTCM ".dtcm" abs32 RWX
294 #else
295 #pragma define_section WRAM ".wram" abs32 RWX
296 #endif
297 #pragma define_section PARENT ".parent" abs32 RWX
298 #pragma define_section VERSION ".version" abs32 RWX
299 #elif   defined(SDK_PRODG)
300 #endif
301 #endif
302 
303 //--------------------------------------------------------------------------------
304 //       Macros for Linker
305 
306 // Specify force-link
307 void    OSi_ReferSymbol(void *symbol);
308 #define SDK_REFER_SYMBOL(symbol) OSi_ReferSymbol((void*)(symbol))
309 
310 // For embedding middleware version string in VERSION section
311 #define SDK_MIDDLEWARE_STRING(vender, module) "[SDK+" vender ":" module "]"
312 #define SDK_DEFINE_MIDDLEWARE(id, vender, module) static char id [] = SDK_MIDDLEWARE_STRING(vender, module)
313 #define SDK_USING_MIDDLEWARE(id) SDK_REFER_SYMBOL(id)
314 
315 //--------------------------------------------------------------------------------
316 //       Macros for Deprecated Functions
317 
318 #define OSi_AbortByUnsupportedRegister(regname, file, line) (OSi_TPanic(file, line, " I/O register \"%s\" is unsupported on this platform!", regname), 0)
319 #define OS_UNSUPPORTED_REGADDR(reg) OSi_AbortByUnsupportedRegister(#reg, __FILE__, __LINE__)
320 #define OS_UNSUPPORTED_REG8(reg)    *(REGType8*)OSi_AbortByUnsupportedRegister(#reg, __FILE__, __LINE__)
321 #define OS_UNSUPPORTED_REG16(reg)   *(REGType16*)OSi_AbortByUnsupportedRegister(#reg, __FILE__, __LINE__)
322 #define OS_UNSUPPORTED_REG32(reg)   *(REGType32*)OSi_AbortByUnsupportedRegister(#reg, __FILE__, __LINE__)
323 #define OS_UNSUPPORTED_REG64(reg)   *(REGType64*)OSi_AbortByUnsupportedRegister(#reg, __FILE__, __LINE__)
324 
325 
326 //--------------------------------------------------------------------------------
327 
328 #ifdef __cplusplus
329 } /* extern "C" */
330 #endif
331 
332 /* NITRO_MISC_H_ */
333 #endif
334