openMSX
MSXDevice.hh
Go to the documentation of this file.
1 #ifndef MSXDEVICE_HH
2 #define MSXDEVICE_HH
3 
4 #include "DeviceConfig.hh"
5 #include "EmuTime.hh"
6 #include "openmsx.hh"
7 #include "serialize_meta.hh"
8 #include "span.hh"
9 #include <string>
10 #include <vector>
11 #include <utility> // for pair
12 
13 namespace openmsx {
14 
15 class XMLElement;
16 class MSXMotherBoard;
17 class MSXCPU;
18 class MSXCPUInterface;
19 class Scheduler;
20 class CliComm;
21 class Reactor;
22 class CommandController;
23 class LedStatus;
24 class PluggingController;
25 class HardwareConfig;
26 class TclObject;
27 
32 class MSXDevice
33 {
34 public:
35  MSXDevice(const MSXDevice&) = delete;
36  MSXDevice& operator=(const MSXDevice&) = delete;
37 
38  using Devices = std::vector<MSXDevice*>;
39 
40  virtual ~MSXDevice() = 0;
41 
44  [[nodiscard]] const HardwareConfig& getHardwareConfig() const {
45  return deviceConfig.getHardwareConfig();
46  }
47 
51  void testRemove(span<const std::unique_ptr<MSXDevice>> removed) const;
52 
57  virtual void reset(EmuTime::param time);
58 
66  [[nodiscard]] virtual byte readIRQVector();
67 
74  virtual void powerDown(EmuTime::param time);
75 
81  virtual void powerUp(EmuTime::param time);
82 
87  [[nodiscard]] virtual std::string getName() const;
88 
93  virtual void getNameList(TclObject& result) const;
94 
98  void getDeviceInfo(TclObject& result) const;
99 
106  void getVisibleMemRegion(unsigned& base, unsigned& size) const;
107 
108  // IO
109 
114  [[nodiscard]] virtual byte readIO(word port, EmuTime::param time);
115 
121  virtual void writeIO(word port, byte value, EmuTime::param time);
122 
131  [[nodiscard]] virtual byte peekIO(word port, EmuTime::param time) const;
132 
133 
134  // Memory
135 
141  [[nodiscard]] virtual byte readMem(word address, EmuTime::param time);
142 
148  virtual void writeMem(word address, byte value, EmuTime::param time);
149 
161  [[nodiscard]] virtual const byte* getReadCacheLine(word start) const;
162 
174  [[nodiscard]] virtual byte* getWriteCacheLine(word start) const;
175 
188  [[nodiscard]] virtual byte peekMem(word address, EmuTime::param time) const;
189 
198  virtual void globalWrite(word address, byte value, EmuTime::param time);
199 
204  virtual void globalRead(word address, EmuTime::param time);
205 
210  void invalidateDeviceRCache() { invalidateDeviceRCache (0x0000, 0x10000); }
211  void invalidateDeviceWCache() { invalidateDeviceWCache (0x0000, 0x10000); }
212  void invalidateDeviceRWCache(unsigned start, unsigned size);
213  void invalidateDeviceRCache (unsigned start, unsigned size);
214  void invalidateDeviceWCache (unsigned start, unsigned size);
215 
219  void fillDeviceRWCache(unsigned start, unsigned size, byte* rwData);
220  void fillDeviceRWCache(unsigned start, unsigned size, const byte* rData, byte* wData);
221  void fillDeviceRCache (unsigned start, unsigned size, const byte* rData);
222  void fillDeviceWCache (unsigned start, unsigned size, byte* wData);
223 
226  [[nodiscard]] MSXMotherBoard& getMotherBoard() const;
227 
231  [[nodiscard]] const XMLElement& getDeviceConfig() const {
232  return *deviceConfig.getXML();
233  }
234  [[nodiscard]] const DeviceConfig& getDeviceConfig2() const { // TODO
235  return deviceConfig;
236  }
237 
240  [[nodiscard]] const Devices& getReferences() const;
241 
242  // convenience functions, these delegate to MSXMotherBoard
243  [[nodiscard]] EmuTime::param getCurrentTime() const;
244  [[nodiscard]] MSXCPU& getCPU() const;
245  [[nodiscard]] MSXCPUInterface& getCPUInterface() const;
246  [[nodiscard]] Scheduler& getScheduler() const;
247  [[nodiscard]] CliComm& getCliComm() const;
248  [[nodiscard]] Reactor& getReactor() const;
249  [[nodiscard]] CommandController& getCommandController() const;
250  [[nodiscard]] PluggingController& getPluggingController() const;
251  [[nodiscard]] LedStatus& getLedStatus() const;
252 
253  template<typename Archive>
254  void serialize(Archive& ar, unsigned version);
255 
256 protected:
262  MSXDevice(const DeviceConfig& config, const std::string& name);
263  explicit MSXDevice(const DeviceConfig& config);
264 
276  friend class DeviceFactory;
277  virtual void init();
278 
283  [[nodiscard]] virtual unsigned getBaseSizeAlignment() const;
284 
291  [[nodiscard]] virtual bool allowUnaligned() const { return false; }
292 
297  virtual void getExtraDeviceInfo(TclObject& result) const;
298 
299 public:
300  // public to allow non-MSXDevices to use these same arrays
301  static inline byte unmappedRead [0x10000]; // Read only
302  static inline byte unmappedWrite[0x10000]; // Write only
303 
304 private:
305  template<typename Action, typename... Args>
306  void clip(unsigned start, unsigned size, Action action, Args... args);
307 
308  void initName(const std::string& name);
309  static void staticInit();
310 
311  void lockDevices();
312  void unlockDevices();
313 
314  void registerSlots();
315  void unregisterSlots();
316 
317  void registerPorts();
318  void unregisterPorts();
319 
320 private:
321  using MemRegions = std::vector<std::pair<unsigned, unsigned>>;
322  MemRegions memRegions;
323  std::vector<byte> inPorts;
324  std::vector<byte> outPorts;
325 
326  DeviceConfig deviceConfig;
327  std::string deviceName;
328 
329  Devices references;
330  Devices referencedBy;
331 
332  int ps = 0;
333  int ss = 0;
334 };
335 
337 
338 #define REGISTER_MSXDEVICE(CLASS, NAME) \
339 REGISTER_POLYMORPHIC_INITIALIZER(MSXDevice, CLASS, NAME);
340 
341 } // namespace openmsx
342 
343 #endif
const HardwareConfig & getHardwareConfig() const
Definition: DeviceConfig.hh:42
const XMLElement * getXML() const
Definition: DeviceConfig.hh:47
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition: MSXDevice.hh:33
MSXMotherBoard & getMotherBoard() const
Get the mother board this device belongs to.
Definition: MSXDevice.cc:76
virtual void init()
Definition: MSXDevice.cc:50
void invalidateDeviceRCache()
Definition: MSXDevice.hh:210
void fillDeviceWCache(unsigned start, unsigned size, byte *wData)
Definition: MSXDevice.cc:525
PluggingController & getPluggingController() const
Definition: MSXDevice.cc:163
virtual void powerUp(EmuTime::param time)
This method is called when MSX is powered up.
Definition: MSXDevice.cc:376
const DeviceConfig & getDeviceConfig2() const
Definition: MSXDevice.hh:234
void fillDeviceRWCache(unsigned start, unsigned size, byte *rwData)
Calls MSXCPUInterface::fillXXCache() for the specific (part of) the slot that this device is located ...
Definition: MSXDevice.cc:511
virtual ~MSXDevice()=0
Definition: MSXDevice.cc:59
CliComm & getCliComm() const
Definition: MSXDevice.cc:147
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:431
void invalidateDeviceWCache()
Definition: MSXDevice.hh:211
void getDeviceInfo(TclObject &result) const
Get device info.
Definition: MSXDevice.cc:391
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:414
virtual byte readIRQVector()
Gets IRQ vector used in IM2.
Definition: MSXDevice.cc:366
virtual void globalWrite(word address, byte value, EmuTime::param time)
Global writes.
Definition: MSXDevice.cc:454
Reactor & getReactor() const
Definition: MSXDevice.cc:151
virtual bool allowUnaligned() const
By default we don't allow unaligned <mem> specifications in the config file.
Definition: MSXDevice.hh:291
virtual unsigned getBaseSizeAlignment() const
The 'base' and 'size' attribute values need to be at least aligned to CacheLine::SIZE.
Definition: MSXDevice.cc:402
MSXDevice(const MSXDevice &)=delete
const XMLElement & getDeviceConfig() const
Get the configuration section for this device.
Definition: MSXDevice.hh:231
std::vector< MSXDevice * > Devices
Definition: MSXDevice.hh:38
virtual byte readMem(word address, EmuTime::param time)
Read a byte from a location at a certain time from this device.
Definition: MSXDevice.cc:425
MSXDevice & operator=(const MSXDevice &)=delete
virtual void getExtraDeviceInfo(TclObject &result) const
Definition: MSXDevice.cc:397
void getVisibleMemRegion(unsigned &base, unsigned &size) const
Returns the range where this device is visible in memory.
Definition: MSXDevice.cc:309
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:436
virtual byte peekIO(word port, EmuTime::param time) const
Read a byte from a given IO port.
Definition: MSXDevice.cc:419
virtual byte peekMem(word address, EmuTime::param time) const
Read a byte from a given memory location.
Definition: MSXDevice.cc:442
MSXCPU & getCPU() const
Definition: MSXDevice.cc:135
const HardwareConfig & getHardwareConfig() const
Returns the hardwareconfig this device belongs to.
Definition: MSXDevice.hh:44
static byte unmappedRead[0x10000]
Definition: MSXDevice.hh:301
LedStatus & getLedStatus() const
Definition: MSXDevice.cc:159
void serialize(Archive &ar, unsigned version)
Definition: MSXDevice.cc:532
void invalidateDeviceRWCache()
Calls MSXCPUInterface::invalidateXXCache() for the specific (part of) the slot that this device is lo...
Definition: MSXDevice.hh:209
Scheduler & getScheduler() const
Definition: MSXDevice.cc:143
virtual void getNameList(TclObject &result) const
Returns list of name(s) of this device.
Definition: MSXDevice.cc:386
virtual byte * getWriteCacheLine(word start) const
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing.
Definition: MSXDevice.cc:465
void fillDeviceRCache(unsigned start, unsigned size, const byte *rData)
Definition: MSXDevice.cc:520
CommandController & getCommandController() const
Definition: MSXDevice.cc:155
void testRemove(span< const std::unique_ptr< MSXDevice >> removed) const
Checks whether this device can be removed (no other device has a reference to it).
Definition: MSXDevice.cc:81
const Devices & getReferences() const
Get the device references that are specified for this device.
Definition: MSXDevice.cc:125
virtual void powerDown(EmuTime::param time)
This method is called when MSX is powered down.
Definition: MSXDevice.cc:371
virtual void globalRead(word address, EmuTime::param time)
Global reads.
Definition: MSXDevice.cc:460
virtual std::string getName() const
Returns a human-readable name for this device.
Definition: MSXDevice.cc:381
virtual void reset(EmuTime::param time)
This method is called on reset.
Definition: MSXDevice.cc:361
EmuTime::param getCurrentTime() const
Definition: MSXDevice.cc:131
static byte unmappedWrite[0x10000]
Definition: MSXDevice.hh:302
MSXCPUInterface & getCPUInterface() const
Definition: MSXDevice.cc:139
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:408
Central administration of Connectors and Pluggables.
Contains the main loop of openMSX.
Definition: Reactor.hh:67
Definition: span.hh:126
This file implemented 3 utility functions:
Definition: Autofire.cc:9
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
REGISTER_BASE_NAME_HELPER(MSXFDC, "FDC")
size_t size(std::string_view utf8)