openMSX
MSXDeviceSwitch.cc
Go to the documentation of this file.
1#include "MSXDeviceSwitch.hh"
3#include "MSXCPUInterface.hh"
4#include "MSXException.hh"
5#include "ranges.hh"
6#include "serialize.hh"
7#include "xrange.hh"
8#include <cassert>
9
10namespace openmsx {
11
13 : MSXDevice(config)
14{
15 ranges::fill(devices, nullptr);
16}
17
19{
20 // all devices must be unregistered
21 assert(ranges::all_of(devices, [](auto* dev) { return dev == nullptr; }));
22 assert(count == 0);
23}
24
26{
27 if (devices[id]) {
28 // TODO implement multiplexing
29 throw MSXException(
30 "Already have a switched device with id ", int(id));
31 }
32 devices[id] = device;
33 if (count == 0) {
35 }
36 ++count;
37}
38
40{
41 --count;
42 if (count == 0) {
44 }
45 assert(devices[id]);
46 devices[id] = nullptr;
47}
48
49void MSXDeviceSwitch::reset(EmuTime::param /*time*/)
50{
51 selected = 0;
52}
53
54byte MSXDeviceSwitch::readIO(word port, EmuTime::param time)
55{
56 if (devices[selected]) {
57 return devices[selected]->readSwitchedIO(port, time);
58 } else {
59 return 0xFF;
60 }
61}
62
63byte MSXDeviceSwitch::peekIO(word port, EmuTime::param time) const
64{
65 if (devices[selected]) {
66 return devices[selected]->peekSwitchedIO(port, time);
67 } else {
68 return 0xFF;
69 }
70}
71
72void MSXDeviceSwitch::writeIO(word port, byte value, EmuTime::param time)
73{
74 if ((port & 0x0F) == 0x00) {
75 selected = value;
76 } else if (devices[selected]) {
77 devices[selected]->writeSwitchedIO(port, value, time);
78 } else {
79 // ignore
80 }
81}
82
83
84template<typename Archive>
85void MSXDeviceSwitch::serialize(Archive& ar, unsigned /*version*/)
86{
87 ar.template serializeBase<MSXDevice>(*this);
88 ar.serialize("selected", selected);
89}
91
92} // namespace openmsx
uintptr_t id
void unregister_IO_InOut_range(byte port, unsigned num, MSXDevice *device)
void register_IO_InOut_range(byte port, unsigned num, MSXDevice *device)
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.
byte peekIO(word port, EmuTime::param time) const override
Read a byte from a given IO port.
byte readIO(word port, EmuTime::param time) override
Read a byte from an IO port at a certain time from this device.
void reset(EmuTime::param time) override
This method is called on reset.
MSXDeviceSwitch(const DeviceConfig &config)
void registerDevice(byte id, MSXSwitchedDevice *device)
void serialize(Archive &ar, unsigned version)
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition MSXDevice.hh:36
MSXCPUInterface & getCPUInterface() const
Definition MSXDevice.cc:133
This file implemented 3 utility functions:
Definition Autofire.cc:11
uint16_t word
16 bit unsigned integer
Definition openmsx.hh:29
constexpr bool all_of(InputRange &&range, UnaryPredicate pred)
Definition ranges.hh:188
constexpr void fill(ForwardRange &&range, const T &value)
Definition ranges.hh:315
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)