openMSX
MSXMapperIO.cc
Go to the documentation of this file.
1 #include "MSXMapperIO.hh"
2 #include "MSXMotherBoard.hh"
3 #include "HardwareConfig.hh"
4 #include "XMLElement.hh"
5 #include "MSXException.hh"
6 #include "StringOp.hh"
7 #include "outer.hh"
8 #include "serialize.hh"
9 #include "stl.hh"
10 #include <algorithm>
11 
12 namespace openmsx {
13 
14 static byte calcReadBackMask(MSXMotherBoard& motherBoard)
15 {
16  std::string_view type = motherBoard.getMachineConfig()->getConfig().getChildData(
17  "MapperReadBackBits", "largest");
18  if (type == "largest") {
19  return 0x00; // all bits can be read
20  }
21  std::string str(type);
22  int bits;
23  if (!StringOp::stringToInt(str, bits)) {
24  throw FatalError("Unknown mapper type: \"", type, "\".");
25  }
26  if (bits < 0 || bits > 8) {
27  throw FatalError("MapperReadBackBits out of range: \"", type, "\".");
28  }
29  return unsigned(-1) << bits;
30 }
31 
33  : MSXDevice(config)
34  , debuggable(getMotherBoard(), getName())
35  , mask(calcReadBackMask(getMotherBoard()))
36 {
37  reset(EmuTime::dummy());
38 }
39 
41 {
42  mappers.push_back(mapper);
43 }
44 
46 {
47  mappers.erase(rfind_unguarded(mappers, mapper));
48 }
49 
50 byte MSXMapperIO::readIO(word port, EmuTime::param time)
51 {
52  byte result = 0xFF;
53  for (auto* mapper : mappers) {
54  result &= mapper->readIO(port, time);
55  }
56  return result | mask;
57 }
58 
59 byte MSXMapperIO::peekIO(word port, EmuTime::param time) const
60 {
61  byte result = 0xFF;
62  for (auto* mapper : mappers) {
63  result &= mapper->peekIO(port, time);
64  }
65  return result | mask;
66 }
67 
68 void MSXMapperIO::writeIO(word port, byte value, EmuTime::param time)
69 {
70  // Note: the mappers are responsible for invalidating/filling the CPU
71  // cache-lines.
72  for (auto* mapper : mappers) {
73  mapper->writeIO(port, value, time);
74  }
75 }
76 
77 
78 // SimpleDebuggable
79 // This debuggable is here for backwards compatibility. For more accurate
80 // results, use the debuggable belonging to a specific mapper.
81 
82 MSXMapperIO::Debuggable::Debuggable(MSXMotherBoard& motherBoard_,
83  const std::string& name_)
84  : SimpleDebuggable(motherBoard_, name_, "Memory mapper registers", 4)
85 {
86 }
87 
88 byte MSXMapperIO::Debuggable::read(unsigned address)
89 {
90  auto& mapperIO = OUTER(MSXMapperIO, debuggable);
91  byte result = 0;
92  for (auto* mapper : mapperIO.mappers) {
93  result = std::max(result, mapper->getSelectedSegment(address));
94  }
95  return result;
96 }
97 
98 void MSXMapperIO::Debuggable::write(unsigned address, byte value,
99  EmuTime::param time)
100 {
101  auto& mapperIO = OUTER(MSXMapperIO, debuggable);
102  mapperIO.writeIO(address, value, time);
103 }
104 
105 
106 template<typename Archive>
107 void MSXMapperIO::serialize(Archive& ar, unsigned version)
108 {
109  if (ar.versionBelow(version, 2)) {
110  // In version 1 we stored the mapper state in MSXMapperIO instead of
111  // in the individual mappers, so distribute the state to them.
112  assert(ar.isLoader());
113  byte registers[4];
114  ar.serialize("registers", registers);
115  for (int page = 0; page < 4; page++) {
116  writeIO(page, registers[page], EmuTime::dummy());
117  }
118  }
119 }
121 
122 } // namespace openmsx
openmsx::MSXDevice
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition: MSXDevice.hh:32
HardwareConfig.hh
serialize.hh
openmsx::MSXMapperIO
MSXMapperIO
Definition: MSXMapperIO.cc:120
openmsx::DeviceConfig
Definition: DeviceConfig.hh:20
openmsx::MSXMemoryMapperInterface
Definition: MSXMapperIO.hh:12
openmsx::SimpleDebuggable
Definition: SimpleDebuggable.hh:12
MSXException.hh
openmsx::MSXMapperIO::unregisterMapper
void unregisterMapper(MSXMemoryMapperInterface *mapper)
Definition: MSXMapperIO.cc:45
XMLElement.hh
openmsx::MSXMapperIO::MSXMapperIO
MSXMapperIO(const DeviceConfig &config)
Definition: MSXMapperIO.cc:32
MSXMapperIO.hh
openmsx::MSXDevice::reset
virtual void reset(EmuTime::param time)
This method is called on reset.
Definition: MSXDevice.cc:361
openmsx::MSXMapperIO::peekIO
byte peekIO(word port, EmuTime::param time) const override
Read a byte from a given IO port.
Definition: MSXMapperIO.cc:59
OUTER
#define OUTER(type, member)
Definition: outer.hh:41
openmsx::Keys::getName
string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:740
openmsx::MSXMotherBoard
Definition: MSXMotherBoard.hh:61
INSTANTIATE_SERIALIZE_METHODS
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:981
openmsx::MSXMapperIO::readIO
byte readIO(word port, EmuTime::param time) override
Read a byte from an IO port at a certain time from this device.
Definition: MSXMapperIO.cc:50
StringOp::stringToInt
int stringToInt(const string &str)
Definition: StringOp.cc:16
outer.hh
StringOp.hh
openmsx::word
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
openmsx::MSXMapperIO
Definition: MSXMapperIO.hh:23
openmsx::mask
constexpr nibble mask[4][13]
Definition: RP5C01.cc:33
gl::max
vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:292
stl.hh
openmsx::MSXMapperIO::writeIO
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.
Definition: MSXMapperIO.cc:68
openmsx::MSXMapperIO::registerMapper
void registerMapper(MSXMemoryMapperInterface *mapper)
Definition: MSXMapperIO.cc:40
rfind_unguarded
auto rfind_unguarded(RANGE &range, const VAL &val)
Similar to the find(_if)_unguarded functions above, but searches from the back to front.
Definition: stl.hh:157
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
MSXMotherBoard.hh
openmsx::MSXMapperIO::serialize
void serialize(Archive &ar, unsigned version)
Definition: MSXMapperIO.cc:107