| 1 |
#ifndef MSXDEVICE_HH |
| 2 |
#define MSXDEVICE_HH |
| 3 |
|
| 4 |
#include "DeviceConfig.hh" |
| 5 |
#include "EmuTime.hh" |
| 6 |
|
| 7 |
#include "IterableBitSet.hh" |
| 8 |
#include "narrow.hh" |
| 9 |
#include "serialize_meta.hh" |
| 10 |
|
| 11 |
#include <array> |
| 12 |
#include <cassert> |
| 13 |
#include <span> |
| 14 |
#include <string> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
namespace openmsx { |
| 18 |
|
| 19 |
using byte = uint8_t; |
| 20 |
|
| 21 |
class XMLElement; |
| 22 |
class MSXMotherBoard; |
| 23 |
class MSXCPU; |
| 24 |
class MSXCPUInterface; |
| 25 |
class Scheduler; |
| 26 |
class MSXCliComm; |
| 27 |
class Reactor; |
| 28 |
class CommandController; |
| 29 |
class LedStatus; |
| 30 |
class PluggingController; |
| 31 |
class HardwareConfig; |
| 32 |
class TclObject; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class MSXDevice |
| 39 |
{ |
| 40 |
public: |
| 41 |
MSXDevice(const MSXDevice&) = delete; |
| 42 |
MSXDevice(MSXDevice&&) = delete; |
| 43 |
MSXDevice& operator=(const MSXDevice&) = delete; |
| 44 |
MSXDevice& operator=(MSXDevice&&) = delete; |
| 45 |
|
| 46 |
using Devices = std::vector<MSXDevice*>; |
| 47 |
|
| 48 |
virtual ~MSXDevice() = 0; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
[[nodiscard]] const HardwareConfig& getHardwareConfig() const { |
| 53 |
return deviceConfig.getHardwareConfig(); |
| 54 |
} |
| 55 |
[[nodiscard]] HardwareConfig& getHardwareConfig() { |
| 56 |
return deviceConfig.getHardwareConfig(); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
void testRemove(std::span<const std::unique_ptr<MSXDevice>> removed) const; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
virtual void reset(EmuTime time); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
[[nodiscard]] virtual byte readIRQVector(); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
virtual void powerDown(EmuTime time); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
virtual void powerUp(EmuTime time); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
[[nodiscard]] virtual const std::string& getName() const; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
virtual void getNameList(TclObject& result) const; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
void getDeviceInfo(TclObject& result) const; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
void getVisibleMemRegion(unsigned& base, unsigned& size) const; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
[[nodiscard]] virtual byte readIO(uint16_t port, EmuTime time); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
virtual void writeIO(uint16_t port, byte value, EmuTime time); |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
[[nodiscard]] virtual byte peekIO(uint16_t port, EmuTime time) const; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
[[nodiscard]] virtual byte readMem(uint16_t address, EmuTime time); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
virtual void writeMem(uint16_t address, byte value, EmuTime time); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
[[nodiscard]] virtual const byte* getReadCacheLine(uint16_t start) const; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
[[nodiscard]] virtual byte* getWriteCacheLine(uint16_t start); |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
[[nodiscard]] virtual byte peekMem(uint16_t address, EmuTime time) const; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
virtual void globalWrite(uint16_t address, byte value, EmuTime time); |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
virtual void globalRead(uint16_t address, EmuTime time); |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
void invalidateDeviceRWCache() { invalidateDeviceRWCache(0x0000, 0x10000); } |
| 221 |
void invalidateDeviceRCache() { invalidateDeviceRCache (0x0000, 0x10000); } |
| 222 |
void invalidateDeviceWCache() { invalidateDeviceWCache (0x0000, 0x10000); } |
| 223 |
void invalidateDeviceRWCache(unsigned start, unsigned size); |
| 224 |
void invalidateDeviceRCache (unsigned start, unsigned size); |
| 225 |
void invalidateDeviceWCache (unsigned start, unsigned size); |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
void fillDeviceRWCache(unsigned start, unsigned size, byte* rwData); |
| 231 |
void fillDeviceRWCache(unsigned start, unsigned size, const byte* rData, byte* wData); |
| 232 |
void fillDeviceRCache (unsigned start, unsigned size, const byte* rData); |
| 233 |
void fillDeviceWCache (unsigned start, unsigned size, byte* wData); |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
[[nodiscard]] MSXMotherBoard& getMotherBoard() const; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
[[nodiscard]] const XMLElement& getDeviceConfig() const { |
| 243 |
return *deviceConfig.getXML(); |
| 244 |
} |
| 245 |
[[nodiscard]] XMLElement& getDeviceConfig() { |
| 246 |
return *deviceConfig.getXML(); |
| 247 |
} |
| 248 |
[[nodiscard]] const DeviceConfig& getDeviceConfig2() const { |
| 249 |
return deviceConfig; |
| 250 |
} |
| 251 |
[[nodiscard]] DeviceConfig& getDeviceConfig2() { |
| 252 |
return deviceConfig; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
[[nodiscard]] const Devices& getReferences() const; |
| 258 |
|
| 259 |
|
| 260 |
[[nodiscard]] EmuTime getCurrentTime() const; |
| 261 |
[[nodiscard]] MSXCPU& getCPU() const; |
| 262 |
[[nodiscard]] MSXCPUInterface& getCPUInterface() const; |
| 263 |
[[nodiscard]] Scheduler& getScheduler() const; |
| 264 |
[[nodiscard]] MSXCliComm& getCliComm() const; |
| 265 |
[[nodiscard]] Reactor& getReactor() const; |
| 266 |
[[nodiscard]] CommandController& getCommandController() const; |
| 267 |
[[nodiscard]] PluggingController& getPluggingController() const; |
| 268 |
[[nodiscard]] LedStatus& getLedStatus() const; |
| 269 |
|
| 270 |
template<typename Archive> |
| 271 |
void serialize(Archive& ar, unsigned version); |
| 272 |
|
| 273 |
protected: |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
MSXDevice(const DeviceConfig& config, std::string_view name); |
| 280 |
explicit MSXDevice(const DeviceConfig& config); |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
friend class DeviceFactory; |
| 294 |
virtual void init(); |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
[[nodiscard]] virtual unsigned getBaseSizeAlignment() const; |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
[[nodiscard]] virtual bool allowUnaligned() const { return false; } |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
virtual void getExtraDeviceInfo(TclObject& result) const; |
| 315 |
|
| 316 |
public: |
| 317 |
|
| 318 |
static inline std::array<byte, 0x10000> unmappedRead; |
| 319 |
static inline std::array<byte, 0x10000> unmappedWrite; |
| 320 |
|
| 321 |
private: |
| 322 |
template<typename Action, typename... Args> |
| 323 |
void clip(unsigned start, unsigned size, Action action, Args... args); |
| 324 |
|
| 325 |
void initName(std::string_view name); |
| 326 |
static void staticInit(); |
| 327 |
|
| 328 |
void lockDevices(); |
| 329 |
void unlockDevices(); |
| 330 |
|
| 331 |
void registerSlots(); |
| 332 |
void unregisterSlots(); |
| 333 |
|
| 334 |
protected: |
| 335 |
void registerPorts(); |
| 336 |
void doRegisterPorts(); |
| 337 |
void unregisterPorts(); |
| 338 |
|
| 339 |
mutable std::string deviceName; |
| 340 |
|
| 341 |
[[nodiscard]] byte getPrimarySlot() const { |
| 342 |
|
| 343 |
assert((0 <= ps) && (ps <= 3)); |
| 344 |
return narrow_cast<byte>(ps); |
| 345 |
} |
| 346 |
|
| 347 |
IterableBitSet<256> inPorts; |
| 348 |
IterableBitSet<256> outPorts; |
| 349 |
|
| 350 |
private: |
| 351 |
struct BaseSize { |
| 352 |
unsigned base; |
| 353 |
unsigned size; |
| 354 |
[[nodiscard]] unsigned end() const { return base + size; } |
| 355 |
}; |
| 356 |
using MemRegions = std::vector<BaseSize>; |
| 357 |
MemRegions memRegions; |
| 358 |
|
| 359 |
DeviceConfig deviceConfig; |
| 360 |
|
| 361 |
Devices references; |
| 362 |
Devices referencedBy; |
| 363 |
|
| 364 |
int ps = 0; |
| 365 |
int ss = 0; |
| 366 |
}; |
| 367 |
|
| 368 |
REGISTER_BASE_NAME_HELPER(MSXDevice, "Device"); |
| 369 |
|
| 370 |
#define REGISTER_MSXDEVICE(CLASS, NAME) \ |
| 371 |
REGISTER_POLYMORPHIC_INITIALIZER(MSXDevice, CLASS, NAME); |
| 372 |
|
| 373 |
} |
| 374 |
|
| 375 |
#endif |