dosbox-staging/include/timer.h

95 lines
2.9 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2002-2021 The DOSBox Team
*
* 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
* GNU General Public License for more details.
*
* 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.
*/
#ifndef DOSBOX_TIMER_H
#define DOSBOX_TIMER_H
#include <cassert>
#include <chrono>
#include <limits>
2021-06-16 13:41:34 -05:00
#include <thread>
/* underlying clock rate in HZ */
2021-08-05 20:46:06 -07:00
constexpr int PIT_TICK_RATE = 1193182;
// Short-hand unit conversions
constexpr auto PIT_TICK_RATE_KHZ = static_cast<double>(PIT_TICK_RATE) / 1000.0;
// The rate at which a repeating event occurs is the frequency, measured in Hertz.
// The inverse of frequency is the time between events, called 'period'.
// In this case, we want the period of every 1000 PIT tick events.
constexpr auto PERIOD_OF_1K_PIT_TICKS = 1000.0 / static_cast<double>(PIT_TICK_RATE);
typedef void (*TIMER_TickHandler)(void);
2020-11-07 11:38:27 +00:00
/* Register a function that gets called every time if 1 or more ticks pass */
void TIMER_AddTickHandler(TIMER_TickHandler handler);
void TIMER_DelTickHandler(TIMER_TickHandler handler);
/* This will add 1 milliscond to all timers */
void TIMER_AddTick(void);
2021-06-19 16:25:41 -05:00
static inline int64_t GetTicks()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
}
2021-06-19 16:25:41 -05:00
static inline int64_t GetTicksUs()
{
return std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
}
2021-06-19 16:25:41 -05:00
static inline int GetTicksDiff(const int64_t new_ticks, const int64_t old_ticks)
{
assert(new_ticks >= old_ticks);
assert((new_ticks - old_ticks) <= std::numeric_limits<int>::max());
return static_cast<int>(new_ticks - old_ticks);
}
2021-06-19 16:25:41 -05:00
static inline int GetTicksSince(const int64_t old_ticks)
{
const auto now = GetTicks();
assert((now - old_ticks) <= std::numeric_limits<int>::max());
return GetTicksDiff(now, old_ticks);
}
2021-06-19 16:25:41 -05:00
static inline int GetTicksUsSince(const int64_t old_ticks)
{
const auto now = GetTicksUs();
assert((now - old_ticks) <= std::numeric_limits<int>::max());
return GetTicksDiff(now, old_ticks);
}
2021-06-19 16:25:41 -05:00
static inline void Delay(const int milliseconds)
{
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
}
2021-06-19 16:25:41 -05:00
static inline void DelayUs(const int microseconds)
2021-06-16 13:58:42 -05:00
{
std::this_thread::sleep_for(std::chrono::microseconds(microseconds));
}
#endif