openMSX
ReverseManager.hh
Go to the documentation of this file.
1 #ifndef REVERSEMANGER_HH
2 #define REVERSEMANGER_HH
3 
4 #include "Schedulable.hh"
5 #include "EventListener.hh"
6 #include "StateChangeListener.hh"
7 #include "EmuTime.hh"
8 #include "MemBuffer.hh"
9 #include <vector>
10 #include <map>
11 #include <memory>
12 #include <cstdint>
13 
14 namespace openmsx {
15 
16 class MSXMotherBoard;
17 class Keyboard;
18 class EventDelay;
19 class EventDistributor;
20 class ReverseCmd;
21 class TclObject;
22 
23 class ReverseManager : private Schedulable, private EventListener
24  , private StateChangeRecorder
25 {
26 public:
27  ReverseManager(MSXMotherBoard& motherBoard);
29 
30  // Keyboard is special because we need to transfer the host keyboard
31  // state on 'reverse goto' to be able to resynchronize when replay
32  // stops. See Keyboard::transferHostKeyMatrix() for more info.
33  void registerKeyboard(Keyboard& keyboard);
34 
35  // To not loose any events we need to flush delayed events before
36  // switching machine. See comments in goTo() for more info.
37  void registerEventDelay(EventDelay& eventDelay);
38 
39  // Should only be used by MSXMotherBoard to be able to transfer
40  // reRecordCount to ReverseManager for version 2 of MSXMotherBoard
41  // serializers.
42  void setReRecordCount(unsigned reRecordCount);
43 
44 private:
45  struct ReverseChunk {
46  ReverseChunk();
47  ReverseChunk(ReverseChunk&& rhs);
48  ReverseChunk& operator=(ReverseChunk&& rhs);
49 
50  EmuTime time;
51  MemBuffer<uint8_t> savestate;
52 
53  // Number of recorded events (or replay index) when this
54  // snapshot was created. So when going back replay should
55  // start at this index.
56  unsigned eventCount;
57  };
58  typedef std::map<unsigned, ReverseChunk> Chunks;
59  typedef std::vector<std::shared_ptr<StateChange>> Events;
60 
61  struct ReverseHistory {
62  void swap(ReverseHistory& other);
63  void clear();
64  unsigned getNextSeqNum(EmuTime::param time) const;
65 
66  Chunks chunks;
67  Events events;
68  };
69 
70  bool isCollecting() const;
71 
72  void start();
73  void stop();
74  void status(TclObject& result) const;
75  void debugInfo(TclObject& result) const;
76  void goBack(const std::vector<TclObject>& tokens);
77  void goTo(const std::vector<TclObject>& tokens);
78  void saveReplay(const std::vector<TclObject>& tokens, TclObject& result);
79  void loadReplay(const std::vector<TclObject>& tokens, TclObject& result);
80 
81  void signalStopReplay(EmuTime::param time);
82  EmuTime::param getEndTime(const ReverseHistory& history) const;
83  void goTo(EmuTime::param targetTime, bool novideo);
84  void goTo(EmuTime::param targetTime, bool novideo,
85  ReverseHistory& history, bool sameTimeLine);
86  void transferHistory(ReverseHistory& oldHistory,
87  unsigned oldEventCount);
88  void transferState(MSXMotherBoard& newBoard);
89  void takeSnapshot(EmuTime::param time);
90  void schedule(EmuTime::param time);
91  void replayNextEvent();
92  template<unsigned N> void dropOldSnapshots(unsigned count);
93 
94  // Schedulable
95  virtual void executeUntil(EmuTime::param time, int userData);
96 
97  // EventListener
98  virtual int signalEvent(const std::shared_ptr<const Event>& event);
99 
100  // StateChangeRecorder
101  virtual void signalStateChange(const std::shared_ptr<StateChange>& event);
102  virtual void stopReplay(EmuTime::param time);
103  virtual bool isReplaying() const;
104 
105  MSXMotherBoard& motherBoard;
106  EventDistributor& eventDistributor;
107  const std::unique_ptr<ReverseCmd> reverseCmd;
108  Keyboard* keyboard;
109  EventDelay* eventDelay;
110  ReverseHistory history;
111  unsigned replayIndex;
112  bool collecting;
113  bool pendingTakeSnapshot;
114 
115  unsigned reRecordCount;
116 
117  friend class ReverseCmd;
118  friend struct Replay;
119 };
120 
121 } // namespace openmsx
122 
123 #endif
This class is responsible for translating host events into MSX events.
Definition: EventDelay.hh:28
void setReRecordCount(unsigned reRecordCount)
void registerKeyboard(Keyboard &keyboard)
Every class that wants to get scheduled at some point must inherit from this class.
Definition: Schedulable.hh:16
ReverseManager(MSXMotherBoard &motherBoard)
void registerEventDelay(EventDelay &eventDelay)