summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/attotime.h
blob: ba801392b7517c5b7ea6168acfaf764fbc8f20dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/**************************************************************************/
/**
 * @file attotime.h
 * Support functions for working with attotime data.
 * @defgroup ATTOTIME
 * @{
 * Support functions for working with attotime data.
 *
 * @class attotime
 *  Attotime is an attosecond-accurate timing system implemented as
 *  96-bit integers.
 *
 *     1 second      = 1e0 seconds
 *     1 millisecond = 1e-3 seconds
 *     1 microsecond = 1e-6 seconds
 *     1 nanosecond  = 1e-9 seconds
 *     1 picosecond  = 1e-12 seconds
 *     1 femtosecond = 1e-15 seconds
 *     1 attosecond  = 1e-18 seconds
 *
 * This may seem insanely accurate, but it has its uses when multiple
 * clocks in the system are run by independent crystals. It is also
 * useful to compute the attotime for something small, say 1 clock tick,
 * and still have it be accurate and useful for scaling.
 *
 * Attotime consists of a 32-bit seconds count and a 64-bit attoseconds
 * count. Because the lower bits are kept as attoseconds and not as a
 * full 64-bit value, there is headroom to make some operations simpler.
 */
/**************************************************************************/
#ifndef MAME_EMU_ATTOTIME_H
#define MAME_EMU_ATTOTIME_H

#pragma once

#include "emucore.h"
#include "xtal.h"

#include <cmath>
#undef min
#undef max

//**************************************************************************
//  CONSTANTS
//**************************************************************************

// core components of the attotime structure
typedef s64 attoseconds_t;
typedef s32 seconds_t;

// core definitions
constexpr attoseconds_t ATTOSECONDS_PER_SECOND_SQRT = 1'000'000'000;
constexpr attoseconds_t ATTOSECONDS_PER_SECOND = ATTOSECONDS_PER_SECOND_SQRT * ATTOSECONDS_PER_SECOND_SQRT;
constexpr attoseconds_t ATTOSECONDS_PER_MILLISECOND = ATTOSECONDS_PER_SECOND / 1'000;
constexpr attoseconds_t ATTOSECONDS_PER_MICROSECOND = ATTOSECONDS_PER_SECOND / 1'000'000;
constexpr attoseconds_t ATTOSECONDS_PER_NANOSECOND = ATTOSECONDS_PER_SECOND / 1'000'000'000;

constexpr seconds_t ATTOTIME_MAX_SECONDS = 1'000'000'000;



//**************************************************************************
//  MACROS
//**************************************************************************

// convert between a double and attoseconds
inline constexpr double ATTOSECONDS_TO_DOUBLE(attoseconds_t x) { return double(x) * 1e-18; }
inline constexpr attoseconds_t DOUBLE_TO_ATTOSECONDS(double x) { return attoseconds_t(x * 1e18); }

// convert between hertz (as a double) and attoseconds
inline constexpr double ATTOSECONDS_TO_HZ(attoseconds_t x) { return double(ATTOSECONDS_PER_SECOND) / double(x); }
template <typename T> inline constexpr attoseconds_t HZ_TO_ATTOSECONDS(T &&x) { return attoseconds_t(ATTOSECONDS_PER_SECOND / x); }
inline constexpr attoseconds_t HZ_TO_ATTOSECONDS(const XTAL &x) { return attoseconds_t(ATTOSECONDS_PER_SECOND / x); }

// macros for converting other seconds types to attoseconds
template <typename T> inline constexpr attoseconds_t ATTOSECONDS_IN_SEC(T &&x) { return attoseconds_t(x) * ATTOSECONDS_PER_SECOND; }
template <typename T> inline constexpr attoseconds_t ATTOSECONDS_IN_MSEC(T &&x) { return attoseconds_t(x) * ATTOSECONDS_PER_MILLISECOND; }
template <typename T> inline constexpr attoseconds_t ATTOSECONDS_IN_USEC(T &&x) { return attoseconds_t(x) * ATTOSECONDS_PER_MICROSECOND; }
template <typename T> inline constexpr attoseconds_t ATTOSECONDS_IN_NSEC(T &&x) { return attoseconds_t(x) * ATTOSECONDS_PER_NANOSECOND; }



//**************************************************************************
//  TYPE DEFINITIONS
//***************************************************************************/

// the attotime structure itself
class attotime
{
	// save_manager needs direct access for save/restore purposes
	friend class save_manager;

public:
	// construction/destruction
	constexpr attotime() noexcept : m_attoseconds(0), m_seconds(0) { }

	/** Constructs with @p secs seconds and @p attos attoseconds. */
	constexpr attotime(seconds_t secs, attoseconds_t attos) noexcept : m_attoseconds(attos), m_seconds(secs) { }

