OXYGENHUB
◆ main openmsx / src / sound / EmuTimer.cc
2.4 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#include "EmuTimer.hh"
2
3
#include "serialize.hh"
4
5
#include <memory>
6
7
namespace openmsx {
8
9
std::unique_ptr<EmuTimer> EmuTimer::createOPM_1(
10
	Scheduler& scheduler, EmuTimerCallback& cb)
11
{
12
	return std::make_unique<EmuTimer>(
13
		scheduler, cb, 1, 3579545, 64 * 2, 1024);
14
}
15
16
std::unique_ptr<EmuTimer> EmuTimer::createOPM_2(
17
	Scheduler& scheduler, EmuTimerCallback& cb)
18
{
19
	return std::make_unique<EmuTimer>(
20
		scheduler, cb, 2, 3579545, 64 * 2 * 16, 256);
21
}
22
23
std::unique_ptr<EmuTimer> EmuTimer::createOPP_2(
24
	Scheduler& scheduler, EmuTimerCallback& cb)
25
{
26
	return std::make_unique<EmuTimer>(
27
		scheduler, cb, 2, 3579545, 64 * 2 * 32, 256);
28
}
29
30
std::unique_ptr<EmuTimer> EmuTimer::createOPL3_1(
31
	Scheduler& scheduler, EmuTimerCallback& cb)
32
{
33
	return std::make_unique<EmuTimer>(
34
		scheduler, cb, 0x40, 3579545, 72 * 4, 256);
35
}
36
37
std::unique_ptr<EmuTimer> EmuTimer::createOPL3_2(
38
	Scheduler& scheduler, EmuTimerCallback& cb)
39
{
40
	return std::make_unique<EmuTimer>(
41
		scheduler, cb, 0x20, 3579545, 72 * 4 * 4, 256);
42
}
43
44
std::unique_ptr<EmuTimer> EmuTimer::createOPL4_1(
45
	Scheduler& scheduler, EmuTimerCallback& cb)
46
{
47
	return std::make_unique<EmuTimer>(
48
		scheduler, cb, 0x40, 33868800, 72 * 38, 256);
49
}
50
51
std::unique_ptr<EmuTimer> EmuTimer::createOPL4_2(
52
	Scheduler& scheduler, EmuTimerCallback& cb)
53
{
54
	return std::make_unique<EmuTimer>(
55
		scheduler, cb, 0x20, 33868800, 72 * 38 * 4, 256);
56
}
57
58
59
EmuTimer::EmuTimer(Scheduler& scheduler_, EmuTimerCallback& cb_,
60
                   uint8_t flag_, unsigned freq_num, unsigned freq_denom,
61
                   int maxVal_)
62
	: Schedulable(scheduler_), cb(cb_)
63
	, maxVal(maxVal_), count(maxVal_)
64
	, flag(flag_)
65
{
66
	clock.setFreq(freq_num, freq_denom);
67
}
68
69
void EmuTimer::setValue(int value)
70
{
71
	count = maxVal - value;
72
}
73
74
void EmuTimer::setStart(bool start, EmuTime time)
75
{
76
	if (start != counting) {
77
		counting = start;
78
		if (start) {
79
			schedule(time);
80
		} else {
81
			unschedule();
82
		}
83
	}
84
}
85
86
void EmuTimer::schedule(EmuTime time)
87
{
88
	clock.reset(time);
89
	clock += count;
90
	setSyncPoint(clock.getTime());
91
}
92
93
void EmuTimer::unschedule()
94
{
95
	removeSyncPoint();
96
}
97
98
void EmuTimer::executeUntil(EmuTime time)
99
{
100
	cb.callback(flag);
101
	schedule(time);
102
}
103
104
template<typename Archive>
105
void EmuTimer::serialize(Archive& ar, unsigned /*version*/)
106
{
107
	ar.template serializeBase<Schedulable>(*this);
108
	ar.serialize("count",    count,
109
	             "counting", counting);
110
}
111
INSTANTIATE_SERIALIZE_METHODS(EmuTimer);
112
113
} // namespace openmsx