10 #define TIMER_IRQ 0 // the IRQ the PIT is connected to
11 #define TIMER_FREQ 1000 // frequency to set the timer to
12 #define CTL 0x43 // control register
13 #define CNT0 0x40 // count register 0
14 #define CNT1 0x41 // count register 1
15 #define CNT2 0x42 // count register 2
17 static tick_t tick
= 0; // current tick number
18 static unsigned long freq
; // current timer frequency
25 static void timer_interrupt(int vector
, struct interrupt_stack
*is
)
31 {if(MULTITASKING
){switch_task(is
);}}
34 void set_frequency(unsigned long new_freq
)
36 unsigned short divisor
;
37 divisor
= 1193180 / new_freq
;
38 freq
= (1193180LL << 32) / divisor
;
41 outb(CNT0
, divisor
>> 8);
44 // timer initialization
48 // Timer's base frequency is 1,193,180 Hz
49 // We set the frequency divider to get
50 // TIMER_FREQ Hz output
52 outb(CNT1
, 18); // LSB only clock divisor (?)
54 interrupt_set_handler(irq_to_int(TIMER_IRQ
), timer_interrupt
);
55 set_frequency(TIMER_FREQ
);
56 unmask_irq(TIMER_IRQ
);
66 uint64_t read_cpu_timestamp(void)
69 __asm__
__volatile__("rdtsc;":"=d"(hi
),"=a"(lo
)::"memory");
70 return (uint64_t)(((uint64_t)hi
<< 32) | lo
);