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 <string>
9 #include <vector>
10 #include <utility> // for pair
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 
44  return deviceConfig.getHardwareConfig();
45  }
46 
50  void testRemove(Devices alreadyRemoved) const;
51 
56  virtual void reset(EmuTime::param time);
57 
65  virtual byte readIRQVector();
66 
73  virtual void powerDown(EmuTime::param time);
74 
80  virtual void powerUp(EmuTime::param time);
81 
86  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  virtual byte readIO(word port, EmuTime::param time);
114 
120  virtual void writeIO(word port, byte value, EmuTime::param time);
121 
130  virtual byte peekIO(word port, EmuTime::param time) const;
131 
132 
133  // Memory
134 
140  virtual byte readMem(word address, EmuTime::param time);
141 
147  virtual void writeMem(word address, byte value, EmuTime::param time);
148 
160  virtual const byte* getReadCacheLine(word start) const;
161 
173  virtual byte* getWriteCacheLine(word start) const;
174 
187  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 
226 
230  const XMLElement& getDeviceConfig() const {
231  return *deviceConfig.getXML();
232  }
233  const DeviceConfig& getDeviceConfig2() const { // TODO
234  return deviceConfig;
235  }
236 
239  const Devices& getReferences() const;
240 
241  // convenience functions, these delegate to MSXMotherBoard
242  EmuTime::param getCurrentTime() const;
243  MSXCPU& getCPU() const;
245  Scheduler& getScheduler() const;
246  CliComm& getCliComm() const;
247  Reactor& getReactor() const;
250  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  virtual unsigned getBaseSizeAlignment() const;
283 
288  virtual void getExtraDeviceInfo(TclObject& result) const;
289 
290 public:
291  // public to allow non-MSXDevices to use these same arrays
292  static inline byte unmappedRead [0x10000]; // Read only
293  static inline byte unmappedWrite[0x10000]; // Write only
294 
295 private:
296  template<typename Action, typename... Args>
297  void clip(unsigned start, unsigned size, Action action, Args... args);
298 
299  void initName(const std::string& name);
300  void staticInit();
301 
302  void lockDevices();
303  void unlockDevices();
304 
305  void registerSlots();
306  void unregisterSlots();
307 
308  void registerPorts();
309  void unregisterPorts();
310 
311 private:
312  using MemRegions = std::vector<std::pair<unsigned, unsigned>>;
313  MemRegions memRegions;
314  std::vector<byte> inPorts;
315  std::vector<byte> outPorts;
316 
317  DeviceConfig deviceConfig;
318  std::string deviceName;
319 
320  Devices references;
321  Devices referencedBy;
322 
323  int ps;
324  int ss;
325 };
326 
328 
329 #define REGISTER_MSXDEVICE(CLASS, NAME) \
330 REGISTER_POLYMORPHIC_INITIALIZER(MSXDevice, CLASS, NAME);
331 
332 } // namespace openmsx
333 
334 #endif
openmsx::HardwareConfig
HardwareConfig
Definition: HardwareConfig.cc:459
openmsx::MSXDevice
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition: MSXDevice.hh:31
openmsx::MSXDevice::MSXDevice
MSXDevice(const MSXDevice &)=delete
openmsx.hh
openmsx::Scheduler
Definition: Scheduler.hh:33
openmsx::MSXDevice::fillDeviceRCache
void fillDeviceRCache(unsigned start, unsigned size, const byte *rData)
Definition: MSXDevice.cc:517
openmsx::MSXDevice::writeIO
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:412
openmsx::MSXDevice::getReadCacheLine
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:429
openmsx::CommandController
Definition: CommandController.hh:17
openmsx::REGISTER_BASE_NAME_HELPER
REGISTER_BASE_NAME_HELPER(MSXFDC, "FDC")
openmsx::MSXDevice::readMem
virtual byte readMem(word address, EmuTime::param time)
Read a byte from a location at a certain time from this device.
Definition: MSXDevice.cc:423
openmsx::MSXDevice::globalRead
virtual void globalRead(word address, EmuTime::param time)
Global reads.
Definition: MSXDevice.cc:458
openmsx::MSXDevice::~MSXDevice
virtual ~MSXDevice()=0
Definition: MSXDevice.cc:57
openmsx::MSXDevice::getWriteCacheLine
virtual byte * getWriteCacheLine(word start) const
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing.
Definition: MSXDevice.cc:463
openmsx::MSXDevice::getScheduler
Scheduler & getScheduler() const
Definition: MSXDevice.cc:142
openmsx::DeviceConfig
Definition: DeviceConfig.hh:19
utf8::unchecked::size
size_t size(std::string_view utf8)
Definition: utf8_unchecked.hh:227
serialize_meta.hh
openmsx::MSXDevice::unmappedWrite
static byte unmappedWrite[0x10000]
Definition: MSXDevice.hh:293
openmsx::XMLElement
XMLElement
Definition: XMLElement.cc:348
openmsx::MSXDevice::operator=
MSXDevice & operator=(const MSXDevice &)=delete
openmsx::MSXDevice::serialize
void serialize(Archive &ar, unsigned version)
Definition: MSXDevice.cc:527
openmsx::MSXDevice::globalWrite
virtual void globalWrite(word address, byte value, EmuTime::param time)
Global writes.
Definition: MSXDevice.cc:452
openmsx::MSXDevice::Devices
std::vector< MSXDevice * > Devices
Definition: MSXDevice.hh:37
openmsx::MSXDevice::getCPUInterface
MSXCPUInterface & getCPUInterface() const
Definition: MSXDevice.cc:138
openmsx::MSXDevice::writeMem
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:434
openmsx::MSXCPU
MSXCPU
Definition: MSXCPU.cc:549
openmsx::MSXCPUInterface
Definition: MSXCPUInterface.hh:63
openmsx::MSXDevice::peekMem
virtual byte peekMem(word address, EmuTime::param time) const
Read a byte from a given memory location.
Definition: MSXDevice.cc:440
openmsx::MSXDevice::readIRQVector
virtual byte readIRQVector()
Gets IRQ vector used in IM2.
Definition: MSXDevice.cc:364
openmsx::MSXDevice::reset
virtual void reset(EmuTime::param time)
This method is called on reset.
Definition: MSXDevice.cc:359
openmsx::Reactor
Contains the main loop of openMSX.
Definition: Reactor.hh:66
openmsx::DeviceFactory
Definition: DeviceFactory.hh:17
openmsx::LedStatus
Definition: LedStatus.hh:16
openmsx::XMLElement
Definition: XMLElement.hh:15
openmsx::MSXDevice::fillDeviceWCache
void fillDeviceWCache(unsigned start, unsigned size, byte *wData)
Definition: MSXDevice.cc:521
openmsx::MSXDevice::invalidateDeviceWCache
void invalidateDeviceWCache()
Definition: MSXDevice.hh:210
openmsx::MSXDevice::getVisibleMemRegion
void getVisibleMemRegion(unsigned &base, unsigned &size) const
Returns the range where this device is visible in memory.
Definition: MSXDevice.cc:307
openmsx::MSXDevice::getCurrentTime
EmuTime::param getCurrentTime() const
Definition: MSXDevice.cc:130
openmsx::MSXDevice::getDeviceInfo
void getDeviceInfo(TclObject &result) const
Get device info.
Definition: MSXDevice.cc:389
openmsx::MSXMotherBoard
Definition: MSXMotherBoard.hh:59
openmsx::MSXCPU
Definition: MSXCPU.hh:28
openmsx::MSXDevice::getMotherBoard
MSXMotherBoard & getMotherBoard() const
Get the mother board this device belongs to.
Definition: MSXDevice.cc:74
openmsx::DeviceConfig::getHardwareConfig
const HardwareConfig & getHardwareConfig() const
Definition: DeviceConfig.hh:42
openmsx::MSXDevice::getLedStatus
LedStatus & getLedStatus() const
Definition: MSXDevice.cc:158
openmsx::MSXDevice::getDeviceConfig2
const DeviceConfig & getDeviceConfig2() const
Definition: MSXDevice.hh:233
openmsx::MSXDevice::getCliComm
CliComm & getCliComm() const
Definition: MSXDevice.cc:146
openmsx::MSXDevice::invalidateDeviceRCache
void invalidateDeviceRCache()
Definition: MSXDevice.hh:209
openmsx::MSXDevice::invalidateDeviceRWCache
void invalidateDeviceRWCache()
Calls MSXCPUInterface::invalidateXXCache() for the specific (part of) the slot that this device is lo...
Definition: MSXDevice.hh:208
openmsx::MSXCPUInterface
MSXCPUInterface
Definition: MSXCPUInterface.cc:1274
openmsx::MSXDevice::powerDown
virtual void powerDown(EmuTime::param time)
This method is called when MSX is powered down.
Definition: MSXDevice.cc:369
openmsx::MSXDevice::getBaseSizeAlignment
virtual unsigned getBaseSizeAlignment() const
The 'base' and 'size' attribute values need to be at least aligned to CacheLine::SIZE.
Definition: MSXDevice.cc:400
openmsx::MSXDevice::getReferences
const Devices & getReferences() const
Get the device references that are specified for this device.
Definition: MSXDevice.cc:124
openmsx::MSXDevice::getCommandController
CommandController & getCommandController() const
Definition: MSXDevice.cc:154
openmsx::MSXDevice::getPluggingController
PluggingController & getPluggingController() const
Definition: MSXDevice.cc:162
openmsx::MSXDevice::getName
virtual std::string getName() const
Returns a human-readable name for this device.
Definition: MSXDevice.cc:379
openmsx::MSXDevice::readIO
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:406
openmsx::DeviceConfig::getXML
const XMLElement * getXML() const
Definition: DeviceConfig.hh:47
EmuTime.hh
openmsx::MSXDevice::unmappedRead
static byte unmappedRead[0x10000]
Definition: MSXDevice.hh:292
openmsx::word
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
openmsx::MSXDevice::init
virtual void init()
Definition: MSXDevice.cc:48
Math::clip
int clip(int x)
Clips x to the range [LO,HI].
Definition: Math.hh:102
openmsx::CliComm
Definition: CliComm.hh:10
openmsx::HardwareConfig
Definition: HardwareConfig.hh:21
openmsx::Scheduler
Scheduler
Definition: Scheduler.cc:133
openmsx::MSXDevice::getHardwareConfig
const HardwareConfig & getHardwareConfig() const
Returns the hardwareconfig this device belongs to.
Definition: MSXDevice.hh:43
openmsx::TclObject
Definition: TclObject.hh:21
DeviceConfig.hh
openmsx::MSXDevice::getNameList
virtual void getNameList(TclObject &result) const
Returns list of name(s) of this device.
Definition: MSXDevice.cc:384
openmsx::MSXDevice::peekIO
virtual byte peekIO(word port, EmuTime::param time) const
Read a byte from a given IO port.
Definition: MSXDevice.cc:417
openmsx::MSXDevice::testRemove
void testRemove(Devices alreadyRemoved) const
Checks whether this device can be removed (no other device has a reference to it).
Definition: MSXDevice.cc:79
openmsx::PluggingController
Central administration of Connectors and Pluggables.
Definition: PluggingController.hh:21
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::MSXDevice::getReactor
Reactor & getReactor() const
Definition: MSXDevice.cc:150
openmsx::MSXDevice::getExtraDeviceInfo
virtual void getExtraDeviceInfo(TclObject &result) const
Definition: MSXDevice.cc:395
openmsx::MSXDevice::powerUp
virtual void powerUp(EmuTime::param time)
This method is called when MSX is powered up.
Definition: MSXDevice.cc:374
openmsx::MSXDevice::getCPU
MSXCPU & getCPU() const
Definition: MSXDevice.cc:134
openmsx::MSXDevice::getDeviceConfig
const XMLElement & getDeviceConfig() const
Get the configuration section for this device.
Definition: MSXDevice.hh:230
openmsx::MSXDevice::fillDeviceRWCache
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:509