fix what is apparently a long standing bug in the floppy bootloader that relied on...
[newos.git] / kernel / time.c
blob8b311a53ee87a1cd44f021223d9ef6818e2f3052
1 /*
2 ** Copyright 2003, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 #include <kernel/kernel.h>
6 #include <kernel/debug.h>
7 #include <kernel/time.h>
8 #include <kernel/arch/time.h>
9 #include <string.h>
11 // The 'rough' counter of system up time, accurate to the rate at
12 // which the timer interrupts run (see TICK_RATE in timer.c).
13 static bigtime_t sys_time;
15 // The delta of usecs that needs to be added to sys_time to get real time
16 static bigtime_t real_time_delta;
18 // Any additional delta that might need to be added to the above two values
19 // to get real time. (in case the RTC isn't in UTC)
20 static bigtime_t tz_delta;
22 // The current name of the timezone, saved for all to see
23 static char tz_name[SYS_MAX_NAME_LEN];
25 int time_init(kernel_args *ka)
27 dprintf("time_init: entry\n");
29 sys_time = 0;
30 real_time_delta = 0;
31 tz_delta = 0;
32 strcpy(tz_name, "UTC");
34 return arch_time_init(ka);
37 int time_init2(kernel_args *ka)
39 real_time_delta = arch_get_rtc_delta();
41 return 0;
44 void time_tick(int tick_rate)
46 sys_time += tick_rate;
47 arch_time_tick();
50 bigtime_t system_time(void)
52 volatile bigtime_t *st = &sys_time;
53 bigtime_t val;
55 retry:
56 // read the system time, make sure we didn't get a partial read
57 val = sys_time;
58 if(val > *st)
59 goto retry;
61 // ask the architectural specific layer to give us a little better precision if it can
62 val += arch_get_time_delta();
64 return val;
67 bigtime_t system_time_lores(void)
69 volatile bigtime_t *st = &sys_time;
70 bigtime_t val;
72 retry:
73 // read the system time, make sure we didn't get a partial read
74 val = sys_time;
75 if(val > *st)
76 goto retry;
78 return val;
81 bigtime_t local_time(void)
83 return system_time() + real_time_delta + tz_delta;