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 
290  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  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  using MemRegions = std::vector<std::pair<unsigned, unsigned>>;
321  MemRegions memRegions;
322  std::vector<byte> inPorts;
323  std::vector<byte> outPorts;
324 
325  DeviceConfig deviceConfig;
326  std::string deviceName;
327 
328  Devices references;
329  Devices referencedBy;
330 
331  int ps;
332  int ss;
333 };
334 
336 
337 #define REGISTER_MSXDEVICE(CLASS, NAME) \
338 REGISTER_POLYMORPHIC_INITIALIZER(MSXDevice, CLASS, NAME);
339 
340 } // namespace openmsx
341 
342 #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:32
openmsx::MSXDevice::MSXDevice
MSXDevice(const MSXDevice &)=delete
openmsx.hh
openmsx::Scheduler
Definition: Scheduler.hh:34
openmsx::MSXDevice::fillDeviceRCache
void fillDeviceRCache(unsigned start, unsigned size, const byte *rData)
Definition: MSXDevice.cc:520
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:414
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:431
openmsx::CommandController
Definition: CommandController.hh:18
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:425
openmsx::MSXDevice::globalRead
virtual void globalRead(word address, EmuTime::param time)
Global reads.
Definition: MSXDevice.cc:460
openmsx::MSXDevice::~MSXDevice
virtual ~MSXDevice()=0
Definition: MSXDevice.cc:58
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:465
openmsx::MSXDevice::getScheduler
Scheduler & getScheduler() const
Definition: MSXDevice.cc:143
openmsx::DeviceConfig
Definition: DeviceConfig.hh:20
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:301
openmsx::XMLElement
XMLElement
Definition: XMLElement.cc:350
openmsx::MSXDevice::operator=
MSXDevice & operator=(const MSXDevice &)=delete
openmsx::MSXDevice::serialize
void serialize(Archive &ar, unsigned version)
Definition: MSXDevice.cc:532
openmsx::MSXDevice::globalWrite
virtual void globalWrite(word address, byte value, EmuTime::param time)
Global writes.
Definition: MSXDevice.cc:454
openmsx::MSXDevice::Devices
std::vector< MSXDevice * > Devices
Definition: MSXDevice.hh:37
openmsx::MSXDevice::getCPUInterface
MSXCPUInterface & getCPUInterface() const
Definition: MSXDevice.cc:139
openmsx::MSXDevice::allowUnaligned
virtual bool allowUnaligned() const
By default we don't allow unaligned <mem> specifications in the config file.
Definition: MSXDevice.hh:290
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:436
openmsx::MSXCPU
MSXCPU
Definition: MSXCPU.cc:563
openmsx::MSXCPUInterface
Definition: MSXCPUInterface.hh:64
openmsx::MSXDevice::peekMem
virtual byte peekMem(word address, EmuTime::param time) const
Read a byte from a given memory location.
Definition: MSXDevice.cc:442
openmsx::MSXDevice::readIRQVector
virtual byte readIRQVector()
Gets IRQ vector used in IM2.
Definition: MSXDevice.cc:366
openmsx::MSXDevice::reset
virtual void reset(EmuTime::param time)
This method is called on reset.
Definition: MSXDevice.cc:361
openmsx::Reactor
Contains the main loop of openMSX.
Definition: Reactor.hh:67
openmsx::DeviceFactory
Definition: DeviceFactory.hh:18
openmsx::LedStatus
Definition: LedStatus.hh:17
openmsx::XMLElement
Definition: XMLElement.hh:16
openmsx::MSXDevice::fillDeviceWCache
void fillDeviceWCache(unsigned start, unsigned size, byte *wData)
Definition: MSXDevice.cc:525
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:309
openmsx::MSXDevice::getCurrentTime
EmuTime::param getCurrentTime() const
Definition: MSXDevice.cc:131
openmsx::MSXDevice::getDeviceInfo
void getDeviceInfo(TclObject &result) const
Get device info.
Definition: MSXDevice.cc:391
openmsx::MSXMotherBoard
Definition: MSXMotherBoard.hh:60
openmsx::MSXCPU
Definition: MSXCPU.hh:29
openmsx::MSXDevice::getMotherBoard
MSXMotherBoard & getMotherBoard() const
Get the mother board this device belongs to.
Definition: MSXDevice.cc:75
openmsx::DeviceConfig::getHardwareConfig
const HardwareConfig & getHardwareConfig() const
Definition: DeviceConfig.hh:42
openmsx::MSXDevice::getLedStatus
LedStatus & getLedStatus() const
Definition: MSXDevice.cc:159
openmsx::MSXDevice::getDeviceConfig2
const DeviceConfig & getDeviceConfig2() const
Definition: MSXDevice.hh:233
openmsx::MSXDevice::getCliComm
CliComm & getCliComm() const
Definition: MSXDevice.cc:147
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:371
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:402
openmsx::MSXDevice::getReferences
const Devices & getReferences() const
Get the device references that are specified for this device.
Definition: MSXDevice.cc:125
openmsx::MSXDevice::getCommandController
CommandController & getCommandController() const
Definition: MSXDevice.cc:155
openmsx::MSXDevice::getPluggingController
PluggingController & getPluggingController() const
Definition: MSXDevice.cc:163
openmsx::MSXDevice::getName
virtual std::string getName() const
Returns a human-readable name for this device.
Definition: MSXDevice.cc:381
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:408
openmsx::DeviceConfig::getXML
const XMLElement * getXML() const
Definition: DeviceConfig.hh:47
EmuTime.hh
openmsx::MSXDevice::unmappedRead
static byte unmappedRead[0x10000]
Definition: MSXDevice.hh:300
openmsx::word
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
openmsx::MSXDevice::init
virtual void init()
Definition: MSXDevice.cc:49
openmsx::CliComm
Definition: CliComm.hh:11
openmsx::HardwareConfig
Definition: HardwareConfig.hh:22
openmsx::Scheduler
Scheduler
Definition: Scheduler.cc:132
openmsx::MSXDevice::getHardwareConfig
const HardwareConfig & getHardwareConfig() const
Returns the hardwareconfig this device belongs to.
Definition: MSXDevice.hh:43
openmsx::TclObject
Definition: TclObject.hh:22
DeviceConfig.hh
openmsx::MSXDevice::getNameList
virtual void getNameList(TclObject &result) const
Returns list of name(s) of this device.
Definition: MSXDevice.cc:386
openmsx::MSXDevice::peekIO
virtual byte peekIO(word port, EmuTime::param time) const
Read a byte from a given IO port.
Definition: MSXDevice.cc:419
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:80
openmsx::PluggingController
Central administration of Connectors and Pluggables.
Definition: PluggingController.hh:22
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::MSXDevice::getReactor
Reactor & getReactor() const
Definition: MSXDevice.cc:151
openmsx::MSXDevice::getExtraDeviceInfo
virtual void getExtraDeviceInfo(TclObject &result) const
Definition: MSXDevice.cc:397
openmsx::MSXDevice::powerUp
virtual void powerUp(EmuTime::param time)
This method is called when MSX is powered up.
Definition: MSXDevice.cc:376
openmsx::MSXDevice::getCPU
MSXCPU & getCPU() const
Definition: MSXDevice.cc:135
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:511