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 "IterableBitSet.hh"
7 #include "openmsx.hh"
8 #include "serialize_meta.hh"
9 #include "span.hh"
10 #include <string>
11 #include <vector>
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 const 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, std::string_view 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(std::string_view 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 protected:
321  std::string deviceName;
322 
323 private:
324  struct BaseSize {
325  unsigned base;
326  unsigned size;
327  [[nodiscard]] unsigned end() const { return base + size; }
328  };
329  using MemRegions = std::vector<BaseSize>;
330  MemRegions memRegions;
331  IterableBitSet<256> inPorts;
332  IterableBitSet<256> outPorts;
333 
334  DeviceConfig deviceConfig;
335 
336  Devices references;
337  Devices referencedBy;
338 
339  int ps = 0;
340  int ss = 0;
341 };
342 
344 
345 #define REGISTER_MSXDEVICE(CLASS, NAME) \
346 REGISTER_POLYMORPHIC_INITIALIZER(MSXDevice, CLASS, NAME);
347 
348 } // namespace openmsx
349 
350 #endif
const HardwareConfig & getHardwareConfig() const
Definition: DeviceConfig.hh:43
const XMLElement * getXML() const
Definition: DeviceConfig.hh:48
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:71
virtual void init()
Definition: MSXDevice.cc:45
void invalidateDeviceRCache()
Definition: MSXDevice.hh:210
void fillDeviceWCache(unsigned start, unsigned size, byte *wData)
Definition: MSXDevice.cc:520
PluggingController & getPluggingController() const
Definition: MSXDevice.cc:158
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: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:506
virtual ~MSXDevice()=0
Definition: MSXDevice.cc:54
CliComm & getCliComm() const
Definition: MSXDevice.cc:142
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:211
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:146
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:397
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:420
MSXDevice & operator=(const MSXDevice &)=delete
virtual void getExtraDeviceInfo(TclObject &result) const
Definition: MSXDevice.cc:392
std::string deviceName
Definition: MSXDevice.hh:321
void getVisibleMemRegion(unsigned &base, unsigned &size) const
Returns the range where this device is visible in memory.
Definition: MSXDevice.cc:302
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:130
const HardwareConfig & getHardwareConfig() const
Returns the hardwareconfig this device belongs to.
Definition: MSXDevice.hh:44
virtual const std::string & getName() const
Returns a human-readable name for this device.
Definition: MSXDevice.cc:376
static byte unmappedRead[0x10000]
Definition: MSXDevice.hh:301
LedStatus & getLedStatus() const
Definition: MSXDevice.cc:154
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:209
Scheduler & getScheduler() const
Definition: MSXDevice.cc:138
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:150
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:76
const Devices & getReferences() const
Get the device references that are specified for this device.
Definition: MSXDevice.cc:120
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 void reset(EmuTime::param time)
This method is called on reset.
Definition: MSXDevice.cc:356
EmuTime::param getCurrentTime() const
Definition: MSXDevice.cc:126
static byte unmappedWrite[0x10000]
Definition: MSXDevice.hh:302
MSXCPUInterface & getCPUInterface() const
Definition: MSXDevice.cc:134
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:68
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