7 #include <arch_config.h>
10 #define TIMER_QUANTUM 100
11 #define TIMER_FREQ PIT_TIMER_BASE_N/(PIT_TIMER_BASE_D*PIT_TIMER_DIVISOR)
12 #define MS_PER_TICK_WHOLE (1000*(PIT_TIMER_BASE_D*PIT_TIMER_DIVISOR)/PIT_TIMER_BASE_N)
13 #define MS_PER_TICK_FRACT ((0x80000000ULL*1000ULL*PIT_TIMER_BASE_D*PIT_TIMER_DIVISOR/PIT_TIMER_BASE_N)&0x7FFFFFFF)
16 extern volatile Sint64 giTimestamp;
17 extern volatile Uint64 giTicks;
18 extern volatile Uint64 giPartMiliseconds;
19 extern void Timer_CallTimers(void);
22 volatile Uint64 giTime_TSCAtLastTick = 0;
23 volatile Uint64 giTime_TSCPerTick = 0;
28 void Time_UpdateTimestamp(void);
29 Uint64 Time_ReadTSC(void);
34 * \brief Return the current timestamp
38 Uint64 tsc = Time_ReadTSC();
39 tsc -= giTime_TSCAtLastTick;
40 tsc *= MS_PER_TICK_WHOLE;
41 if( giTime_TSCPerTick ) {
42 tsc /= giTime_TSCPerTick;
46 return giTimestamp + tsc;
50 * \fn int Time_Setup(void)
51 * \brief Sets the system time from the Realtime-Clock
55 Log_Log("Timer", "PIT Timer firing at %iHz, %i.0x%08x miliseconds per tick",
56 TIMER_FREQ, MS_PER_TICK_WHOLE, MS_PER_TICK_FRACT);
58 // TODO: Read time from RTC
64 * \brief Called on the timekeeping IRQ
66 void Time_UpdateTimestamp(void)
68 Uint64 curTSC = Time_ReadTSC();
70 if( giTime_TSCAtLastTick )
72 giTime_TSCPerTick = curTSC - giTime_TSCAtLastTick;
74 giTime_TSCAtLastTick = curTSC;
77 giTimestamp += MS_PER_TICK_WHOLE;
78 giPartMiliseconds += MS_PER_TICK_FRACT;
79 if(giPartMiliseconds > 0x80000000) {
81 giPartMiliseconds -= 0x80000000;
89 * \fn void Time_TimerThread(void)
91 void Time_TimerThread(void)
94 Threads_SetName("TIMER");
96 next = giTimestamp + TIMER_QUANTUM;
99 while(giTimestamp < next) Threads_Yield();
100 next = giTimestamp + TIMER_QUANTUM;
106 Uint64 Time_ReadTSC(void)
109 __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
110 return ((Uint64)d << 32) | a;