-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpit.c
57 lines (40 loc) · 1003 Bytes
/
pit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <video.h>
#include <asm.h>
#include <types.h>
int
pit_setup(){
/* setting counter 1, 1ms = 0x4a9 ticks */
outb((1 << 6) | (3 << 4) | (2 << 1), 0x43);
outb(0xa9, 0x41);
outb(0x04, 0x41);
return 0;
}
void
pit_delay(uint32 ms){
uint16 prev, diff;
uint8 low, high;
outb((1 << 6), 0x43);
inb(0x41, low);
inb(0x41, high);
prev = ((high & 0xff) << 8) | (low & 0xff);
while(ms>0){
do{
outb((1 << 6), 0x43);
inb(0x41, low);
inb(0x41, high);
diff = prev - (((high & 0xff) << 8) | (low & 0xff));
if(diff <= 0)
diff += 0x4a9;
}while(diff <= 0x254);
/* half ms */
do{
outb((1 << 6), 0x43);
inb(0x41, low);
inb(0x41, high);
diff = prev - (((high & 0xff) << 8) | (low & 0xff));
if(diff <= 0)
diff += 0x4a9;
}while(diff >= 0x254);
ms--;
}
}