/*-1* Copyright (c) 1998 Michael Smith <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <stand.h>27#include <btxv86.h>28#include "bootstrap.h"29#include "libi386.h"3031time_t getsecs(void);32static int bios_seconds(void);3334/*35* Return the BIOS time-of-day value.36*37* XXX uses undocumented BCD support from libsa.38*/39static int40bios_seconds(void)41{42int hr, minute, sec;4344v86.ctl = 0;45v86.addr = 0x1a; /* int 0x1a, function 2 */46v86.eax = 0x0200;47v86int();4849hr = bcd2bin((v86.ecx & 0xff00) >> 8); /* hour in %ch */50minute = bcd2bin(v86.ecx & 0xff); /* minute in %cl */51sec = bcd2bin((v86.edx & 0xff00) >> 8); /* second in %dh */5253return (hr * 3600 + minute * 60 + sec);54}5556/*57* Return the time in seconds since the beginning of the day.58*59* Some BIOSes (notably qemu) don't correctly read the RTC60* registers in an atomic way, sometimes returning bogus values.61* Therefore we "debounce" the reading by accepting it only when62* we got 8 identical values in succession.63*64* If we pass midnight, don't wrap back to 0.65*/66time_t67time(time_t *t)68{69static time_t lasttime;70time_t now, check;71int same, try;7273same = try = 0;74check = bios_seconds();75do {76now = check;77check = bios_seconds();78if (check != now)79same = 0;80} while (++same < 8 && ++try < 1000);8182if (now < lasttime)83now += 24 * 3600;84lasttime = now;8586if (t != NULL)87*t = now;88return(now);89}9091time_t92getsecs(void)93{94time_t n = 0;95time(&n);96return n;97}9899/*100* Use the BIOS Wait function to pause for (period) microseconds.101*102* Resolution of this function is variable, but typically around103* 1ms.104*/105void106delay(int period)107{108v86.ctl = 0;109v86.addr = 0x15; /* int 0x15, function 0x86 */110v86.eax = 0x8600;111v86.ecx = period >> 16;112v86.edx = period & 0xffff;113v86int();114}115116117