Initial revision
[pintos-anon] / src / devices / timer.c
1 #include "timer.h"
2 #include "debug.h"
3 #include "interrupt.h"
4 #include "io.h"
5   
6 static volatile uint64_t ticks;
7
8 static void
9 irq20_timer (struct intr_args *args UNUSED)
10 {
11   ticks++;
12   intr_yield_on_return ();
13 }
14
15 /* Sets up the 8254 Programmable Interrupt Timer (PIT) to
16    interrupt PIT_FREQ times per second, and registers the
17    corresponding interrupt. */
18 void
19 timer_init (void) 
20 {
21   /* 8254 input frequency divided by TIMER_FREQ, rounded to
22      nearest. */
23   uint16_t count = (1193180 + TIMER_FREQ / 2) / TIMER_FREQ;
24
25   outb (0x43, 0x34);    /* CW: counter 0, LSB then MSB, mode 2, binary. */
26   outb (0x40, count & 0xff);
27   outb (0x40, count >> 8);
28
29   intr_register (0x20, 0, IF_OFF, irq20_timer);
30 }
31
32 uint64_t
33 timer_ticks (void) 
34 {
35   enum if_level old_level = intr_disable ();
36   uint64_t t = ticks;
37   intr_set_level (old_level);
38   return t;
39 }
40
41 uint64_t
42 timer_elapsed (uint64_t then) 
43 {
44   uint64_t now = timer_ticks ();
45   return now > then ? now - then : ((uint64_t) -1 - then) + now + 1;
46 }
47
48 /* Suspends execution for at least DURATION ticks. */
49 void
50 timer_wait_until (uint64_t duration) 
51 {
52   /* FIXME */
53 }