1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     os_Tick.h
4   Copyright (C)2009 Nintendo Co., Ltd.  All rights reserved.
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   $Rev: 23609 $
11  *---------------------------------------------------------------------------
12 
13 
14 */
15 
16 /* Please see man pages for details
17 
18 
19 
20 */
21 
22 #ifndef NN_OS_OS_TICK_H_
23 #define NN_OS_OS_TICK_H_
24 
25 #include <nn/svc.h>
26 #include <nn/os/os_HardwareParamsSelect.h>
27 #include <nn/fnd/fnd_TimeSpan.h>
28 #include <nn/math/math_Misccellaneous.h>
29 
30 #ifdef __cplusplus
31 
32 namespace nn { namespace os {
33 
34 /* Please see man pages for details
35 
36 
37 
38 
39 
40 
41 */
42 class Tick
43 {
44 public:
45 
46     /* Please see man pages for details
47 
48 
49 
50 */
m_Tick(tick)51     explicit Tick(s64 tick = 0) : m_Tick(tick) {}
52 
53     /* Please see man pages for details
54 
55 
56 
57 */
58     Tick(nn::fnd::TimeSpan span);
59 
60     /* Please see man pages for details
61 
62     */
s64()63     operator s64() const { return m_Tick; }
64 
65     /* Please see man pages for details
66 
67     */
68     operator nn::fnd::TimeSpan() const;
69 
70     /* Please see man pages for details
71 
72 
73 
74 */
75     nn::fnd::TimeSpan ToTimeSpan() const;
76 
77     /* Please see man pages for details
78 
79 
80 
81 */
82     static Tick GetSystemCurrent();
83 
84     /* Please see man pages for details
85 
86     */
87     static const s64 TICKS_PER_SECOND  = NN_HW_TICKS_PER_SECOND;
88 
89     Tick& operator-=(Tick rhs) { this->m_Tick -= rhs.m_Tick; return *this; }
90     Tick operator-(Tick rhs) const { Tick ret(*this); return ret -= rhs; }
91 
92     Tick& operator+=(Tick rhs) { this->m_Tick += rhs.m_Tick; return *this; }
93     Tick operator+(Tick rhs) const { Tick ret(*this); return ret += rhs; }
94 
95     inline Tick& operator+=(fnd::TimeSpan rhs);
96     Tick operator+(fnd::TimeSpan rhs) const { Tick ret(*this); return ret += rhs; }
97 
98 private:
99     s64 m_Tick;
100 };
101 
102 
GetSystemCurrent()103 inline Tick Tick::GetSystemCurrent()
104 {
105     return Tick(nn::svc::GetSystemTick());
106 }
107 
Tick(nn::fnd::TimeSpan span)108 inline Tick::Tick(nn::fnd::TimeSpan span) : m_Tick(nnmathMultiplyAndDivide(span.GetNanoSeconds(), TICKS_PER_SECOND, 1000 * 1000 * 1000))
109 {
110 }
111 
TimeSpan()112 inline Tick::operator nn::fnd::TimeSpan() const
113 {
114     return nn::fnd::TimeSpan::FromNanoSeconds(nnmathMultiplyAndDivide(m_Tick, 1000 * 1000 * 1000, TICKS_PER_SECOND));
115 }
116 
ToTimeSpan()117 inline nn::fnd::TimeSpan Tick::ToTimeSpan() const
118 {
119     return *this;
120 }
121 
122 inline Tick& Tick::operator+=(fnd::TimeSpan rhs)
123 {
124     const s64 tick = nnmathMultiplyAndDivide(rhs.GetNanoSeconds(), TICKS_PER_SECOND, 1000 * 1000 * 1000);
125     this->m_Tick += tick;
126     return *this;
127 }
128 
129 }}
130 
131 #endif // __cplusplus
132 
133 // C declarations follow
134 
135 #include <nn/util/detail/util_CLibImpl.h>
136 
137 /* Please see man pages for details
138 
139 
140 
141 
142 
143 
144 
145 
146 */
147 
148 /* Please see man pages for details
149 
150 
151 
152 
153 
154 */
nnosTickConvertFromNanoSeconds(s64 ns)155 NN_EXTERN_C inline s64 nnosTickConvertFromNanoSeconds(s64 ns)
156 {
157     return nnmathMultiplyAndDivide(ns, NN_HW_TICKS_PER_SECOND, 1000 * 1000 * 1000);
158 }
159 
160 /* Please see man pages for details
161 
162 
163 
164 
165 
166 */
nnosTickConvertFromMicroSeconds(s64 us)167 NN_EXTERN_C inline s64 nnosTickConvertFromMicroSeconds(s64 us)
168 {
169     return nnmathMultiplyAndDivide(us, NN_HW_TICKS_PER_SECOND, 1000 * 1000);
170 }
171 
172 /* Please see man pages for details
173 
174 
175 
176 
177 
178 */
nnosTickConvertFromMilliSeconds(s64 ms)179 NN_EXTERN_C inline s64 nnosTickConvertFromMilliSeconds(s64  ms)
180 {
181     return nnmathMultiplyAndDivide(ms, NN_HW_TICKS_PER_SECOND, 1000);
182 }
183 
184 /* Please see man pages for details
185 
186 
187 
188 
189 
190 */
nnosTickConvertFromSeconds(s64 s)191 NN_EXTERN_C inline s64 nnosTickConvertFromSeconds(s64 s)
192 {
193     return nnmathMultiplyAndDivide(s, NN_HW_TICKS_PER_SECOND, 1);
194 }
195 
196 /* Please see man pages for details
197 
198 
199 
200 
201 
202 */
nnosTickConvertToNanoSeconds(s64 tick)203 NN_EXTERN_C inline s64 nnosTickConvertToNanoSeconds(s64 tick)
204 {
205     return nnmathMultiplyAndDivide(tick, 1000 * 1000 * 1000, NN_HW_TICKS_PER_SECOND);
206 }
207 
208 /* Please see man pages for details
209 
210 
211 
212 
213 
214 */
nnosTickConvertToMicroSeconds(s64 tick)215 NN_EXTERN_C inline s64 nnosTickConvertToMicroSeconds(s64 tick)
216 {
217     return nnmathMultiplyAndDivide(tick, 1000 * 1000, NN_HW_TICKS_PER_SECOND);
218 }
219 
220 /* Please see man pages for details
221 
222 
223 
224 
225 
226 */
nnosTickConvertToMilliSeconds(s64 tick)227 NN_EXTERN_C inline s64 nnosTickConvertToMilliSeconds(s64 tick)
228 {
229     return nnmathMultiplyAndDivide(tick, 1000, NN_HW_TICKS_PER_SECOND);
230 }
231 
232 /* Please see man pages for details
233 
234 
235 
236 
237 
238 */
nnosTickConvertToSeconds(s64 tick)239 NN_EXTERN_C inline s64 nnosTickConvertToSeconds(s64 tick)
240 {
241     return nnmathMultiplyAndDivide(tick, 1, NN_HW_TICKS_PER_SECOND);
242 }
243 
244 /* Please see man pages for details
245 
246 */
247 NN_EXTERN_C s64 nnosTickGetSystemCurrent(void);
248 
249 /*
250 
251 
252 
253 */
254 
255 #endif /* NN_OS_OS_TICK_H_ */
256