10 #define TIMER_QUANTUM 100
11 #define TIMER_RATE 13 // (Max: 15, Min: 2) - 15 = 1Hz, 13 = 4Hz, 10 = 1024Hz
12 #define TIMER_FREQ (32768>>TIMER_RATE) //Hz
13 #define MS_PER_TICK_WHOLE (1000/(TIMER_FREQ))
14 #define MS_PER_TICK_FRACT ((Uint64)(1000*TIMER_FREQ-((Uint64)MS_PER_TICK_WHOLE)*0x80000000/TIMER_FREQ))
17 typedef struct sTimer {
19 void (*Callback)(void*);
24 void Time_Interrupt();
25 void Timer_CallTimers();
29 Sint64 giTimestamp = 0;
30 Uint64 giPartMiliseconds = 0;
31 tTimer gTimers[NUM_TIMERS];
35 * \fn int Time_Setup()
36 * \brief Sets the system time from the Realtime-Clock
42 outb(0x70, inb(0x70)&0x7F); // Disable NMIs
43 __asm__ __volatile__ ("cli"); // Disable normal interrupts
45 // Set IRQ8 firing rate
46 outb(0x70, 0x0A); // Set the index to register A
47 val = inb(0x71); // Get the current value of register A
48 outb(0x70, 0x0A); // Reset index to A
51 outb(0x71, val); // Update the timer rate
54 outb(0x70, 0x0B); // Set the index to register B
55 val = inb(0x71); // Read the current value of register B
56 outb(0x70, 0x0B); // Set the index again (a read will reset the index to register D)
57 outb(0x71, val | 0x40); // Write the previous value or'd with 0x40. This turns on bit 6 of register D
59 __asm__ __volatile__ ("sti"); // Disable normal interrupts
60 outb(0x70, inb(0x70)|0x80); // Disable NMIs
62 // Install IRQ Handler
63 IRQ_AddHandler(8, Time_Interrupt);
68 * \fn void Time_Interrupt()
69 * \brief Called on the timekeeping IRQ
74 giTimestamp += MS_PER_TICK_WHOLE;
75 giPartMiliseconds += MS_PER_TICK_FRACT;
76 if(giPartMiliseconds > 0x80000000) {
78 giPartMiliseconds -= 0x80000000;
81 //Log("giTimestamp = %lli", giTimestamp);
85 // Make sure the RTC Fires again
86 outb(0x70, 0x0C); // Select register C
87 inb(0x71); // Just throw away contents.
92 * \fn void Time_TimerThread()
94 void Time_TimerThread()
97 Threads_SetName("TIMER");
99 next = giTimestamp + TIMER_QUANTUM;
102 while(giTimestamp < next) Threads_Yield();
103 next = giTimestamp + TIMER_QUANTUM;
111 * \brief Return the current timestamp
119 * \fn void Timer_CallTimers()
121 void Timer_CallTimers()
124 void (*callback)(void *);
130 if(gTimers[i].Callback == NULL) continue;
131 if(giTimestamp < gTimers[i].FiresAfter) continue;
132 callback = gTimers[i].Callback;
133 gTimers[i].Callback = NULL;
134 callback(gTimers[i].Argument);
139 * \fn int Time_CreateTimer(int Delta, void *Callback, void *Argument)
141 int Time_CreateTimer(int Delta, void *Callback, void *Argument)
145 if(Callback == NULL) return -1;
151 if(gTimers[ret].Callback != NULL) continue;
152 gTimers[ret].Callback = Callback;
153 gTimers[ret].FiresAfter = giTimestamp + Delta;
154 gTimers[ret].Argument = Argument;
155 //Log("Callback = %p", Callback);
156 //Log("Timer %i fires at %lli", ret, gTimers[ret].FiresAfter);
163 * \fn void Time_RemoveTimer(int ID)
165 void Time_RemoveTimer(int ID)
167 if(ID < 0 || ID >= NUM_TIMERS) return;
168 gTimers[ID].Callback = NULL;
172 * \fn void Time_Delay(int Delay)
173 * \brief Delay for a small ammount of time
175 void Time_Delay(int Delay)
177 Sint64 dest = giTimestamp + Delay;
178 while(dest < giTimestamp) Threads_Yield();
183 EXPORT(Time_CreateTimer);
184 EXPORT(Time_RemoveTimer);