-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpit.c
More file actions
46 lines (36 loc) · 833 Bytes
/
pit.c
File metadata and controls
46 lines (36 loc) · 833 Bytes
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
#include "pit.h"
#include "terminal.h"
#include "irq.h"
volatile dword timer_ticks = 0; //volatile extremely important since those values change via interrupts
volatile dword timer_seconds = 0;
volatile byte waiting;
void pit_set_timer_phase(word hz)
{
word divisor = 1193180 / hz;
outb(0x43, 0x36);
outb(0x40, divisor & 0xff);
outb(0x40, divisor >> 8);
}
void timer_handler(struct regs *r)
{
timer_ticks++;
if (timer_ticks % 18 == 0 && waiting)
{
terminal_write_str("One second has passed!\n");
timer_seconds++;
}
}
void init_pit()
{
set_irq_handler(0, timer_handler);
}
void timer_wait(dword seconds)
{
waiting = 1;
timer_seconds = 0;
while (timer_seconds < seconds)
{
asm("nop"); //making sure the loop actually runs :)
}
waiting = 0;
}