openMSX
Main Page
Namespaces
Classes
Files
File List
File Members
MSXDeviceSwitch.cc
Go to the documentation of this file.
1
#include "
MSXDeviceSwitch.hh
"
2
#include "
MSXSwitchedDevice.hh
"
3
#include "
MSXCPUInterface.hh
"
4
#include "
MSXException.hh
"
5
#include "
StringOp.hh
"
6
#include "
serialize.hh
"
7
#include <cassert>
8
9
namespace
openmsx {
10
11
MSXDeviceSwitch::MSXDeviceSwitch
(
const
DeviceConfig
& config)
12
:
MSXDevice
(config)
13
{
14
for
(
int
i = 0; i < 256; ++i) {
15
devices[i] =
nullptr
;
16
}
17
count = 0;
18
selected = 0;
19
}
20
21
MSXDeviceSwitch::~MSXDeviceSwitch
()
22
{
23
for
(
int
i = 0; i < 256; ++i) {
24
// all devices must be unregistered
25
assert(!devices[i]);
26
}
27
assert(count == 0);
28
}
29
30
void
MSXDeviceSwitch::registerDevice
(
byte
id
,
MSXSwitchedDevice
* device)
31
{
32
if
(devices[
id
]) {
33
// TODO implement multiplexing
34
throw
MSXException
(
StringOp::Builder
() <<
35
"Already have a switched device with id "
<<
int
(
id
));
36
}
37
devices[id] = device;
38
if
(count == 0) {
39
for
(
byte
port = 0x40; port < 0x50; ++port) {
40
getCPUInterface
().
register_IO_In
(port,
this
);
41
getCPUInterface
().
register_IO_Out
(port,
this
);
42
}
43
}
44
++count;
45
}
46
47
void
MSXDeviceSwitch::unregisterDevice
(
byte
id
)
48
{
49
--count;
50
if
(count == 0) {
51
for
(
byte
port = 0x40; port < 0x50; ++port) {
52
getCPUInterface
().
unregister_IO_Out
(port,
this
);
53
getCPUInterface
().
unregister_IO_In
(port,
this
);
54
}
55
}
56
assert(devices[
id
]);
57
devices[id] =
nullptr
;
58
}
59
60
bool
MSXDeviceSwitch::hasRegisteredDevices
()
const
61
{
62
return
count != 0;
63
}
64
65
void
MSXDeviceSwitch::reset
(
EmuTime::param
/*time*/
)
66
{
67
selected = 0;
68
}
69
70
byte
MSXDeviceSwitch::readIO
(
word
port,
EmuTime::param
time)
71
{
72
if
(devices[selected]) {
73
return
devices[selected]->
readSwitchedIO
(port, time);
74
}
else
{
75
return
0xFF;
76
}
77
}
78
79
byte
MSXDeviceSwitch::peekIO
(
word
port,
EmuTime::param
time)
const
80
{
81
if
(devices[selected]) {
82
return
devices[selected]->
peekSwitchedIO
(port, time);
83
}
else
{
84
return
0xFF;
85
}
86
}
87
88
void
MSXDeviceSwitch::writeIO
(
word
port,
byte
value,
EmuTime::param
time)
89
{
90
if
((port & 0x0F) == 0x00) {
91
selected = value;
92
}
else
if
(devices[selected]) {
93
devices[selected]->
writeSwitchedIO
(port, value, time);
94
}
else
{
95
// ignore
96
}
97
}
98
99
100
template
<
typename
Archive>
101
void
MSXDeviceSwitch::serialize
(Archive& ar,
unsigned
/*version*/
)
102
{
103
ar.template serializeBase<MSXDevice>(*this);
104
ar.serialize(
"selected"
, selected);
105
}
106
INSTANTIATE_SERIALIZE_METHODS
(
MSXDeviceSwitch
);
107
108
}
// namespace openmsx
Generated on Mon May 20 2013 12:16:58 for openMSX by
1.8.1.2