Skip to content

Commit

Permalink
port: use gettimeofday() as timing source; use smaller tick multiplie…
Browse files Browse the repository at this point in the history
…r to avoid overflows
  • Loading branch information
fgsfdsfgs committed Aug 24, 2023
1 parent caaf823 commit 87529f3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
4 changes: 2 additions & 2 deletions port/include/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <PR/ultratypes.h>

u64 sysGetTicks(void);
u64 sysGetTicksPerSecond(void);
void sysInitTicks(void);
u64 sysGetMicroseconds(void);

void sysFatalError(const char *fmt, ...) __attribute__((noreturn));

Expand Down
5 changes: 4 additions & 1 deletion port/src/libultra.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#define EEPROM_FNAME "./eeprom.bin"
#define OS_COUNTER_RATE 46875000ULL

#define OS_COUNTER_NUM (OS_COUNTER_RATE / 1000ULL)
#define OS_COUNTER_DEN (1000000ULL / 1000ULL)

u64 osClockRate = OS_CLOCK_RATE;
u32 osMemSize = 16 * 1024 * 1024; /* expansion pak installed plus some extra */
s32 osTvType = OS_TV_NTSC; /* 0 = PAL, 1 = NTSC, 2 = MPAL */
Expand All @@ -26,7 +29,7 @@ s32 osViClock = VI_NTSC_CLOCK;
OSTime osGetTime(void)
{
// u64 should be enough to last a while
return (sysGetTicks() * OS_COUNTER_RATE) / sysGetTicksPerSecond();
return (sysGetMicroseconds() * OS_COUNTER_NUM) / OS_COUNTER_DEN;
}

u32 osGetCount(void)
Expand Down
1 change: 1 addition & 0 deletions port/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ static void cleanup(void)

int main(int argc, const char **argv)
{
sysInitTicks();
fsInit();
configInit();
videoInit();
Expand Down
26 changes: 9 additions & 17 deletions port/src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,25 @@
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <SDL.h>
#include <PR/ultratypes.h>
#include "system.h"

static u64 startTick = 0;
static u64 tickFreq = 0;
#define USEC_IN_SEC 1000000ULL

static void sysInitTicks(void)
{
tickFreq = SDL_GetPerformanceFrequency();
startTick = SDL_GetPerformanceCounter();
}
static u64 startTick = 0;

u64 sysGetTicks(void)
void sysInitTicks(void)
{
if (tickFreq == 0) {
sysInitTicks();
}
return (SDL_GetPerformanceCounter() - startTick);
startTick = sysGetMicroseconds();
}

u64 sysGetTicksPerSecond(void)
u64 sysGetMicroseconds(void)
{
if (tickFreq == 0) {
sysInitTicks();
}
return tickFreq;
struct timeval tv;
gettimeofday(&tv, NULL);
return ((u64)tv.tv_sec * USEC_IN_SEC + (u64)tv.tv_usec) - startTick;
}

void sysFatalError(const char *fmt, ...)
Expand Down

0 comments on commit 87529f3

Please sign in to comment.