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& operator=(const MSXDevice&) = delete;
40
41 using Devices = std::vector<MSXDevice*>;
42
43 virtual ~MSXDevice() = 0;
44
47 [[nodiscard]] const HardwareConfig& getHardwareConfig() const {
48 return deviceConfig.getHardwareConfig();
49 }
50
54 void testRemove(std::span<const std::unique_ptr<MSXDevice>> removed) const;
55
60 virtual void reset(EmuTime::param time);
61
69 [[nodiscard]] virtual byte readIRQVector();
70
77 virtual void powerDown(EmuTime::param time);
78
84 virtual void powerUp(EmuTime::param time);
85
90 [[nodiscard]] virtual const std::string& getName() const;
91
96 virtual void getNameList(TclObject& result) const;
97
101 void getDeviceInfo(TclObject& result) const;
102
109 void getVisibleMemRegion(unsigned& base, unsigned& size) const;
110
111 // IO
112
117 [[nodiscard]] virtual byte readIO(word port, EmuTime::param time);
118
124 virtual void writeIO(word port, byte value, EmuTime::param time);
125
134 [[nodiscard]] virtual byte peekIO(word port, EmuTime::param time) const;
135
136
137 // Memory
138
144 [[nodiscard]] virtual byte readMem(word address, EmuTime::param time);
145
151 virtual void writeMem(word address, byte value, EmuTime::param time);
152
164 [[nodiscard]] virtual const byte* getReadCacheLine(word start) const;
165
177 [[nodiscard]] virtual byte* getWriteCacheLine(word start) const;
178
191 [[nodiscard]] virtual byte peekMem(word address, EmuTime::param time) const;
192
201 virtual void globalWrite(word address, byte value, EmuTime::param time);
202
207 virtual void globalRead(word address, EmuTime::param time);
208
213 void invalidateDeviceRCache() { invalidateDeviceRCache (0x0000, 0x10000); }
214 void invalidateDeviceWCache() { invalidateDeviceWCache (0x0000, 0x10000); }
215 void invalidateDeviceRWCache(unsigned start, unsigned size);
216 void invalidateDeviceRCache (unsigned start, unsigned size);
217 void invalidateDeviceWCache (unsigned start, unsigned size);
218
222 void fillDeviceRWCache(unsigned start, unsigned size, byte* rwData);
223 void fillDeviceRWCache(unsigned start, unsigned size, const byte* rData, byte* wData);
224 void fillDeviceRCache (unsigned start, unsigned size, const byte* rData);
225 void fillDeviceWCache (unsigned start, unsigned size, byte* wData);
226
229 [[nodiscard]] MSXMotherBoard& getMotherBoard() const;
230
234 [[nodiscard]] const XMLElement& getDeviceConfig() const {
235 return *deviceConfig.getXML();
236 }
237 [[nodiscard]] const DeviceConfig& getDeviceConfig2() const { // TODO
238 return deviceConfig;
239 }
240
243 [[nodiscard]] const Devices& getReferences() const;
244
245 // convenience functions, these delegate to MSXMotherBoard
246 [[nodiscard]] EmuTime::param getCurrentTime() const;
247 [[nodiscard]] MSXCPU& getCPU() const;
248 [[nodiscard]] MSXCPUInterface& getCPUInterface() const;
249 [[nodiscard]] Scheduler& getScheduler() const;
250 [[nodiscard]] MSXCliComm& getCliComm() const;
251 [[nodiscard]] Reactor& getReactor() const;
252 [[nodiscard]] CommandController& getCommandController() const;
253 [[nodiscard]] PluggingController& getPluggingController() const;
254 [[nodiscard]] LedStatus& getLedStatus() const;
255
256 template<typename Archive>
257 void serialize(Archive& ar, unsigned version);
258
259protected:
265 MSXDevice(const DeviceConfig& config, std::string_view name);
266 explicit MSXDevice(const DeviceConfig& config);
267
279 friend class DeviceFactory;
280 virtual void init();
281
286 [[nodiscard]] virtual unsigned getBaseSizeAlignment() const;
287
294 [[nodiscard]] virtual bool allowUnaligned() const { return false; }
295
300 virtual void getExtraDeviceInfo(TclObject& result) const;
301
302public:
303 // public to allow non-MSXDevices to use these same arrays
304 static inline std::array<byte, 0x10000> unmappedRead; // Read only
305 static inline std::array<byte, 0x10000> unmappedWrite; // Write only
306
307private:
308 template<typename Action, typename... Args>
309 void clip(unsigned start, unsigned size, Action action, Args... args);
310
311 void initName(std::string_view name);
312 static void staticInit();
313
314 void lockDevices();
315 void unlockDevices();
316
317 void registerSlots();
318 void unregisterSlots();
319
320 void registerPorts();
321 void unregisterPorts();
322
323protected:
324 std::string deviceName;
325
326 [[nodiscard]] byte getPrimarySlot() const {
327 // must already be resolved to an actual slot
328 assert((0 <= ps) && (ps <= 3));
329 return narrow_cast<byte>(ps);
330 }
331
332private:
333 struct BaseSize {
334 unsigned base;
335 unsigned size;
336 [[nodiscard]] unsigned end() const { return base + size; }
337 };
338 using MemRegions = std::vector<BaseSize>;
339 MemRegions memRegions;
340 IterableBitSet<256> inPorts;
341 IterableBitSet<256> outPorts;
342
343 DeviceConfig deviceConfig;
344
345 Devices references;
346 Devices referencedBy;
347
348 int ps = 0;
349 int ss = 0;
350};
351
353
354#define REGISTER_MSXDEVICE(CLASS, NAME) \
355REGISTER_POLYMORPHIC_INITIALIZER(MSXDevice, CLASS, NAME);
356
357} // namespace openmsx
358
359#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:213
void fillDeviceWCache(unsigned start, unsigned size, byte *wData)
Definition MSXDevice.cc:520
PluggingController & getPluggingController() const
Definition MSXDevice.cc:157
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:304
MSXDevice & operator=(const MSXDevice &)=delete
const HardwareConfig & getHardwareConfig() const
Returns the hardwareconfig this device belongs to.
Definition MSXDevice.hh:47
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:214
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:294
const DeviceConfig & getDeviceConfig2() const
Definition MSXDevice.hh:237
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:41
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:324
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:305
void invalidateDeviceRWCache()
Calls MSXCPUInterface::invalidateXXCache() for the specific (part of) the slot that this device is lo...
Definition MSXDevice.hh:212
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
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:234
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:326
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:72
This file implemented 3 utility functions:
Definition Autofire.cc:9
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)