openMSX
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 
12  : MSXDevice(config)
13 {
14  for (auto& dev : devices) {
15  dev = nullptr;
16  }
17  count = 0;
18  selected = 0;
19 }
20 
22 {
23  for (auto& dev : devices) {
24  // all devices must be unregistered
25  assert(!dev); (void)dev;
26  }
27  assert(count == 0);
28 }
29 
31 {
32  if (devices[id]) {
33  // TODO implement multiplexing
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 
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 
61 {
62  return count != 0;
63 }
64 
66 {
67  selected = 0;
68 }
69 
71 {
72  if (devices[selected]) {
73  return devices[selected]->readSwitchedIO(port, time);
74  } else {
75  return 0xFF;
76  }
77 }
78 
80 {
81  if (devices[selected]) {
82  return devices[selected]->peekSwitchedIO(port, time);
83  } else {
84  return 0xFF;
85  }
86 }
87 
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 }
107 
108 } // namespace openmsx
virtual byte peekSwitchedIO(word port, EmuTime::param time) const =0
MSXCPUInterface & getCPUInterface() const
Definition: MSXDevice.cc:159
unsigned char byte
8 bit unsigned integer
Definition: openmsx.hh:33
bool hasRegisteredDevices() const
void registerDevice(byte id, MSXSwitchedDevice *device)
void unregister_IO_In(byte port, MSXDevice *device)
virtual void reset(EmuTime::param time)
This method is called on reset.
void unregisterDevice(byte id)
virtual byte readIO(word port, EmuTime::param time)
Read a byte from an IO port at a certain time from this device.
void register_IO_In(byte port, MSXDevice *device)
Devices can register their In ports.
void register_IO_Out(byte port, MSXDevice *device)
Devices can register their Out ports.
MSXDeviceSwitch(const DeviceConfig &config)
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX...
Definition: MSXDevice.hh:32
unsigned short word
16 bit unsigned integer
Definition: openmsx.hh:38
void unregister_IO_Out(byte port, MSXDevice *device)
virtual byte readSwitchedIO(word port, EmuTime::param time)=0
virtual void writeIO(word port, byte value, EmuTime::param time)
Write a byte to a given IO port at a certain time to this device.
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:802
void serialize(Archive &ar, unsigned version)
virtual byte peekIO(word port, EmuTime::param time) const
Read a byte from a given IO port.
virtual void writeSwitchedIO(word port, byte value, EmuTime::param time)=0