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 Setting;
58 class StateChangeDistributor;
59 
60 class MSXMotherBoard final
61 {
62 public:
63  MSXMotherBoard(const MSXMotherBoard&) = delete;
64  MSXMotherBoard& operator=(const MSXMotherBoard&) = delete;
65 
66  explicit MSXMotherBoard(Reactor& reactor);
68 
69  const std::string& getMachineID() const { return machineID; }
70  const std::string& getMachineName() const { return machineName; }
71 
76  bool execute();
77 
80  void fastForward(EmuTime::param time, bool fast);
81 
83  void exitCPULoopAsync();
84  void exitCPULoopSync();
85 
89  void pause();
90  void unpause();
91 
92  void powerUp();
93 
94  void doReset();
95  void activate(bool active);
96  bool isActive() const { return active; }
97  bool isFastForwarding() const { return fastForwarding; }
98 
100 
101  const HardwareConfig* getMachineConfig() const { return machineConfig; }
102  void setMachineConfig(HardwareConfig* machineConfig);
103  std::string getMachineType() const;
104  bool isTurboR() const;
105 
106  std::string loadMachine(const std::string& machine);
107 
108  using Extensions = std::vector<std::unique_ptr<HardwareConfig>>;
109  const Extensions& getExtensions() const { return extensions; }
110  HardwareConfig* findExtension(string_view extensionName);
111  std::string loadExtension(string_view extensionName, string_view slotname);
112  std::string insertExtension(string_view name,
113  std::unique_ptr<HardwareConfig> extension);
114  void removeExtension(const HardwareConfig& extension);
115 
116  // The following classes are unique per MSX machine
118  MSXCommandController& getMSXCommandController() { return *msxCommandController; }
119  Scheduler& getScheduler() { return *scheduler; }
120  MSXEventDistributor& getMSXEventDistributor() { return *msxEventDistributor; }
121  StateChangeDistributor& getStateChangeDistributor() { return *stateChangeDistributor; }
122  CartridgeSlotManager& getSlotManager() { return *slotManager; }
123  RealTime& getRealTime() { return *realTime; }
124  Debugger& getDebugger() { return *debugger; }
125  MSXMixer& getMSXMixer() { return *msxMixer; }
127  MSXCPU& getCPU();
132  JoystickPortIf& getJoystickPort(unsigned port);
135  ReverseManager& getReverseManager() { return *reverseManager; }
136  Reactor& getReactor() { return reactor; }
137  VideoSourceSetting& getVideoSource() { return videoSourceSetting; }
138 
139  // convenience methods
142 
145  EmuTime::param getCurrentTime();
146 
149  void addDevice(MSXDevice& device);
150  void removeDevice(MSXDevice& device);
151 
159 
168  template<typename T, typename ... Args>
169  std::shared_ptr<T> getSharedStuff(string_view name, Args&& ...args)
170  {
171  auto& weak = sharedStuffMap[name];
172  auto shared = std::static_pointer_cast<T>(weak.lock());
173  if (shared) return shared;
174 
175  shared = std::make_shared<T>(std::forward<Args>(args)...);
176  weak = shared;
177  return shared;
178  }
179 
185  {
186  assert(mapperIOCounter);
187  return *mapperIO;
188  }
189  void destroyMapperIO();
190 
198  std::string getUserName(const std::string& hwName);
199  void freeUserName(const std::string& hwName, const std::string& userName);
200 
201  template<typename Archive>
202  void serialize(Archive& ar, unsigned version);
203 
204 private:
205  void powerDown();
206  void deleteMachine();
207 
208  Reactor& reactor;
209  std::string machineID;
210  std::string machineName;
211 
212  std::vector<MSXDevice*> availableDevices; // no ownership, no order
213 
216 
217  std::unique_ptr<MSXMapperIO> mapperIO;
218  unsigned mapperIOCounter;
219 
220  // These two should normally be the same, only during savestate loading
221  // machineConfig will already be filled in, but machineConfig2 not yet.
222  // This is important when an exception happens during loading of
223  // machineConfig2 (otherwise machineConfig2 gets deleted twice).
224  // See also HardwareConfig::serialize() and setMachineConfig()
225  std::unique_ptr<HardwareConfig> machineConfig2;
226  HardwareConfig* machineConfig;
227 
228  Extensions extensions; // order matters: later extension might depend on earlier ones
229 
230  // order of unique_ptr's is important!
231  std::unique_ptr<AddRemoveUpdate> addRemoveUpdate;
232  std::unique_ptr<MSXCliComm> msxCliComm;
233  std::unique_ptr<MSXEventDistributor> msxEventDistributor;
234  std::unique_ptr<StateChangeDistributor> stateChangeDistributor;
235  std::unique_ptr<MSXCommandController> msxCommandController;
236  std::unique_ptr<Scheduler> scheduler;
237  std::unique_ptr<EventDelay> eventDelay;
238  std::unique_ptr<RealTime> realTime;
239  std::unique_ptr<Debugger> debugger;
240  std::unique_ptr<MSXMixer> msxMixer;
241  std::unique_ptr<PluggingController> pluggingController;
242  std::unique_ptr<MSXCPU> msxCpu;
243  std::unique_ptr<MSXCPUInterface> msxCpuInterface;
244  std::unique_ptr<PanasonicMemory> panasonicMemory;
245  std::unique_ptr<MSXDeviceSwitch> deviceSwitch;
246  std::unique_ptr<CassettePortInterface> cassettePort;
247  std::unique_ptr<JoystickPortIf> joystickPort[2];
248  std::unique_ptr<JoyPortDebuggable> joyPortDebuggable;
249  std::unique_ptr<RenShaTurbo> renShaTurbo;
250  std::unique_ptr<LedStatus> ledStatus;
251  VideoSourceSetting videoSourceSetting;
252 
253  std::unique_ptr<CartridgeSlotManager> slotManager;
254  std::unique_ptr<ReverseManager> reverseManager;
255  std::unique_ptr<ResetCmd> resetCommand;
256  std::unique_ptr<LoadMachineCmd> loadMachineCommand;
257  std::unique_ptr<ListExtCmd> listExtCommand;
258  std::unique_ptr<ExtCmd> extCommand;
259  std::unique_ptr<RemoveExtCmd> removeExtCommand;
260  std::unique_ptr<MachineNameInfo> machineNameInfo;
261  std::unique_ptr<MachineTypeInfo> machineTypeInfo;
262  std::unique_ptr<DeviceInfo> deviceInfo;
263  friend class DeviceInfo;
264 
265  std::unique_ptr<FastForwardHelper> fastForwardHelper;
266 
267  std::unique_ptr<SettingObserver> settingObserver;
268  friend class SettingObserver;
269  BooleanSetting& powerSetting;
270 
271  bool powered;
272  bool active;
273  bool fastForwarding;
274 };
276 
277 class ExtCmd final : public RecordedCommand
278 {
279 public:
280  ExtCmd(MSXMotherBoard& motherBoard, std::string commandName);
281  void execute(array_ref<TclObject> tokens, TclObject& result,
282  EmuTime::param time) override;
283  std::string help(const std::vector<std::string>& tokens) const override;
284  void tabCompletion(std::vector<std::string>& tokens) const override;
285 private:
286  MSXMotherBoard& motherBoard;
287  std::string commandName;
288 };
289 
290 } // namespace openmsx
291 
292 #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()
MSXCPUInterface & getCPUInterface()
std::string insertExtension(string_view name, std::unique_ptr< HardwareConfig > extension)
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
const std::string & getMachineName() 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()
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)
const std::string & getMachineID() const
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:15
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
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().