[comp.os.minix] Weide RTC program for ATARI ST

bert@bebux.UUCP (Bert Reuling) (01/04/89)

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file". To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g.. If this archive is complete, you
# will see the following message at the end:
#                 "End of archive"
#
# Contents:
#
#   README
#   rtc.c
#
# Wrapped by bert@tjilp on Wed Jan  4 11:22:15 1989
PATH=/bin:/usr/bin:/usr/local/bin ; export PATH
if test -f 'README' -a "${1}" != "-c"
then
   echo "$0: Will not overwrite existing file: README"
else
   echo "x - README (1118 characters)"
sed 's/^X//' <<\END_OF_SHAR >README
X
X
X
X                               rtc
X
X                        Copyright (c) 1988
X
X                           Bert Reuling
X
X              This rtc program may be copied freely.
X
X
X
X     This rtc program is for use with the Weide RTC clock module.
X     This is a clock module for the ATARI 260, 520 and 1040ST. As
X     this  clock  module  is  NOT  compatible  with  the  MEGA ST
X     built-in clock, you cannot use the  megartc.c  program  from
X     the distribution. The module is produced by:
X
X
X                        Weide Elektronik
X                        Regerstrasse 34
X                        D-4010 Hilden
X                        Tel. 02103-41226
X                        BRD (West Germany)
X
X
X     Please send bug reports, remarks, etc to:
X
X                       ...!hp4nl!bebux!bert
X
X
X                 /             /      Bert Reuling
X                /-,  ,--, ,-, /-      p/a Radio Holland bv
X               /  / /--- /   /        Jan Rebelstraat 20
X               `-'  `--  `   `--      1069 CC Amsterdam
X                                      The Netherlands
X
X                  MINIX werkgroep UNIXgg/HCC
END_OF_SHAR
   if test 1118 -ne `wc -c <'README'`
   then
      echo "$0: unpacked with wrong size: README"
   fi
fi
if test -f 'rtc.c' -a "${1}" != "-c"
then
   echo "$0: Will not overwrite existing file: rtc.c"
else
   echo "x - rtc.c (3660 characters)"
sed 's/^X//' <<\END_OF_SHAR >rtc.c
X/*
X *  rtc.c  -  Weijde RTC for minix v1.0. Copyright (c) 1988 Bert Reuling
X */
X#include <stdio.h>
X#include <fcntl.h>
X
X#define RTC_ADDRESS   0x00fffc00L
X
Xstatic int verbose;
Xstatic char *name, *Copyright = "\0rtc v1.0 Copyright (c) 1988 Bert Reuling";
X
Xmain(argc, argv)
Xint argc;
Xchar *argv[];
X{
X   char msg[20];
X   int regn, regs[12], fd, i, year, month, day,
X       hour, minute, second, wkd, lyr;
X   long clock, ticks = 0L;
X   static char *nday[7] = {
X      "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
X   };
X   static char *nmonth[13] = {
X      "Jan", "Feb", "Mar", "Apr", "May", "Jun",
X      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
X   };
X   static int mdays[2][12] = {
X      { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
X      { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
X   };
X
X   if (getuid() != 0) {
X      fprintf(stderr, "%s: cannot execute. (must be root)\n", argv[0]);
X      exit(-1);
X   }
X
X   name = argv[0];
X   verbose = (argc != 1);
X
X   message(1, "open /dev/mem");
X
X   if ((fd = open("/dev/mem", O_RDWR, 0)) < 0)
X      message(1, "failed");
X
X   message(2, "reading rtc registers");
X
X   for (i = 0x9600; i < 0x960d; i++) {
X      if (lseek(fd, RTC_ADDRESS, 0) == -1)
X         message(2, "seek failed");
X      if (write(fd, &i, 2) != 2) {
X         sprintf(msg, "register set %d failed\n", (i & 0x000f));
X         message(2, msg);
X      }
X      if (lseek(fd, RTC_ADDRESS, 0) == -1)
X         message(2, "seek failed");
X      if (read(fd, &regn, 2) != 2) {
X         sprintf(msg, "register get %d failed", (i & 0x000f));
X         message(2, msg);
X      }
X      regs[i & 0x0f] = regn & 0x000f;
X      sprintf(msg, "register %2d = $%02x", (i & 0x000f), (regn & 0x00ff));
X      message(2, msg);
X   }
X
X   message(3, "closing /dev/mem");
X
X   if (close(fd) == -1)
X      message(3, "failed");
X
X   message(4, "assemble rtc data");
X
X   year = 1900 + regs[12] * 10 + regs[11];
X   month = regs[10] * 10 + regs[9] - 1;
X   day = regs[8] * 10 + regs[7];
X   wkd = regs[6];
X   hour = (regs[5] & 0x03) * 10 + regs[4];
X   minute = regs[3] * 10 + regs[2];
X   second = regs[1] * 10 + regs[0];
X   lyr = isleap(year);
X
X   if (year < 1970)
X      message(4, "error in year part");
X   if ((month < 0) || (month > 11))
X      message(4,"error in month part");
X   if ((day < 1) || (day > 31))
X      message(4, "error in day part");
X   if ((wkd < 0) || (wkd > 6))
X      message(4, "error in week-day part");
X   if ((hour < 0) || (hour > 23))
X      message(4,  "error in hour part");
X   if ((minute < 0) || (minute > 59))
X      message(4, "error in minute part");
X   if ((second < 0) || (second > 59))
X      message(4, "error in seconds part");
X
X   for (i = 1970; i < year; i++)
X      ticks += (long) (isleap(i) ? 366 : 365);
X   for (i = 0; i < month; i++)
X      ticks += (long) mdays[lyr][i];
X   ticks -= 1L;
X   ticks += (long) day;
X   ticks *= 24L;
X   ticks += (long) hour;
X   ticks *= 60L;
X   ticks += (long) minute;
X   ticks *= 60L;
X   ticks += (long) second;
X
X   message(5,  "checking time");
X
X   time(&clock);
X   if (clock < ticks)
X      message(5,  "time is set backwards");
X
X   message(6,  "setting system time");
X
X   if (stime(&ticks) < 0)
X      message(6, "failed");
X
X   printf("%s %s %2d %02d:%02d:%02d %d\n", nday[wkd], nmonth[month], day, hour, minute, second, year);
X
X   exit(0);
X}
X
X/*
X *  isleap  -  return 1 if 'year' is a leap year
X */
Xisleap(year)
Xint year;
X{
X   return (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0));
X}
X
X/*
X *  message  -  print error/diagnostics message
X */
Xmessage(phase, message_string)
Xint phase;
Xchar *message_string;
X{
X   if (verbose)
X      printf("%s: ** phase %d - %s\n", name, phase, message_string);
X}
X
END_OF_SHAR
   if test 3660 -ne `wc -c <'rtc.c'`
   then
      echo "$0: unpacked with wrong size: rtc.c"
   fi
fi
echo "End of archive"
exit 0
#
#
#