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