00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "timer.h"
00011 #include "array.h"
00012
00013 long timerTicks = 0;
00014
00015 void timerInit(void) {
00016 consoleOut("timerInit: ");
00017 createGate(&IDT, 0x20, (long)int20h_wrapper, SEL_P0_CODE, D_TRAP);
00018 timerSetFreq(100);
00019 picEnableIRQ(0);
00020 consoleOut("pass\n");
00021 consoleOut("enabling ints: ");
00022 asm("sti");
00023 consoleOut("pass\n");
00024 }
00025
00026 void timerSetFreq(short freq) {
00027 long counter = PIT_FREQ / freq;
00028
00029 outportb(TIMER_CTRL, TIMER0 + TIMER_WHOLE + TIMER_MODE2);
00030 outportb(TIMER0_CNTR, counter % 256);
00031 outportb(TIMER0_CNTR, counter / 256);
00032 return;
00033 }
00034
00035 void timerHandler() {
00036 char *scr = (char *)0x000b8000;
00037
00038
00039 timerTicks++;
00040
00041 outportb (M_PIC, EOI);
00042 timerScheduler();
00043 }
00044
00045 void timerScheduler(void) {
00046 char *desc;
00047
00048
00049 if(firstTask == NULL ||
00050 firstTask->next == NULL) return;
00051
00052
00053 do {
00054 currentTask = (Task *)currentTask->next;
00055 if(currentTask == NULL)
00056 currentTask = (Task *)firstTask;
00057 } while(taskNotBusy(currentTask));
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 asm("movl $0xffffffff, %%eax\n"
00070 "addl $0x1, %%eax\n"
00071 :
00072 :
00073 : "eax");
00074 jumpToTSS(currentTask->selector);
00075 }
00076
00077 long timerGetTicks(void) {
00078 return timerTicks;
00079 }