diff --git a/kernel/src/arch/Timer.cpp b/kernel/src/arch/Timer.cpp index e564c492..3530095f 100644 --- a/kernel/src/arch/Timer.cpp +++ b/kernel/src/arch/Timer.cpp @@ -58,7 +58,7 @@ namespace Timer { void tick() { - timer_ticks++; + timer_ticks += ARCH_TIMER_RESOLUTION; } usize raw_ticks() @@ -68,20 +68,17 @@ namespace Timer usize ticks() { - return ticks_ms() / 1000; + return ticks_us() / US_PER_SECOND; } usize ticks_ms() { - return timer_ticks / ARCH_TIMER_FREQ; + return timer_ticks / 1000; } - 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 + usize ticks_us() { - if constexpr (ARCH_TIMER_FREQ > 1000) return timer_ticks / (ARCH_TIMER_FREQ / 1000); - else - return timer_ticks * (1000 / ARCH_TIMER_FREQ); + return timer_ticks; } usize ticks_ns() @@ -91,22 +88,22 @@ namespace Timer usize boot() { - return boot_timestamp; + return boot_timestamp / US_PER_SECOND; } usize boot_ms() { - return boot_timestamp * MS_PER_SECOND; + return boot_timestamp / 1000; } usize boot_us() { - return boot_timestamp * US_PER_SECOND; + return boot_timestamp; } usize boot_ns() { - return boot_timestamp * NS_PER_SECOND; + return boot_timestamp * 1000; } usize clock() @@ -131,14 +128,12 @@ namespace Timer void init() { - boot_timestamp = bootloader_time_to_unix(bootboot.datetime); + boot_timestamp = bootloader_time_to_unix(bootboot.datetime) * US_PER_SECOND; arch_init(); } } -static_assert(IsPowerOfTwo, "ARCH_TIMER_FREQ must be a power of two"); - bool should_invoke_scheduler() { - return (timer_ticks % ARCH_TIMER_FREQ) == 0; + return (timer_ticks % 1000) == 0; } diff --git a/kernel/src/arch/x86_64/Timer.cpp b/kernel/src/arch/x86_64/Timer.cpp index 26e441cc..05aab10b 100644 --- a/kernel/src/arch/x86_64/Timer.cpp +++ b/kernel/src/arch/x86_64/Timer.cpp @@ -7,8 +7,8 @@ const u64 base_frequency = 1193182; void Timer::arch_init() { - constexpr u16 divisor = (u16)(base_frequency / (ARCH_TIMER_FREQ * 1000)); - static_assert(divisor >= 100, "ARCH_TIMER_FREQ is too high"); + constexpr u16 divisor = (u16)(base_frequency / ((1000 / ARCH_TIMER_RESOLUTION) * 1000)); + static_assert(divisor >= 100, "ARCH_TIMER_RESOLUTION is too low"); IO::outb(PIT_CHANNEL_0, (u8)(divisor & 0xFF)); IO::outb(0x80, 0); // short delay IO::outb(PIT_CHANNEL_0, (u8)((divisor & 0xFF00) >> 8)); diff --git a/kernel/src/arch/x86_64/Timer.h b/kernel/src/arch/x86_64/Timer.h index b3955f7c..5619121e 100644 --- a/kernel/src/arch/x86_64/Timer.h +++ b/kernel/src/arch/x86_64/Timer.h @@ -1,4 +1,5 @@ #pragma once #include -const usize ARCH_TIMER_FREQ = 4; +// Every timer tick is equivalent to 250 microseconds. +const usize ARCH_TIMER_RESOLUTION = 250;