blob: 0e52d0ef4264109c39a33d9e68149ebf1ac0bbda [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"
swissChilief97c262021-04-04 10:20:21 -07006#include "task.h"
swissChilidefeb0d2021-02-18 15:28:36 -08007
8static ulong tick = 0;
9
swissChili19ef4182021-02-21 17:45:51 -080010static void timer_cb(struct registers *regs)
swissChilidefeb0d2021-02-18 15:28:36 -080011{
swissChiliaed6ff32021-05-29 17:51:04 -070012 if (tasks_initialized)
13 {
14 // Preemptive multitasking!
swissChili1e8b7562021-12-22 21:22:57 -080015 switch_task(*regs);
swissChiliaed6ff32021-05-29 17:51:04 -070016 }
swissChilidefeb0d2021-02-18 15:28:36 -080017}
18
19void init_timer(uint hz)
20{
21 add_interrupt_handler(IRQ_TO_INT(0), timer_cb);
22
23 uint divisor = TIMER_FREQ / hz;
24
swissChilidefeb0d2021-02-18 15:28:36 -080025 outb(0x43, 0x36);
swissChili825d46b2021-02-21 10:14:16 -080026 uchar l = divisor & 0xff, h = (divisor >> 8) & 0xff;
swissChilidefeb0d2021-02-18 15:28:36 -080027
28 outb(0x40, l);
swissChilidefeb0d2021-02-18 15:28:36 -080029 outb(0x40, h);
swissChilidefeb0d2021-02-18 15:28:36 -080030}