Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / doscmd / timer.c
1 /*
2 ** No copyright?!
3 */
4
5 #include <sys/cdefs.h>
6 __FBSDID("$FreeBSD: src/usr.bin/doscmd/timer.c,v 1.2.2.2 2002/04/25 11:04:51 tg Exp $");
7
8 #include "doscmd.h"
9
10 static void
11 int08(regcontext_t *REGS __unused)
12 {
13     *(u_long *)&BIOSDATA[0x6c] += 1;    /* ticks since midnight */
14     while (*(u_long *)&BIOSDATA[0x6c] >= 24*60*6*182) {
15         *(u_long *)&BIOSDATA[0x6c] -= 24*60*6*182;
16         BIOSDATA[0x70]++;               /* # times past mn */
17     }
18     /* What is the real BIOS' sequence? */
19     send_eoi();
20     softint(0x1c);
21 }
22
23 static void
24 int1c(regcontext_t *REGS __unused)
25 {
26 }
27
28 unsigned char timer;
29
30 static u_char
31 inb_timer(int port __unused)
32 {
33     return (--timer);
34 }
35
36 void
37 timer_init(void)
38 {
39     u_long              vec;
40     struct itimerval    itv;
41     struct timeval      tv;
42     time_t              tv_sec;
43     struct timezone     tz;
44     struct tm           tm;
45
46     vec = insert_hardint_trampoline();
47     ivec[0x08] = vec;
48     register_callback(vec, int08, "int 08");
49     
50     vec = insert_softint_trampoline();
51     ivec[0x1c] = vec;
52     register_callback(vec, int1c, "int 1c");
53
54     define_input_port_handler(0x42, inb_timer);
55     define_input_port_handler(0x40, inb_timer);
56     
57     /* Initialize time counter BIOS variable. */
58     gettimeofday(&tv, &tz);
59     tv_sec = tv.tv_sec;
60     tm = *localtime(&tv_sec);
61     *(u_long *)&BIOSDATA[0x6c] =
62         (((tm.tm_hour * 60 + tm.tm_min) * 60) + tm.tm_sec) * 182 / 10;
63
64     itv.it_interval.tv_sec = 0;
65     itv.it_interval.tv_usec = 54925;    /* 1193182/65536 times per second */
66     itv.it_value.tv_sec = 0;
67     itv.it_value.tv_usec = 54925;       /* 1193182/65536 times per second */
68     if (! timer_disable)
69         setitimer(ITIMER_REAL, &itv, 0);
70 }