gettimeoftheday, clock_gettime, tick_microsecs, tick_nanosec
Posted: Sun Dec 14, 2014 9:21 am
The functions gettimeoftheday(), clock_gettime(), tick_microsecs, tick_nanosecs in libogc are all buggy.
In this post there is a decription of the issues:
http://devkitpro.org/viewtopic.php?f=3&t=3056
To sum up:
In lwp_watchdog.h the macros tick_microsecs and tick_nanosecs should be defined as:
#define tick_microsecs(ticks) ((((u64)(ticks)*8)/(u64)(TB_TIMER_CLOCK/125))%1000000)
#define tick_nanosecs(ticks) ((((u64)(ticks)*8000)/(u64)(TB_TIMER_CLOCK/125))%1000000000)
This partially fixes gettimeoftheday().
Moreover in timesupp.c the function clock_gettime() should be patched as:
-tp->tv_nsec = tick_to_nanosecs(gettick());
+tp->tv_nsec = tick_nanosecs(gettick());
Anyhow also with these fixes gettimeoftheday() and clock_gettime() are not fully correct.
The result of tv_sec is correct for both, reppresenting the number of seconds since 00:00 hours, Jan 1, 1970 UTC.
tv_nsec in clock_gettime and tv_usec in __libogc_gettod_r should be respectively the number of nanoseconds and microseconds expired in the current second but instead they are values taken from the internal CPU timer which is not aligned with the RTC timer of the WII. I do not know any other solution.
In this post there is a decription of the issues:
http://devkitpro.org/viewtopic.php?f=3&t=3056
To sum up:
In lwp_watchdog.h the macros tick_microsecs and tick_nanosecs should be defined as:
#define tick_microsecs(ticks) ((((u64)(ticks)*8)/(u64)(TB_TIMER_CLOCK/125))%1000000)
#define tick_nanosecs(ticks) ((((u64)(ticks)*8000)/(u64)(TB_TIMER_CLOCK/125))%1000000000)
This partially fixes gettimeoftheday().
Moreover in timesupp.c the function clock_gettime() should be patched as:
-tp->tv_nsec = tick_to_nanosecs(gettick());
+tp->tv_nsec = tick_nanosecs(gettick());
Anyhow also with these fixes gettimeoftheday() and clock_gettime() are not fully correct.
The result of tv_sec is correct for both, reppresenting the number of seconds since 00:00 hours, Jan 1, 1970 UTC.
tv_nsec in clock_gettime and tv_usec in __libogc_gettod_r should be respectively the number of nanoseconds and microseconds expired in the current second but instead they are values taken from the internal CPU timer which is not aligned with the RTC timer of the WII. I do not know any other solution.