pit.c (696B)
1 #include "oxytoxin.h" 2 3 volatile uint64_t g_ticks = 0; 4 5 extern void irq0_stub(void); 6 7 void pit_handler(void) { 8 g_ticks++; 9 outb(0x20, 0x20); 10 } 11 12 void pit_init(uint32_t freq) { 13 /* calc divisor */ 14 uint32_t divisor = pit_base_freq / freq; 15 16 /* limit */ 17 if (divisor > 0xffff) divisor = 0xffff; 18 if (divisor < 1) divisor = 1; 19 20 /* command byte */ 21 outb(pit_comm, 0x36); 22 /* lsb */ 23 outb(pit_chan0, (uint8_t)(divisor & 0xff)); 24 /* msb */ 25 outb(pit_chan0, (uint8_t)((divisor >> 8) & 0xff)); 26 27 idt_register(0x20, irq0_stub); 28 } 29 30 void pit_sleep(uint32_t ticks_to_wait) { 31 uint64_t target_ticks = g_ticks + ticks_to_wait; 32 while (g_ticks < target_ticks) { 33 asm volatile ("hlt"); 34 } 35 } 36