3a13904f819d0776793194cee9f37f7c474166de
[tpg/acess2.git] / Kernel / arch / x86 / time.c
1 /*
2  * Acess2 Kernel
3  * Timekeeping
4  * arch/x86/time.c
5  */
6 #include <common.h>
7
8 // === MACROS ===
9 #define TIMER_FREQ      1024    //Hz
10 #define MS_PER_TICK_WHOLE       (1000/(TIMER_FREQ))
11 #define MS_PER_TICK_FRACT       ((Uint64)(1000*TIMER_FREQ-((Uint64)MS_PER_TICK_WHOLE)*0x80000000/TIMER_FREQ))
12
13 // === PROTOTYPES ===
14 void    Time_Interrupt();
15
16 // === GLOBALS ===
17 Uint64  giTicks = 0;
18 Sint64  giTimestamp = 0;
19 Uint64  giPartMiliseconds = 0;
20
21 // === CODE ===
22 /**
23  * \fn int Time_Setup()
24  * \brief Sets the system time from the Realtime-Clock
25  */
26 int Time_Setup()
27 {
28         Uint8   val;
29         
30         outb(0x70, inb(0x70)&0x7F);     // Disable NMIs
31         __asm__ __volatile__ ("cli");   // Disable normal interrupts
32         
33         // Enable IRQ8
34         outb(0x70, 0x0B);       // Set the index to register B
35         val = inb(0x71);        // Read the current value of register B
36         outb(0x70, 0x0B);       // Set the index again (a read will reset the index to register D)
37         outb(0x71, val | 0x40); // Write the previous value or'd with 0x40. This turns on bit 6 of register D
38         
39         __asm__ __volatile__ ("sti");   // Disable normal interrupts
40         outb(0x70, inb(0x70)|0x80);     // Disable NMIs
41         
42         // Install IRQ Handler
43         IRQ_AddHandler(8, Time_Interrupt);
44         return 0;
45 }
46
47 /**
48  * \fn void Time_Interrupt()
49  * \brief Called on the timekeeping IRQ
50  */
51 void Time_Interrupt()
52 {
53         giTicks ++;
54         giTimestamp += MS_PER_TICK_WHOLE;
55         giPartMiliseconds += MS_PER_TICK_FRACT;
56         if(giPartMiliseconds > 0x80000000) {
57                 giTimestamp ++;
58                 giPartMiliseconds -= 0x80000000;
59         }
60 }
61
62 /**
63  * \fn Sint64 now()
64  * \brief Return the current timestamp
65  */
66 Sint64 now()
67 {
68         return giTimestamp;
69 }

UCC git Repository :: git.ucc.asn.au