Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / arch / i386-pc / battclock / readbattclock.c
blobba386f8c1d11588297a2a32eddb23c67a2b2cae2
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: ReadBattClock() function.
6 Lang: english
7 */
8 #include "battclock_intern.h"
10 #define CENTURY 0x32
11 #define YEAR 0x09
12 #define MONTH 0x08
13 #define MDAY 0x07
14 #define HOUR 0x04
15 #define MIN 0x02
16 #define SEC 0x00
17 #define STATUS_A 0x0A
18 #define STATUS_B 0x0B
19 #define HEALTH 0x0E
21 inline unsigned char read_port(unsigned char port);
22 inline int bcd_to_dec(int x);
24 /*****************************************************************************
26 NAME */
27 #include <proto/battclock.h>
28 #include <proto/utility.h>
29 #include <utility/date.h>
31 AROS_LH0(ULONG, ReadBattClock,
33 /* SYNOPSIS */
34 /* void */
36 /* LOCATION */
37 struct BattClockBase *, BattClockBase, 2, Battclock)
39 /* FUNCTION
40 Return the value stored in the battery back up clock. This value
41 is the number of seconds that have elapsed since midnight on the
42 1st of January 1978 (00:00:00 1.1.1978).
44 If the value of the battery clock is invalid, then the clock will
45 be reset.
47 INPUTS
49 RESULT
50 The number of seconds since 1.1.1978 00:00:00
52 NOTES
54 EXAMPLE
56 BUGS
58 SEE ALSO
59 WriteBattClock, ResetBattClock
61 INTERNALS
63 HISTORY
64 27-11-96 digulla automatically created from
65 battclock_lib.fd and clib/battclock_protos.h
67 *****************************************************************************/
69 AROS_LIBFUNC_INIT
71 struct ClockData date;
72 UWORD century;
73 UWORD status_b;
74 ULONG secs;
76 date.sec = read_port(SEC);
77 date.min = read_port(MIN);
78 date.hour = read_port(HOUR);
79 date.mday = read_port(MDAY);
80 date.month = read_port(MONTH);
81 date.year = read_port(YEAR);
82 century = read_port(CENTURY);
83 status_b = read_port(STATUS_B);
85 if ((status_b & 0x04) == 0) {
86 date.sec = bcd_to_dec(date.sec);
87 date.min = bcd_to_dec(date.min);
88 date.hour = bcd_to_dec(date.hour);
89 date.mday = bcd_to_dec(date.mday);
90 date.month = bcd_to_dec(date.month);
91 date.year = bcd_to_dec(date.year);
92 century = bcd_to_dec(century);
95 date.year = century * 100 + date.year;
97 secs=Date2Amiga(&date);
99 return secs;
101 AROS_LIBFUNC_EXIT
102 } /* ReadBattClock */
104 /* help functions */
105 unsigned char read_port(unsigned char port)
107 unsigned char tmp;
109 asm volatile (
110 "outb %1,$0x70 \n\t" \
111 "inb $0x71,%0" \
112 : "=a"(tmp) \
113 : "a"(port));
115 return tmp;
118 inline int bcd_to_dec(int x)
120 return ( (x >> 4) * 10 + (x & 0x0f) );