OXYGENHUB
◆ main openmsx / src / RealTime.hh
2.3 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef MSXREALTIME_HH
2
#define MSXREALTIME_HH
3
4
#include "EmuTime.hh"
5
#include "EventListener.hh"
6
#include "Observer.hh"
7
#include "Schedulable.hh"
8
9
#include <cstdint>
10
11
namespace openmsx {
12
13
class MSXMotherBoard;
14
class GlobalSettings;
15
class EventDistributor;
16
class EventDelay;
17
class BooleanSetting;
18
class SpeedManager;
19
class ThrottleManager;
20
class Setting;
21
22
class RealTime final : private Schedulable, private EventListener
23
                     , private Observer<Setting>
24
                     , private Observer<SpeedManager>
25
                     , private Observer<ThrottleManager>
26
{
27
public:
28
	explicit RealTime(
29
		MSXMotherBoard& motherBoard, GlobalSettings& globalSettings,
30
		EventDelay& eventDelay);
31
	~RealTime();
32
33
	/** Convert EmuTime to RealTime.
34
	  */
35
	[[nodiscard]] double getRealDuration(EmuTime time1, EmuTime time2) const;
36
37
	/** Convert RealTime to EmuTime.
38
	  */
39
	[[nodiscard]] EmuDuration getEmuDuration(double realDur) const;
40
41
	/** Check that there is enough real time left before we reach as certain
42
	  * point in emulated time.
43
	  * @param us Real time duration is micro seconds.
44
	  * @param time Point in emulated time.
45
	  */
46
	[[nodiscard]] bool timeLeft(uint64_t us, EmuTime time) const;
47
48
	void resync();
49
50
	void enable();
51
	void disable();
52
53
private:
54
	/** Synchronize EmuTime with RealTime.
55
	  * @param time The current emulation time.
56
	  * @param allowSleep Is this method allowed to sleep, typically the
57
	  *                   result of a previous call to timeLeft() is passed.
58
	  */
59
	void sync(EmuTime time, bool allowSleep);
60
61
	// Schedulable
62
	void executeUntil(EmuTime time) override;
63
64
	// EventListener
65
	bool signalEvent(const Event& event) override;
66
67
	// Observer<Setting>
68
	void update(const Setting& setting) noexcept override;
69
	// Observer<SpeedManager>
70
	void update(const SpeedManager& speedManager) noexcept override;
71
	// Observer<ThrottleManager>
72
	void update(const ThrottleManager& throttleManager) noexcept override;
73
74
	void internalSync(EmuTime time, bool allowSleep);
75
76
	MSXMotherBoard& motherBoard;
77
	EventDistributor& eventDistributor;
78
	EventDelay& eventDelay;
79
	SpeedManager& speedManager;
80
	ThrottleManager& throttleManager;
81
	BooleanSetting& pauseSetting;
82
	BooleanSetting& powerSetting;
83
84
	uint64_t idealRealTime;
85
	EmuTime emuTime = EmuTime::zero();
86
	double sleepAdjust;
87
	bool enabled = true;
88
};
89
90
} // namespace openmsx
91
92
#endif