MushOS  0.1
A UNIX-like OS prototype, written from scratch
Loading...
Searching...
No Matches
timer.c
1#include "timer.h"
2
3#include "../drivers/ports_io.h"
4
5#include "interruption_tables.h"
6#include "interruptions.h"
7#include "task.h"
8
9
10static u_dword tick = 0;
11
12static void timer_callback(registers* regs) {
13 tick++;
14 switch_task();
15}
16
17void init_timer(u_dword frequency) {
18 // Firstly, register our timer callback.
19 set_interrupt_handler(IRQ0, &timer_callback);
20
21 // The value we send to the PIT is the value to divide it's input clock
22 // (1193180 Hz) by, to get our required frequency. Important to note is
23 // that the divisor must be small enough to fit into 16-bits.
24 u_dword divisor = 1193180 / frequency;
25
26 // Send the command byte.
27 port_byte_out(0x43, 0x36);
28
29 // Divisor has to be sent byte-wise, so split here into upper/lower bytes.
30 u_byte l = (u_byte)(divisor & 0xFF);
31 u_byte h = (u_byte)( (divisor>>8) & 0xFF );
32
33 // Send the frequency divisor.
34 port_byte_out(0x40, l);
35 port_byte_out(0x40, h);
36}