timer.c

Commit: d8432d7b Author: jokerz Raw Copy
1
/* ============================================================================
2
 * Pactor64 — PIT Timer Driver (x86-64)
3
 * IRQ0 (INT 0x20), 100 Hz default, monotonic tick counter
4
 * ============================================================================ */
5
#include "../include/pactor64.h"
6
7
/* === Tick Counters === */
8
static volatile uint64_t timer_ticks    = 0;
9
static volatile uint64_t timer_seconds  = 0;
10
11
/* Forward declaration — defined in idt.c */
12
extern void irq_install(int irq, void (*handler)(registers_t *regs));
13
14
/* === Timer ISR (IRQ0) === */
15
static void timer_isr(registers_t *regs) {
16
    (void)regs;
17
    timer_ticks++;
18
    if (timer_ticks % 100 == 0) {
19
        timer_seconds++;
20
    }
21
}
22
23
/* === Initialize PIT at given frequency === */
24
void timer_init(uint32_t hz) {
25
    uint32_t divisor = 1193182 / hz;
26
27
    /* Channel 0, lobyte/hibyte, rate generator (mode 2) */
28
    outb(0x43, 0x34);
29
    outb(0x40, divisor & 0xFF);
30
    outb(0x40, (divisor >> 8) & 0xFF);
31
32
    /* Register our ISR for IRQ0 and unmask it */
33
    irq_install(0x20, timer_isr);
34
    pic_unmask_irq(0);
35
}
36
37
uint64_t timer_get_ticks(void) {
38
    return timer_ticks;
39
}
40
41
uint64_t timer_get_seconds(void) {
42
    return timer_seconds;
43
}
44