80 lines
1.3 KiB
C++
80 lines
1.3 KiB
C++
|
#include "arch/Timer.h"
|
||
|
|
||
|
static u64 timer_ticks = 0;
|
||
|
static u64 boot_timestamp;
|
||
|
|
||
|
namespace Timer
|
||
|
{
|
||
|
void tick()
|
||
|
{
|
||
|
timer_ticks++;
|
||
|
}
|
||
|
|
||
|
usize ticks()
|
||
|
{
|
||
|
return ticks_ms() / 1000;
|
||
|
}
|
||
|
|
||
|
usize ticks_ms()
|
||
|
{
|
||
|
return timer_ticks / ARCH_TIMER_FREQ;
|
||
|
}
|
||
|
|
||
|
usize ticks_us() // We want a bit of precision; if there are 10 ticks/ms, do not return the truncated ms value *
|
||
|
// 1000, but ticks * 100 (1000/10), which is more precise
|
||
|
{
|
||
|
if (ARCH_TIMER_FREQ > 1000) [[unlikely]]
|
||
|
return timer_ticks / (ARCH_TIMER_FREQ / 1000);
|
||
|
return timer_ticks * (1000 / ARCH_TIMER_FREQ);
|
||
|
}
|
||
|
|
||
|
usize ticks_ns()
|
||
|
{
|
||
|
return ticks_us() * 1000;
|
||
|
}
|
||
|
|
||
|
usize boot()
|
||
|
{
|
||
|
return boot_timestamp;
|
||
|
}
|
||
|
|
||
|
usize boot_ms()
|
||
|
{
|
||
|
return boot_timestamp * MS_PER_SECOND;
|
||
|
}
|
||
|
|
||
|
usize boot_us()
|
||
|
{
|
||
|
return boot_timestamp * US_PER_SECOND;
|
||
|
}
|
||
|
|
||
|
usize boot_ns()
|
||
|
{
|
||
|
return boot_timestamp * NS_PER_SECOND;
|
||
|
}
|
||
|
|
||
|
usize clock()
|
||
|
{
|
||
|
return boot() + ticks();
|
||
|
}
|
||
|
|
||
|
usize clock_ms()
|
||
|
{
|
||
|
return boot_ms() + ticks_ms();
|
||
|
}
|
||
|
|
||
|
usize clock_us()
|
||
|
{
|
||
|
return boot_us() + ticks_us();
|
||
|
}
|
||
|
|
||
|
usize clock_ns()
|
||
|
{
|
||
|
return boot_ns() + ticks_ns();
|
||
|
}
|
||
|
|
||
|
void init()
|
||
|
{
|
||
|
arch_init();
|
||
|
}
|
||
|
}
|