	constexpr attotime(const attotime& that) noexcept : m_attoseconds(that.m_attoseconds), m_seconds(that.m_seconds) { }

	// assignment
	attotime &operator=(const attotime& that) noexcept
	{
		this->m_seconds = that.m_seconds;
		this->m_attoseconds = that.m_attoseconds;
		return *this;
	}

	// queries
	constexpr bool is_zero() const noexcept { return (m_seconds == 0 && m_attoseconds == 0); }
	/** Test if value is above @ref ATTOTIME_MAX_SECONDS (considered an overflow) */
	constexpr bool is_never() const noexcept { return (m_seconds >= ATTOTIME_MAX_SECONDS); }

	// conversion to other forms
	constexpr double as_double() const noexcept { return double(m_seconds) + ATTOSECONDS_TO_DOUBLE(m_attoseconds); }
	constexpr attoseconds_t as_attoseconds() const noexcept;
	double as_hz() const noexcept { assert(!is_zero()); return m_seconds == 0 ? ATTOSECONDS_TO_HZ(m_attoseconds) : is_never() ? 0.0 : 1.0 / as_double(); }
	double as_khz() const noexcept { assert(!is_zero()); return m_seconds == 0 ? double(ATTOSECONDS_PER_MILLISECOND) / double(m_attoseconds) : is_never() ? 0.0 : 1e-3 / as_double(); }
	double as_mhz() const noexcept { assert(!is_zero()); return m_seconds == 0 ? double(ATTOSECONDS_PER_MICROSECOND) / double(m_attoseconds) : is_never() ? 0.0 : 1e-6 / as_double(); }
	u64 as_ticks(u32 frequency) const;
	u64 as_ticks(const XTAL &xtal) const { return as_ticks(xtal.value()); }
	/** Convert to string using at @p precision */
	const char *as_string(int precision = 9) const;

	/** Convert to string for human readability in logs */
	std::string to_string() const;

	/** @return the attoseconds portion. */
	constexpr attoseconds_t attoseconds() const noexcept { return m_attoseconds; }
	/** @return the seconds portion. */
	constexpr seconds_t seconds() const noexcept { return m_seconds; }

	void set_seconds(seconds_t seconds) { m_seconds = seconds;}
	void set_attoseconds(attoseconds_t attoseconds) { m_attoseconds = attoseconds;}

	static attotime from_double(double _time);
	static attotime from_ticks(u64 ticks, u32 frequency);
	static attotime from_ticks(u64 ticks, const XTAL &xtal) { return from_ticks(ticks, xtal.value()); }
	/** Create an attotime from a integer count of seconds @seconds */
	static constexpr attotime from_seconds(s32 seconds) { return attotime(seconds, 0); }
	/** Create an attotime from a integer count of milliseconds @msec */
	static constexpr attotime from_msec(s64 msec) { return attotime(msec / 1000, (msec % 1000) * (ATTOSECONDS_PER_SECOND / 1000)); }
	/** Create an attotime from a integer count of microseconds @usec */
	static constexpr attotime from_usec(s64 usec) { return attotime(usec / 1000000, (usec % 1000000) * (ATTOSECONDS_PER_SECOND / 1000000)); }
	/** Create an attotime from a integer count of nanoseconds @nsec */
	static constexpr attotime from_nsec(s64 nsec) { return attotime(nsec / 1000000000, (nsec % 1000000000) * (ATTOSECONDS_PER_SECOND / 1000000000)); }
	/** Create an attotime from at the given frequency @frequency */
	static attotime from_hz(u32 frequency) { return (frequency > 1) ? attotime(0, HZ_TO_ATTOSECONDS(frequency)) : (frequency == 1) ? attotime(1, 0) : attotime::never; }
	static attotime from_hz(int frequency) { return (frequency > 0) ? from_hz(u32(frequency)) : attotime::never; }
	static attotime from_hz(const XTAL &xtal) { return (xtal.dvalue() > 1.0) ? attotime(0, HZ_TO_ATTOSECONDS(xtal)) : from_hz(xtal.dvalue()); }
	static attotime from_hz(double frequency)
	{
		if (frequency > 1.0)
			return attotime(0, HZ_TO_ATTOSECONDS(frequency));
		else if (frequency > 0.0)
		{
			double i, f = modf(1.0 / frequency, &i);
			return attotime(i, f * ATTOSECONDS_PER_SECOND);
		}
		else
			return attotime::never;
	}

	// math
	attotime &operator+=(const attotime &right) noexcept;
	attotime &operator-=(const attotime &right) noexcept;
	attotime &operator*=(u32 factor);
	attotime &operator/=(u32 factor);

