blob: 4227949f51d402cd6bee46d3d026da5661570b72 [file] [log] [blame]
swissChilidefeb0d2021-02-18 15:28:36 -08001#include "timer.h"
swissChilidefeb0d2021-02-18 15:28:36 -08002#include "io.h"
swissChili825d46b2021-02-21 10:14:16 -08003#include "log.h"
4#include "pic.h"
5#include "registers.h"
swissChilidefeb0d2021-02-18 15:28:36 -08006
7static ulong tick = 0;
8
9static void timer_cb(struct registers regs)
10{
swissChili825d46b2021-02-21 10:14:16 -080011 if (++tick % 100 == 0)
12 kprintf("Timer tick %d\n", tick);
swissChilidefeb0d2021-02-18 15:28:36 -080013}
14
15void init_timer(uint hz)
16{
17 add_interrupt_handler(IRQ_TO_INT(0), timer_cb);
18
19 uint divisor = TIMER_FREQ / hz;
20
21 kprintf("Divisor is %d\n", divisor);
22
23 outb(0x43, 0x36);
24 io_wait();
swissChili825d46b2021-02-21 10:14:16 -080025 uchar l = divisor & 0xff, h = (divisor >> 8) & 0xff;
swissChilidefeb0d2021-02-18 15:28:36 -080026
27 outb(0x40, l);
28 io_wait();
29 outb(0x40, h);
30 io_wait();
31
32 kprintf("Initialized timer\n");
33}