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 "narrow.hh"
8#include "openmsx.hh"
9#include "serialize_meta.hh"
10#include <array>
11#include <cassert>
12#include <span>
13#include <string>
14#include <vector>
15
16namespace openmsx {
17
18class XMLElement;
19class MSXMotherBoard;
20class MSXCPU;
21class MSXCPUInterface;
22class Scheduler;
23class MSXCliComm;
24class Reactor;
25class CommandController;
26class LedStatus;
27class PluggingController;
28class HardwareConfig;
29class TclObject;
30
36{
37public:
38 MSXDevice(const MSXDevice&) = delete;
39 MSXDevice(MSXDevice&&) = delete;
40 MSXDevice& operator=(const MSXDevice&) = delete;
42
43 using Devices = std::vector<MSXDevice*>;
44
45 virtual ~MSXDevice() = 0;
46
49 [[nodiscard]] const HardwareConfig& getHardwareConfig() const {
50 return deviceConfig.getHardwareConfig();
51 }
52
56 void testRemove(std::span<const std::unique_ptr<MSXDevice>> removed) const;
57
62 virtual void reset(EmuTime::param time);
63
71 [[nodiscard]] virtual byte readIRQVector();
72
79 virtual void powerDown(EmuTime::param time);
80
86 virtual void powerUp(EmuTime::param time);
87
92 [[nodiscard]] virtual const std::string& getName() const;
93
98 virtual void getNameList(TclObject& result) const;
99
103 void getDeviceInfo(TclObject& result) const;
104
111 void getVisibleMemRegion(unsigned& base, unsigned& size) const;
112
113 // IO
114
119 [[nodiscard]] virtual byte readIO(word port, EmuTime::param time);
120
126 virtual void writeIO(word port, byte value, EmuTime::param time);
127
136 [[nodiscard]] virtual byte peekIO(word port, EmuTime::param time) const;
137
138
139 // Memory
140
146 [[nodiscard]] virtual byte readMem(word address, EmuTime::param time);
147
153 virtual void writeMem(word address, byte value, EmuTime::param time);
154
166 [[nodiscard]] virtual const byte* getReadCacheLine(word start) const;
167
179 [[nodiscard]] virtual byte* getWriteCacheLine(word start) const;
180
193 [[nodiscard]] virtual byte peekMem(word address, EmuTime::param time) const;
194
203 virtual void globalWrite(word address, byte value, EmuTime::param time);
204
209 virtual void globalRead(word address, EmuTime::param time);
210
215 void invalidateDeviceRCache() { invalidateDeviceRCache (0x0000, 0x10000); }
216 void invalidateDeviceWCache() { invalidateDeviceWCache (0x0000, 0x10000); }
217 void invalidateDeviceRWCache(unsigned start, unsigned size);
218 void invalidateDeviceRCache (unsigned start, unsigned size);
219 void invalidateDeviceWCache (unsigned start, unsigned size);
220
224 void fillDeviceRWCache(unsigned start, unsigned size, byte* rwData);
225 void fillDeviceRWCache(unsigned start, unsigned size, const byte* rData, byte* wData);
226 void fillDeviceRCache (unsigned start, unsigned size, const byte* rData);
227 void fillDeviceWCache (unsigned start, unsigned size, byte* wData);
228
231 [[nodiscard]] MSXMotherBoard& getMotherBoard() const;
232
236 [[nodiscard]] const XMLElement& getDeviceConfig() const {
237 return *deviceConfig.getXML();
238 }
239 [[nodiscard]] const DeviceConfig& getDeviceConfig2() const { // TODO
240 return deviceConfig;
241 }
242
245 [[nodiscard]] const Devices& getReferences() const;
246
247 // convenience functions, these delegate to MSXMotherBoard
248 [[nodiscard]] EmuTime::param getCurrentTime() const;
249 [[nodiscard]] MSXCPU& getCPU() const;
250 [[nodiscard]] MSXCPUInterface& getCPUInterface() const;
251 [[nodiscard]] Scheduler& getScheduler() const;
252 [[nodiscard]] MSXCliComm& getCliComm() const;
253 [[nodiscard]] Reactor& getReactor() const;
254 [[nodiscard]] CommandController& getCommandController() const;
255 [[nodiscard]] PluggingController& getPluggingController() const;
256 [[nodiscard]] LedStatus& getLedStatus() const;
257
258 template<typename Archive>
259 void serialize(Archive& ar, unsigned version);
260
261protected:
267 MSXDevice(const DeviceConfig& config, std::string_view name);
268 explicit MSXDevice(const DeviceConfig& config);
269
281 friend class DeviceFactory;
282 virtual void init();
283
288 [[nodiscard]] virtual unsigned getBaseSizeAlignment() const;
289
296 [[nodiscard]] virtual bool allowUnaligned() const { return false; }
297
302 virtual void getExtraDeviceInfo(TclObject& result) const;
303
304public:
305 // public to allow non-MSXDevices to use these same arrays
306 static inline std::array<byte, 0x10000> unmappedRead; // Read only
307 static inline std::array<byte, 0x10000> unmappedWrite; // Write only
308
309private:
310 template<typename Action, typename... Args>
311 void clip(unsigned start, unsigned size, Action action, Args... args);
312
313 void initName(std::string_view name);
314 static void staticInit();
315
316 void lockDevices();
317 void unlockDevices();
318
319 void registerSlots();
320 void unregisterSlots();
321
322 void registerPorts();
323 void unregisterPorts();
324
325protected:
326 std::string deviceName;
327
328 [[nodiscard]] byte getPrimarySlot() const {
329 // must already be resolved to an actual slot
330 assert((0 <= ps) && (ps <= 3));
331 return narrow_cast<byte>(ps);
332 }
333
334private:
335 struct BaseSize {
336 unsigned base;
337 unsigned size;
338 [[nodiscard]] unsigned end() const { return base + size; }
339 };
340 using MemRegions = std::vector<BaseSize>;
341 MemRegions memRegions;
342 IterableBitSet<256> inPorts;
343 IterableBitSet<256> outPorts;
344
345 DeviceConfig deviceConfig;
346
347 Devices references;
348 Devices referencedBy;
349
350 int ps = 0;
351 int ss = 0;
352};
353
355
356#define REGISTER_MSXDEVICE(CLASS, NAME) \
357REGISTER_POLYMORPHIC_INITIALIZER(MSXDevice, CLASS, NAME);
358
359} // namespace openmsx
360
361#endif
IterableBitSet.
const HardwareConfig & getHardwareConfig() const
const XMLElement * getXML() const
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition MSXDevice.hh:36
MSXMotherBoard & getMotherBoard() const
Get the mother board this device belongs to.
Definition MSXDevice.cc:70
virtual void init()
Definition MSXDevice.cc:44
void invalidateDeviceRCache()
Definition MSXDevice.hh:215
void fillDeviceWCache(unsigned start, unsigned size, byte *wData)
Definition MSXDevice.cc:520
PluggingController & getPluggingController() const
Definition MSXDevice.cc:157
MSXDevice(MSXDevice &&)=delete
virtual void powerUp(EmuTime::param time)
This method is called when MSX is powered up.
Definition MSXDevice.cc:370
static std::array< byte, 0x10000 > unmappedRead
Definition MSXDevice.hh:306
MSXDevice & operator=(const MSXDevice &)=delete
const HardwareConfig & getHardwareConfig() const
Returns the hardwareconfig this device belongs to.
Definition MSXDevice.hh:49
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:53
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:425
void invalidateDeviceWCache()
Definition MSXDevice.hh:216
void getDeviceInfo(TclObject &result) const
Get device info.
Definition MSXDevice.cc:385
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:408
virtual byte readIRQVector()
Gets IRQ vector used in IM2.
Definition MSXDevice.cc:360
virtual void globalWrite(word address, byte value, EmuTime::param time)
Global writes.
Definition MSXDevice.cc:448
Reactor & getReactor() const
Definition MSXDevice.cc:145
virtual bool allowUnaligned() const
By default we don't allow unaligned <mem> specifications in the config file.
Definition MSXDevice.hh:296
const DeviceConfig & getDeviceConfig2() const
Definition MSXDevice.hh:239
virtual unsigned getBaseSizeAlignment() const
The 'base' and 'size' attribute values need to be at least aligned to CacheLine::SIZE.
Definition MSXDevice.cc:396
MSXDevice(const MSXDevice &)=delete
std::vector< MSXDevice * > Devices
Definition MSXDevice.hh:43
virtual byte readMem(word address, EmuTime::param time)
Read a byte from a location at a certain time from this device.
Definition MSXDevice.cc:419
virtual void getExtraDeviceInfo(TclObject &result) const
Definition MSXDevice.cc:391
std::string deviceName
Definition MSXDevice.hh:326
void getVisibleMemRegion(unsigned &base, unsigned &size) const
Returns the range where this device is visible in memory.
Definition MSXDevice.cc:301
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:430
virtual byte peekIO(word port, EmuTime::param time) const
Read a byte from a given IO port.
Definition MSXDevice.cc:413
virtual byte peekMem(word address, EmuTime::param time) const
Read a byte from a given memory location.
Definition MSXDevice.cc:436
MSXCPU & getCPU() const
Definition MSXDevice.cc:129
virtual const std::string & getName() const
Returns a human-readable name for this device.
Definition MSXDevice.cc:375
LedStatus & getLedStatus() const
Definition MSXDevice.cc:153
void serialize(Archive &ar, unsigned version)
Definition MSXDevice.cc:527
static std::array< byte, 0x10000 > unmappedWrite
Definition MSXDevice.hh:307
void invalidateDeviceRWCache()
Calls MSXCPUInterface::invalidateXXCache() for the specific (part of) the slot that this device is lo...
Definition MSXDevice.hh:214
Scheduler & getScheduler() const
Definition MSXDevice.cc:137
virtual void getNameList(TclObject &result) const
Returns list of name(s) of this device.
Definition MSXDevice.cc:380
MSXDevice & operator=(MSXDevice &&)=delete
void testRemove(std::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:75
const XMLElement & getDeviceConfig() const
Get the configuration section for this device.
Definition MSXDevice.hh:236
virtual byte * getWriteCacheLine(word start) const
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing.
Definition MSXDevice.cc:459
byte getPrimarySlot() const
Definition MSXDevice.hh:328
void fillDeviceRCache(unsigned start, unsigned size, const byte *rData)
Definition MSXDevice.cc:515
CommandController & getCommandController() const
Definition MSXDevice.cc:149
const Devices & getReferences() const
Get the device references that are specified for this device.
Definition MSXDevice.cc:119
virtual void powerDown(EmuTime::param time)
This method is called when MSX is powered down.
Definition MSXDevice.cc:365
virtual void globalRead(word address, EmuTime::param time)
Global reads.
Definition MSXDevice.cc:454
virtual void reset(EmuTime::param time)
This method is called on reset.
Definition MSXDevice.cc:355
EmuTime::param getCurrentTime() const
Definition MSXDevice.cc:125
MSXCPUInterface & getCPUInterface() const
Definition MSXDevice.cc:133
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:402
MSXCliComm & getCliComm() const
Definition MSXDevice.cc:141
Central administration of Connectors and Pluggables.
Contains the main loop of openMSX.
Definition Reactor.hh:74
This file implemented 3 utility functions:
Definition Autofire.cc:11
uint16_t word
16 bit unsigned integer
Definition openmsx.hh:29
#define REGISTER_BASE_NAME_HELPER(B, N)
constexpr auto end(const zstring_view &x)