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 <array>
10#include <span>
11#include <string>
12#include <vector>
13
14namespace openmsx {
15
16class XMLElement;
17class MSXMotherBoard;
18class MSXCPU;
19class MSXCPUInterface;
20class Scheduler;
21class CliComm;
22class Reactor;
23class CommandController;
24class LedStatus;
25class PluggingController;
26class HardwareConfig;
27class TclObject;
28
34{
35public:
36 MSXDevice(const MSXDevice&) = delete;
37 MSXDevice& operator=(const MSXDevice&) = delete;
38
39 using Devices = std::vector<MSXDevice*>;
40
41 virtual ~MSXDevice() = 0;
42
45 [[nodiscard]] const HardwareConfig& getHardwareConfig() const {
46 return deviceConfig.getHardwareConfig();
47 }
48
52 void testRemove(std::span<const std::unique_ptr<MSXDevice>> removed) const;
53
58 virtual void reset(EmuTime::param time);
59
67 [[nodiscard]] virtual byte readIRQVector();
68
75 virtual void powerDown(EmuTime::param time);
76
82 virtual void powerUp(EmuTime::param time);
83
88 [[nodiscard]] virtual const std::string& getName() const;
89
94 virtual void getNameList(TclObject& result) const;
95
99 void getDeviceInfo(TclObject& result) const;
100
107 void getVisibleMemRegion(unsigned& base, unsigned& size) const;
108
109 // IO
110
115 [[nodiscard]] virtual byte readIO(word port, EmuTime::param time);
116
122 virtual void writeIO(word port, byte value, EmuTime::param time);
123
132 [[nodiscard]] virtual byte peekIO(word port, EmuTime::param time) const;
133
134
135 // Memory
136
142 [[nodiscard]] virtual byte readMem(word address, EmuTime::param time);
143
149 virtual void writeMem(word address, byte value, EmuTime::param time);
150
162 [[nodiscard]] virtual const byte* getReadCacheLine(word start) const;
163
175 [[nodiscard]] virtual byte* getWriteCacheLine(word start) const;
176
189 [[nodiscard]] virtual byte peekMem(word address, EmuTime::param time) const;
190
199 virtual void globalWrite(word address, byte value, EmuTime::param time);
200
205 virtual void globalRead(word address, EmuTime::param time);
206
211 void invalidateDeviceRCache() { invalidateDeviceRCache (0x0000, 0x10000); }
212 void invalidateDeviceWCache() { invalidateDeviceWCache (0x0000, 0x10000); }
213 void invalidateDeviceRWCache(unsigned start, unsigned size);
214 void invalidateDeviceRCache (unsigned start, unsigned size);
215 void invalidateDeviceWCache (unsigned start, unsigned size);
216
220 void fillDeviceRWCache(unsigned start, unsigned size, byte* rwData);
221 void fillDeviceRWCache(unsigned start, unsigned size, const byte* rData, byte* wData);
222 void fillDeviceRCache (unsigned start, unsigned size, const byte* rData);
223 void fillDeviceWCache (unsigned start, unsigned size, byte* wData);
224
227 [[nodiscard]] MSXMotherBoard& getMotherBoard() const;
228
232 [[nodiscard]] const XMLElement& getDeviceConfig() const {
233 return *deviceConfig.getXML();
234 }
235 [[nodiscard]] const DeviceConfig& getDeviceConfig2() const { // TODO
236 return deviceConfig;
237 }
238
241 [[nodiscard]] const Devices& getReferences() const;
242
243 // convenience functions, these delegate to MSXMotherBoard
244 [[nodiscard]] EmuTime::param getCurrentTime() const;
245 [[nodiscard]] MSXCPU& getCPU() const;
246 [[nodiscard]] MSXCPUInterface& getCPUInterface() const;
247 [[nodiscard]] Scheduler& getScheduler() const;
248 [[nodiscard]] CliComm& getCliComm() const;
249 [[nodiscard]] Reactor& getReactor() const;
250 [[nodiscard]] CommandController& getCommandController() const;
251 [[nodiscard]] PluggingController& getPluggingController() const;
252 [[nodiscard]] LedStatus& getLedStatus() const;
253
254 template<typename Archive>
255 void serialize(Archive& ar, unsigned version);
256
257protected:
263 MSXDevice(const DeviceConfig& config, std::string_view name);
264 explicit MSXDevice(const DeviceConfig& config);
265
277 friend class DeviceFactory;
278 virtual void init();
279
284 [[nodiscard]] virtual unsigned getBaseSizeAlignment() const;
285
292 [[nodiscard]] virtual bool allowUnaligned() const { return false; }
293
298 virtual void getExtraDeviceInfo(TclObject& result) const;
299
300public:
301 // public to allow non-MSXDevices to use these same arrays
302 static inline std::array<byte, 0x10000> unmappedRead; // Read only
303 static inline std::array<byte, 0x10000> unmappedWrite; // Write only
304
305private:
306 template<typename Action, typename... Args>
307 void clip(unsigned start, unsigned size, Action action, Args... args);
308
309 void initName(std::string_view name);
310 static void staticInit();
311
312 void lockDevices();
313 void unlockDevices();
314
315 void registerSlots();
316 void unregisterSlots();
317
318 void registerPorts();
319 void unregisterPorts();
320
321protected:
322 std::string deviceName;
323
324 [[nodiscard]] byte getPrimarySlot() const { return ps; };
325
326private:
327 struct BaseSize {
328 unsigned base;
329 unsigned size;
330 [[nodiscard]] unsigned end() const { return base + size; }
331 };
332 using MemRegions = std::vector<BaseSize>;
333 MemRegions memRegions;
334 IterableBitSet<256> inPorts;
335 IterableBitSet<256> outPorts;
336
337 DeviceConfig deviceConfig;
338
339 Devices references;
340 Devices referencedBy;
341
342 int ps = 0;
343 int ss = 0;
344};
345
347
348#define REGISTER_MSXDEVICE(CLASS, NAME) \
349REGISTER_POLYMORPHIC_INITIALIZER(MSXDevice, CLASS, NAME);
350
351} // namespace openmsx
352
353#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:34
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:211
void fillDeviceWCache(unsigned start, unsigned size, byte *wData)
Definition: MSXDevice.cc:519
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:302
MSXDevice & operator=(const MSXDevice &)=delete
const HardwareConfig & getHardwareConfig() const
Returns the hardwareconfig this device belongs to.
Definition: MSXDevice.hh:45
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:505
virtual ~MSXDevice()=0
Definition: MSXDevice.cc:53
CliComm & getCliComm() const
Definition: MSXDevice.cc:141
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:212
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:292
const DeviceConfig & getDeviceConfig2() const
Definition: MSXDevice.hh:235
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:39
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:322
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:526
static std::array< byte, 0x10000 > unmappedWrite
Definition: MSXDevice.hh:303
void invalidateDeviceRWCache()
Calls MSXCPUInterface::invalidateXXCache() for the specific (part of) the slot that this device is lo...
Definition: MSXDevice.hh:210
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:232
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:324
void fillDeviceRCache(unsigned start, unsigned size, const byte *rData)
Definition: MSXDevice.cc:514
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
Central administration of Connectors and Pluggables.
Contains the main loop of openMSX.
Definition: Reactor.hh:68
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)