f7d02cc10d3f32b1ac5396a7962156b30487f7d6
[tpg/acess2.git] / Kernel / arch / x86 / time.c
1 /*
2  * Acess2 Kernel
3  * Timekeeping
4  * arch/x86/time.c
5  */
6 #include <common.h>
7
8 // === MACROS ===
9 #define NUM_TIMERS      8
10 #define TIMER_QUANTUM   100
11 #define TIMER_FREQ      1024    //Hz
12 #define MS_PER_TICK_WHOLE       (1000/(TIMER_FREQ))
13 #define MS_PER_TICK_FRACT       ((Uint64)(1000*TIMER_FREQ-((Uint64)MS_PER_TICK_WHOLE)*0x80000000/TIMER_FREQ))
14
15 // === TYPEDEFS ===
16 typedef struct sTimer {
17          int    FiresAfter;
18         void    (*Callback)(void*);
19         void    *Argument;
20 } tTimer;
21
22 // === PROTOTYPES ===
23 void    Time_Interrupt();
24 void    Timer_CallTimers();
25
26 // === GLOBALS ===
27 Uint64  giTicks = 0;
28 Sint64  giTimestamp = 0;
29 Uint64  giPartMiliseconds = 0;
30 tTimer  gTimers[NUM_TIMERS];
31
32 // === CODE ===
33 /**
34  * \fn int Time_Setup()
35  * \brief Sets the system time from the Realtime-Clock
36  */
37 int Time_Setup()
38 {
39         Uint8   val;
40         
41         outb(0x70, inb(0x70)&0x7F);     // Disable NMIs
42         __asm__ __volatile__ ("cli");   // Disable normal interrupts
43         
44         // Enable IRQ8
45         outb(0x70, 0x0B);       // Set the index to register B
46         val = inb(0x71);        // Read the current value of register B
47         outb(0x70, 0x0B);       // Set the index again (a read will reset the index to register D)
48         outb(0x71, val | 0x40); // Write the previous value or'd with 0x40. This turns on bit 6 of register D
49         
50         __asm__ __volatile__ ("sti");   // Disable normal interrupts
51         outb(0x70, inb(0x70)|0x80);     // Disable NMIs
52         
53         // Install IRQ Handler
54         IRQ_AddHandler(8, Time_Interrupt);
55         return 0;
56 }
57
58 /**
59  * \fn void Time_Interrupt()
60  * \brief Called on the timekeeping IRQ
61  */
62 void Time_Interrupt()
63 {
64         giTicks ++;
65         giTimestamp += MS_PER_TICK_WHOLE;
66         giPartMiliseconds += MS_PER_TICK_FRACT;
67         if(giPartMiliseconds > 0x80000000) {
68                 giTimestamp ++;
69                 giPartMiliseconds -= 0x80000000;
70         }
71         
72         Timer_CallTimers();
73 }
74
75 /**
76  * \fn void Time_TimerThread()
77  */
78 #if 0
79 void Time_TimerThread()
80 {
81         Sint64  next;
82         Threads_SetName("TIMER");
83         
84         next = giTimestamp + TIMER_QUANTUM;
85         for(;;)
86         {
87                 while(giTimestamp < next)       Threads_Yield();
88                 next = giTimestamp + TIMER_QUANTUM;     
89                 Timer_CallTimers();
90         }
91 }
92 #endif
93
94 /**
95  * \fn Sint64 now()
96  * \brief Return the current timestamp
97  */
98 Sint64 now()
99 {
100         return giTimestamp;
101 }
102
103 /**
104  * \fn void Timer_CallTimers()
105  */
106 void Timer_CallTimers()
107 {
108          int    i;
109         void    (*callback)(void *);
110         
111         for(i = 0;
112                 i < NUM_TIMERS;
113                 i ++)
114         {
115                 if(gTimers[i].Callback == NULL) continue;
116                 if(giTimestamp < gTimers[i].FiresAfter) continue;
117                 callback = gTimers[i].Callback;
118                 gTimers[i].Callback = NULL;
119                 callback(gTimers[i].Argument);
120         }
121 }
122
123 /**
124  * \fn int Time_CreateTimer(int Delta, void *Callback, void *Argument)
125  */
126 int Time_CreateTimer(int Delta, void *Callback, void *Argument)
127 {
128          int    ret;
129         for(ret = 0;
130                 ret < NUM_TIMERS;
131                 ret++)
132         {
133                 if(gTimers[ret].Callback != NULL)       continue;
134                 gTimers[ret].Callback = Callback;
135                 gTimers[ret].FiresAfter = giTimestamp + Delta;
136                 gTimers[ret].Argument = Argument;
137                 return ret;
138         }
139         return -1;
140 }
141
142 /**
143  * \fn void Time_RemoveTimer(int ID)
144  */
145 void Time_RemoveTimer(int ID)
146 {
147         if(ID < 0 || ID >= NUM_TIMERS)  return;
148         gTimers[ID].Callback = NULL;
149 }
150
151 /**
152  * \fn void Time_Delay(int Delay)
153  * \brief Delay for a small ammount of time
154  */
155 void Time_Delay(int Delay)
156 {
157         Sint64  dest = giTimestamp + Delay;
158         while(dest < giTimestamp)       Threads_Yield();
159 }

UCC git Repository :: git.ucc.asn.au