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