Main Page | Modules | File List | File Members

timer.c

00001 /* ndk - [ timer.c ]
00002  *
00003  * Contains code to initialize and control
00004  * the x86's programmable interval timer.
00005  *
00006  * (c)2002 dcipher / neuraldk
00007  *           www.neuraldk.org
00008  */
00009 
00010 #include "timer.h"
00011 #include "array.h" // just a test
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);  // 100 times per second
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, /*0x43*/TIMER0 + TIMER_WHOLE + TIMER_MODE2);
00030   outportb(TIMER0_CNTR, counter % 256);  // send LSB
00031   outportb(TIMER0_CNTR, counter / 256);  // send MSB
00032   return;
00033 }
00034 
00035 void timerHandler() {
00036   char *scr = (char *)0x000b8000;
00037 
00038   // VIDINC scr[0]++;
00039   timerTicks++;
00040 
00041   outportb (M_PIC, EOI);
00042   timerScheduler();
00043 }
00044 
00045 void timerScheduler(void) {
00046   char *desc;
00047 
00048   /* if there's 0 or 1 task, no need to cycle */
00049   if(firstTask == NULL ||
00050      firstTask->next == NULL) return;
00051 
00052   /* standard round robin scheduler */
00053   do {
00054     currentTask = (Task *)currentTask->next;
00055     if(currentTask == NULL)
00056       currentTask = (Task *)firstTask;
00057   } while(taskNotBusy(currentTask));
00058 
00059   /* check to see if current task's busy bit is set (just curious... */
00060   /*
00061   desc = (char *)arrayElementAt(&GDT, currentTask->selector / 8);
00062   desc+=5;
00063   if(*desc & 2) consoleOut("BUSY!\n");
00064   else consoleOut("N/B\n");
00065    */
00066 
00067   //consoleOut("jmp to %x\n", currentTask->selector);
00068   // this is just a test... regarding my taskGate!!!
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 }

Generated on Sun Nov 21 18:26:11 2004 for ndk by doxygen 1.3.2