Set TFS_DIR by default when the TFS_DIR env is not set
[thunix.git] / kernel / timer.c
blob9edea0f83b475029ee0ee28e48785d5a606046ab
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;
11 //extern current_DOR;
13 #define TIME_REQUESTS 64
15 struct timer_list {
16 unsigned long timer_ticks;
17 void (*fn) ();
18 struct timer_list *next;
19 } timer_list[TIME_REQUESTS], * next_timer = NULL;
23 void add_timer( unsigned long timer_ticks, void (*fn)(void))
25 struct timer_list *p;
27 if (!fn)
28 return ;
29 cli();
30 if (timer_ticks <= 0)
31 (fn)();
32 else {
33 for (p = timer_list; p < timer_list + TIME_REQUESTS ; p ++)
34 if (!p->fn)
35 break;
36 if (p >= timer_list + TIME_REQUESTS)
37 panic("No more time requests free");
38 p->fn = fn;
39 p->timer_ticks = timer_ticks;
40 next_timer = p;
41 while (p->next && p->timer_ticks > p->next->timer_ticks) {
42 p->timer_ticks -= p->next->timer_ticks;
43 fn = p->fn;
44 p->fn = p->next->fn;
45 p->next->fn = fn;
47 timer_ticks = p->timer_ticks;
48 p->timer_ticks = p->next->timer_ticks;
49 p->next->timer_ticks = timer_ticks;
50 p = p->next;
53 sti();
57 /*
58 * Yeah, we are implenmenting our SLEEP function,
60 * Hope it works well cause i just know the folloing
61 * ways to implenment it by now. And you konw, it's
62 * very simple!
65 void sleep(unsigned long sleep_value)
67 unsigned long now_ticks = timer_ticks;
68 do{
70 }while ( timer_ticks < now_ticks + sleep_value);
75 void do_timer(void)
79 * Yeah, we also implenment a COUNT_DOWN here.
80 * it's sounds like we have lots things well.
83 if (count_down)
84 count_down --;
87 if (next_timer) {
88 next_timer->timer_ticks --;
89 while (next_timer && next_timer->timer_ticks <=0) {
90 void (*fn) (void);
92 fn = next_timer->fn;
93 next_timer->fn = NULL;
94 next_timer = next_timer->next;
95 (fn)();
99 //if (current_DOR & 0xf0)
100 // do_floppy_timer();
103 void timer_init(int hz)
105 unsigned int divisor = 1193180/hz;
106 outb(0x36, 0x43);
107 outb(divisor&0xff, 0x40);
108 outb(divisor>>8, 0x40);
109 set_trap_gate(0x20,timer_interrupt);
110 outb(inb(0x21)&0xfe, 0x21);