dev_mk48txx.cc Source File
Back to the index.
src
devices
dev_mk48txx.cc
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2006-2009 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
* COMMENT: Mostek MK48Txx Real Time Clock
29
*
30
* TODO:
31
* Only the MK48T08 is implemented so far.
32
*/
33
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37
#include <time.h>
38
39
#include "
cpu.h
"
40
#include "
device.h
"
41
#include "
emul.h
"
42
#include "
machine.h
"
43
#include "
memory.h
"
44
#include "
misc.h
"
45
46
#include "
thirdparty/mk48txxreg.h
"
47
48
49
#define MK48TXX_LEN MK48T08_CLKSZ
50
51
#define BCD(x) ((((x) / 10) << 4) + ((x) % 10))
52
53
struct
mk48txx_data
{
54
uint8_t
reg
[
MK48TXX_LEN
];
55
};
56
57
58
void
mk48txx_update_regs
(
struct
mk48txx_data
*d)
59
{
60
struct
tm *tmp;
61
time_t timet;
62
63
timet = time(NULL);
64
tmp = gmtime(&timet);
65
66
d->
reg
[
MK48T08_CLKOFF
+
MK48TXX_ISEC
] =
BCD
(tmp->tm_sec);
67
d->
reg
[
MK48T08_CLKOFF
+
MK48TXX_IMIN
] =
BCD
(tmp->tm_min);
68
d->
reg
[
MK48T08_CLKOFF
+
MK48TXX_IHOUR
] =
BCD
(tmp->tm_hour);
69
d->
reg
[
MK48T08_CLKOFF
+
MK48TXX_IWDAY
] = tmp->tm_wday + 1;
70
d->
reg
[
MK48T08_CLKOFF
+
MK48TXX_IDAY
] =
BCD
(tmp->tm_mday);
71
d->
reg
[
MK48T08_CLKOFF
+
MK48TXX_IMON
] =
BCD
(tmp->tm_mon + 1);
72
d->
reg
[
MK48T08_CLKOFF
+
MK48TXX_IYEAR
] =
BCD
(tmp->tm_year % 100);
73
}
74
75
76
DEVICE_ACCESS
(mk48txx)
77
{
78
struct
mk48txx_data
*d = (
struct
mk48txx_data
*) extra;
79
uint64_t idata = 0, odata = 0;
80
81
if
(writeflag ==
MEM_WRITE
)
82
idata =
memory_readmax64
(
cpu
,
data
, len);
83
84
if
(writeflag ==
MEM_READ
)
85
odata = d->
reg
[relative_addr];
86
87
if
(relative_addr <
MK48T08_CLKOFF
||
88
relative_addr >=
MK48T08_CLKOFF
+
MK48TXX_ISEC
) {
89
/* Reads and writes to the RAM part of the mk48txx, or
90
the clock registers, are OK: */
91
if
(writeflag ==
MEM_WRITE
)
92
d->
reg
[relative_addr] = idata;
93
goto
ret;
94
}
95
96
switch
(relative_addr) {
97
98
case
MK48T08_CLKOFF
+
MK48TXX_ICSR
:
99
if
(writeflag ==
MEM_WRITE
) {
100
if
((idata &
MK48TXX_CSR_READ
) &&
101
!(d->
reg
[relative_addr] & MK48TXX_CSR_READ)) {
102
/* Switching the read bit from 0 to 1 causes
103
registers to be "froozen". In the emulator,
104
simply updating them with data from the
105
host should be good enough. */
106
mk48txx_update_regs
(d);
107
}
108
d->
reg
[relative_addr] = idata;
109
}
110
break
;
111
112
default
:
if
(writeflag ==
MEM_READ
)
113
fatal
(
"[ mk48txx: unimplemented READ from offset 0x%x ]"
114
"\n"
, (
int
)relative_addr);
115
else
116
fatal
(
"[ mk48txx: unimplemented WRITE to offset 0x%x: "
117
"0x%x ]\n"
, (
int
)relative_addr, (
int
)idata);
118
exit(1);
119
}
120
121
ret:
122
if
(writeflag ==
MEM_READ
)
123
memory_writemax64
(
cpu
,
data
, len, odata);
124
125
return
1;
126
}
127
128
129
DEVINIT
(mk48txx)
130
{
131
struct
mk48txx_data
*d;
132
133
CHECK_ALLOCATION
(d = (
struct
mk48txx_data
*) malloc(
sizeof
(
struct
mk48txx_data
)));
134
memset(d, 0,
sizeof
(
struct
mk48txx_data
));
135
136
mk48txx_update_regs
(d);
137
138
memory_device_register
(
devinit
->
machine
->
memory
,
devinit
->
name
,
139
devinit
->
addr
,
MK48TXX_LEN
, dev_mk48txx_access, (
void
*)d,
140
DM_DEFAULT
, NULL);
141
142
return
1;
143
}
144
MK48TXX_ISEC
#define MK48TXX_ISEC
Definition:
mk48txxreg.h:72
memory_readmax64
uint64_t memory_readmax64(struct cpu *cpu, unsigned char *buf, int len)
Definition:
memory.cc:55
DEVINIT
DEVINIT(mk48txx)
Definition:
dev_mk48txx.cc:129
fatal
void fatal(const char *fmt,...)
Definition:
main.cc:152
DM_DEFAULT
#define DM_DEFAULT
Definition:
memory.h:130
memory.h
misc.h
devinit::name
char * name
Definition:
device.h:43
mk48txxreg.h
MEM_READ
#define MEM_READ
Definition:
memory.h:116
machine::memory
struct memory * memory
Definition:
machine.h:126
MK48TXX_IHOUR
#define MK48TXX_IHOUR
Definition:
mk48txxreg.h:74
MK48TXX_IDAY
#define MK48TXX_IDAY
Definition:
mk48txxreg.h:76
CHECK_ALLOCATION
#define CHECK_ALLOCATION(ptr)
Definition:
misc.h:239
BCD
#define BCD(x)
Definition:
dev_mk48txx.cc:51
MK48TXX_ICSR
#define MK48TXX_ICSR
Definition:
mk48txxreg.h:71
MK48TXX_IWDAY
#define MK48TXX_IWDAY
Definition:
mk48txxreg.h:75
data
u_short data
Definition:
siireg.h:79
mk48txx_data::reg
uint8_t reg[MK48TXX_LEN]
Definition:
dev_mk48txx.cc:54
MEM_WRITE
#define MEM_WRITE
Definition:
memory.h:117
MK48TXX_LEN
#define MK48TXX_LEN
Definition:
dev_mk48txx.cc:49
devinit
Definition:
device.h:40
MK48TXX_IMIN
#define MK48TXX_IMIN
Definition:
mk48txxreg.h:73
cpu.h
cpu
Definition:
cpu.h:326
devinit::machine
struct machine * machine
Definition:
device.h:41
memory_writemax64
void memory_writemax64(struct cpu *cpu, unsigned char *buf, int len, uint64_t data)
Definition:
memory.cc:89
DEVICE_ACCESS
DEVICE_ACCESS(mk48txx)
Definition:
dev_mk48txx.cc:76
mk48txx_update_regs
void mk48txx_update_regs(struct mk48txx_data *d)
Definition:
dev_mk48txx.cc:58
memory_device_register
void memory_device_register(struct memory *mem, const char *, uint64_t baseaddr, uint64_t len, int(*f)(struct cpu *, struct memory *, uint64_t, unsigned char *, size_t, int, void *), void *extra, int flags, unsigned char *dyntrans_data)
Definition:
memory.cc:339
mk48txx_data
Definition:
dev_mk48txx.cc:53
device.h
devinit::addr
uint64_t addr
Definition:
device.h:46
machine.h
MK48TXX_IYEAR
#define MK48TXX_IYEAR
Definition:
mk48txxreg.h:78
MK48TXX_CSR_READ
#define MK48TXX_CSR_READ
Definition:
mk48txxreg.h:101
emul.h
MK48T08_CLKOFF
#define MK48T08_CLKOFF
Definition:
mk48txxreg.h:116
MK48TXX_IMON
#define MK48TXX_IMON
Definition:
mk48txxreg.h:77
Generated on Fri Dec 7 2018 19:52:23 for GXemul by
1.8.13