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  selected = 0;
63 }
64 
66 {
67  if (devices[selected]) {
68  return devices[selected]->readSwitchedIO(port, time);
69  } else {
70  return 0xFF;
71  }
72 }
73 
75 {
76  if (devices[selected]) {
77  return devices[selected]->peekSwitchedIO(port, time);
78  } else {
79  return 0xFF;
80  }
81 }
82 
84 {
85  if ((port & 0x0F) == 0x00) {
86  selected = value;
87  } else if (devices[selected]) {
88  devices[selected]->writeSwitchedIO(port, value, time);
89  } else {
90  // ignore
91  }
92 }
93 
94 
95 template<typename Archive>
96 void MSXDeviceSwitch::serialize(Archive& ar, unsigned /*version*/)
97 {
98  ar.template serializeBase<MSXDevice>(*this);
99  ar.serialize("selected", selected);
100 }
102 
103 } // namespace openmsx
virtual byte peekSwitchedIO(word port, EmuTime::param time) const =0
MSXCPUInterface & getCPUInterface() const
Definition: MSXDevice.cc:144
unsigned char byte
8 bit unsigned integer
Definition: openmsx.hh:33
void registerDevice(byte id, MSXSwitchedDevice *device)
void unregister_IO_In(byte port, MSXDevice *device)
void unregisterDevice(byte id)
void reset(EmuTime::param time) override
This method is called on reset.
void register_IO_In(byte port, MSXDevice *device)
Devices can register their In ports.
void writeIO(word port, byte value, EmuTime::param time) override
Write a byte to a given IO port at a certain time to this device.
void register_IO_Out(byte port, MSXDevice *device)
Devices can register their Out ports.
MSXDeviceSwitch(const DeviceConfig &config)
byte readIO(word port, EmuTime::param time) override
Read a byte from an IO port at a certain time from this device.
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
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:802
void serialize(Archive &ar, unsigned version)
byte peekIO(word port, EmuTime::param time) const override
Read a byte from a given IO port.
virtual void writeSwitchedIO(word port, byte value, EmuTime::param time)=0