1 |
/* |
2 |
* Copyright (C) 2006-2007 Anders Gavare. All rights reserved. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
6 |
* |
7 |
* 1. Redistributions of source code must retain the above copyright |
8 |
* notice, this list of conditions and the following disclaimer. |
9 |
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
* notice, this list of conditions and the following disclaimer in the |
11 |
* documentation and/or other materials provided with the distribution. |
12 |
* 3. The name of the author may not be used to endorse or promote products |
13 |
* derived from this software without specific prior written permission. |
14 |
* |
15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
* SUCH DAMAGE. |
26 |
* |
27 |
* |
28 |
* $Id: dev_mk48txx.c,v 1.8 2007/06/15 19:57:33 debug Exp $ |
29 |
* |
30 |
* COMMENT: Mostek MK48Txx Real Time Clock |
31 |
* |
32 |
* TODO: |
33 |
* Only the MK48T08 is implemented so far. |
34 |
*/ |
35 |
|
36 |
#include <stdio.h> |
37 |
#include <stdlib.h> |
38 |
#include <string.h> |
39 |
#include <time.h> |
40 |
|
41 |
#include "cpu.h" |
42 |
#include "device.h" |
43 |
#include "emul.h" |
44 |
#include "machine.h" |
45 |
#include "memory.h" |
46 |
#include "misc.h" |
47 |
|
48 |
|
49 |
#include "mk48txxreg.h" |
50 |
|
51 |
|
52 |
#define MK48TXX_LEN MK48T08_CLKSZ |
53 |
|
54 |
#define BCD(x) ((((x) / 10) << 4) + ((x) % 10)) |
55 |
|
56 |
struct mk48txx_data { |
57 |
uint8_t reg[MK48TXX_LEN]; |
58 |
}; |
59 |
|
60 |
|
61 |
void mk48txx_update_regs(struct mk48txx_data *d) |
62 |
{ |
63 |
struct tm *tmp; |
64 |
time_t timet; |
65 |
|
66 |
timet = time(NULL); |
67 |
tmp = gmtime(&timet); |
68 |
|
69 |
d->reg[MK48T08_CLKOFF + MK48TXX_ISEC] = BCD(tmp->tm_sec); |
70 |
d->reg[MK48T08_CLKOFF + MK48TXX_IMIN] = BCD(tmp->tm_min); |
71 |
d->reg[MK48T08_CLKOFF + MK48TXX_IHOUR] = BCD(tmp->tm_hour); |
72 |
d->reg[MK48T08_CLKOFF + MK48TXX_IWDAY] = tmp->tm_wday + 1; |
73 |
d->reg[MK48T08_CLKOFF + MK48TXX_IDAY] = BCD(tmp->tm_mday); |
74 |
d->reg[MK48T08_CLKOFF + MK48TXX_IMON] = BCD(tmp->tm_mon + 1); |
75 |
d->reg[MK48T08_CLKOFF + MK48TXX_IYEAR] = BCD(tmp->tm_year % 100); |
76 |
} |
77 |
|
78 |
|
79 |
DEVICE_ACCESS(mk48txx) |
80 |
{ |
81 |
struct mk48txx_data *d = extra; |
82 |
uint64_t idata = 0, odata = 0; |
83 |
|
84 |
if (writeflag == MEM_WRITE) |
85 |
idata = memory_readmax64(cpu, data, len); |
86 |
|
87 |
if (writeflag == MEM_READ) |
88 |
odata = d->reg[relative_addr]; |
89 |
|
90 |
if (relative_addr < MK48T08_CLKOFF || |
91 |
relative_addr >= MK48T08_CLKOFF + MK48TXX_ISEC) { |
92 |
/* Reads and writes to the RAM part of the mk48txx, or |
93 |
the clock registers, are OK: */ |
94 |
if (writeflag == MEM_WRITE) |
95 |
d->reg[relative_addr] = idata; |
96 |
goto ret; |
97 |
} |
98 |
|
99 |
switch (relative_addr) { |
100 |
|
101 |
case MK48T08_CLKOFF + MK48TXX_ICSR: |
102 |
if (writeflag == MEM_WRITE) { |
103 |
if ((idata & MK48TXX_CSR_READ) && |
104 |
!(d->reg[relative_addr] & MK48TXX_CSR_READ)) { |
105 |
/* Switching the read bit from 0 to 1 causes |
106 |
registers to be "froozen". In the emulator, |
107 |
simply updating them with data from the |
108 |
host should be good enough. */ |
109 |
mk48txx_update_regs(d); |
110 |
} |
111 |
d->reg[relative_addr] = idata; |
112 |
} |
113 |
break; |
114 |
|
115 |
default:if (writeflag == MEM_READ) |
116 |
fatal("[ mk48txx: unimplemented READ from offset 0x%x ]" |
117 |
"\n", (int)relative_addr); |
118 |
else |
119 |
fatal("[ mk48txx: unimplemented WRITE to offset 0x%x: " |
120 |
"0x%x ]\n", (int)relative_addr, (int)idata); |
121 |
exit(1); |
122 |
} |
123 |
|
124 |
ret: |
125 |
if (writeflag == MEM_READ) |
126 |
memory_writemax64(cpu, data, len, odata); |
127 |
|
128 |
return 1; |
129 |
} |
130 |
|
131 |
|
132 |
DEVINIT(mk48txx) |
133 |
{ |
134 |
struct mk48txx_data *d; |
135 |
|
136 |
CHECK_ALLOCATION(d = malloc(sizeof(struct mk48txx_data))); |
137 |
memset(d, 0, sizeof(struct mk48txx_data)); |
138 |
|
139 |
mk48txx_update_regs(d); |
140 |
|
141 |
memory_device_register(devinit->machine->memory, devinit->name, |
142 |
devinit->addr, MK48TXX_LEN, dev_mk48txx_access, (void *)d, |
143 |
DM_DEFAULT, NULL); |
144 |
|
145 |
return 1; |
146 |
} |
147 |
|