From dbf04ee24f71c83d2ba73013ae530df3f3d028e3 Mon Sep 17 00:00:00 2001 From: "John Hodge (sonata)" Date: Wed, 8 May 2013 14:52:36 +0800 Subject: [PATCH] Kernel/x86 - Untested MicroSleep function --- KernelLand/Kernel/arch/x86/time.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/KernelLand/Kernel/arch/x86/time.c b/KernelLand/Kernel/arch/x86/time.c index 9b59b30b..c79d4593 100644 --- a/KernelLand/Kernel/arch/x86/time.c +++ b/KernelLand/Kernel/arch/x86/time.c @@ -15,6 +15,7 @@ #define TIMER_FREQ (0x8000>>TIMER_RATE) //Hz #define MS_PER_TICK_WHOLE (1000/(TIMER_FREQ)) #define MS_PER_TICK_FRACT ((0x80000000*(1000%TIMER_FREQ))/TIMER_FREQ) +#define US_PER_TICK (1000*1000/(TIMER_FREQ)) // === IMPORTS === extern volatile Sint64 giTimestamp; @@ -119,6 +120,21 @@ void Time_Interrupt(int IRQ, void *Ptr) inb(0x71); // Just throw away contents. } +void Time_MicroSleep(Uint16 Microsecs) // max 64 ms +{ + Uint64 cur_tsc = Time_ReadTSC(); + // tsc_per_us * Microsec + Uint64 delta_tsc = (Uint64)Microsecs * giTime_TSCPerTick / US_PER_TICK; + Uint64 tgt_tsc = cur_tsc + delta_tsc; + + if( tgt_tsc < cur_tsc ) + while(Time_ReadTSC() > cur_tsc) + ; + + while( Time_ReadTSC() < tgt_tsc ) + ; +} + Uint64 Time_ReadTSC(void) { Uint32 a, d; -- 2.20.1