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