clock.c revision 1.1
11.1Schuck/*	$NetBSD: clock.c,v 1.1 1995/07/25 23:12:22 chuck Exp $ */
21.1Schuck
31.1Schuck/*
41.1Schuck * Copyright (c) 1992, 1993
51.1Schuck *	The Regents of the University of California.  All rights reserved.
61.1Schuck * Copyright (c) 1994 Gordon W. Ross
71.1Schuck * Copyright (c) 1993 Adam Glass
81.1Schuck *
91.1Schuck * This software was developed by the Computer Systems Engineering group
101.1Schuck * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
111.1Schuck * contributed to Berkeley.
121.1Schuck *
131.1Schuck * All advertising materials mentioning features or use of this software
141.1Schuck * must display the following acknowledgement:
151.1Schuck *	This product includes software developed by the University of
161.1Schuck *	California, Lawrence Berkeley Laboratory.
171.1Schuck *
181.1Schuck * Redistribution and use in source and binary forms, with or without
191.1Schuck * modification, are permitted provided that the following conditions
201.1Schuck * are met:
211.1Schuck * 1. Redistributions of source code must retain the above copyright
221.1Schuck *    notice, this list of conditions and the following disclaimer.
231.1Schuck * 2. Redistributions in binary form must reproduce the above copyright
241.1Schuck *    notice, this list of conditions and the following disclaimer in the
251.1Schuck *    documentation and/or other materials provided with the distribution.
261.1Schuck * 3. All advertising materials mentioning features or use of this software
271.1Schuck *    must display the following acknowledgement:
281.1Schuck *	This product includes software developed by the University of
291.1Schuck *	California, Berkeley and its contributors.
301.1Schuck * 4. Neither the name of the University nor the names of its contributors
311.1Schuck *    may be used to endorse or promote products derived from this software
321.1Schuck *    without specific prior written permission.
331.1Schuck *
341.1Schuck * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
351.1Schuck * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
361.1Schuck * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
371.1Schuck * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
381.1Schuck * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
391.1Schuck * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
401.1Schuck * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
411.1Schuck * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
421.1Schuck * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
431.1Schuck * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
441.1Schuck * SUCH DAMAGE.
451.1Schuck *
461.1Schuck *	@(#)clock.c	8.1 (Berkeley) 6/11/93
471.1Schuck */
481.1Schuck
491.1Schuck/*
501.1Schuck * Clock driver.
511.1Schuck */
521.1Schuck
531.1Schuck#include <sys/types.h>
541.1Schuck#include "clockreg.h"
551.1Schuckstatic struct clockreg *clockreg = (struct clockreg *) CLOCK_ADDR;
561.1Schuck
571.1Schuckint hz = 1; /* XXX ? */
581.1Schuck
591.1Schuck/*
601.1Schuck * BCD to decimal and decimal to BCD.
611.1Schuck */
621.1Schuck#define	FROMBCD(x)	(((x) >> 4) * 10 + ((x) & 0xf))
631.1Schuck#define	TOBCD(x)	(((x) / 10 * 16) + ((x) % 10))
641.1Schuck
651.1Schuck#define	SECDAY		(24 * 60 * 60)
661.1Schuck#define	SECYR		(SECDAY * 365)
671.1Schuck#define	LEAPYEAR(y)	(((y) & 3) == 0)
681.1Schuck
691.1Schuck/*
701.1Schuck * This code is defunct after 2068.
711.1Schuck * Will Unix still be here then??
721.1Schuck */
731.1Schuckconst short dayyr[12] =
741.1Schuck    { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
751.1Schuck
761.1Schuckstatic u_long chiptotime(sec, min, hour, day, mon, year)
771.1Schuck	register int sec, min, hour, day, mon, year;
781.1Schuck{
791.1Schuck	register int days, yr;
801.1Schuck
811.1Schuck	sec = FROMBCD(sec);
821.1Schuck	min = FROMBCD(min);
831.1Schuck	hour = FROMBCD(hour);
841.1Schuck	day = FROMBCD(day);
851.1Schuck	mon = FROMBCD(mon);
861.1Schuck	year = FROMBCD(year) + YEAR0;
871.1Schuck	if (year < 70) year = 70;
881.1Schuck
891.1Schuck	/* simple sanity checks */
901.1Schuck	if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31)
911.1Schuck		return (0);
921.1Schuck	days = 0;
931.1Schuck	for (yr = 70; yr < year; yr++)
941.1Schuck		days += LEAPYEAR(yr) ? 366 : 365;
951.1Schuck	days += dayyr[mon - 1] + day - 1;
961.1Schuck	if (LEAPYEAR(yr) && mon > 2)
971.1Schuck		days++;
981.1Schuck	/* now have days since Jan 1, 1970; the rest is easy... */
991.1Schuck	return (days * SECDAY + hour * 3600 + min * 60 + sec);
1001.1Schuck}
1011.1Schuck
1021.1Schuck/*
1031.1Schuck * Set up the system's time, given a `reasonable' time value.
1041.1Schuck */
1051.1Schucktime_t getsecs()
1061.1Schuck{
1071.1Schuck	register struct clockreg *cl = clockreg;
1081.1Schuck	int sec, min, hour, day, mon, year;
1091.1Schuck
1101.1Schuck	cl->cl_csr |= CLK_READ;		/* enable read (stop time) */
1111.1Schuck	sec = cl->cl_sec;
1121.1Schuck	min = cl->cl_min;
1131.1Schuck	hour = cl->cl_hour;
1141.1Schuck	day = cl->cl_mday;
1151.1Schuck	mon = cl->cl_month;
1161.1Schuck	year = cl->cl_year;
1171.1Schuck	cl->cl_csr &= ~CLK_READ;	/* time wears on */
1181.1Schuck	return(chiptotime(sec, min, hour, day, mon, year));
1191.1Schuck}
1201.1Schuck
1211.1Schuck/*
1221.1Schuck * delay
1231.1Schuck */
1241.1Schuck
1251.1Schuckint getticks()
1261.1Schuck{
1271.1Schuck	return((int)getsecs());
1281.1Schuck}
129