Make thunix can be run on a old bochs
[thunix.git] / kernel / timer.c
blobf742fadd4c37098bca30450c7532643b64237d19
1 #include <asm/io.h>
2 #include <asm/system.h>
3 #include <timer.h>
4 #include <types.h>
5 #include <thunix.h>
7 volatile unsigned long timer_ticks = 0;
8 volatile unsigned long count_down = 0;
10 extern timer_interrupt(void);
12 //extern current_DOR;
14 #define TIME_REQUESTS 64
16 struct timer_list {
17 unsigned long timer_ticks;
18 void (*fn) ();
19 struct timer_list *next;
20 } timer_list[TIME_REQUESTS], * next_timer = NULL;
24 void add_timer( unsigned long timer_ticks, void (*fn)(void))
26 struct timer_list *p;
28 if (!fn)
29 return ;
30 cli();
31 if (timer_ticks <= 0)
32 (fn)();
33 else {
34 for (p = timer_list; p < timer_list + TIME_REQUESTS ; p ++)
35 if (!p->fn)
36 break;
37 if (p >= timer_list + TIME_REQUESTS)
38 panic("No more time requests free");
39 p->fn = fn;
40 p->timer_ticks = timer_ticks;
41 next_timer = p;
42 while (p->next && p->timer_ticks > p->next->timer_ticks) {
43 p->timer_ticks -= p->next->timer_ticks;
44 fn = p->fn;
45 p->fn = p->next->fn;
46 p->next->fn = fn;
48 timer_ticks = p->timer_ticks;
49 p->timer_ticks = p->next->timer_ticks;
50 p->next->timer_ticks = timer_ticks;
51 p = p->next;
54 sti();
58 /*
59 * Yeah, we are implenmenting our SLEEP function,
61 * Hope it works well cause i just know the folloing
62 * ways to implenment it by now. And you konw, it's
63 * very simple!
66 void sleep(unsigned long sleep_value)
68 unsigned long now_ticks = timer_ticks;
69 do{
71 }while ( timer_ticks < now_ticks + sleep_value);
76 void do_timer(void)
80 * Yeah, we also implenment a COUNT_DOWN here.
81 * it's sounds like we have lots things well.
84 if (count_down)
85 count_down --;
88 if (next_timer) {
89 next_timer->timer_ticks --;
90 while (next_timer && next_timer->timer_ticks <=0) {
91 void (*fn) (void);
93 fn = next_timer->fn;
94 next_timer->fn = NULL;
95 next_timer = next_timer->next;
96 (fn)();
100 //if (current_DOR & 0xf0)
101 // do_floppy_timer();
104 void timer_init(int hz)
106 unsigned int divisor = 1193180/hz;
107 outb(0x36, 0x43);
108 outb(divisor&0xff, 0x40);
109 outb(divisor>>8, 0x40);
110 set_trap_gate(0x20,timer_interrupt);
111 outb(inb(0x21)&0xfe, 0x21);