1 /*---------------------------------------------------------------------------*
2   Project:     Revolution OS
3   File:        os.h
4   Programmers: Shiki Okasaka
5                Hashida Takayuki
6                Tian
7                Carl
8                Dante
9                HIRATSU Daisuke
10                Kazuki Hirose
11 
12   Copyright 1998-2005 Nintendo.  All rights reserved.
13 
14   These coded instructions, statements, and computer programs contain
15   proprietary information of Nintendo of America Inc. and/or Nintendo
16   Company Ltd., and are protected by Federal copyright law.  They may
17   not be disclosed to third parties or copied or duplicated in any form,
18   in whole or in part, without the prior written consent of Nintendo.
19 
20   $Log: os.h,v $
21   Revision 1.12  2006/08/09 07:53:51  wada_jumpei
22   Added OSGetAppGamename and OSGetAppType.
23 
24   Revision 1.11  07/05/2006 01:20:07  ooizumi
25   Added OS_CONSOLE_RVL_NDEV2_1.
26 
27   Revision 1.10  05/15/2006 01:02:47  kawaset
28   Updated default clock value.
29 
30   Revision 1.9  02/28/2006 12:17:04  hirose
31   Updated console type information.
32 
33   Revision 1.8  02/15/2006 07:53:27  hirose
34   Updated console type definitions.
35 
36   Revision 1.7  02/08/2006 10:22:27  mitu
37   fixed for win32 application.
38 
39   Revision 1.6  02/08/2006 02:47:19  hirose
40   Updated Arena function definitions and console type macros.
41 
42   Revision 1.5  2006/02/04 11:56:44  hashida
43   (none)
44 
45   Revision 1.4  01/28/2006 09:33:12  hirose
46   Moved memory size APIs to OSMemory.h
47   Enabled MEM2 arena functions to all platform.
48 
49   Revision 1.3  01/21/2006 02:01:17  hirose
50   Fixed #ifdef for time base definitions.
51 
52   Revision 1.2  01/07/2006 06:18:20  hirose
53   Opened MEM1 macros to all targets. Added OSIsMEM?Region() macros.
54 
55   Revision 1.1.1.1  2005/12/29 06:53:27  hiratsu
56   Initial import.
57 
58   Revision 1.8  2005/12/27 10:07:24  hirose
59   Added MEM1 arena macros. Deleted obsolete definitions.
60 
61   Revision 1.7  12/26/2005 12:34:44  hiratsu
62   Renamed GDDR3 API.:  BMEM -> MEM2.
63 
64 
65     59    1/23/03 9:38 Shiki
66     Defined OS_CONSOLE_TDEV, etc.
67 
68     58    8/29/02 12:49 Shiki
69     Revised __VA_ARGS__ availability test code for NDEBUG, SNSYS.
70 
71     57    8/29/02 9:49 Shiki
72     Revised __VA_ARGS__ availability test code for SNSYS.
73 
74     56     8/26/02 13:32:00 Shiki
75     Removed old WIN emulator specific OS_TIMER_CLOCK definition.
76     Fixed __VA_ARGS__ availability test code.
77 
78     55     8/22/02 11:57:00 Shiki
79     Clean up.
80 
81     54     8/22/02 10:48:00 Shiki
82     Put #ifndef around ASSERTMSGn() macros.
83     Refined ASSERTMSG() using the C9x extension for MW 1.3.x users
84     Added another const keyword to the OSPanic() function prototype.
85 
86     53     8/20/02 21:33:00 Shiki
87     Added OSFatal().
88 
89     52    8/12/02 15:16 Shiki
90     Added _padding field to OSStopwatch{}.
91 
92     51    8/09/02 08:54:00 Shiki
93     Added const keywords to the OSReport() function prototype.
94 
95     50     8/07/02 21:41 Shiki
96     Added OSRegisterVersion().
97 
98     49     8/05/02 18:04:00 Shiki
99     Added const keywords to relevant function prototypes.
100 
101     48    11/27/01 21:12 Shiki
102     Included <revolution/os/OSModule.h>.
103 
104     47    11/12/01 12:58 Shiki
105     Added OS[Get/Set]ProgressiveMode().
106 
107     46    10/29/01 11:08 Shiki
108     Included <revolution/os/OSSemaphore.h>.
109 
110     45    10/25/01 13:58 Shiki
111     Fixed OS_LANG_*.
112 
113     44    10/18/01 13:50 Shiki
114     Included <revolution/os/OSExpansion.h>.
115 
116     43     9/03/01 14:43:00 Shiki
117     Included <revolution/os/OSSerial.h>
118 
119     42    7/11/01 10:20p Hashida
120     Added OSReset.h
121 
122     41    01/07/05 21:06 Shiki
123     Added OSGetProgressiveMode()/OSSetProgressiveMode().
124 
125     40    6/11/01 7:53p Tian
126     Changed to use AT_ADDRESS macro
127 
128     39    5/17/01 1:26p Shiki
129     Updated OS_CORE_CLOCK, OS_BUS_CLOCK.
130 
131     38    5/09/01 1:42p Carl
132     Conditional zed #define ASSERT.
133 
134     37    01/04/27 17:45 Shiki
135     Added OS_LANG_NEUTRAL.
136 
137     36    01/04/27 17:05 Shiki
138     Removed duplicated OS*etSoundMode() declarations.
139 
140     35    01/04/27 16:43 Shiki
141     Fixed OS_LANG_DUTCH value.
142 
143     34    01/04/27 16:25 Shiki
144     Added OS_LANG_*.
145 
146     33    01/04/19 13:30 Shiki
147     Included <revolution/os/OSFont.h>.
148 
149     32    01/02/15 10:55 Shiki
150     Added msec and usec to OSCalendarTime.
151 
152     31    01/02/13 20:06 Shiki
153     Added sound mode and calendar functions.
154 
155     30    11/09/00 7:40p Tian
156     Added calibrated clock values to HW2
157 
158     29    9/11/00 2:59p Shiki
159     Added OS_CONSOLE_RETAIL2.
160 
161     28    9/07/00 7:54p Shiki
162     Fixed OS_*_CLOCK defines and related macros for handling final clock
163     setup.
164 
165     27    9/07/00 2:57p Shiki
166     Removed OSSetTick() and OSSetTime().
167 
168     26    9/05/00 5:18p Shiki
169     Added OS_CONSOLE_DEVHW2.
170 
171     25    8/16/00 6:20p Shiki
172     Added OSDiffTick().
173 
174     24    7/14/00 3:48p Shiki
175     Added OSGetRTC()/OSSetRTC().
176 
177     23    7/10/00 6:40p Dante
178     Bug Fix (on the PC OSGetTick returns milliseconds)
179 
180     22    6/15/00 4:47a Shiki
181     Rollback MINNOW clock speed defines for backward boot ROM
182     compatibility.
183 
184     21    6/15/00 4:41a Shiki
185     Revised real-time clock API for HW1.
186 
187     20    6/14/00 4:53p Shiki
188     Fixed HW1_DRIP clock settings.
189 
190     19    5/18/00 9:28a Shiki
191     Added OSAllocFromArenaLo() and OSAllocFromArenaHi().
192 
193     18    5/14/00 1:38p Shiki
194     Fixed DRIP clock settings.
195 
196     17    5/10/00 4:24p Shiki
197     Added clock configuration for DRIP.
198 
199     16    3/29/00 3:05p Hashida
200     Added memory size APIs.
201 
202     15    3/27/00 2:02p Tian
203     Added OSFastCast.h
204 
205     14    3/16/00 7:04p Shiki
206     Removed timer API. Included <os/OSAlarm.h>.
207 
208     13    3/14/00 2:44p Shiki
209     Added ASSERTMSGn() macros.
210 
211     12    3/01/00 3:29p Tian
212     Cleaned up clock rate settings.  They were wrong for GEKKO.
213 
214     11    2/15/00 6:03p Shiki
215     Revised OS_CONSOLE_* define names.
216 
217     10    2/01/00 1:50p Shiki
218     Added OS_CONSOLE_DEVELOPMENT1 define name.
219 
220     9     1/18/00 6:22p Shiki
221       Included <revolution/os/OSMutex.h>.
222 
223     8     1/18/00 5:46p Tian
224     MINNOW_MARLIN -> MINNOW
225 
226     7     1/18/00 3:29p Shiki
227     Included <revolution/os/OSMessage.h>.
228 
229     6     1/18/00 1:57p Tian
230     Changed the address translation macros for MINNOW_MARLIN to enable FIFO
231     emulation.  Because ORs/ANDs are used, we sacrifice some safety.
232 
233     5     1/18/00 10:43a Shiki
234     Implemented basic thread API.
235 
236     4     1/10/00 2:43p Hashida
237     Changed so that OSTicksToMicroseconds and OSMicrosecondsToTicks macros
238     works with 33MHz busclock systems.
239 
240     3     12/16/99 2:52p Tian
241     New core/bus speeds for Gekko + Spruce
242 
243     2     12/15/99 3:08p Shiki
244     Added OS_*_CLOCK defines for MINNOW.
245 
246     17    10/07/99 4:13p Shiki
247     Added Idle Function API section.
248 
249     16    8/23/99 7:18p Shiki
250     Added OS_BASE_CACHED and  OS_BASE_UNCACHED.
251 
252     15    8/02/99 2:20p Shiki
253     Included OSInterrupt.h
254 
255     14    99/07/27 4:51p Shiki
256     Added function versions of address translation APIs.
257 
258     13    99/07/27 3:43p Shiki
259     Revised OSTicksToNanoseconds() and  OSNanosecondsToTicks().
260 
261     12    99/07/27 3:16p Shiki
262     Fixed time-cycle conversion macros.
263 
264     11    99/07/12 9:37p Shiki
265     Included OSError.h
266 
267     10    99/07/12 3:24p Shiki
268     Cleanup. Removed EPPC specific OSReport() and OSPanic() declarations.
269 
270     9     6/04/99 3:02p Tianli01
271     Cleanup.  Added rounding macros, OSTicksToCycles.
272 
273     8     5/25/99 5:28p Shiki
274     Added the following line:
275     #include <revolution/os/OSAlloc.h>
276 
277     7     5/19/99 5:13p Shiki
278     Renamed Halt() to OSHalt().
279 
280     6     5/19/99 4:20p Shiki
281     Cleaned up address translation macros.
282 
283     5     5/18/99 5:06p Shiki
284     Fixed address translation macros.
285 
286     4     5/11/99 4:42p Shiki
287     Refreshed include tree.
288 
289     2     5/03/99 5:26p Tianli01
290     PPC->EPPC
291 
292     1     4/30/99 12:49p Tianli01
293 
294     10    4/21/99 4:12p Tianli01
295     Added OSGetConsoleType.  Changed OSInterruptLevel to BOOL.  Cleaned up
296     Interrupt interface.
297 
298     9     4/19/99 5:04p Shiki
299     Revised real-time clock API related definitions.
300 
301     8     7/15/99 3:46p Shiki
302     1. Cleaned up ASSERT stuff
303     2. Moved to _DEBUG (avoid DEBUG)
304 
305     7     4/15/99 2:45p Tianli01
306     Removed OS_BASE_CLOCK (it should have been OS_BUS_CLOCK).  Added
307     ASSERTs for PPC.
308 
309     6     4/13/99 5:48p Tianli01
310     Rearranged.  Added stopwatch APIs.
311 
312     5     4/12/99 7:45p Tianli01
313     Deleted OSEvent stuff.  Move to DEBUG for consistency (avoid NDEBUG).
314     Fixed Halt/OSPanic for PPC target
315 
316     4     4/01/99 7:53p Tianli01
317     Added OSTimer interfaces.  Fixed time base constants for Arthur.
318 
319     3     3/31/99 6:10p Tianli01
320     Added arena APIs (not implemented) , address translation constants for
321     assembly code, PPC OSReport APIs (only relevant for Arthur board).
322 
323     2     3/26/99 2:06p Tianli01
324     Broke up into more header files (OSContext.h, OSEmu.h, OSException.h),
325     cleaned up, address translation macros added.
326 
327     1     3/04/99 2:22p Tianli01
328     Initial check-in to new tree
329 
330     5     12/18/98 11:28a Shiki
331     Removed declaration of  __OSLoadContext2()
332 
333     4     12/18/98 9:25a Shiki
334     Fixed definition of OSContext for Gecko
335 
336     3     12/17/98 9:04a Shiki
337     Fixed ASSERT macro
338 
339     2     12/16/98 8:54p Shiki
340     1. Used OSContext for windows stack environment instead of jmp_buf
341        to support __OSWinCall() API
342     2. Added the __cdecl modifiers to inline assembler functions
343 
344     1     12/15/98 10:08p Shiki
345 
346   Change History:
347     12/15/1998  Shiki Okasaka   1. Added __OSRaiseException() and
348                                    __OSRaiseInterrupt()
349                                 2. Revised __OSMach structure
350     12/12/1998  Shiki Okasaka   Added OSSwitchStack()
351     12/11/1998  Shiki Okasaka   1. Moved bEnabled member from __OSMach to
352                                    OSContext
353                                 2. Fixed OSTime from u64 to s64
354     12/10/1998  Shiki Okasaka   Revised to reflect the coding guidelines
355     12/07/1998  Shiki Okasaka   Included <types.h>
356     12/06/1998  Shiki Okasaka   1. Revised __OSInitMach not to time-out
357                                    but to take data pointer
358                                 2. Added OSClearContext()
359     12/05/1998  Shiki Okasaka   1. Fixed __OSMach to keep stack bottom
360                                 2. Revised __OSInitMach to time-out
361                                 3. Revised OSSaveContext()
362     12/03/1998  Shiki Okasaka   1. Added interrupt time-out
363                                 2. Fixed OSSaveContext()
364     12/02/1998  Shiki Okasaka   2nd Edition
365     11/28/1998  Shiki Okasaka   Created
366 
367   $NoKeywords: $
368  *---------------------------------------------------------------------------*/
369 
370 #ifndef __OS_H__
371 #define __OS_H__
372 
373 #ifndef __DOLPHIN_RC__
374 #include <revolution/types.h>
375 #include <revolution/gx/GXStruct.h>
376 #endif  // __DOLPHIN_RC__
377 
378 #include <stdarg.h>
379 
380 #ifdef __cplusplus
381 extern "C" {
382 #endif
383 
384 /*---------------------------------------------------------------------------*
385     Arena API
386  *---------------------------------------------------------------------------*/
387 
388 void*   OSGetMEM1ArenaHi( void );
389 void*   OSGetMEM1ArenaLo( void );
390 void    OSSetMEM1ArenaHi( void* newHi );
391 void    OSSetMEM1ArenaLo( void* newLo );
392 
393 void*   OSGetMEM2ArenaHi( void );
394 void*   OSGetMEM2ArenaLo( void );
395 void    OSSetMEM2ArenaHi( void* newHi );
396 void    OSSetMEM2ArenaLo( void* newLo );
397 
398 void*   OSAllocFromMEM1ArenaLo( u32 size, u32 align );
399 void*   OSAllocFromMEM1ArenaHi( u32 size, u32 align );
400 void*   OSAllocFromMEM2ArenaLo( u32 size, u32 align );
401 void*   OSAllocFromMEM2ArenaHi( u32 size, u32 align );
402 
403 // For backward compatibility
404 void*   OSGetArenaHi( void );           // Same as OSGetMEM1ArenaHi
405 void*   OSGetArenaLo( void );           // Same as OSGetMEM1ArenaLo
406 void    OSSetArenaHi( void* newHi );    // Same as OSSetMEM1ArenaHi
407 void    OSSetArenaLo( void* newLo );    // Same as OSSetMEM1ArenaLo
408 
409 #define OSAllocFromArenaLo  OSAllocFromMEM1ArenaLo
410 #define OSAllocFromArenaHi  OSAllocFromMEM1ArenaHi
411 
412 #ifdef HOLLYWOOD_EMU
413 #define OS_MEM2_EMU_SIZE 0x00100000   // 1Mbyte.
414 #endif
415 
416 /*---------------------------------------------------------------------------*
417     Round APIs
418  *---------------------------------------------------------------------------*/
419 
420 // Most Dolphin subsystems require 32 byte alignment
421 #define OSRoundUp32B(x)       (((u32)(x) + 32 - 1) & ~(32 - 1))
422 #define OSRoundDown32B(x)     (((u32)(x)) & ~(32 - 1))
423 
424 /*---------------------------------------------------------------------------*
425     Basic OS API
426  *---------------------------------------------------------------------------*/
427 
428 void    OSInit          ( void );
429 
430 #define OS_CONSOLE_MASK             0xf0000000
431 #define OS_CONSOLE_RETAIL           0x00000000
432 #define OS_CONSOLE_DEVELOPMENT      0x10000000
433 #define OS_CONSOLE_TDEV             0x20000000
434 
435 #define OS_CONSOLE_RVL_RETAIL3      0x00000023
436 #define OS_CONSOLE_RVL_RETAIL2      0x00000022
437 #define OS_CONSOLE_RVL_RETAIL1      0x00000021
438 #define OS_CONSOLE_RVL_PP2_2        0x00000020
439 #define OS_CONSOLE_RVL_PP2_1        0x00000012
440 #define OS_CONSOLE_RVL_PP1          0x00000011
441 #define OS_CONSOLE_RVL_PP0          0x00000010
442 #define OS_CONSOLE_RVL_NDEV2_1      0x10000021
443 #define OS_CONSOLE_RVL_NDEV2_0      0x10000020
444 #define OS_CONSOLE_RVL_NDEV1_2      0x10000012
445 #define OS_CONSOLE_RVL_NDEV1_1      0x10000011
446 #define OS_CONSOLE_RVL_NDEV1_0      0x10000010
447 
448 #define OS_CONSOLE_RETAIL4          0x00000004
449 #define OS_CONSOLE_RETAIL3          0x00000003
450 #define OS_CONSOLE_RETAIL2          0x00000002
451 #define OS_CONSOLE_RETAIL1          0x00000001
452 #define OS_CONSOLE_TDEVHW4          0x20000007
453 #define OS_CONSOLE_TDEVHW3          0x20000006
454 #define OS_CONSOLE_TDEVHW2          0x20000005
455 #define OS_CONSOLE_TDEVHW1          0x20000004
456 #define OS_CONSOLE_RVL_EMULATOR     0x10000008   // For Revolution emulator kit
457 #define OS_CONSOLE_DEVHW4           0x10000007
458 #define OS_CONSOLE_DEVHW3           0x10000006
459 #define OS_CONSOLE_DEVHW2           0x10000005
460 #define OS_CONSOLE_DEVHW1           0x10000004
461 #define OS_CONSOLE_MINNOW           0x10000003
462 #define OS_CONSOLE_ARTHUR           0x10000002
463 #define OS_CONSOLE_PC_EMULATOR      0x10000001
464 #define OS_CONSOLE_EMULATOR         0x10000000
465 
466 
467 u32     OSGetConsoleType( void );
468 
469 #define OS_SOUND_MODE_MONO          0u
470 #define OS_SOUND_MODE_STEREO        1u
471 
472 u32     OSGetSoundMode( void );
473 void    OSSetSoundMode( u32 mode );
474 
475 #define OS_PROGRESSIVE_MODE_OFF     0u
476 #define OS_PROGRESSIVE_MODE_ON      1u
477 
478 u32     OSGetProgressiveMode( void );
479 void    OSSetProgressiveMode( u32 on );
480 
481 #define OS_LANG_ENGLISH         0u
482 #define OS_LANG_GERMAN          1u
483 #define OS_LANG_FRENCH          2u
484 #define OS_LANG_SPANISH         3u
485 #define OS_LANG_ITALIAN         4u
486 #define OS_LANG_DUTCH           5u
487 
488 u8      OSGetLanguage( void );
489 void    OSSetLanguage( u8 language );
490 
491 #define OS_EURGB60_OFF          0u
492 #define OS_EURGB60_ON           1u
493 
494 u32     OSGetEuRgb60Mode( void );
495 void    OSSetEuRgb60Mode( u32 on );
496 
497 void    OSRegisterVersion( const char* id );
498 
499 /*---------------------------------------------------------------------------*
500     Interrupt API
501  *---------------------------------------------------------------------------*/
502 
503 BOOL    OSDisableInterrupts ( void );
504 BOOL    OSEnableInterrupts  ( void );
505 BOOL    OSRestoreInterrupts ( BOOL level );
506 
507 /*---------------------------------------------------------------------------*
508     Address Translation API
509  *---------------------------------------------------------------------------*/
510 
511 // Declarations
512 void* OSPhysicalToCached    ( u32   paddr  );
513 void* OSPhysicalToUncached  ( u32   paddr  );
514 u32   OSCachedToPhysical    ( const void* caddr  );
515 u32   OSUncachedToPhysical  ( const void* ucaddr );
516 void* OSCachedToUncached    ( const void* caddr  );
517 void* OSUncachedToCached    ( const void* ucaddr );
518 
519 // Upper words of the masks, since UIMM is only 16 bits
520 #define OS_CACHED_REGION_PREFIX         0x8000
521 #define OS_UNCACHED_REGION_PREFIX       0xC000
522 #define OS_PHYSICAL_MASK                0x3FFF
523 
524 #define OS_BASE_CACHED                  (OS_CACHED_REGION_PREFIX << 16)
525 #define OS_BASE_UNCACHED                (OS_UNCACHED_REGION_PREFIX << 16)
526 
527 // Macro overrides
528 #ifndef _DEBUG
529 #define OSPhysicalToCached(paddr)       ((void*) ((u32)(paddr) + OS_BASE_CACHED))
530 #define OSPhysicalToUncached(paddr)     ((void*) ((u32)(paddr) + OS_BASE_UNCACHED))
531 #define OSCachedToPhysical(caddr)       ((u32)   ((u8*)(caddr) - OS_BASE_CACHED))
532 #define OSUncachedToPhysical(ucaddr)    ((u32)   ((u8*)(ucaddr)- OS_BASE_UNCACHED))
533 #define OSCachedToUncached(caddr)       ((void*) ((u8*)(caddr) + (OS_BASE_UNCACHED - OS_BASE_CACHED)))
534 #define OSUncachedToCached(ucaddr)      ((void*) ((u8*)(ucaddr)- (OS_BASE_UNCACHED - OS_BASE_CACHED)))
535 #endif  // _DEBUG
536 
537 // Memory region identifiers
538 #define OS_MEM1_REGION_PREFIX           0x00000000
539 #define OS_MEM2_REGION_PREFIX           0x10000000
540 #define OS_MEM_REGION_ID_MASK           0x30000000
541 
542 #define OSIsMEM1Region(addr)            (((u32)(addr) & OS_MEM_REGION_ID_MASK) == OS_MEM1_REGION_PREFIX)
543 #define OSIsMEM2Region(addr)            (((u32)(addr) & OS_MEM_REGION_ID_MASK) == OS_MEM2_REGION_PREFIX)
544 
545 /*---------------------------------------------------------------------------*
546     Real-time Clock API
547  *---------------------------------------------------------------------------*/
548 typedef s64         OSTime;
549 typedef u32         OSTick;
550 
551 #ifndef WIN32
552 u32 __OSBusClock    AT_ADDRESS(OS_BASE_CACHED | 0x00F8);    // sync with OSLoMem.h
553 u32 __OSCoreClock   AT_ADDRESS(OS_BASE_CACHED | 0x00FC);    // sync with OSLoMem.h
554 #define OS_BUS_CLOCK        __OSBusClock
555 #define OS_CORE_CLOCK       __OSCoreClock
556 #define OS_TIMER_CLOCK      (OS_BUS_CLOCK/4)
557 #endif
558 
559 // Default Configuration
560 #ifndef OS_CORE_CLOCK
561 #define OS_CORE_CLOCK       729000000u
562 #endif
563 #ifndef OS_BUS_CLOCK
564 #define OS_BUS_CLOCK        243000000u
565 #endif
566 #ifndef OS_TIMER_CLOCK
567 #define OS_TIMER_CLOCK      (OS_BUS_CLOCK/4)
568 #endif
569 
570 #define OSTicksToCycles( ticks )        (((ticks) * ((OS_CORE_CLOCK * 2) / OS_TIMER_CLOCK)) / 2)
571 #define OSTicksToSeconds( ticks )       ((ticks) / OS_TIMER_CLOCK)
572 #define OSTicksToMilliseconds( ticks )  ((ticks) / (OS_TIMER_CLOCK / 1000))
573 #define OSTicksToMicroseconds( ticks )  (((ticks) * 8) / (OS_TIMER_CLOCK / 125000))
574 #define OSTicksToNanoseconds( ticks )   (((ticks) * 8000) / (OS_TIMER_CLOCK / 125000))
575 #define OSSecondsToTicks( sec )         ((sec)  * OS_TIMER_CLOCK)
576 #define OSMillisecondsToTicks( msec )   ((msec) * (OS_TIMER_CLOCK / 1000))
577 #define OSMicrosecondsToTicks( usec )   (((usec) * (OS_TIMER_CLOCK / 125000)) / 8)
578 #define OSNanosecondsToTicks( nsec )    (((nsec) * (OS_TIMER_CLOCK / 125000)) / 8000)
579 
580 #define OSDiffTick(tick1, tick0)        ((s32) (tick1) - (s32) (tick0))
581 
582 OSTick  OSGetTick( void );
583 OSTime  OSGetTime( void );
584 
585 typedef struct OSCalendarTime
586 {
587     int sec;    // seconds after the minute [0, 61]
588     int min;    // minutes after the hour [0, 59]
589     int hour;   // hours since midnight [0, 23]
590     int mday;   // day of the month [1, 31]
591     int mon;    // month since January [0, 11]
592     int year;   // years since 0000
593     int wday;   // days since Sunday [0, 6]
594     int yday;   // days since January 1 [0, 365]
595 
596     int msec;   // milliseconds after the second [0,999]
597     int usec;   // microseconds after the millisecond [0,999]
598 } OSCalendarTime;
599 
600 OSTime OSCalendarTimeToTicks( OSCalendarTime* td );
601 void   OSTicksToCalendarTime( OSTime ticks, OSCalendarTime* td );
602 
603 /*---------------------------------------------------------------------------*
604     Stopwatch API
605  *---------------------------------------------------------------------------*/
606 
607 typedef struct OSStopwatch
608 {
609     char*       name;       // name of this stopwatch
610     u32         hits;       // number of times turned on and off
611     OSTime      total;      // total time running
612     OSTime      min;        // smallest time measured
613     OSTime      max;        // longest time measured
614 
615     OSTime      last;       // time at which this sw was last started
616     BOOL        running;    // TRUE if sw is running
617     u32         _padding;
618 } OSStopwatch;
619 
620 
621 void    OSInitStopwatch     ( OSStopwatch* sw, char* name );
622 void    OSStartStopwatch    ( OSStopwatch* sw );
623 void    OSStopStopwatch     ( OSStopwatch* sw );
624 OSTime  OSCheckStopwatch    ( OSStopwatch* sw );
625 void    OSResetStopwatch    ( OSStopwatch* sw );
626 void    OSDumpStopwatch     ( OSStopwatch* sw );
627 
628 /*----------------------------------------------------------------------*
629     Debug API
630  *----------------------------------------------------------------------*/
631 
632 #define OSHalt(msg)             OSPanic(__FILE__, __LINE__, msg)
633 
634 #ifdef _DEBUG
635 
636 #ifndef ASSERT
637 #define ASSERT(exp)                                             \
638     (void) ((exp) ||                                            \
639             (OSPanic(__FILE__, __LINE__, "Failed assertion " #exp), 0))
640 #endif
641 
642 #ifndef ASSERTMSG
643 #if defined(__STDC_VERSION__) && (199901L <= __STDC_VERSION__) || defined(__MWERKS__)  || defined(__SN__)
644 #define ASSERTMSG(exp, ...)                                     \
645     (void) ((exp) ||                                            \
646             (OSPanic(__FILE__, __LINE__, __VA_ARGS__), 0))
647 #else
648 #define ASSERTMSG(exp, msg)                                     \
649     (void) ((exp) ||                                            \
650             (OSPanic(__FILE__, __LINE__, (msg)), 0))
651 #endif
652 #endif
653 
654 #ifndef ASSERTMSG1
655 #define ASSERTMSG1(exp, msg, param1)                            \
656     (void) ((exp) ||                                            \
657             (OSPanic(__FILE__, __LINE__, (msg),                 \
658                      (param1)), 0))
659 #endif
660 
661 #ifndef ASSERTMSG2
662 #define ASSERTMSG2(exp, msg, param1, param2)                    \
663     (void) ((exp) ||                                            \
664             (OSPanic(__FILE__, __LINE__, (msg),                 \
665                      (param1), (param2)), 0))
666 #endif
667 
668 #ifndef ASSERTMSG3
669 #define ASSERTMSG3(exp, msg, param1, param2, param3)            \
670     (void) ((exp) ||                                            \
671             (OSPanic(__FILE__, __LINE__, (msg),                 \
672                      (param1), (param2), (param3)), 0))
673 #endif
674 
675 #ifndef ASSERTMSG4
676 #define ASSERTMSG4(exp, msg, param1, param2, param3, param4)    \
677     (void) ((exp) ||                                            \
678             (OSPanic(__FILE__, __LINE__, (msg),                 \
679                      (param1), (param2), (param3), (param4)), 0))
680 #endif
681 
682 #else   // _DEBUG
683 
684 #ifndef ASSERT
685 #define ASSERT(exp)                                             ((void) 0)
686 #endif
687 
688 #ifndef ASSERTMSG
689 #if defined(__STDC_VERSION__) && (199901L <= __STDC_VERSION__) || defined(__MWERKS__)  || defined(__SN__)
690 #define ASSERTMSG(exp, ...)                                     ((void) 0)
691 #else
692 #define ASSERTMSG(exp, msg)                                     ((void) 0)
693 #endif
694 #endif
695 
696 #ifndef ASSERTMSG1
697 #define ASSERTMSG1(exp, msg, param1)                            ((void) 0)
698 #endif
699 #ifndef ASSERTMSG2
700 #define ASSERTMSG2(exp, msg, param1, param2)                    ((void) 0)
701 #endif
702 #ifndef ASSERTMSG3
703 #define ASSERTMSG3(exp, msg, param1, param2, param3)            ((void) 0)
704 #endif
705 #ifndef ASSERTMSG4
706 #define ASSERTMSG4(exp, msg, param1, param2, param3, param4)    ((void) 0)
707 #endif
708 
709 #endif  // _DEBUG
710 
711 #ifndef __DOLPHIN_RC__
712 
713 void OSPanic  ( const char* file, int line, const char* msg, ... );
714 void OSReport ( const char* msg, ... );
715 void OSVReport( const char* msg, va_list list );
716 void OSFatal  ( GXColor fg, GXColor bg, const char* msg );
717 
718 #endif  // __DOLPHIN_RC__
719 
720 #ifdef __cplusplus
721 }
722 #endif
723 
724 /*---------------------------------------------------------------------------*
725     Get infomation APIs
726  *---------------------------------------------------------------------------*/
727 
728 const char*	OSGetAppGamename(void);
729 const u8	OSGetAppType(void);
730 
731 /*---------------------------------------------------------------------------*
732     Advanced OS APIs
733  *---------------------------------------------------------------------------*/
734 
735 #include <revolution/os/OSAlloc.h>     // Memory allocation routines and defines
736 #include <revolution/os/OSAlarm.h>     // Alarm routines and defines
737 #include <revolution/os/OSCache.h>     // Cache operation APIs
738 #include <revolution/os/OSContext.h>   // Context structures and defines
739 #include <revolution/os/OSError.h>     // Error handler routines and defines
740 #include <revolution/os/OSException.h> // Exception handling routines and defines
741 #include <revolution/os/OSExpansion.h> // Expansion interface APIs
742 #include <revolution/os/OSFastCast.h>  // Fast float/int conversion
743 #include <revolution/os/OSFont.h>      // ROM font routines and defines
744 #include <revolution/os/OSInterrupt.h> // Interrupt handling routines and defines
745 #include <revolution/os/OSMemory.h>    // Memory size and protection APIs
746 #include <revolution/os/OSMessage.h>   // Message routines and defines
747 #include <revolution/os/OSModule.h>    // Relocatable module routines and defines
748 #include <revolution/os/OSMutex.h>     // Mutex routines and defines
749 #include <revolution/os/OSResetSW.h>   // Reset switch APIs
750 #include <revolution/os/OSSerial.h>    // Serial interface APIs
751 #include <revolution/os/OSSemaphore.h> // Semaphore routines and defines
752 #include <revolution/os/OSThread.h>    // Thread routines and defines
753 #include <revolution/os/OSReset.h>     // Reset APIs
754 
755 #endif  // __OS_H__
756