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 "string_view.hh"
9 #include "xxhash.hh"
10 #include "openmsx.hh"
11 #include "RecordedCommand.hh"
12 #include <cassert>
13 #include <memory>
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  string_view getMachineID() const { return machineID; }
69  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 
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(string_view extensionName);
110  std::string loadExtension(string_view extensionName, string_view slotname);
111  std::string insertExtension(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 
158 
167  template<typename T, typename ... Args>
168  std::shared_ptr<T> getSharedStuff(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
MSXCommandController & getMSXCommandController()
MSXDevice * findDevice(string_view name)
Find a MSXDevice by name.
Contains the main loop of openMSX.
Definition: Reactor.hh:64
PluggingController & getPluggingController()
string_view getMachineName() const
MSXCPUInterface & getCPUInterface()
std::string insertExtension(string_view name, std::unique_ptr< HardwareConfig > extension)
Definition: span.hh:34
bool isFastForwarding() const
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
void pause()
Pause MSX machine.
void removeDevice(MSXDevice &device)
MSXMapperIO & getMapperIO() const
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)
Central administration of Connectors and Pluggables.
void fastForward(EmuTime::param time, bool fast)
Run emulation until a certain time in fast forward mode.
CassettePortInterface & getCassettePort()
const Extensions & getExtensions() const
HardwareConfig * findExtension(string_view extensionName)
bool execute()
Run emulation.
InfoCommand & getMachineInfoCommand()
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
const HardwareConfig * getMachineConfig() const
std::string loadMachine(const std::string &machine)
void setMachineConfig(HardwareConfig *machineConfig)
PanasonicMemory & getPanasonicMemory()
CartridgeSlotManager & getSlotManager()
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX...
Definition: MSXDevice.hh:31
ReverseManager & getReverseManager()
CommandController & getCommandController()
std::string getUserName(const std::string &hwName)
Keep track of which &#39;usernames&#39; are in use.
JoystickPortIf & getJoystickPort(unsigned port)
void removeExtension(const HardwareConfig &extension)
std::string loadExtension(string_view extensionName, string_view slotname)
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
std::vector< std::unique_ptr< HardwareConfig > > Extensions
void activate(bool active)
std::shared_ptr< T > getSharedStuff(string_view name, Args &&...args)
Some MSX device parts are shared between several MSX devices (e.g.
void serialize(Archive &ar, unsigned version)
MSXEventDistributor & getMSXEventDistributor()
std::string getMachineType() const
Ren-Sha Turbo is the autofire in several MSX 2+ models and in the MSX turbo R.
Definition: RenShaTurbo.hh:20
MSXMapperIO & createMapperIO()
All memory mappers in one MSX machine share the same four (logical) memory mapper registers...
EmuTime::param getCurrentTime()
Convenience method: This is the same as getScheduler().getCurrentTime().
VideoSourceSetting & getVideoSource()
MSXMotherBoard & operator=(const MSXMotherBoard &)=delete
string_view getMachineID() const
void addDevice(MSXDevice &device)
All MSXDevices should be registered by the MotherBoard.
MSXDeviceSwitch & getDeviceSwitch()
MSXMotherBoard(const MSXMotherBoard &)=delete
RenShaTurbo & getRenShaTurbo()
StateChangeDistributor & getStateChangeDistributor()
void exitCPULoopAsync()
See CPU::exitCPULoopAsync().