Various Changes
[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         Log("giTimestamp = %lli", giTimestamp);
73         
74         Timer_CallTimers();
75 }
76
77 /**
78  * \fn void Time_TimerThread()
79  */
80 #if 0
81 void Time_TimerThread()
82 {
83         Sint64  next;
84         Threads_SetName("TIMER");
85         
86         next = giTimestamp + TIMER_QUANTUM;
87         for(;;)
88         {
89                 while(giTimestamp < next)       Threads_Yield();
90                 next = giTimestamp + TIMER_QUANTUM;     
91                 Timer_CallTimers();
92         }
93 }
94 #endif
95
96 /**
97  * \fn Sint64 now()
98  * \brief Return the current timestamp
99  */
100 Sint64 now()
101 {
102         return giTimestamp;
103 }
104
105 /**
106  * \fn void Timer_CallTimers()
107  */
108 void Timer_CallTimers()
109 {
110          int    i;
111         void    (*callback)(void *);
112         
113         for(i = 0;
114                 i < NUM_TIMERS;
115                 i ++)
116         {
117                 Log("%i", i);
118                 if(gTimers[i].Callback == NULL) continue;
119                 Log("%i - %lli < %lli", i, giTimestamp, gTimers[i].FiresAfter);
120                 if(giTimestamp < gTimers[i].FiresAfter) continue;
121                 callback = gTimers[i].Callback;
122                 gTimers[i].Callback = NULL;
123                 callback(gTimers[i].Argument);
124         }
125 }
126
127 /**
128  * \fn int Time_CreateTimer(int Delta, void *Callback, void *Argument)
129  */
130 int Time_CreateTimer(int Delta, void *Callback, void *Argument)
131 {
132          int    ret;
133         
134         if(Callback == NULL)    return -1;
135         
136         for(ret = 0;
137                 ret < NUM_TIMERS;
138                 ret++)
139         {
140                 if(gTimers[ret].Callback != NULL)       continue;
141                 gTimers[ret].Callback = Callback;
142                 gTimers[ret].FiresAfter = giTimestamp + Delta;
143                 gTimers[ret].Argument = Argument;
144                 Log("Callback = %p", Callback);
145                 Log("Timer %i fires at %lli", ret, gTimers[ret].FiresAfter);
146                 return ret;
147         }
148         return -1;
149 }
150
151 /**
152  * \fn void Time_RemoveTimer(int ID)
153  */
154 void Time_RemoveTimer(int ID)
155 {
156         if(ID < 0 || ID >= NUM_TIMERS)  return;
157         gTimers[ID].Callback = NULL;
158 }
159
160 /**
161  * \fn void Time_Delay(int Delay)
162  * \brief Delay for a small ammount of time
163  */
164 void Time_Delay(int Delay)
165 {
166         Sint64  dest = giTimestamp + Delay;
167         while(dest < giTimestamp)       Threads_Yield();
168 }

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