openMSX
MSXMotherBoard.hh
Go to the documentation of this file.
1 #ifndef MSXMOTHERBOARD_HH
2 #define MSXMOTHERBOARD_HH
3 
4 #include "EmuTime.hh"
5 #include "VideoSourceSetting.hh"
6 #include "hash_map.hh"
7 #include "serialize_meta.hh"
8 #include "xxhash.hh"
9 #include "openmsx.hh"
10 #include "RecordedCommand.hh"
11 #include <cassert>
12 #include <memory>
13 #include <string_view>
14 #include <vector>
15 
16 namespace openmsx {
17 
18 class AddRemoveUpdate;
19 class BooleanSetting;
20 class CartridgeSlotManager;
21 class CassettePortInterface;
22 class CliComm;
23 class CommandController;
24 class Debugger;
25 class DeviceInfo;
26 class EventDelay;
27 class ExtCmd;
28 class FastForwardHelper;
29 class HardwareConfig;
30 class InfoCommand;
31 class JoyPortDebuggable;
32 class JoystickPortIf;
33 class LedStatus;
34 class ListExtCmd;
35 class LoadMachineCmd;
36 class MachineNameInfo;
37 class MachineTypeInfo;
38 class MSXCliComm;
39 class MSXCommandController;
40 class MSXCPU;
41 class MSXCPUInterface;
42 class MSXDevice;
43 class MSXDeviceSwitch;
44 class MSXEventDistributor;
45 class MSXMapperIO;
46 class MSXMixer;
47 class PanasonicMemory;
48 class PluggingController;
49 class Reactor;
50 class RealTime;
51 class RemoveExtCmd;
52 class RenShaTurbo;
53 class ResetCmd;
54 class ReverseManager;
55 class SettingObserver;
56 class Scheduler;
57 class StateChangeDistributor;
58 
59 class MSXMotherBoard final
60 {
61 public:
62  MSXMotherBoard(const MSXMotherBoard&) = delete;
63  MSXMotherBoard& operator=(const MSXMotherBoard&) = delete;
64 
65  explicit MSXMotherBoard(Reactor& reactor);
67 
68  std::string_view getMachineID() const { return machineID; }
69  std::string_view getMachineName() const { return machineName; }
70 
75  bool execute();
76 
79  void fastForward(EmuTime::param time, bool fast);
80 
82  void exitCPULoopAsync();
83  void exitCPULoopSync();
84 
88  void pause();
89  void unpause();
90 
91  void powerUp();
92 
93  void doReset();
94  void activate(bool active);
95  bool isActive() const { return active; }
96  bool isFastForwarding() const { return fastForwarding; }
97 
98  byte readIRQVector();
99 
100  const HardwareConfig* getMachineConfig() const { return machineConfig; }
101  void setMachineConfig(HardwareConfig* machineConfig);
102  std::string getMachineType() const;
103  bool isTurboR() const;
104 
105  std::string loadMachine(const std::string& machine);
106 
107  using Extensions = std::vector<std::unique_ptr<HardwareConfig>>;
108  const Extensions& getExtensions() const { return extensions; }
109  HardwareConfig* findExtension(std::string_view extensionName);
110  std::string loadExtension(std::string_view extensionName, std::string slotname);
111  std::string insertExtension(std::string_view name,
112  std::unique_ptr<HardwareConfig> extension);
113  void removeExtension(const HardwareConfig& extension);
114 
115  // The following classes are unique per MSX machine
117  MSXCommandController& getMSXCommandController() { return *msxCommandController; }
118  Scheduler& getScheduler() { return *scheduler; }
119  MSXEventDistributor& getMSXEventDistributor() { return *msxEventDistributor; }
120  StateChangeDistributor& getStateChangeDistributor() { return *stateChangeDistributor; }
121  CartridgeSlotManager& getSlotManager() { return *slotManager; }
122  RealTime& getRealTime() { return *realTime; }
123  Debugger& getDebugger() { return *debugger; }
124  MSXMixer& getMSXMixer() { return *msxMixer; }
126  MSXCPU& getCPU();
131  JoystickPortIf& getJoystickPort(unsigned port);
134  ReverseManager& getReverseManager() { return *reverseManager; }
135  Reactor& getReactor() { return reactor; }
136  VideoSourceSetting& getVideoSource() { return videoSourceSetting; }
137 
138  // convenience methods
141 
144  EmuTime::param getCurrentTime();
145 
148  void addDevice(MSXDevice& device);
149  void removeDevice(MSXDevice& device);
150 
157  MSXDevice* findDevice(std::string_view name);
158 
167  template<typename T, typename ... Args>
168  std::shared_ptr<T> getSharedStuff(std::string_view name, Args&& ...args)
169  {
170  auto& weak = sharedStuffMap[name];
171  auto shared = std::static_pointer_cast<T>(weak.lock());
172  if (shared) return shared;
173 
174  shared = std::make_shared<T>(std::forward<Args>(args)...);
175  weak = shared;
176  return shared;
177  }
178 
184  {
185  assert(mapperIOCounter);
186  return *mapperIO;
187  }
188  void destroyMapperIO();
189 
197  std::string getUserName(const std::string& hwName);
198  void freeUserName(const std::string& hwName, const std::string& userName);
199 
200  template<typename Archive>
201  void serialize(Archive& ar, unsigned version);
202 
203 private:
204  void powerDown();
205  void deleteMachine();
206 
207  Reactor& reactor;
208  std::string machineID;
209  std::string machineName;
210 
211  std::vector<MSXDevice*> availableDevices; // no ownership, no order
212 
215 
216  std::unique_ptr<MSXMapperIO> mapperIO;
217  unsigned mapperIOCounter;
218 
219  // These two should normally be the same, only during savestate loading
220  // machineConfig will already be filled in, but machineConfig2 not yet.
221  // This is important when an exception happens during loading of
222  // machineConfig2 (otherwise machineConfig2 gets deleted twice).
223  // See also HardwareConfig::serialize() and setMachineConfig()
224  std::unique_ptr<HardwareConfig> machineConfig2;
225  HardwareConfig* machineConfig;
226 
227  Extensions extensions; // order matters: later extension might depend on earlier ones
228 
229  // order of unique_ptr's is important!
230  std::unique_ptr<AddRemoveUpdate> addRemoveUpdate;
231  std::unique_ptr<MSXCliComm> msxCliComm;
232  std::unique_ptr<MSXEventDistributor> msxEventDistributor;
233  std::unique_ptr<StateChangeDistributor> stateChangeDistributor;
234  std::unique_ptr<MSXCommandController> msxCommandController;
235  std::unique_ptr<Scheduler> scheduler;
236  std::unique_ptr<EventDelay> eventDelay;
237  std::unique_ptr<RealTime> realTime;
238  std::unique_ptr<Debugger> debugger;
239  std::unique_ptr<MSXMixer> msxMixer;
240  std::unique_ptr<PluggingController> pluggingController;
241  std::unique_ptr<MSXCPU> msxCpu;
242  std::unique_ptr<MSXCPUInterface> msxCpuInterface;
243  std::unique_ptr<PanasonicMemory> panasonicMemory;
244  std::unique_ptr<MSXDeviceSwitch> deviceSwitch;
245  std::unique_ptr<CassettePortInterface> cassettePort;
246  std::unique_ptr<JoystickPortIf> joystickPort[2];
247  std::unique_ptr<JoyPortDebuggable> joyPortDebuggable;
248  std::unique_ptr<RenShaTurbo> renShaTurbo;
249  std::unique_ptr<LedStatus> ledStatus;
250  VideoSourceSetting videoSourceSetting;
251 
252  std::unique_ptr<CartridgeSlotManager> slotManager;
253  std::unique_ptr<ReverseManager> reverseManager;
254  std::unique_ptr<ResetCmd> resetCommand;
255  std::unique_ptr<LoadMachineCmd> loadMachineCommand;
256  std::unique_ptr<ListExtCmd> listExtCommand;
257  std::unique_ptr<ExtCmd> extCommand;
258  std::unique_ptr<RemoveExtCmd> removeExtCommand;
259  std::unique_ptr<MachineNameInfo> machineNameInfo;
260  std::unique_ptr<MachineTypeInfo> machineTypeInfo;
261  std::unique_ptr<DeviceInfo> deviceInfo;
262  friend class DeviceInfo;
263 
264  std::unique_ptr<FastForwardHelper> fastForwardHelper;
265 
266  std::unique_ptr<SettingObserver> settingObserver;
267  friend class SettingObserver;
268  BooleanSetting& powerSetting;
269 
270  bool powered;
271  bool active;
272  bool fastForwarding;
273 };
275 
276 class ExtCmd final : public RecordedCommand
277 {
278 public:
279  ExtCmd(MSXMotherBoard& motherBoard, std::string commandName);
280  void execute(span<const TclObject> tokens, TclObject& result,
281  EmuTime::param time) override;
282  std::string help(const std::vector<std::string>& tokens) const override;
283  void tabCompletion(std::vector<std::string>& tokens) const override;
284 private:
285  MSXMotherBoard& motherBoard;
286  std::string commandName;
287 };
288 
289 } // namespace openmsx
290 
291 #endif
openmsx::HardwareConfig
HardwareConfig
Definition: HardwareConfig.cc:459
openmsx::MSXMixer
Definition: MSXMixer.hh:27
openmsx::MSXMotherBoard::findExtension
HardwareConfig * findExtension(std::string_view extensionName)
Definition: MSXMotherBoard.cc:353
openmsx::MSXDevice
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition: MSXDevice.hh:31
openmsx::MSXMotherBoard::getReactor
Reactor & getReactor()
Definition: MSXMotherBoard.hh:135
openmsx::MSXMotherBoard::removeExtension
void removeExtension(const HardwareConfig &extension)
Definition: MSXMotherBoard.cc:361
openmsx::MSXMotherBoard::createMapperIO
MSXMapperIO & createMapperIO()
All memory mappers in one MSX machine share the same four (logical) memory mapper registers.
Definition: MSXMotherBoard.cc:656
openmsx::ExtCmd
Definition: MSXMotherBoard.hh:276
openmsx::VideoSourceSetting
Definition: VideoSourceSetting.hh:10
openmsx.hh
openmsx::MSXMotherBoard::getSlotManager
CartridgeSlotManager & getSlotManager()
Definition: MSXMotherBoard.hh:121
openmsx::MSXMotherBoard::getExtensions
const Extensions & getExtensions() const
Definition: MSXMotherBoard.hh:108
openmsx::SettingObserver
Definition: MSXMotherBoard.cc:166
openmsx::MSXMotherBoard::activate
void activate(bool active)
Definition: MSXMotherBoard.cc:626
openmsx::Scheduler
Definition: Scheduler.hh:33
openmsx::MSXMotherBoard::getSharedStuff
std::shared_ptr< T > getSharedStuff(std::string_view name, Args &&...args)
Some MSX device parts are shared between several MSX devices (e.g.
Definition: MSXMotherBoard.hh:168
openmsx::MSXMotherBoard::getVideoSource
VideoSourceSetting & getVideoSource()
Definition: MSXMotherBoard.hh:136
openmsx::MSXMotherBoard::getCurrentTime
EmuTime::param getCurrentTime()
Convenience method: This is the same as getScheduler().getCurrentTime().
Definition: MSXMotherBoard.cc:490
xxhash.hh
openmsx::CommandController
Definition: CommandController.hh:17
openmsx::MSXMotherBoard::fastForward
void fastForward(EmuTime::param time, bool fast)
Run emulation until a certain time in fast forward mode.
Definition: MSXMotherBoard.cc:506
openmsx::MSXMotherBoard::doReset
void doReset()
Definition: MSXMotherBoard.cc:551
openmsx::ExtCmd::execute
void execute(span< const TclObject > tokens, TclObject &result, EmuTime::param time) override
This is like the execute() method of the Command class, it only has an extra time parameter.
Definition: MSXMotherBoard.cc:835
openmsx::MSXEventDistributor
Definition: MSXEventDistributor.hh:13
openmsx::DeviceInfo
Definition: MSXMotherBoard.cc:136
openmsx::MSXMapperIO
MSXMapperIO
Definition: MSXMapperIO.cc:120
openmsx::Debugger
Definition: Debugger.hh:22
serialize_meta.hh
openmsx::MSXMotherBoard::loadMachine
std::string loadMachine(const std::string &machine)
Definition: MSXMotherBoard.cc:290
openmsx::MSXMotherBoard::insertExtension
std::string insertExtension(std::string_view name, std::unique_ptr< HardwareConfig > extension)
Definition: MSXMotherBoard.cc:337
openmsx::MSXMotherBoard::loadExtension
std::string loadExtension(std::string_view extensionName, std::string slotname)
Definition: MSXMotherBoard.cc:321
openmsx::MSXDeviceSwitch
Definition: MSXDeviceSwitch.hh:55
openmsx::MSXMotherBoard::getJoystickPort
JoystickPortIf & getJoystickPort(unsigned port)
Definition: MSXMotherBoard.cc:426
openmsx::StateChangeDistributor
Definition: StateChangeDistributor.hh:14
VideoSourceSetting.hh
openmsx::MSXMotherBoard::addDevice
void addDevice(MSXDevice &device)
All MSXDevices should be registered by the MotherBoard.
Definition: MSXMotherBoard.cc:541
XXHasher
Definition: xxhash.hh:153
openmsx::MSXMotherBoard::getStateChangeDistributor
StateChangeDistributor & getStateChangeDistributor()
Definition: MSXMotherBoard.hh:120
openmsx::MSXMotherBoard::getPanasonicMemory
PanasonicMemory & getPanasonicMemory()
Definition: MSXMotherBoard.cc:397
openmsx::ExtCmd::tabCompletion
void tabCompletion(std::vector< std::string > &tokens) const override
Attempt tab completion for this command.
Definition: MSXMotherBoard.cc:855
openmsx::MSXMotherBoard::removeDevice
void removeDevice(MSXDevice &device)
Definition: MSXMotherBoard.cc:546
openmsx::MSXMotherBoard::exitCPULoopAsync
void exitCPULoopAsync()
See CPU::exitCPULoopAsync().
Definition: MSXMotherBoard.cc:637
openmsx::MSXCPU
MSXCPU
Definition: MSXCPU.cc:563
openmsx::MSXMotherBoard::findDevice
MSXDevice * findDevice(std::string_view name)
Find a MSXDevice by name.
Definition: MSXMotherBoard.cc:649
openmsx::MSXMotherBoard::getCommandController
CommandController & getCommandController()
Definition: MSXMotherBoard.cc:480
openmsx::MSXCPUInterface
Definition: MSXCPUInterface.hh:63
openmsx::MSXMotherBoard::getMSXEventDistributor
MSXEventDistributor & getMSXEventDistributor()
Definition: MSXMotherBoard.hh:119
openmsx::JoystickPortIf
Definition: JoystickPort.hh:12
openmsx::MSXMotherBoard::getDeviceSwitch
MSXDeviceSwitch & getDeviceSwitch()
Definition: MSXMotherBoard.cc:405
openmsx::MSXMotherBoard::setMachineConfig
void setMachineConfig(HardwareConfig *machineConfig)
Definition: MSXMotherBoard.cc:262
openmsx::MSXMotherBoard::operator=
MSXMotherBoard & operator=(const MSXMotherBoard &)=delete
openmsx::MSXMotherBoard::isFastForwarding
bool isFastForwarding() const
Definition: MSXMotherBoard.hh:96
span
Definition: span.hh:34
openmsx::Reactor
Contains the main loop of openMSX.
Definition: Reactor.hh:66
openmsx::SERIALIZE_CLASS_VERSION
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
openmsx::MSXMotherBoard::getRenShaTurbo
RenShaTurbo & getRenShaTurbo()
Definition: MSXMotherBoard.cc:457
openmsx::MSXMotherBoard::getPluggingController
PluggingController & getPluggingController()
Definition: MSXMotherBoard.cc:375
openmsx::MSXMotherBoard::Extensions
std::vector< std::unique_ptr< HardwareConfig > > Extensions
Definition: MSXMotherBoard.hh:107
openmsx::MSXMotherBoard::getMachineType
std::string getMachineType() const
Definition: MSXMotherBoard.cc:273
RecordedCommand.hh
openmsx::MSXMotherBoard::unpause
void unpause()
Definition: MSXMotherBoard.cc:533
openmsx::MSXMotherBoard::getMapperIO
MSXMapperIO & getMapperIO() const
Definition: MSXMotherBoard.hh:183
openmsx::MSXMotherBoard::powerUp
void powerUp()
Definition: MSXMotherBoard.cc:577
openmsx::LedStatus
Definition: LedStatus.hh:16
openmsx::ExtCmd::help
std::string help(const std::vector< std::string > &tokens) const override
Print help for this command.
Definition: MSXMotherBoard.cc:850
openmsx::MSXMotherBoard::getMachineID
std::string_view getMachineID() const
Definition: MSXMotherBoard.hh:68
openmsx::MSXMotherBoard::getMachineInfoCommand
InfoCommand & getMachineInfoCommand()
Definition: MSXMotherBoard.cc:485
openmsx::MSXMotherBoard::getReverseManager
ReverseManager & getReverseManager()
Definition: MSXMotherBoard.hh:134
openmsx::InfoCommand
Definition: InfoCommand.hh:11
openmsx::MSXMotherBoard
Definition: MSXMotherBoard.hh:59
openmsx::MSXCPU
Definition: MSXCPU.hh:28
openmsx::MSXMotherBoard::getMSXCommandController
MSXCommandController & getMSXCommandController()
Definition: MSXMotherBoard.hh:117
openmsx::MSXMotherBoard::execute
bool execute()
Run emulation.
Definition: MSXMotherBoard.cc:495
openmsx::PanasonicMemory
Definition: PanasonicMemory.hh:14
openmsx::CassettePortInterface
Definition: CassettePort.hh:18
openmsx::MSXMotherBoard::getMSXMixer
MSXMixer & getMSXMixer()
Definition: MSXMotherBoard.hh:124
openmsx::MSXMotherBoard::getScheduler
Scheduler & getScheduler()
Definition: MSXMotherBoard.hh:118
openmsx::MSXMotherBoard::readIRQVector
byte readIRQVector()
Definition: MSXMotherBoard.cc:568
openmsx::RealTime
Definition: RealTime.hh:21
openmsx::MSXMotherBoard::destroyMapperIO
void destroyMapperIO()
Definition: MSXMotherBoard.cc:675
openmsx::MSXCPUInterface
MSXCPUInterface
Definition: MSXCPUInterface.cc:1274
hash_map.hh
openmsx::MSXMotherBoard::getUserName
std::string getUserName(const std::string &hwName)
Keep track of which 'usernames' are in use.
Definition: MSXMotherBoard.cc:695
openmsx::MSXMotherBoard::getLedStatus
LedStatus & getLedStatus()
Definition: MSXMotherBoard.cc:468
openmsx::MSXMotherBoard::getCPUInterface
MSXCPUInterface & getCPUInterface()
Definition: MSXMotherBoard.cc:391
EmuTime.hh
openmsx::MSXMotherBoard::isActive
bool isActive() const
Definition: MSXMotherBoard.hh:95
openmsx::MSXMotherBoard::MSXMotherBoard
MSXMotherBoard(const MSXMotherBoard &)=delete
openmsx::BooleanSetting
Definition: BooleanSetting.hh:8
openmsx::RecordedCommand
Commands that directly influence the MSX state should send and events so that they can be recorded by...
Definition: RecordedCommand.hh:45
openmsx::ReverseManager
Definition: ReverseManager.hh:27
openmsx::MSXDevice
MSXDevice
Definition: MSXDevice.cc:538
openmsx::MSXMapperIO
Definition: MSXMapperIO.hh:22
openmsx::CliComm
Definition: CliComm.hh:10
openmsx::MSXMotherBoard::getMachineConfig
const HardwareConfig * getMachineConfig() const
Definition: MSXMotherBoard.hh:100
openmsx::MSXMotherBoard::isTurboR
bool isTurboR() const
Definition: MSXMotherBoard.cc:283
openmsx::HardwareConfig
Definition: HardwareConfig.hh:21
openmsx::Scheduler
Scheduler
Definition: Scheduler.cc:133
openmsx::ExtCmd::ExtCmd
ExtCmd(MSXMotherBoard &motherBoard, std::string commandName)
Definition: MSXMotherBoard.cc:825
openmsx::TclObject
Definition: TclObject.hh:21
openmsx::RenShaTurbo
Ren-Sha Turbo is the autofire in several MSX 2+ models and in the MSX turbo R.
Definition: RenShaTurbo.hh:20
openmsx::MSXCommandController
Definition: MSXCommandController.hh:20
openmsx::MSXMotherBoard::getCassettePort
CassettePortInterface & getCassettePort()
Definition: MSXMotherBoard.cc:413
openmsx::MSXMotherBoard::getDebugger
Debugger & getDebugger()
Definition: MSXMotherBoard.hh:123
openmsx::MSXMotherBoard::serialize
void serialize(Archive &ar, unsigned version)
Definition: MSXMotherBoard.cc:1055
openmsx::PluggingController
Central administration of Connectors and Pluggables.
Definition: PluggingController.hh:21
openmsx::MSXMotherBoard::~MSXMotherBoard
~MSXMotherBoard()
Definition: MSXMotherBoard.cc:233
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::MSXMotherBoard::getMSXCliComm
CliComm & getMSXCliComm()
Definition: MSXMotherBoard.cc:370
openmsx::CartridgeSlotManager
Definition: CartridgeSlotManager.hh:15
openmsx::MSXMotherBoard::freeUserName
void freeUserName(const std::string &hwName, const std::string &userName)
Definition: MSXMotherBoard.cc:707
openmsx::MSXMotherBoard::getRealTime
RealTime & getRealTime()
Definition: MSXMotherBoard.hh:122
openmsx::MSXMotherBoard::getMachineName
std::string_view getMachineName() const
Definition: MSXMotherBoard.hh:69
openmsx::MSXMotherBoard::getCPU
MSXCPU & getCPU()
Definition: MSXMotherBoard.cc:384
hash_map
Definition: hash_map.hh:31
openmsx::MSXDeviceSwitch
MSXDeviceSwitch
Definition: MSXDeviceSwitch.cc:97
openmsx::MSXMotherBoard::exitCPULoopSync
void exitCPULoopSync()
Definition: MSXMotherBoard.cc:644
openmsx::MSXMotherBoard::pause
void pause()
Pause MSX machine.
Definition: MSXMotherBoard.cc:525