openMSX
MSXDevice.cc
Go to the documentation of this file.
1 #include "MSXDevice.hh"
2 #include "XMLElement.hh"
3 #include "MSXMotherBoard.hh"
4 #include "HardwareConfig.hh"
6 #include "MSXCPUInterface.hh"
7 #include "MSXCPU.hh"
8 #include "CacheLine.hh"
9 #include "TclObject.hh"
10 #include "MSXException.hh"
11 #include "ranges.hh"
12 #include "serialize.hh"
13 #include "stl.hh"
14 #include "unreachable.hh"
15 #include "view.hh"
16 #include <cassert>
17 #include <cstring>
18 #include <iterator> // for back_inserter
19 
20 using std::string;
21 
22 namespace openmsx {
23 
26 
27 
28 MSXDevice::MSXDevice(const DeviceConfig& config, const string& name)
29  : deviceConfig(config)
30 {
31  initName(name);
32 }
33 
35  : deviceConfig(config)
36 {
37  initName(getDeviceConfig().getAttribute("id"));
38 }
39 
40 void MSXDevice::initName(const string& name)
41 {
42  deviceName = name;
43  if (getMotherBoard().findDevice(deviceName)) {
44  unsigned n = 0;
45  do {
46  deviceName = strCat(name, " (", ++n, ')');
47  } while (getMotherBoard().findDevice(deviceName));
48  }
49 }
50 
52 {
53  staticInit();
54 
55  lockDevices();
56  registerSlots();
57  registerPorts();
58 }
59 
61 {
62  unregisterPorts();
63  unregisterSlots();
64  unlockDevices();
65  assert(referencedBy.empty());
66 }
67 
68 void MSXDevice::staticInit()
69 {
70  static bool alreadyInit = false;
71  if (alreadyInit) return;
72  alreadyInit = true;
73 
74  memset(unmappedRead, 0xFF, sizeof(unmappedRead));
75 }
76 
78 {
80 }
81 
82 void MSXDevice::testRemove(Devices removed) const
83 {
84  auto all = referencedBy;
85  ranges::sort(all);
86  ranges::sort(removed);
87  Devices rest;
88  ranges::set_difference(all, removed, back_inserter(rest));
89  if (!rest.empty()) {
90  string msg = "Still in use by";
91  for (auto& d : rest) {
92  strAppend(msg, ' ', d->getName());
93  }
94  throw MSXException(std::move(msg));
95  }
96 }
97 
98 void MSXDevice::lockDevices()
99 {
100  // This code can only handle backward references: the thing that is
101  // referenced must already be instantiated, we don't try to change the
102  // instantiation order. For the moment this is good enough (only ADVRAM
103  // (an extension) uses it to refer to the VDP (inside a machine)). If
104  // needed we can implement something more sophisticated later without
105  // changing the format of the config files.
106  for (auto& c : getDeviceConfig().getChildren("device")) {
107  const auto& name = c->getAttribute("idref");
108  auto* dev = getMotherBoard().findDevice(name);
109  if (!dev) {
110  throw MSXException(
111  "Unsatisfied dependency: '", getName(),
112  "' depends on unavailable device '",
113  name, "'.");
114  }
115  references.push_back(dev);
116  dev->referencedBy.push_back(this);
117  }
118 }
119 
120 void MSXDevice::unlockDevices()
121 {
122  for (auto& r : references) {
123  move_pop_back(r->referencedBy, rfind_unguarded(r->referencedBy, this));
124  }
125 }
126 
128 {
129  // init() must already be called
130  return references;
131 }
132 
133 EmuTime::param MSXDevice::getCurrentTime() const
134 {
135  return getMotherBoard().getCurrentTime();
136 }
138 {
139  return getMotherBoard().getCPU();
140 }
142 {
143  return getMotherBoard().getCPUInterface();
144 }
146 {
147  return getMotherBoard().getScheduler();
148 }
150 {
151  return getMotherBoard().getMSXCliComm();
152 }
154 {
155  return getMotherBoard().getReactor();
156 }
158 {
160 }
162 {
163  return getMotherBoard().getLedStatus();
164 }
166 {
168 }
169 
170 void MSXDevice::registerSlots()
171 {
172  MemRegions tmpMemRegions;
173  for (auto& m : getDeviceConfig().getChildren("mem")) {
174  unsigned base = m->getAttributeAsInt("base");
175  unsigned size = m->getAttributeAsInt("size");
176  if ((base >= 0x10000) || (size > 0x10000) || ((base + size) > 0x10000)) {
177  throw MSXException(
178  "Invalid memory specification for device ",
179  getName(), " should be in range "
180  "[0x0000,0x10000).");
181  }
182  tmpMemRegions.emplace_back(base, size);
183  }
184  if (tmpMemRegions.empty()) {
185  return;
186  }
187 
188  // find primary and secondary slot specification
189  auto& slotManager = getMotherBoard().getSlotManager();
190  auto* primaryConfig = getDeviceConfig2().getPrimary();
191  auto* secondaryConfig = getDeviceConfig2().getSecondary();
192  if (primaryConfig) {
193  ps = slotManager.getSlotNum(primaryConfig->getAttribute("slot"));
194  } else {
195  throw MSXException("Invalid memory specification");
196  }
197  if (secondaryConfig) {
198  const auto& ss_str = secondaryConfig->getAttribute("slot");
199  ss = slotManager.getSlotNum(ss_str);
200  if ((-16 <= ss) && (ss <= -1) && (ss != ps)) {
201  throw MSXException(
202  "Invalid secondary slot specification: \"",
203  ss_str, "\".");
204  }
205  } else {
206  ss = 0;
207  }
208 
209  // This is only for backwards compatibility: in the past we added extra
210  // attributes "primary_slot" and "secondary_slot" (in each MSXDevice
211  // config) instead of changing the 'any' value of the slot attribute of
212  // the (possibly shared) <primary> and <secondary> tags. When loading
213  // an old savestate these tags can still occur, so keep this code. Also
214  // remove these attributes to convert to the new format.
215  const auto& config = getDeviceConfig();
216  if (config.hasAttribute("primary_slot")) {
217  auto& mutableConfig = const_cast<XMLElement&>(config);
218  const auto& primSlot = config.getAttribute("primary_slot");
219  ps = slotManager.getSlotNum(primSlot);
220  mutableConfig.removeAttribute("primary_slot");
221  if (config.hasAttribute("secondary_slot")) {
222  const auto& secondSlot = config.getAttribute("secondary_slot");
223  ss = slotManager.getSlotNum(secondSlot);
224  mutableConfig.removeAttribute("secondary_slot");
225  }
226  }
227 
228  // decode special values for 'ss'
229  if ((-128 <= ss) && (ss < 0)) {
230  if ((0 <= ps) && (ps < 4) &&
231  getCPUInterface().isExpanded(ps)) {
232  ss += 128;
233  } else {
234  ss = 0;
235  }
236  }
237 
238  // decode special values for 'ps'
239  if (ps == -256) {
240  slotManager.getAnyFreeSlot(ps, ss);
241  } else if (ps < 0) {
242  // specified slot by name (carta, cartb, ...)
243  slotManager.getSpecificSlot(-ps - 1, ps, ss);
244  } else {
245  // numerical specified slot (0, 1, 2, 3)
246  }
247  assert((0 <= ps) && (ps <= 3));
248 
249  if (!getCPUInterface().isExpanded(ps)) {
250  ss = -1;
251  }
252 
253  // Store actual slot in config. This has two purposes:
254  // - Make sure that devices that are grouped under the same
255  // <primary>/<secondary> tags actually use the same slot. (This
256  // matters when the value of some of the slot attributes is "any").
257  // - Fix the slot number so that it remains the same after a
258  // savestate/loadstate.
259  assert(primaryConfig);
260  primaryConfig->setAttribute("slot", strCat(ps));
261  if (secondaryConfig) {
262  string slot = (ss == -1) ? "X" : strCat(ss);
263  secondaryConfig->setAttribute("slot", slot);
264  } else {
265  if (ss != -1) {
266  throw MSXException(
267  "Missing <secondary> tag for device", getName());
268  }
269  }
270 
271  int logicalSS = (ss == -1) ? 0 : ss;
272  for (auto& r : tmpMemRegions) {
274  *this, ps, logicalSS, r.first, r.second);
275  memRegions.push_back(r);
276  }
277 
278  // Mark the slot as 'in-use' so that future searches for free external
279  // slots don't return this slot anymore. If the slot was not an
280  // external slot, this call has no effect. Multiple MSXDevices from the
281  // same extension (the same HardwareConfig) can all allocate the same
282  // slot (later they should also all free this slot).
283  slotManager.allocateSlot(ps, ss, getHardwareConfig());
284 }
285 
286 void MSXDevice::unregisterSlots()
287 {
288  if (memRegions.empty()) return;
289 
290  int logicalSS = (ss == -1) ? 0 : ss;
291  for (auto& r : memRegions) {
293  *this, ps, logicalSS, r.first, r.second);
294  }
295 
296  // See comments above about allocateSlot() for more details:
297  // - has no effect for non-external slots
298  // - can be called multiple times for the same slot
300 }
301 
302 void MSXDevice::getVisibleMemRegion(unsigned& base, unsigned& size) const
303 {
304  // init() must already be called
305  if (memRegions.empty()) {
306  base = 0;
307  size = 0;
308  return;
309  }
310 
311  auto lowest = min_value(view::transform(memRegions,
312  [](auto& r) { return r.first; }));
313  auto highest = max_value(view::transform(memRegions,
314  [](auto& r) { return r.first + r.second; }));
315  assert(lowest <= highest);
316  base = lowest;
317  size = highest - lowest;
318 }
319 
320 void MSXDevice::registerPorts()
321 {
322  for (auto& i : getDeviceConfig().getChildren("io")) {
323  unsigned base = i->getAttributeAsInt("base");
324  unsigned num = i->getAttributeAsInt("num");
325  const auto& type = i->getAttribute("type", "IO");
326  if (((base + num) > 256) || (num == 0) ||
327  ((type != "I") && (type != "O") && (type != "IO"))) {
328  throw MSXException("Invalid IO port specification");
329  }
330  for (unsigned port = base; port < base + num; ++port) {
331  if ((type == "I") || (type == "IO")) {
332  getCPUInterface().register_IO_In(port, this);
333  inPorts.push_back(port);
334  }
335  if ((type == "O") || (type == "IO")) {
336  getCPUInterface().register_IO_Out(port, this);
337  outPorts.push_back(port);
338  }
339  }
340  }
341 }
342 
343 void MSXDevice::unregisterPorts()
344 {
345  for (auto& p : inPorts) {
347  }
348  for (auto& p : outPorts) {
350  }
351 }
352 
353 
354 void MSXDevice::reset(EmuTime::param /*time*/)
355 {
356  // nothing
357 }
358 
360 {
361  return 0xFF;
362 }
363 
364 void MSXDevice::powerDown(EmuTime::param /*time*/)
365 {
366  // nothing
367 }
368 
369 void MSXDevice::powerUp(EmuTime::param time)
370 {
371  reset(time);
372 }
373 
374 string MSXDevice::getName() const
375 {
376  return deviceName;
377 }
378 
380 {
381  result.addListElement(getName());
382 }
383 
385 {
386  result.addDictKeyValue("type", getDeviceConfig().getName());
387  getExtraDeviceInfo(result);
388 }
389 
391 {
392  // nothing
393 }
394 
395 
396 byte MSXDevice::readIO(word /*port*/, EmuTime::param /*time*/)
397 {
398  // read from unmapped IO
399  return 0xFF;
400 }
401 
402 void MSXDevice::writeIO(word /*port*/, byte /*value*/, EmuTime::param /*time*/)
403 {
404  // write to unmapped IO, do nothing
405 }
406 
407 byte MSXDevice::peekIO(word /*port*/, EmuTime::param /*time*/) const
408 {
409  return 0xFF;
410 }
411 
412 
413 byte MSXDevice::readMem(word /*address*/, EmuTime::param /*time*/)
414 {
415  // read from unmapped memory
416  return 0xFF;
417 }
418 
419 const byte* MSXDevice::getReadCacheLine(word /*start*/) const
420 {
421  return nullptr; // uncacheable
422 }
423 
424 void MSXDevice::writeMem(word /*address*/, byte /*value*/,
425  EmuTime::param /*time*/)
426 {
427  // write to unmapped memory, do nothing
428 }
429 
430 byte MSXDevice::peekMem(word address, EmuTime::param /*time*/) const
431 {
432  word base = address & CacheLine::HIGH;
433  if (const byte* cache = getReadCacheLine(base)) {
434  word offset = address & CacheLine::LOW;
435  return cache[offset];
436  } else {
437  // peek not supported for this device
438  return 0xFF;
439  }
440 }
441 
442 void MSXDevice::globalWrite(word /*address*/, byte /*value*/,
443  EmuTime::param /*time*/)
444 {
445  UNREACHABLE;
446 }
447 
448 void MSXDevice::globalRead(word /*address*/, EmuTime::param /*time*/)
449 {
450  UNREACHABLE;
451 }
452 
454 {
455  return nullptr; // uncacheable
456 }
457 
459 {
460  getCPU().invalidateMemCache(start, size);
461 }
462 
463 template<typename Archive>
464 void MSXDevice::serialize(Archive& ar, unsigned /*version*/)
465 {
466  // When this method is called, the method init() has already been
467  // called (thus also registerSlots() and registerPorts()).
468  ar.serialize("name", deviceName);
469 }
471 
472 } // namespace openmsx
virtual void init()
Definition: MSXDevice.cc:51
const DeviceConfig & getDeviceConfig2() const
Definition: MSXDevice.hh:221
MSXDevice * findDevice(string_view name)
Find a MSXDevice by name.
void freeSlot(int ps, int ss, const HardwareConfig &hwConfig)
auto transform(Range &&range, UnaryOp op)
Definition: view.hh:312
Contains the main loop of openMSX.
Definition: Reactor.hh:64
virtual byte * getWriteCacheLine(word start) const
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing...
Definition: MSXDevice.cc:453
void registerMemDevice(MSXDevice &device, int ps, int ss, int base, int size)
Devices can register themself in the MSX slotstructure.
PluggingController & getPluggingController()
CliComm & getCliComm() const
Definition: MSXDevice.cc:149
MSXCPUInterface & getCPUInterface()
const Devices & getReferences() const
Get the device references that are specified for this device.
Definition: MSXDevice.cc:127
auto min_value(InputIterator first, InputIterator last)
Definition: stl.hh:262
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
virtual byte readMem(word address, EmuTime::param time)
Read a byte from a location at a certain time from this device.
Definition: MSXDevice.cc:413
virtual void writeMem(word address, byte value, EmuTime::param time)
Write a given byte to a given location at a certain time to this device.
Definition: MSXDevice.cc:424
virtual void globalWrite(word address, byte value, EmuTime::param time)
Global writes.
Definition: MSXDevice.cc:442
MSXMotherBoard & getMotherBoard() const
virtual byte readIRQVector()
Gets IRQ vector used in IM2.
Definition: MSXDevice.cc:359
virtual std::string getName() const
Returns a human-readable name for this device.
Definition: MSXDevice.cc:374
virtual byte readIO(word port, EmuTime::param time)
Read a byte from an IO port at a certain time from this device.
Definition: MSXDevice.cc:396
Central administration of Connectors and Pluggables.
void unregister_IO_In(byte port, MSXDevice *device)
void move_pop_back(VECTOR &v, typename VECTOR::iterator it)
Erase the pointed to element from the given vector.
Definition: stl.hh:191
void strAppend(std::string &result, Ts &&...ts)
Definition: strCat.hh:648
void invalidateMemCache(word start, unsigned size)
Invalidate the CPU its cache for the interval [start, start + size) For example MSXMemoryMapper and M...
Definition: MSXCPU.cc:147
void serialize(Archive &ar, unsigned version)
Definition: MSXDevice.cc:464
virtual void globalRead(word address, EmuTime::param time)
Global reads.
Definition: MSXDevice.cc:448
void getDeviceInfo(TclObject &result) const
Get device info.
Definition: MSXDevice.cc:384
virtual void powerUp(EmuTime::param time)
This method is called when MSX is powered up.
Definition: MSXDevice.cc:369
virtual byte peekMem(word address, EmuTime::param time) const
Read a byte from a given memory location.
Definition: MSXDevice.cc:430
void register_IO_In(byte port, MSXDevice *device)
Devices can register their In ports.
auto max_value(InputIterator first, InputIterator last)
Definition: stl.hh:281
void register_IO_Out(byte port, MSXDevice *device)
Devices can register their Out ports.
virtual void getExtraDeviceInfo(TclObject &result) const
Definition: MSXDevice.cc:390
void unregisterMemDevice(MSXDevice &device, int ps, int ss, int base, int size)
EmuTime::param getCurrentTime() const
Definition: MSXDevice.cc:133
LedStatus & getLedStatus() const
Definition: MSXDevice.cc:161
virtual const byte * getReadCacheLine(word start) const
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for reading...
Definition: MSXDevice.cc:419
CartridgeSlotManager & getSlotManager()
virtual void powerDown(EmuTime::param time)
This method is called when MSX is powered down.
Definition: MSXDevice.cc:364
void sort(RandomAccessRange &&range)
Definition: ranges.hh:35
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
MSXMotherBoard & getMotherBoard() const
Get the mother board this device belongs to.
Definition: MSXDevice.cc:77
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.
Definition: MSXDevice.cc:402
CommandController & getCommandController()
void unregister_IO_Out(byte port, MSXDevice *device)
auto set_difference(InputRange1 &&range1, InputRange2 &&range2, OutputIter out)
Definition: ranges.hh:209
MSXCPU & getCPU() const
Definition: MSXDevice.cc:137
XMLElement * getSecondary() const
Definition: DeviceConfig.hh:55
Scheduler & getScheduler() const
Definition: MSXDevice.cc:145
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
static byte unmappedRead[0x10000]
Definition: MSXDevice.hh:274
CommandController & getCommandController() const
Definition: MSXDevice.cc:157
std::vector< MSXDevice * > Devices
Definition: MSXDevice.hh:37
void addListElement(T t)
Definition: TclObject.hh:121
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:840
static byte unmappedWrite[0x10000]
Definition: MSXDevice.hh:275
const HardwareConfig & getHardwareConfig() const
Returns the hardwareconfig this device belongs to.
Definition: MSXDevice.hh:43
PluggingController & getPluggingController() const
Definition: MSXDevice.cc:165
XMLElement * getPrimary() const
Definition: DeviceConfig.hh:51
MSXCPUInterface & getCPUInterface() const
Definition: MSXDevice.cc:141
virtual byte peekIO(word port, EmuTime::param time) const
Read a byte from a given IO port.
Definition: MSXDevice.cc:407
virtual void reset(EmuTime::param time)
This method is called on reset.
Definition: MSXDevice.cc:354
virtual void getNameList(TclObject &result) const
Returns list of name(s) of this device.
Definition: MSXDevice.cc:379
EmuTime::param getCurrentTime()
Convenience method: This is the same as getScheduler().getCurrentTime().
std::string strCat(Ts &&...ts)
Definition: strCat.hh:577
void addDictKeyValue(const Key &key, const Value &value)
Definition: TclObject.hh:135
const XMLElement & getDeviceConfig() const
Get the configuration section for this device.
Definition: MSXDevice.hh:218
void getVisibleMemRegion(unsigned &base, unsigned &size) const
Returns the range where this device is visible in memory.
Definition: MSXDevice.cc:302
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
void testRemove(Devices alreadyRemoved) const
Checks whether this device can be removed (no other device has a reference to it).
Definition: MSXDevice.cc:82
MSXDevice(const MSXDevice &)=delete
constexpr auto size(const C &c) -> decltype(c.size())
Definition: span.hh:62
const std::string & getAttribute(string_view attName) const
Definition: XMLElement.cc:231
void invalidateMemCache(word start, unsigned size)
Invalidate CPU memory-mapping cache.
Definition: MSXDevice.cc:458
virtual ~MSXDevice()=0
Definition: MSXDevice.cc:60
Reactor & getReactor() const
Definition: MSXDevice.cc:153
#define UNREACHABLE
Definition: unreachable.hh:38