source: trunk/packages/xen-3.1/xen-3.1/tools/ioemu/target-i386-dm/rtc-dm.c @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

File size: 2.8 KB
Line 
1/*
2 * QEMU MC146818 RTC emulation
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "vl.h"
26
27//#define DEBUG_CMOS
28
29struct RTCState {
30    uint8_t cmos_data[128];
31    uint8_t cmos_index;
32};
33
34void rtc_set_memory(RTCState *s, int addr, int val)
35{
36    if (addr >= 0 && addr <= 127)
37        s->cmos_data[addr] = val;
38}
39
40static void cmos_ioport_write(void *opaque, uint32_t addr, uint32_t data)
41{
42    RTCState *s = opaque;
43
44    if ((addr & 1) == 0) {
45        s->cmos_index = data & 0x7f;
46    } else {
47#ifdef DEBUG_CMOS
48        printf("cmos: write index=0x%02x val=0x%02x\n",
49               s->cmos_index, data);
50#endif       
51        s->cmos_data[s->cmos_index] = data;
52    }
53}
54
55static uint32_t cmos_ioport_read(void *opaque, uint32_t addr)
56{
57    RTCState *s = opaque;
58    int ret;
59    if ((addr & 1) == 0) {
60        return 0xff;
61    } else {
62        ret = s->cmos_data[s->cmos_index];
63#ifdef DEBUG_CMOS
64        printf("cmos: read index=0x%02x val=0x%02x\n",
65               s->cmos_index, ret);
66#endif
67        return ret;
68    }
69}
70
71static void rtc_save(QEMUFile *f, void *opaque)
72{
73    RTCState *s = opaque;
74
75    qemu_put_buffer(f, s->cmos_data, 128);
76    qemu_put_8s(f, &s->cmos_index);
77}
78
79static int rtc_load(QEMUFile *f, void *opaque, int version_id)
80{
81    RTCState *s = opaque;
82
83    if (version_id != 1)
84        return -EINVAL;
85
86    qemu_get_buffer(f, s->cmos_data, 128);
87    qemu_get_8s(f, &s->cmos_index);
88
89    return 0;
90}
91
92RTCState *rtc_init(int base, int irq)
93{
94    RTCState *s;
95
96    s = qemu_mallocz(sizeof(RTCState));
97    if (!s)
98        return NULL;
99
100    register_ioport_write(base, 2, 1, cmos_ioport_write, s);
101    register_ioport_read(base, 2, 1, cmos_ioport_read, s);
102
103    register_savevm("mc146818rtc", base, 1, rtc_save, rtc_load, s);
104    return s;
105}
106
107void rtc_set_date(RTCState *s, const struct tm *tm) {}
Note: See TracBrowser for help on using the repository browser.