1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     os_Tick.h
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  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   $Rev: 47819 $
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 
43 
44 
45 
46 
47 
48 
49 
50 
51 */
52 class Tick
53 {
54 public:
55     /* Please see man pages for details
56 
57     */
58     static const s64 TICKS_PER_SECOND  = NN_HW_TICKS_PER_SECOND;
59 
60 public:
61 
62 //
63 //
64 
65     /* Please see man pages for details
66 
67 
68 
69     */
m_Tick(tick)70     explicit Tick(s64 tick = 0) : m_Tick(tick) {}
71 
72     /* Please see man pages for details
73 
74 
75 
76     */
77     Tick(nn::fnd::TimeSpan span);
78 
79     /* Please see man pages for details
80 
81 
82 
83     */
84     static Tick GetSystemCurrent();
85 
86 //
87 
88 //
89 //
90 
91     /* Please see man pages for details
92 
93     */
s64()94     operator s64() const { return m_Tick; }
95 
96     /* Please see man pages for details
97 
98     */
99     operator nn::fnd::TimeSpan() const;
100 
101     /* Please see man pages for details
102 
103 
104 
105     */
106     nn::fnd::TimeSpan ToTimeSpan() const;
107 
108 //
109 
110 //
111 //
112 
113     //----------------------------------------------------------------------
114     //
115     //
116     //
117     //
118     //----------------------------------------------------------------------
119     Tick& operator-=(Tick rhs);
120 
121     //----------------------------------------------------------------------
122     //
123     //
124     //
125     //
126     //----------------------------------------------------------------------
127     Tick operator-(Tick rhs) const;
128 
129     //----------------------------------------------------------------------
130     //
131     //
132     //
133     //
134     //----------------------------------------------------------------------
135     Tick& operator+=(Tick rhs);
136 
137     //----------------------------------------------------------------------
138     //
139     //
140     //
141     //
142     //----------------------------------------------------------------------
143     Tick operator+(Tick rhs) const;
144 
145     //----------------------------------------------------------------------
146     //
147     //
148     //
149     //
150     //----------------------------------------------------------------------
151     Tick& operator+=(fnd::TimeSpan rhs);
152 
153     //----------------------------------------------------------------------
154     //
155     //
156     //
157     //
158     //----------------------------------------------------------------------
159     Tick operator+(fnd::TimeSpan rhs) const;
160 
161 //
162 
163 //
164 //
165 
166     //----------------------------------------------------------------------
167     //
168     //
169     //
170     //
171     //
172     //
173     //
174     //----------------------------------------------------------------------
175     s64 ToNanoSeconds() const;
176 
177     //----------------------------------------------------------------------
178     //
179     //
180     //
181     //
182     //
183     //
184     //
185     //----------------------------------------------------------------------
186     s64 ToMicroSeconds() const;
187 
188     //----------------------------------------------------------------------
189     //
190     //
191     //
192     //
193     //
194     //
195     //
196     //----------------------------------------------------------------------
197     s64 ToMilliSeconds() const;
198 
199     //----------------------------------------------------------------------
200     //
201     //
202     //
203     //
204     //
205     //
206     //
207     //----------------------------------------------------------------------
208     s64 ToSeconds() const;
209 
210 //
211 
212 private:
213     s64 m_Tick;
214 };
215 
216 
GetSystemCurrent()217 inline Tick Tick::GetSystemCurrent()
218 {
219     return Tick(nn::svc::GetSystemTick());
220 }
221 
Tick(nn::fnd::TimeSpan span)222 inline Tick::Tick(nn::fnd::TimeSpan span)
223     : m_Tick( nnmathMultiplyRate32(
224                 span.GetNanoSeconds(),
225                 math::MakeRate32<TICKS_PER_SECOND, 1000 * 1000 * 1000>::VALUE) )
226 {
227 }
228 
TimeSpan()229 inline Tick::operator nn::fnd::TimeSpan() const
230 {
231     return nn::fnd::TimeSpan::FromNanoSeconds(
232         nnmathMultiplyRate(
233             m_Tick,
234             math::MakeRate<1000 * 1000 * 1000, TICKS_PER_SECOND>::VALUE ));
235 }
236 
ToTimeSpan()237 inline nn::fnd::TimeSpan Tick::ToTimeSpan() const
238 {
239     return *this;
240 }
241 
242 inline Tick& Tick::operator+=(fnd::TimeSpan rhs)
243 {
244     const s64 tick = nnmathMultiplyRate32(
245                         rhs.GetNanoSeconds(),
246                         math::MakeRate32<TICKS_PER_SECOND, 1000 * 1000 * 1000>::VALUE );
247     this->m_Tick += tick;
248     return *this;
249 }
250 
251 inline Tick& Tick::operator-=(Tick rhs)               { this->m_Tick -= rhs.m_Tick; return *this; }
252 inline Tick  Tick::operator-(Tick rhs)          const { Tick ret(*this); return ret -= rhs; }
253 inline Tick& Tick::operator+=(Tick rhs)               { this->m_Tick += rhs.m_Tick; return *this; }
254 inline Tick  Tick::operator+(Tick rhs)          const { Tick ret(*this); return ret += rhs; }
255 inline Tick  Tick::operator+(fnd::TimeSpan rhs) const { Tick ret(*this); return ret += rhs; }
256 
ToNanoSeconds()257 inline s64 Tick::ToNanoSeconds() const
258 {
259     return nnmathMultiplyRate(
260                 m_Tick,
261                 math::MakeRate<1000 * 1000 * 1000, TICKS_PER_SECOND>::VALUE );
262 }
ToMicroSeconds()263 inline s64 Tick::ToMicroSeconds() const
264 {
265     return nnmathMultiplyRate32(
266                 m_Tick,
267                 math::MakeRate32<1000 * 1000, TICKS_PER_SECOND>::VALUE );
268 }
ToMilliSeconds()269 inline s64 Tick::ToMilliSeconds() const
270 {
271     return nnmathMultiplyRate32(
272                 m_Tick,
273                 math::MakeRate32<1000, TICKS_PER_SECOND>::VALUE );
274 }
ToSeconds()275 inline s64 Tick::ToSeconds() const
276 {
277     return nnmathMultiplyRate32(
278                 m_Tick,
279                 math::MakeRate32<1, TICKS_PER_SECOND>::VALUE );
280 }
281 
282 }}
283 
284 #endif // __cplusplus
285 
286 // Below is the C declaration
287 
288 #include <nn/util/detail/util_CLibImpl.h>
289 
290 /* Please see man pages for details
291 
292 
293 
294 
295 
296 
297 
298 
299 */
300 
301 /* Please see man pages for details
302 
303 
304 
305 
306 
307 */
nnosTickConvertFromNanoSeconds(s64 ns)308 NN_EXTERN_C inline s64 nnosTickConvertFromNanoSeconds(s64 ns)
309 {
310     return nnmathMultiplyRate32(ns, nnmathMakeRate32(NN_HW_TICKS_PER_SECOND, 1000 * 1000 * 1000));
311 }
312 
313 /* Please see man pages for details
314 
315 
316 
317 
318 
319 */
nnosTickConvertFromMicroSeconds(s64 us)320 NN_EXTERN_C inline s64 nnosTickConvertFromMicroSeconds(s64 us)
321 {
322     return nnmathMultiplyRate(us, nnmathMakeRate(NN_HW_TICKS_PER_SECOND, 1000 * 1000));
323 }
324 
325 /* Please see man pages for details
326 
327 
328 
329 
330 
331 */
nnosTickConvertFromMilliSeconds(s64 ms)332 NN_EXTERN_C inline s64 nnosTickConvertFromMilliSeconds(s64  ms)
333 {
334     return nnmathMultiplyRate(ms, nnmathMakeRate(NN_HW_TICKS_PER_SECOND, 1000));
335 }
336 
337 /* Please see man pages for details
338 
339 
340 
341 
342 
343 */
nnosTickConvertFromSeconds(s64 s)344 NN_EXTERN_C inline s64 nnosTickConvertFromSeconds(s64 s)
345 {
346     return nnmathMultiplyRate(s, nnmathMakeRate(NN_HW_TICKS_PER_SECOND, 1));
347 }
348 
349 /* Please see man pages for details
350 
351 
352 
353 
354 
355 */
nnosTickConvertToNanoSeconds(s64 tick)356 NN_EXTERN_C inline s64 nnosTickConvertToNanoSeconds(s64 tick)
357 {
358     return nnmathMultiplyRate(tick, nnmathMakeRate(1000 * 1000 * 1000, NN_HW_TICKS_PER_SECOND));
359 }
360 
361 /* Please see man pages for details
362 
363 
364 
365 
366 
367 */
nnosTickConvertToMicroSeconds(s64 tick)368 NN_EXTERN_C inline s64 nnosTickConvertToMicroSeconds(s64 tick)
369 {
370     return nnmathMultiplyRate32(tick, nnmathMakeRate32(1000 * 1000, NN_HW_TICKS_PER_SECOND));
371 }
372 
373 /* Please see man pages for details
374 
375 
376 
377 
378 
379 */
nnosTickConvertToMilliSeconds(s64 tick)380 NN_EXTERN_C inline s64 nnosTickConvertToMilliSeconds(s64 tick)
381 {
382     return nnmathMultiplyRate32(tick, nnmathMakeRate32(1000, NN_HW_TICKS_PER_SECOND));
383 }
384 
385 /* Please see man pages for details
386 
387 
388 
389 
390 
391 */
nnosTickConvertToSeconds(s64 tick)392 NN_EXTERN_C inline s64 nnosTickConvertToSeconds(s64 tick)
393 {
394     return nnmathMultiplyRate32(tick, nnmathMakeRate32(1, NN_HW_TICKS_PER_SECOND));
395 }
396 
397 /* Please see man pages for details
398 
399 */
400 NN_EXTERN_C s64 nnosTickGetSystemCurrent(void);
401 
402 /*
403 
404 
405 
406 */
407 
408 #endif /* NN_OS_OS_TICK_H_ */
409