#include "pit.hpp" #include "idt.hpp" #include "portio.hpp" #include "tasking.hpp" static volatile std::uint32_t ticks = 0; static void timer_callback(const Registers& regs) { ticks = ticks + 1; schedule(const_cast(regs)); } void pit_initialize(std::uint32_t frequency) { // Firstly, register our timer callback. idt_register_callback(32, timer_callback); // The value we send to the PIT is the value to divide it's input clock // (1193180 Hz) by, to get our required frequency. Important to note is // that the divisor must be small enough to fit into 16-bits. auto divisor = 1193180 / frequency; // Send the command byte. outb(0x43, 0x36); // Send the frequency divisor. outb(0x40, divisor & 0xFF); outb(0x40, (divisor >> 8) & 0xFF); } void pit_busy_wait(std::int32_t tks) { const auto end = ticks + tks; while (end - ticks > 0) asm volatile("nop"); }