Initial import from FreeBSD RELENG_4:
[games.git] / sys / boot / pc98 / libpc98 / time.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/boot/pc98/libpc98/time.c,v 1.3.2.1 2000/12/30 12:01:06 nyan Exp $
27  */
28
29 #include <stand.h>
30 #include <btxv86.h>
31 #ifdef PC98
32 #include <machine/cpufunc.h>
33 #endif
34 #include "bootstrap.h"
35 #include "libi386.h"
36
37 /*
38  * Return the time in seconds since the beginning of the day.
39  *
40  * If we pass midnight, don't wrap back to 0.
41  *
42  * XXX uses undocumented BCD support from libstand.
43  */
44
45 time_t
46 time(time_t *t)
47 {
48     static time_t       lasttime, now;
49     int                 hr, minute, sec;
50     
51 #ifdef PC98
52     unsigned char       bios_time[6];
53 #endif
54
55     v86.ctl = 0;
56 #ifdef PC98
57     v86.addr = 0x1c;            /* int 0x1c, function 0 */
58     v86.eax = 0x0000;
59     v86.es  = VTOPSEG(bios_time);
60     v86.ebx = VTOPOFF(bios_time);
61 #else
62     v86.addr = 0x1a;            /* int 0x1a, function 2 */
63     v86.eax = 0x0200;
64 #endif
65     v86int();
66
67 #ifdef PC98
68     hr = bcd2bin(bios_time[3]);
69     minute = bcd2bin(bios_time[4]);
70     sec = bcd2bin(bios_time[5]);
71 #else
72     hr = bcd2bin((v86.ecx & 0xff00) >> 8);      /* hour in %ch */
73     minute = bcd2bin(v86.ecx & 0xff);           /* minute in %cl */
74     sec = bcd2bin((v86.edx & 0xff00) >> 8);     /* second in %dh */
75 #endif
76     
77     now = hr * 3600 + minute * 60 + sec;
78     if (now < lasttime)
79         now += 24 * 3600;
80     lasttime = now;
81     
82     if (t != NULL)
83         *t = now;
84     return(now);
85 }
86
87 /*
88  * Use the BIOS Wait function to pause for (period) microseconds.
89  *
90  * Resolution of this function is variable, but typically around
91  * 1ms.
92  */
93 void
94 delay(int period)
95 {
96 #ifdef PC98
97     int i;
98     period = (period + 500) / 1000;
99     for( ; period != 0 ; period--)
100         for(i=800;i != 0; i--)
101             outb(0x5f,0);       /* wait 600ns */
102 #else
103     v86.ctl = 0;
104     v86.addr = 0x15;            /* int 0x15, function 0x86 */
105     v86.eax = 0x8600;
106     v86.ecx = period >> 16;
107     v86.edx = period & 0xffff;
108     v86int();
109 #endif
110 }