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  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.str();
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  for (auto* mapper : mappers) {
71  mapper->writeIO(port, value, time);
72  }
73  invalidateMemCache(0x4000 * (port & 0x03), 0x4000);
74 }
75 
76 
77 // SimpleDebuggable
78 // This debuggable is here for backwards compatibility. For more accurate
79 // results, use the debuggable belonging to a specific mapper.
80 
81 MSXMapperIO::Debuggable::Debuggable(MSXMotherBoard& motherBoard_,
82  const std::string& name_)
83  : SimpleDebuggable(motherBoard_, name_, "Memory mapper registers", 4)
84 {
85 }
86 
87 byte MSXMapperIO::Debuggable::read(unsigned address)
88 {
89  auto& mapperIO = OUTER(MSXMapperIO, debuggable);
90  byte result = 0;
91  for (auto* mapper : mapperIO.mappers) {
92  result = std::max(result, mapper->getSelectedSegment(address));
93  }
94  return result;
95 }
96 
97 void MSXMapperIO::Debuggable::write(unsigned address, byte value,
98  EmuTime::param time)
99 {
100  auto& mapperIO = OUTER(MSXMapperIO, debuggable);
101  mapperIO.writeIO(address, value, time);
102 }
103 
104 
105 template<typename Archive>
106 void MSXMapperIO::serialize(Archive& ar, unsigned version)
107 {
108  if (ar.versionBelow(version, 2)) {
109  // In version 1 we stored the mapper state in MSXMapperIO instead of
110  // in the individual mappers, so distribute the state to them.
111  assert(ar.isLoader());
112  byte registers[4];
113  ar.serialize("registers", registers);
114  for (int page = 0; page < 4; page++) {
115  writeIO(page, registers[page], EmuTime::dummy());
116  }
117  }
118 }
120 
121 } // namespace openmsx
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
byte peekIO(word port, EmuTime::param time) const override
Read a byte from a given IO port.
Definition: MSXMapperIO.cc:59
void registerMapper(MSXMemoryMapperInterface *mapper)
Definition: MSXMapperIO.cc:40
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:287
void unregisterMapper(MSXMemoryMapperInterface *mapper)
Definition: MSXMapperIO.cc:45
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
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX...
Definition: MSXDevice.hh:31
void serialize(Archive &ar, unsigned version)
Definition: MSXMapperIO.cc:106
MSXMapperIO(const DeviceConfig &config)
Definition: MSXMapperIO.cc:32
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
int stringToInt(const string &str)
Definition: StringOp.cc:14
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:840
std::string str() const
Definition: string_view.cc:12
string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:589
virtual void reset(EmuTime::param time)
This method is called on reset.
Definition: MSXDevice.cc:354
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:166
#define OUTER(type, member)
Definition: outer.hh:38
void invalidateMemCache(word start, unsigned size)
Invalidate CPU memory-mapping cache.
Definition: MSXDevice.cc:458