-
Notifications
You must be signed in to change notification settings - Fork 7
/
timer.cpp
57 lines (47 loc) · 1 KB
/
timer.cpp
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
56
57
#include <i86.h>
#include <dos.h>
#include <conio.h>
static void (__interrupt __far *prev_timer_handler)();
static volatile unsigned long timer_ticks;
static unsigned timer_counter;
static unsigned timer_sum;
void __interrupt __far timer_handler()
{
unsigned old_sum = timer_sum;
++timer_ticks;
timer_sum += timer_counter;
if (timer_sum < old_sum) {
_chain_intr(prev_timer_handler);
} else {
outp(0x20, 0x20);
}
}
void timer_setup(unsigned frequency)
{
timer_ticks = 0;
timer_counter = 0x1234DD / frequency;
timer_sum = 0;
prev_timer_handler = _dos_getvect(0x08);
_dos_setvect(0x08, timer_handler);
_disable();
outp(0x43, 0x34);
outp(0x40, timer_counter & 256);
outp(0x40, timer_counter >> 8);
_enable();
}
void timer_shutdown()
{
_disable();
outp(0x43, 0x34);
outp(0x40, 0);
outp(0x40, 0);
_enable();
_dos_setvect(0x08, prev_timer_handler);
}
unsigned long timer_get() {
unsigned long result;
_disable();
result = timer_ticks;
_enable();
return result;
}