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 
12 namespace openmsx {
13 
14 class XMLElement;
15 class MSXMotherBoard;
16 class MSXCPU;
17 class MSXCPUInterface;
18 class Scheduler;
19 class CliComm;
20 class Reactor;
21 class CommandController;
22 class LedStatus;
23 class PluggingController;
24 class HardwareConfig;
25 class TclObject;
26 
31 class MSXDevice
32 {
33 public:
34  MSXDevice(const MSXDevice&) = delete;
35  MSXDevice& operator=(const MSXDevice&) = delete;
36 
37  using Devices = std::vector<MSXDevice*>;
38 
39  virtual ~MSXDevice() = 0;
40 
43  [[nodiscard]] const HardwareConfig& getHardwareConfig() const {
44  return deviceConfig.getHardwareConfig();
45  }
46 
50  void testRemove(span<const std::unique_ptr<MSXDevice>> removed) const;
51 
56  virtual void reset(EmuTime::param time);
57 
65  [[nodiscard]] virtual byte readIRQVector();
66 
73  virtual void powerDown(EmuTime::param time);
74 
80  virtual void powerUp(EmuTime::param time);
81 
86  [[nodiscard]] virtual std::string getName() const;
87 
92  virtual void getNameList(TclObject& result) const;
93 
97  void getDeviceInfo(TclObject& result) const;
98 
105  void getVisibleMemRegion(unsigned& base, unsigned& size) const;
106 
107  // IO
108 
113  [[nodiscard]] virtual byte readIO(word port, EmuTime::param time);
114 
120  virtual void writeIO(word port, byte value, EmuTime::param time);
121 
130  [[nodiscard]] virtual byte peekIO(word port, EmuTime::param time) const;
131 
132 
133  // Memory
134 
140  [[nodiscard]] virtual byte readMem(word address, EmuTime::param time);
141 
147  virtual void writeMem(word address, byte value, EmuTime::param time);
148 
160  [[nodiscard]] virtual const byte* getReadCacheLine(word start) const;
161 
173  [[nodiscard]] virtual byte* getWriteCacheLine(word start) const;
174 
187  [[nodiscard]] virtual byte peekMem(word address, EmuTime::param time) const;
188 
197  virtual void globalWrite(word address, byte value, EmuTime::param time);
198 
203  virtual void globalRead(word address, EmuTime::param time);
204 
209  void invalidateDeviceRCache() { invalidateDeviceRCache (0x0000, 0x10000); }
210  void invalidateDeviceWCache() { invalidateDeviceWCache (0x0000, 0x10000); }
211  void invalidateDeviceRWCache(unsigned start, unsigned size);
212  void invalidateDeviceRCache (unsigned start, unsigned size);
213  void invalidateDeviceWCache (unsigned start, unsigned size);
214 
218  void fillDeviceRWCache(unsigned start, unsigned size, byte* rwData);
219  void fillDeviceRWCache(unsigned start, unsigned size, const byte* rData, byte* wData);
220  void fillDeviceRCache (unsigned start, unsigned size, const byte* rData);
221  void fillDeviceWCache (unsigned start, unsigned size, byte* wData);
222 
225  [[nodiscard]] MSXMotherBoard& getMotherBoard() const;
226 
230  [[nodiscard]] const XMLElement& getDeviceConfig() const {
231  return *deviceConfig.getXML();
232  }
233  [[nodiscard]] const DeviceConfig& getDeviceConfig2() const { // TODO
234  return deviceConfig;
235  }
236 
239  [[nodiscard]] const Devices& getReferences() const;
240 
241  // convenience functions, these delegate to MSXMotherBoard
242  [[nodiscard]] EmuTime::param getCurrentTime() const;
243  [[nodiscard]] MSXCPU& getCPU() const;
244  [[nodiscard]] MSXCPUInterface& getCPUInterface() const;
245  [[nodiscard]] Scheduler& getScheduler() const;
246  [[nodiscard]] CliComm& getCliComm() const;
247  [[nodiscard]] Reactor& getReactor() const;
248  [[nodiscard]] CommandController& getCommandController() const;
249  [[nodiscard]] PluggingController& getPluggingController() const;
250  [[nodiscard]] LedStatus& getLedStatus() const;
251 
252  template<typename Archive>
253  void serialize(Archive& ar, unsigned version);
254 
255 protected:
261  MSXDevice(const DeviceConfig& config, const std::string& name);
262  explicit MSXDevice(const DeviceConfig& config);
263 
275  friend class DeviceFactory;
276  virtual void init();
277 
282  [[nodiscard]] virtual unsigned getBaseSizeAlignment() const;
283 
290  [[nodiscard]] virtual bool allowUnaligned() const { return false; }
291 
296  virtual void getExtraDeviceInfo(TclObject& result) const;
297 
298 public:
299  // public to allow non-MSXDevices to use these same arrays
300  static inline byte unmappedRead [0x10000]; // Read only
301  static inline byte unmappedWrite[0x10000]; // Write only
302 
303 private:
304  template<typename Action, typename... Args>
305  void clip(unsigned start, unsigned size, Action action, Args... args);
306 
307  void initName(const std::string& name);
308  static void staticInit();
309 
310  void lockDevices();
311  void unlockDevices();
312 
313  void registerSlots();
314  void unregisterSlots();
315 
316  void registerPorts();
317  void unregisterPorts();
318 
319 private:
320  struct BaseSize {
321  unsigned base;
322  unsigned size;
323  [[nodiscard]] unsigned end() const { return base + size; }
324  };
325  using MemRegions = std::vector<BaseSize>;
326  MemRegions memRegions;
327  std::vector<byte> inPorts;
328  std::vector<byte> outPorts;
329 
330  DeviceConfig deviceConfig;
331  std::string deviceName;
332 
333  Devices references;
334  Devices referencedBy;
335 
336  int ps = 0;
337  int ss = 0;
338 };
339 
341 
342 #define REGISTER_MSXDEVICE(CLASS, NAME) \
343 REGISTER_POLYMORPHIC_INITIALIZER(MSXDevice, CLASS, NAME);
344 
345 } // namespace openmsx
346 
347 #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:32
MSXMotherBoard & getMotherBoard() const
Get the mother board this device belongs to.
Definition: MSXDevice.cc:73
virtual void init()
Definition: MSXDevice.cc:47
void invalidateDeviceRCache()
Definition: MSXDevice.hh:209
void fillDeviceWCache(unsigned start, unsigned size, byte *wData)
Definition: MSXDevice.cc:520
PluggingController & getPluggingController() const
Definition: MSXDevice.cc:160
virtual void powerUp(EmuTime::param time)
This method is called when MSX is powered up.
Definition: MSXDevice.cc:371
const DeviceConfig & getDeviceConfig2() const
Definition: MSXDevice.hh:233
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:506
virtual ~MSXDevice()=0
Definition: MSXDevice.cc:56
CliComm & getCliComm() const
Definition: MSXDevice.cc:144
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:426
void invalidateDeviceWCache()
Definition: MSXDevice.hh:210
void getDeviceInfo(TclObject &result) const
Get device info.
Definition: MSXDevice.cc:386
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:409
virtual byte readIRQVector()
Gets IRQ vector used in IM2.
Definition: MSXDevice.cc:361
virtual void globalWrite(word address, byte value, EmuTime::param time)
Global writes.
Definition: MSXDevice.cc:449
Reactor & getReactor() const
Definition: MSXDevice.cc:148
virtual bool allowUnaligned() const
By default we don't allow unaligned <mem> specifications in the config file.
Definition: MSXDevice.hh:290
virtual unsigned getBaseSizeAlignment() const
The 'base' and 'size' attribute values need to be at least aligned to CacheLine::SIZE.
Definition: MSXDevice.cc:397
MSXDevice(const MSXDevice &)=delete
const XMLElement & getDeviceConfig() const
Get the configuration section for this device.
Definition: MSXDevice.hh:230
std::vector< MSXDevice * > Devices
Definition: MSXDevice.hh:37
virtual byte readMem(word address, EmuTime::param time)
Read a byte from a location at a certain time from this device.
Definition: MSXDevice.cc:420
MSXDevice & operator=(const MSXDevice &)=delete
virtual void getExtraDeviceInfo(TclObject &result) const
Definition: MSXDevice.cc:392
void getVisibleMemRegion(unsigned &base, unsigned &size) const
Returns the range where this device is visible in memory.
Definition: MSXDevice.cc:306
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:431
virtual byte peekIO(word port, EmuTime::param time) const
Read a byte from a given IO port.
Definition: MSXDevice.cc:414
virtual byte peekMem(word address, EmuTime::param time) const
Read a byte from a given memory location.
Definition: MSXDevice.cc:437
MSXCPU & getCPU() const
Definition: MSXDevice.cc:132
const HardwareConfig & getHardwareConfig() const
Returns the hardwareconfig this device belongs to.
Definition: MSXDevice.hh:43
static byte unmappedRead[0x10000]
Definition: MSXDevice.hh:300
LedStatus & getLedStatus() const
Definition: MSXDevice.cc:156
void serialize(Archive &ar, unsigned version)
Definition: MSXDevice.cc:527
void invalidateDeviceRWCache()
Calls MSXCPUInterface::invalidateXXCache() for the specific (part of) the slot that this device is lo...
Definition: MSXDevice.hh:208
Scheduler & getScheduler() const
Definition: MSXDevice.cc:140
virtual void getNameList(TclObject &result) const
Returns list of name(s) of this device.
Definition: MSXDevice.cc:381
virtual byte * getWriteCacheLine(word start) const
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing.
Definition: MSXDevice.cc:460
void fillDeviceRCache(unsigned start, unsigned size, const byte *rData)
Definition: MSXDevice.cc:515
CommandController & getCommandController() const
Definition: MSXDevice.cc:152
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:78
const Devices & getReferences() const
Get the device references that are specified for this device.
Definition: MSXDevice.cc:122
virtual void powerDown(EmuTime::param time)
This method is called when MSX is powered down.
Definition: MSXDevice.cc:366
virtual void globalRead(word address, EmuTime::param time)
Global reads.
Definition: MSXDevice.cc:455
virtual std::string getName() const
Returns a human-readable name for this device.
Definition: MSXDevice.cc:376
virtual void reset(EmuTime::param time)
This method is called on reset.
Definition: MSXDevice.cc:356
EmuTime::param getCurrentTime() const
Definition: MSXDevice.cc:128
static byte unmappedWrite[0x10000]
Definition: MSXDevice.hh:301
MSXCPUInterface & getCPUInterface() const
Definition: MSXDevice.cc:136
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:403
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)
constexpr auto end(const zstring_view &x)
Definition: zstring_view.hh:84