	// constants
	static const attotime never;
	static const attotime zero;

	// friend functions
	friend attotime operator+(const attotime &left, const attotime &right) noexcept;
	friend attotime operator-(const attotime &left, const attotime &right) noexcept;
	friend attotime operator*(const attotime &left, u32 factor);
	friend attotime operator*(u32 factor, const attotime &right);
	friend attotime operator/(const attotime &left, u32 factor);
	friend constexpr bool operator==(const attotime &left, const attotime &right) noexcept;
	friend constexpr bool operator!=(const attotime &left, const attotime &right) noexcept;
	friend constexpr bool operator<(const attotime &left, const attotime &right) noexcept;
	friend constexpr bool operator<=(const attotime &left, const attotime &right) noexcept;
	friend constexpr bool operator>(const attotime &left, const attotime &right) noexcept;
	friend constexpr bool operator>=(const attotime &left, const attotime &right) noexcept;

private:
	// members
	attoseconds_t   m_attoseconds;
	seconds_t       m_seconds;
};
/** @} */


class precise_clock
{
public:
	precise_clock(u32 rate = 1, u32 divider = 1) :
		m_rate(rate),
		m_divider(1),
		m_period((rate == 0) ? ATTOSECONDS_PER_SECOND : ((ATTOSECONDS_PER_SECOND + rate - 1) / rate)),
		m_base(attotime::zero),
		m_tick_base(0)
	{
	}

	void reset_tick_base() { m_tick_base = 0; }

	void set_rate(attotime const &base, u32 rate, u32 divider = 0)
	{
		// remember the ticks at the old rate at the base time before setting it
		m_tick_base = tick(base);
		m_base = base;

		// configure the new rate
		m_rate = rate;
		if (divider != 0)
			m_divider = divider;
		m_period = (rate == 0) ? ATTOSECONDS_PER_SECOND : ((ATTOSECONDS_PER_SECOND + rate - 1) / rate);
	}

	u64 tick(attotime const &time)
	{
		attotime delta = time - m_base;
		u64 count = delta.seconds() * m_rate + delta.attoseconds() / m_period;
		return m_tick_base + ((m_divider == 1) ? count : (count / m_divider));
	}

	attotime tick_time(u64 ticknum)
	{
		// can't get time of ticks prior to the last rate switch
		if (ticknum < m_tick_base)
			return attotime::zero;
		ticknum = (ticknum - m_tick_base) * m_divider;

		u64 seconds = ticknum / m_rate;
		attotime delta(seconds, (ticknum - seconds * m_rate) * m_period);
		return delta + m_base;
	}

private:
	u32 m_rate;					// integral rate in Hz
	u32 m_divider;				// clock divider (or 1 if no divider)
	attoseconds_t m_period;		// period, measured in attoseconds
	attotime m_base;			// time of last rate change
	u64 m_tick_base;			// ticks at last rate change
};


//**************************************************************************
//  INLINE FUNCTIONS
//**************************************************************************

/** handle addition between two attotimes */
inline attotime operator+(const attotime &left, const attotime &right) noexcept
{
	attotime result;

	// if one of the items is never, return never
	if (left.m_seconds >= ATTOTIME_MAX_SECONDS || right.m_seconds >= ATTOTIME_MAX_SECONDS)
		return attotime::never;

	// add the seconds and attoseconds
	result.m_attoseconds = left.m_attoseconds + right.m_attoseconds;
	result.m_seconds = left.m_seconds + right.m_seconds;

	// normalize and return
	if (result.m_attoseconds >= ATTOSECONDS_PER_SECOND)
	{
		result.m_attoseconds -= ATTOSECONDS_PER_SECOND;
		result.m_seconds++;
	}

	// overflow
	if (result.m_seconds >= ATTOTIME_MAX_SECONDS)
		return attotime::never;
	return result;
}

inline attotime &attotime::operator+=(const attotime &right) noexcept
{
	// if one of the items is never, return never
	if (this->m_seconds >= ATTOTIME_MAX_SECONDS || right.m_seconds >= ATTOTIME_MAX_SECONDS)
		return *this = never;

	// add the seconds and attoseconds
	m_attoseconds += right.m_attoseconds;
	m_seconds += right.m_seconds;

	// normalize and return
	if (this->m_attoseconds >= ATTOSECONDS_PER_SECOND)
	{
		this->m_attoseconds -= ATTOSECONDS_PER_SECOND;
		this->m_seconds++;
	}

	// overflow
	if (this->m_seconds >= ATTOTIME_MAX_SECONDS)
		return *this = never;
	return *this;
}


