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 "serialize_meta.hh"
6 #include "string_ref.hh"
7 #include "openmsx.hh"
8 #include "noncopyable.hh"
9 #include "RecordedCommand.hh"
10 #include <memory>
11 #include <set>
12 #include <vector>
13 
14 namespace openmsx {
15 
16 class AddRemoveUpdate;
17 class BooleanSetting;
18 class CartridgeSlotManager;
19 class CassettePortInterface;
20 class CliComm;
21 class CommandController;
22 class Debugger;
23 class DeviceInfo;
24 class EventDelay;
25 class ExtCmd;
26 class FastForwardHelper;
27 class HardwareConfig;
28 class InfoCommand;
29 class JoyPortDebuggable;
30 class JoystickPortIf;
31 class LedStatus;
32 class ListExtCmd;
33 class LoadMachineCmd;
34 class MachineNameInfo;
35 class MSXCliComm;
36 class MSXCommandController;
37 class MSXCPU;
38 class MSXCPUInterface;
39 class MSXDevice;
40 class MSXDeviceSwitch;
41 class MSXEventDistributor;
42 class MSXMapperIO;
43 class MSXMixer;
44 class PanasonicMemory;
45 class PluggingController;
46 class Reactor;
47 class RealTime;
48 class RemoveExtCmd;
49 class RenShaTurbo;
50 class ResetCmd;
51 class ReverseManager;
52 class SettingObserver;
53 class Scheduler;
54 class Setting;
55 class StateChangeDistributor;
56 class VideoSourceSetting;
57 
58 class MSXMotherBoard final : private noncopyable
59 {
60 public:
61  explicit MSXMotherBoard(Reactor& reactor);
63 
64  const std::string& getMachineID() const { return machineID; }
65  const std::string& getMachineName() const { return machineName; }
66 
71  bool execute();
72 
75  void fastForward(EmuTime::param time, bool fast);
76 
78  void exitCPULoopAsync();
79  void exitCPULoopSync();
80 
84  void pause();
85  void unpause();
86 
87  void powerUp();
88 
89  void doReset();
90  void activate(bool active);
91  bool isActive() const { return active; }
92  bool isFastForwarding() const { return fastForwarding; }
93 
95 
96  const HardwareConfig* getMachineConfig() const { return machineConfig; }
97  void setMachineConfig(HardwareConfig* machineConfig);
98  bool isTurboR() const;
99 
100  std::string loadMachine(const std::string& machine);
101 
102  typedef std::vector<std::unique_ptr<HardwareConfig>> Extensions;
103  const Extensions& getExtensions() const { return extensions; }
104  HardwareConfig* findExtension(string_ref extensionName);
105  std::string loadExtension(string_ref extensionName, string_ref slotname);
106  std::string insertExtension(string_ref name,
107  std::unique_ptr<HardwareConfig> extension);
108  void removeExtension(const HardwareConfig& extension);
109 
110  // The following classes are unique per MSX machine
112  MSXCommandController& getMSXCommandController() { return *msxCommandController; }
113  Scheduler& getScheduler() { return *scheduler; }
114  MSXEventDistributor& getMSXEventDistributor() { return *msxEventDistributor; }
115  StateChangeDistributor& getStateChangeDistributor() { return *stateChangeDistributor; }
116  CartridgeSlotManager& getSlotManager() { return *slotManager; }
117  RealTime& getRealTime() { return *realTime; }
118  Debugger& getDebugger() { return *debugger; }
119  MSXMixer& getMSXMixer() { return *msxMixer; }
121  MSXCPU& getCPU();
126  JoystickPortIf& getJoystickPort(unsigned port);
129  ReverseManager& getReverseManager() { return *reverseManager; }
130  Reactor& getReactor() { return reactor; }
131  VideoSourceSetting& getVideoSource() { return *videoSourceSetting; }
132 
133  // convenience methods
136 
140 
143  void addDevice(MSXDevice& device);
144  void removeDevice(MSXDevice& device);
145 
153 
162  template<typename T, typename ... Args>
163  std::shared_ptr<T> getSharedStuff(string_ref name, Args&& ...args)
164  {
165  auto& weak = sharedStuffMap[name];
166  auto shared = std::static_pointer_cast<T>(weak.lock());
167  if (shared) return shared;
168 
169  shared = std::make_shared<T>(std::forward<Args>(args)...);
170  weak = shared;
171  return shared;
172  }
173 
178  void destroyMapperIO();
179 
187  std::string getUserName(const std::string& hwName);
188  void freeUserName(const std::string& hwName, const std::string& userName);
189 
190  template<typename Archive>
191  void serialize(Archive& ar, unsigned version);
192 
193 private:
194  void powerDown();
195  void deleteMachine();
196 
197  Reactor& reactor;
198  std::string machineID;
199  std::string machineName;
200 
201  std::vector<MSXDevice*> availableDevices; // no ownership
202 
203  StringMap<std::weak_ptr<void>> sharedStuffMap;
205 
206  std::unique_ptr<MSXMapperIO> mapperIO;
207  unsigned mapperIOCounter;
208 
209  // These two should normally be the same, only during savestate loading
210  // machineConfig will already be filled in, but machineConfig2 not yet.
211  // This is important when an exception happens during loading of
212  // machineConfig2 (otherwise machineConfig2 gets deleted twice).
213  // See also HardwareConfig::serialize() and setMachineConfig()
214  std::unique_ptr<HardwareConfig> machineConfig2;
215  HardwareConfig* machineConfig;
216 
217  Extensions extensions;
218 
219  // order of unique_ptr's is important!
220  std::unique_ptr<AddRemoveUpdate> addRemoveUpdate;
221  std::unique_ptr<MSXCliComm> msxCliComm;
222  std::unique_ptr<MSXEventDistributor> msxEventDistributor;
223  std::unique_ptr<StateChangeDistributor> stateChangeDistributor;
224  std::unique_ptr<MSXCommandController> msxCommandController;
225  std::unique_ptr<Scheduler> scheduler;
226  std::unique_ptr<EventDelay> eventDelay;
227  std::unique_ptr<RealTime> realTime;
228  std::unique_ptr<Debugger> debugger;
229  std::unique_ptr<MSXMixer> msxMixer;
230  std::unique_ptr<PluggingController> pluggingController;
231  std::unique_ptr<MSXCPU> msxCpu;
232  std::unique_ptr<MSXCPUInterface> msxCpuInterface;
233  std::unique_ptr<PanasonicMemory> panasonicMemory;
234  std::unique_ptr<MSXDeviceSwitch> deviceSwitch;
235  std::unique_ptr<CassettePortInterface> cassettePort;
236  std::unique_ptr<JoystickPortIf> joystickPort[2];
237  std::unique_ptr<JoyPortDebuggable> joyPortDebuggable;
238  std::unique_ptr<RenShaTurbo> renShaTurbo;
239  std::unique_ptr<LedStatus> ledStatus;
240  std::unique_ptr<VideoSourceSetting> videoSourceSetting;
241 
242  std::unique_ptr<CartridgeSlotManager> slotManager;
243  std::unique_ptr<ReverseManager> reverseManager;
244  std::unique_ptr<ResetCmd> resetCommand;
245  std::unique_ptr<LoadMachineCmd> loadMachineCommand;
246  std::unique_ptr<ListExtCmd> listExtCommand;
247  std::unique_ptr<ExtCmd> extCommand;
248  std::unique_ptr<RemoveExtCmd> removeExtCommand;
249  std::unique_ptr<MachineNameInfo> machineNameInfo;
250  std::unique_ptr<DeviceInfo> deviceInfo;
251  friend class DeviceInfo;
252 
253  std::unique_ptr<FastForwardHelper> fastForwardHelper;
254 
255  std::unique_ptr<SettingObserver> settingObserver;
256  friend class SettingObserver;
257  BooleanSetting& powerSetting;
258 
259  bool powered;
260  bool active;
261  bool fastForwarding;
262 };
264 
265 class ExtCmd final : public RecordedCommand
266 {
267 public:
268  ExtCmd(MSXMotherBoard& motherBoard, string_ref commandName);
269  void execute(array_ref<TclObject> tokens, TclObject& result,
270  EmuTime::param time) override;
271  std::string help(const std::vector<std::string>& tokens) const override;
272  void tabCompletion(std::vector<std::string>& tokens) const override;
273 private:
274  MSXMotherBoard& motherBoard;
275  std::string commandName;
276 };
277 
278 } // namespace openmsx
279 
280 #endif
const std::string & getMachineID() const
MSXCommandController & getMSXCommandController()
Contains the main loop of openMSX.
Definition: Reactor.hh:62
PluggingController & getPluggingController()
ExtCmd(MSXMotherBoard &motherBoard, string_ref commandName)
MSXCPUInterface & getCPUInterface()
unsigned char byte
8 bit unsigned integer
Definition: openmsx.hh:27
void pause()
Pause MSX machine.
void removeDevice(MSXDevice &device)
Commands that directly influence the MSX state should send and events so that they can be recorded by...
void freeUserName(const std::string &hwName, const std::string &userName)
const HardwareConfig * getMachineConfig() const
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
Central administration of Connectors and Pluggables.
void fastForward(EmuTime::param time, bool fast)
Run emulation until a certain time in fast forward mode.
void tabCompletion(std::vector< std::string > &tokens) const override
Attempt tab completion for this command.
CassettePortInterface & getCassettePort()
void execute(array_ref< 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...
MSXDevice * findDevice(string_ref name)
Find a MSXDevice by name.
bool execute()
Run emulation.
InfoCommand & getMachineInfoCommand()
This class implements a subset of the proposal for std::array_ref (proposed for the next c++ standard...
Definition: array_ref.hh:19
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
std::string loadMachine(const std::string &machine)
void setMachineConfig(HardwareConfig *machineConfig)
std::string help(const std::vector< std::string > &tokens) const override
Print help for this command.
HardwareConfig * findExtension(string_ref extensionName)
const std::string & getMachineName() const
PanasonicMemory & getPanasonicMemory()
CartridgeSlotManager & getSlotManager()
std::string insertExtension(string_ref name, std::unique_ptr< HardwareConfig > extension)
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:7
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX...
Definition: MSXDevice.hh:32
ReverseManager & getReverseManager()
CommandController & getCommandController()
std::string getUserName(const std::string &hwName)
Keep track of which 'usernames' are in use.
MSXMotherBoard(Reactor &reactor)
bool isFastForwarding() const
JoystickPortIf & getJoystickPort(unsigned port)
void removeExtension(const HardwareConfig &extension)
void activate(bool active)
std::shared_ptr< T > getSharedStuff(string_ref name, Args &&...args)
Some MSX device parts are shared between several MSX devices (e.g.
MSXMapperIO * createMapperIO()
All memory mappers in one MSX machine share the same four (logical) memory mapper registers...
void serialize(Archive &ar, unsigned version)
std::string loadExtension(string_ref extensionName, string_ref slotname)
MSXEventDistributor & getMSXEventDistributor()
std::vector< std::unique_ptr< HardwareConfig > > Extensions
Ren-Sha Turbo is the autofire in several MSX 2+ models and in the MSX turbo R.
Definition: RenShaTurbo.hh:21
EmuTime::param getCurrentTime()
Convenience method: This is the same as getScheduler().getCurrentTime().
VideoSourceSetting & getVideoSource()
Based on boost::noncopyable, see boost documentation: http://www.boost.org/libs/utility.
Definition: noncopyable.hh:12
void addDevice(MSXDevice &device)
All MSXDevices should be registered by the MotherBoard.
MSXDeviceSwitch & getDeviceSwitch()
RenShaTurbo & getRenShaTurbo()
StateChangeDistributor & getStateChangeDistributor()
void exitCPULoopAsync()
See CPU::exitCPULoopAsync().
const Extensions & getExtensions() const