swissChili | defeb0d | 2021-02-18 15:28:36 -0800 | [diff] [blame] | 1 | #include "timer.h" |
swissChili | defeb0d | 2021-02-18 15:28:36 -0800 | [diff] [blame] | 2 | #include "io.h" |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame^] | 3 | #include "log.h" |
| 4 | #include "pic.h" |
| 5 | #include "registers.h" |
swissChili | defeb0d | 2021-02-18 15:28:36 -0800 | [diff] [blame] | 6 | |
| 7 | static ulong tick = 0; |
| 8 | |
| 9 | static void timer_cb(struct registers regs) |
| 10 | { |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame^] | 11 | if (++tick % 100 == 0) |
| 12 | kprintf("Timer tick %d\n", tick); |
swissChili | defeb0d | 2021-02-18 15:28:36 -0800 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | void 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(); |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame^] | 25 | uchar l = divisor & 0xff, h = (divisor >> 8) & 0xff; |
swissChili | defeb0d | 2021-02-18 15:28:36 -0800 | [diff] [blame] | 26 | |
| 27 | outb(0x40, l); |
| 28 | io_wait(); |
| 29 | outb(0x40, h); |
| 30 | io_wait(); |
| 31 | |
| 32 | kprintf("Initialized timer\n"); |
| 33 | } |