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
18 #include <sys/times.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
34 #define RTC_CONFIG_A 0x0A
35 #define RTC_CONFIG_B 0x0B
36 #define RTC_CONFIG_C 0x0C
37 #define RTC_CONFIG_D 0x0D
43 _DEFUN (_times, _times (buf),
50 /* Read a value from the RTC port. */
52 rtc_read (unsigned char reg)
54 *(volatile unsigned char *)(RTC_PORT) = reg;
55 return *(volatile unsigned char *)(RTC_PORT + 1);
58 /* Write a value to the RTC port. */
60 rtc_write (unsigned char reg, unsigned char val)
62 *(volatile unsigned char *)(RTC_PORT) = reg;
63 *(volatile unsigned char *)(RTC_PORT + 1) = val;
66 /* Convert BCD to Decimal. */
67 #define BCD2DEC(BYTE) ((((BYTE >> 4) & 0xF) * 10) + (BYTE & 0xF))
70 * time -- return current time in seconds.
73 _DEFUN (time, time (t),
79 tm.tm_sec = BCD2DEC(rtc_read(RTC_SECONDS));
80 tm.tm_min = BCD2DEC(rtc_read(RTC_MINUTES));
81 tm.tm_hour = BCD2DEC(rtc_read(RTC_HOURS));
82 tm.tm_wday = BCD2DEC(rtc_read(RTC_DAY_OF_WEEK)) - 1;
83 tm.tm_mday = BCD2DEC(rtc_read(RTC_DATE_OF_MONTH));
84 tm.tm_mon = BCD2DEC(rtc_read(RTC_MONTH)) - 1;
85 tm.tm_year = BCD2DEC(rtc_read(RTC_YEAR)) + 100;
87 /* FIXME. Not really sure how to handle this. */
99 * _gettimeofday -- implement in terms of time, which means we can't
100 * return the microseconds.
103 _DEFUN (_gettimeofday, _gettimeofday (tv, tz),
104 struct timeval *tv _AND
107 struct timezone *tz = tzvp;
110 tz->tz_minuteswest = tz->tz_dsttime = 0;
113 tv->tv_sec = time(0);