/** handle subtraction between two attotimes */
inline attotime operator-(const attotime &left, const attotime &right) noexcept
{
	attotime result;

	// if time1 is never, return never
	if (left.m_seconds >= ATTOTIME_MAX_SECONDS)
		return attotime::never;

	// add the seconds and attoseconds
	result.m_attoseconds = left.m_attoseconds - right.m_attoseconds;
	result.m_seconds = left.m_seconds - right.m_seconds;

	// normalize and return
	if (result.m_attoseconds < 0)
	{
		result.m_attoseconds += ATTOSECONDS_PER_SECOND;
		result.m_seconds--;
	}
	return result;
}

inline attotime &attotime::operator-=(const attotime &right) noexcept
{
	// if time1 is never, return never
	if (this->m_seconds >= ATTOTIME_MAX_SECONDS)
		return *this = never;

	// add the seconds and attoseconds
	m_attoseconds -= right.m_attoseconds;
	m_seconds -= right.m_seconds;

	// normalize and return
	if (this->m_attoseconds < 0)
	{
		this->m_attoseconds += ATTOSECONDS_PER_SECOND;
		this->m_seconds--;
	}
	return *this;
}


/** handle multiplication by an integral factor; defined in terms of the assignment operators */
inline attotime operator*(const attotime &left, u32 factor)
{
	attotime result = left;
	result *= factor;
	return result;
}

inline attotime operator*(u32 factor, const attotime &right)
{
	attotime result = right;
	result *= factor;
	return result;
}

/** handle division by an integral factor; defined in terms of the assignment operators */
inline attotime operator/(const attotime &left, u32 factor)
{
	attotime result = left;
	result /= factor;
	return result;
}


/** handle comparisons between attotimes */
inline constexpr bool operator==(const attotime &left, const attotime &right) noexcept
{
	return (left.m_seconds == right.m_seconds && left.m_attoseconds == right.m_attoseconds);
}

inline constexpr bool operator!=(const attotime &left, const attotime &right) noexcept
{
	return (left.m_seconds != right.m_seconds || left.m_attoseconds != right.m_attoseconds);
}

inline constexpr bool operator<(const attotime &left, const attotime &right) noexcept
{
	return (left.m_seconds < right.m_seconds || (left.m_seconds == right.m_seconds && left.m_attoseconds < right.m_attoseconds));
}

inline constexpr bool operator<=(const attotime &left, const attotime &right) noexcept
{
	return (left.m_seconds < right.m_seconds || (left.m_seconds == right.m_seconds && left.m_attoseconds <= right.m_attoseconds));
}

inline constexpr bool operator>(const attotime &left, const attotime &right) noexcept
{
	return (left.m_seconds > right.m_seconds || (left.m_seconds == right.m_seconds && left.m_attoseconds > right.m_attoseconds));
}

inline constexpr bool operator>=(const attotime &left, const attotime &right) noexcept
{
	return (left.m_seconds > right.m_seconds || (left.m_seconds == right.m_seconds && left.m_attoseconds >= right.m_attoseconds));
}


/** Convert to an attoseconds value, clamping to +/- 1 second */
inline constexpr attoseconds_t attotime::as_attoseconds() const noexcept
{
	return
			(m_seconds == 0) ? m_attoseconds :                              // positive values between 0 and 1 second
			(m_seconds == -1) ? (m_attoseconds - ATTOSECONDS_PER_SECOND) :  // negative values between -1 and 0 seconds
			(m_seconds > 0) ? ATTOSECONDS_PER_SECOND :                      // out-of-range positive values
			-ATTOSECONDS_PER_SECOND;                                        // out-of-range negative values
}


/** as_ticks - convert to ticks at @p frequency */
inline u64 attotime::as_ticks(u32 frequency) const
{
	u32 fracticks = (attotime(0, m_attoseconds) * frequency).m_seconds;
	return mulu_32x32(m_seconds, frequency) + fracticks;
}


/** Create an attotime from a tick count @ticks at the given frequency @frequency  */
inline attotime attotime::from_ticks(u64 ticks, u32 frequency)
{
	if (frequency > 0)
	{
		attoseconds_t attos_per_tick = HZ_TO_ATTOSECONDS(frequency);

		if (ticks < frequency)
			return attotime(0, ticks * attos_per_tick);

		u32 remainder;
		s32 secs = divu_64x32_rem(ticks, frequency, remainder);
		return attotime(secs, u64(remainder) * attos_per_tick);
	}
	else
		return attotime::never;
}

/** Create an attotime from floating point count of seconds @p _time */
inline attotime attotime::from_double(double _time)
{
	seconds_t secs = floor(_time);
	_time -= double(secs);
	attoseconds_t attos = DOUBLE_TO_ATTOSECONDS(_time);
	return attotime(secs, attos);
}


#endif // MAME_EMU_ATTOTIME_H