Changes for 4.5.0 snapshot
[newlib-cygwin.git] / libgloss / moxie / qemu-time.c
blob0049c24eebc740d2882aabaceac11a2dfb6f21a9
1 /* qemu-time.c -- stubs so clock can be linked in.
3 * Copyright (c) 2008 Anthony Green
5 * The authors hereby grant permission to use, copy, modify, distribute,
6 * and license this software and its documentation for any purpose, provided
7 * that existing copyright notices are retained in all copies and that this
8 * notice is included verbatim in any distributions. No written agreement,
9 * license, or royalty fee is required for any of the authorized uses.
10 * Modifications to this software may be copyrighted by their authors
11 * and need not follow the licensing terms described here, provided that
12 * the new terms are clearly indicated on the first page of each file where
13 * they apply.
15 #include <errno.h>
16 #include <time.h>
17 #include <sys/time.h>
18 #include <sys/times.h>
19 #include "glue.h"
21 /* This is the base address of the mc146818 RTC i/o port. */
22 #define RTC_PORT 0x400
24 #define RTC_SECONDS 0x00
25 #define RTC_SECONDS_ALARM 0x01
26 #define RTC_MINUTES 0x02
27 #define RTC_MINUTES_ALARM 0x03
28 #define RTC_HOURS 0x04
29 #define RTC_HOURS_ALARM 0x05
30 #define RTC_DAY_OF_WEEK 0x06
31 #define RTC_DATE_OF_MONTH 0x07
32 #define RTC_MONTH 0x08
33 #define RTC_YEAR 0x09
34 #define RTC_CONFIG_A 0x0A
35 #define RTC_CONFIG_B 0x0B
36 #define RTC_CONFIG_C 0x0C
37 #define RTC_CONFIG_D 0x0D
40 * _times -- FIXME
42 int
43 _times (struct tms *buf)
45 errno = EINVAL;
46 return (-1);
49 /* Read a value from the RTC port. */
50 static unsigned char
51 rtc_read (unsigned char reg)
53 *(volatile unsigned char *)(RTC_PORT) = reg;
54 return *(volatile unsigned char *)(RTC_PORT + 1);
57 /* Write a value to the RTC port. */
58 static void
59 rtc_write (unsigned char reg, unsigned char val)
61 *(volatile unsigned char *)(RTC_PORT) = reg;
62 *(volatile unsigned char *)(RTC_PORT + 1) = val;
65 /* Convert BCD to Decimal. */
66 #define BCD2DEC(BYTE) ((((BYTE >> 4) & 0xF) * 10) + (BYTE & 0xF))
69 * time -- return current time in seconds.
71 time_t
72 time (time_t *t)
74 struct tm tm;
75 time_t ret;
77 tm.tm_sec = BCD2DEC(rtc_read(RTC_SECONDS));
78 tm.tm_min = BCD2DEC(rtc_read(RTC_MINUTES));
79 tm.tm_hour = BCD2DEC(rtc_read(RTC_HOURS));
80 tm.tm_wday = BCD2DEC(rtc_read(RTC_DAY_OF_WEEK)) - 1;
81 tm.tm_mday = BCD2DEC(rtc_read(RTC_DATE_OF_MONTH));
82 tm.tm_mon = BCD2DEC(rtc_read(RTC_MONTH)) - 1;
83 tm.tm_year = BCD2DEC(rtc_read(RTC_YEAR)) + 100;
85 /* FIXME. Not really sure how to handle this. */
86 tm.tm_isdst = 1;
88 ret = mktime(&tm);
90 if (t)
91 *t = ret;
93 return ret;
97 * _gettimeofday -- implement in terms of time, which means we can't
98 * return the microseconds.
101 _gettimeofday (struct timeval *tv,
102 void *tzvp)
104 struct timezone *tz = tzvp;
105 struct tm tm;
106 if (tz)
107 tz->tz_minuteswest = tz->tz_dsttime = 0;
109 tv->tv_usec = 0;
110 tv->tv_sec = time(0);
112 return 0;