1 /*---------------------------------------------------------------------------*
2   Project:  Cafe OS Core
3   File:     OSCore.h
4 
5   Copyright (C) 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  *---------------------------------------------------------------------------*/
14 
15 #ifndef __OSTIME_H__
16 #define __OSTIME_H__
17 
18 #define OSMicrosecondsToNanoseconds(mics) ((mics) * 1000LL)
19 #define OSMillisecondsToNanoseconds(mils) ((mils) * 1000000LL)
20 #define OSSecondsToNanoseconds(secs)      ((secs) * 1000000000LL)
21 
22 #define OSNanosecondsToMicroseconds(nano) ((nano) / 1000LL)
23 #define OSNanosecondsToMilliseconds(nano) ((nano) / 1000000LL)
24 #define OSNanosecondsToSeconds(nano)      ((nano) / 1000000000LL)
25 
26 #ifndef _ASSEMBLER
27 #include <types.h>
28 
29 #ifdef __ghs__
30     #include <ppc_ghs.h>  // for __MFTBR(U)
31 #endif
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 typedef s64 OSTime;
38 typedef s64 OSTimeSeconds;
39 typedef s64 OSTimeMilliseconds;
40 typedef s64 OSTimeMicroseconds;
41 typedef s64 OSTimeNanoseconds;
42 typedef u32 OSTick;
43 
44 #define __OSBusClock    (OSGetSystemInfo()->busClockSpeed)
45 #define __OSCoreClock   (OSGetSystemInfo()->coreClockSpeed)
46 
47 #define OS_BUS_CLOCK        __OSBusClock
48 #define OS_CORE_CLOCK       __OSCoreClock
49 #define OS_TIMER_CLOCK      (OS_BUS_CLOCK/4)
50 
51 #define OSTicksToCycles( ticks )        (((ticks) * ((OS_CORE_CLOCK * 2) / OS_TIMER_CLOCK)) / 2)
52 #define OSTicksToSeconds( ticks )       ((ticks) / OS_TIMER_CLOCK)
53 #define OSSecondsToTicks( sec )         ((sec)  * OS_TIMER_CLOCK)
54 
55 #if LEGACY_TIME_CONVERSIONS
56     #define OSTicksToMilliseconds( ticks )  ((ticks) / (OS_TIMER_CLOCK / 1000))
57     #define OSTicksToMicroseconds( ticks )  (((ticks) * 8) / (OS_TIMER_CLOCK / 125000))
58     #define OSTicksToNanoseconds( ticks )   (((ticks) * 8000) / (OS_TIMER_CLOCK / 125000))
59 
60     #define OSMillisecondsToTicks( msec )   ((msec) * (OS_TIMER_CLOCK / 1000))
61     #define OSMicrosecondsToTicks( usec )   (((usec) * (OS_TIMER_CLOCK / 125000)) / 8)
62     #define OSNanosecondsToTicks( nsec )    (((nsec) * (OS_TIMER_CLOCK / 125000)) / 8000)
63 #else
64     #define OSTicksToMilliseconds( ticks )  ((((u64)(ticks)) * 1000ULL)       / ((u64) OS_TIMER_CLOCK))
65     #define OSTicksToMicroseconds( ticks )  ((((u64)(ticks)) * 1000000ULL)    / ((u64) OS_TIMER_CLOCK))
66     #define OSTicksToNanoseconds( ticks )   ((((u64)(ticks)) * 32000ULL) / ((u64) OS_TIMER_CLOCK / 31250))
67 
68     #define OSMillisecondsToTicks( msec )   ((((u64)(msec)) * OS_TIMER_CLOCK)  / 1000)
69     #define OSMicrosecondsToTicks( usec )   ((((u64)(usec)) * OS_TIMER_CLOCK)  / 1000000)
70     #define OSNanosecondsToTicks( nsec )    ((((u64)(nsec)) * ((u64) OS_TIMER_CLOCK / 31250))  / 32000)
71 #endif
72 
73 
74 
75 #define OSDiffTick(tick1, tick0)        ((s32) (tick1) - (s32) (tick0))
76 
77 OSTick  OSGetTick( void );
78 OSTime  OSGetTime( void );
79 OSTime  OSGetSystemTime( void );
80 
81 typedef struct OSCalendarTime
82 {
83     int sec;    // seconds after the minute [0, 59]
84     int min;    // minutes after the hour [0, 59]
85     int hour;   // hours since midnight [0, 23]
86     int mday;   // day of the month [1, 31]
87     int mon;    // month since January [0, 11]
88     int year;   // years since 0000
89     int wday;   // days since Sunday [0, 6]
90     int yday;   // days since January 1 [0, 365]
91 
92     int msec;   // milliseconds after the second [0,999]
93     int usec;   // microseconds after the millisecond [0,999]
94 } OSCalendarTime;
95 
96 OSTime OSCalendarTimeToTicks( OSCalendarTime* td );
97 void   OSTicksToCalendarTime( OSTime ticks, OSCalendarTime* td );
98 
99 void OSWaitMicroseconds(OSTimeMicroseconds microseconds);
100 
101 #ifdef __ghs__
OSInlineGetSystemTime(void)102 static inline OSTime OSInlineGetSystemTime(void)
103 {
104     u32 timeLower;
105     u32 timeUpper1;
106     u32 timeUpper2;
107 
108     OSTime resultTime;
109 
110     do
111     {
112         timeUpper1 = __MFTBU();
113         timeLower  = __MFTB();
114         timeUpper2 = __MFTBU();
115     } while (timeUpper1 != timeUpper2);
116 
117     resultTime = (((OSTime) timeUpper1) << 32) | ((OSTime) timeLower);
118     return resultTime;
119 }
120 #endif
121 
122 #endif // _ASSEMBLER
123 
124 #ifdef __cplusplus
125 }
126 #endif
127 
128 #endif  // __OSTIME_H__
129 
130