2002-07-27 13:08:48 +00:00
|
|
|
/*
|
2021-01-14 18:29:34 +00:00
|
|
|
* Copyright (C) 2002-2021 The DOSBox Team
|
2002-07-27 13:08:48 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2004-08-04 09:12:57 +00:00
|
|
|
* GNU General Public License for more details.
|
2002-07-27 13:08:48 +00:00
|
|
|
*
|
2019-01-25 14:09:58 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-07-27 13:08:48 +00:00
|
|
|
*/
|
|
|
|
|
2005-03-24 10:32:09 +00:00
|
|
|
#ifndef DOSBOX_TIMER_H
|
|
|
|
#define DOSBOX_TIMER_H
|
|
|
|
|
2021-06-16 08:11:16 -05:00
|
|
|
#include <cassert>
|
2021-06-16 15:50:43 -05:00
|
|
|
#include <cmath>
|
2021-06-16 13:41:34 -05:00
|
|
|
#include <cstdlib>
|
2021-06-16 08:11:16 -05:00
|
|
|
|
2021-06-07 23:04:40 -05:00
|
|
|
#include <chrono>
|
2021-06-16 08:11:16 -05:00
|
|
|
#include <limits>
|
2021-06-16 13:41:34 -05:00
|
|
|
#include <thread>
|
2021-06-07 23:04:40 -05:00
|
|
|
|
2002-07-27 13:08:48 +00:00
|
|
|
/* underlying clock rate in HZ */
|
|
|
|
|
2002-09-15 11:46:06 +00:00
|
|
|
#define PIT_TICK_RATE 1193182
|
|
|
|
|
2004-02-07 18:37:16 +00:00
|
|
|
typedef void (*TIMER_TickHandler)(void);
|
2002-07-27 13:08:48 +00:00
|
|
|
|
2020-11-07 11:38:27 +00:00
|
|
|
/* Register a function that gets called every time if 1 or more ticks pass */
|
2004-02-07 18:37:16 +00:00
|
|
|
void TIMER_AddTickHandler(TIMER_TickHandler handler);
|
|
|
|
void TIMER_DelTickHandler(TIMER_TickHandler handler);
|
2002-07-27 13:08:48 +00:00
|
|
|
|
2002-11-17 08:38:49 +00:00
|
|
|
/* This will add 1 milliscond to all timers */
|
|
|
|
void TIMER_AddTick(void);
|
2002-07-27 13:08:48 +00:00
|
|
|
|
2021-06-07 23:04:40 -05:00
|
|
|
static inline int64_t GetTicks(void)
|
|
|
|
{
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::steady_clock::now().time_since_epoch())
|
|
|
|
.count();
|
|
|
|
}
|
|
|
|
|
2021-06-16 08:11:16 -05:00
|
|
|
static inline int64_t GetTicksUs(void)
|
|
|
|
{
|
|
|
|
return std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
|
std::chrono::steady_clock::now().time_since_epoch())
|
|
|
|
.count();
|
|
|
|
}
|
|
|
|
|
2021-06-07 23:04:40 -05:00
|
|
|
static inline int GetTicksDiff(int64_t new_ticks, int64_t old_ticks)
|
|
|
|
{
|
|
|
|
assert(new_ticks >= old_ticks);
|
2021-06-16 08:11:16 -05:00
|
|
|
assert((new_ticks - old_ticks) <= std::numeric_limits<int>::max());
|
2021-06-07 23:04:40 -05:00
|
|
|
return static_cast<int>(new_ticks - old_ticks);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int GetTicksSince(int64_t old_ticks)
|
|
|
|
{
|
|
|
|
const auto now = GetTicks();
|
2021-06-16 08:11:16 -05:00
|
|
|
assert((now - old_ticks) <= std::numeric_limits<int>::max());
|
|
|
|
return GetTicksDiff(now, old_ticks);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int GetTicksUsSince(int64_t old_ticks)
|
|
|
|
{
|
|
|
|
const auto now = GetTicksUs();
|
|
|
|
assert((now - old_ticks) <= std::numeric_limits<int>::max());
|
2021-06-07 23:04:40 -05:00
|
|
|
return GetTicksDiff(now, old_ticks);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void Delay(int milliseconds)
|
|
|
|
{
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
|
|
|
|
}
|
|
|
|
|
2021-06-16 13:58:42 -05:00
|
|
|
static inline void DelayUs(int microseconds)
|
|
|
|
{
|
|
|
|
std::this_thread::sleep_for(std::chrono::microseconds(microseconds));
|
|
|
|
}
|
|
|
|
|
2021-06-18 09:09:20 -05:00
|
|
|
// The duration to use for precise sleep
|
|
|
|
static constexpr int precise_delay_duration_us = 100;
|
|
|
|
// The tolerance to allow for sleep variation
|
|
|
|
static constexpr int precise_delay_tolerance_us = precise_delay_duration_us;
|
|
|
|
// The estimate of how long the sleep should take (microseconds)
|
|
|
|
static constexpr double precise_delay_default_estimate = 5e-5;
|
|
|
|
// Use the estimate value as the defaul mean time taken
|
|
|
|
static constexpr double precise_delay_default_mean = 5e-5;
|
|
|
|
|
2021-06-16 13:41:34 -05:00
|
|
|
static inline void DelayPrecise(int milliseconds)
|
|
|
|
{
|
|
|
|
static double estimate = precise_delay_default_estimate;
|
|
|
|
static double mean = precise_delay_default_mean;
|
|
|
|
static double m2 = 0;
|
|
|
|
static int64_t count = 1;
|
|
|
|
|
|
|
|
double seconds = milliseconds / 1e3;
|
|
|
|
|
2021-06-18 09:09:20 -05:00
|
|
|
// sleep as long as we can, then spinlock the rest
|
2021-06-16 13:41:34 -05:00
|
|
|
while (seconds > estimate) {
|
|
|
|
const auto start = GetTicksUs();
|
2021-06-18 09:09:20 -05:00
|
|
|
DelayUs(precise_delay_duration_us);
|
2021-06-16 13:41:34 -05:00
|
|
|
const double observed = GetTicksUsSince(start) / 1e6;
|
|
|
|
seconds -= observed;
|
|
|
|
|
|
|
|
++count;
|
|
|
|
const double delta = observed - mean;
|
|
|
|
mean += delta / count;
|
|
|
|
m2 += delta * (observed - mean);
|
2021-06-16 15:50:43 -05:00
|
|
|
const double stddev = std::sqrt(m2 / (count - 1));
|
2021-06-16 13:41:34 -05:00
|
|
|
estimate = mean + stddev;
|
|
|
|
}
|
|
|
|
|
|
|
|
// spin lock
|
|
|
|
const auto spin_start = GetTicksUs();
|
|
|
|
const int spin_remain = static_cast<int>(seconds * 1e6);
|
|
|
|
while (GetTicksUsSince(spin_start) <= spin_remain);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool CanDelayPrecise(void)
|
|
|
|
{
|
|
|
|
bool is_precise = true;
|
|
|
|
|
|
|
|
for (int i=0;i<10;i++) {
|
|
|
|
const auto start = GetTicksUs();
|
2021-06-18 09:09:20 -05:00
|
|
|
DelayUs(precise_delay_duration_us);
|
2021-06-16 13:41:34 -05:00
|
|
|
const auto elapsed = GetTicksUsSince(start);
|
2021-06-18 09:09:20 -05:00
|
|
|
if (std::abs(elapsed - precise_delay_duration_us) > precise_delay_tolerance_us) is_precise = false;
|
2021-06-16 13:41:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return is_precise;
|
|
|
|
}
|
2002-07-27 13:08:48 +00:00
|
|
|
#endif
|