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
|
|
|
|
|
2022-06-09 12:26:45 -07:00
|
|
|
#include "bit_view.h"
|
2021-06-16 08:11:16 -05:00
|
|
|
|
2022-06-09 12:26:45 -07:00
|
|
|
#include <cassert>
|
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 */
|
|
|
|
|
2021-08-05 20:46:06 -07:00
|
|
|
constexpr int PIT_TICK_RATE = 1193182;
|
2002-09-15 11:46:06 +00:00
|
|
|
|
2021-08-07 08:23:52 -07:00
|
|
|
// 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.
|
2022-06-21 16:09:31 -07:00
|
|
|
constexpr auto period_of_1k_pit_ticks = 1000.0 / static_cast<double>(PIT_TICK_RATE);
|
2022-06-21 16:58:24 -07:00
|
|
|
constexpr auto period_of_1k_pit_ticks_f = static_cast<float>(period_of_1k_pit_ticks);
|
2021-08-07 08:23:52 -07:00
|
|
|
|
2022-05-24 14:03:00 -07:00
|
|
|
/* PIT operating modes represented in 3 bits:
|
|
|
|
1 to 3 Operating mode :
|
|
|
|
0 0 0 = Mode 0 (interrupt on terminal count)
|
|
|
|
0 0 1 = Mode 1 (hardware re-triggerable one-shot)
|
|
|
|
0 1 0 = Mode 2 (rate generator)
|
|
|
|
0 1 1 = Mode 3 (square wave generator)
|
|
|
|
1 0 0 = Mode 4 (software triggered strobe)
|
|
|
|
1 0 1 = Mode 5 (hardware triggered strobe)
|
|
|
|
1 1 0 = Mode 2 (rate generator, same as 010b)
|
|
|
|
1 1 1 = Mode 3 (square wave generator, same as 011b)
|
|
|
|
Refs: http://www.osdever.net/bkerndev/Docs/pit.htm
|
|
|
|
https://wiki.osdev.org/Programmable_Interval_Timer#Operating_Modes
|
|
|
|
*/
|
|
|
|
enum class PitMode : uint8_t {
|
2022-06-21 15:19:50 -07:00
|
|
|
InterruptOnTerminalCount = 0b0'0'0,
|
|
|
|
OneShot = 0b0'0'1,
|
|
|
|
RateGenerator = 0b0'1'0,
|
|
|
|
SquareWave = 0b0'1'1,
|
|
|
|
SoftwareStrobe = 0b1'0'0,
|
|
|
|
HardwareStrobe = 0b1'0'1,
|
|
|
|
RateGeneratorAlias = 0b1'1'0,
|
|
|
|
SquareWaveAlias = 0b1'1'1,
|
2022-05-24 14:03:00 -07:00
|
|
|
Inactive,
|
|
|
|
};
|
|
|
|
|
2022-06-09 12:26:45 -07:00
|
|
|
/* PPI Port B Control Register
|
|
|
|
Bit System Description
|
|
|
|
~~~ ~~~~~~ ~~~~~~~~~~~
|
|
|
|
0 XT & PC Timer 2 gate to speaker output (read+write)
|
|
|
|
1 XT & PC Speaker data state (read+write)
|
|
|
|
4 XT & PC Toggles with each read
|
|
|
|
5 XT-only Toggles with each read
|
|
|
|
PC-only Mirrors timer 2 gate to speaker output
|
|
|
|
7 XT-only Clear keyboard buffer
|
|
|
|
*/
|
|
|
|
|
|
|
|
union PpiPortB {
|
|
|
|
uint8_t data = {0};
|
2022-06-21 14:30:53 -07:00
|
|
|
bit_view<0, 1> timer2_gating;
|
|
|
|
bit_view<1, 1> speaker_output;
|
|
|
|
bit_view<4, 1> read_toggle;
|
|
|
|
bit_view<5, 1> xt_read_toggle;
|
|
|
|
bit_view<5, 1> timer2_gating_alias;
|
|
|
|
bit_view<7, 1> xt_clear_keyboard;
|
|
|
|
// virtual view of the timer and speaker output fields
|
|
|
|
bit_view<0, 2> timer2_gating_and_speaker_out;
|
2022-06-09 12:26:45 -07:00
|
|
|
};
|
|
|
|
|
2022-06-21 19:24:08 -07:00
|
|
|
const char *pit_mode_to_string(const PitMode mode);
|
2022-05-24 14:03:00 -07:00
|
|
|
|
|
|
|
/* PC Speakers functions, tightly related to the timer functions */
|
2022-06-21 14:32:43 -07:00
|
|
|
void PCSPEAKER_SetCounter(const int count, const PitMode pit_mode);
|
2022-06-21 14:30:53 -07:00
|
|
|
void PCSPEAKER_SetType(const PpiPortB &port_b);
|
2022-06-21 14:32:43 -07:00
|
|
|
void PCSPEAKER_SetPITControl(const PitMode pit_mode);
|
2022-05-24 14:03:00 -07:00
|
|
|
|
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-10-05 14:11:26 -05:00
|
|
|
extern const std::chrono::steady_clock::time_point system_start_time;
|
|
|
|
|
2021-06-19 16:25:41 -05:00
|
|
|
static inline int64_t GetTicks()
|
2021-06-07 23:04:40 -05:00
|
|
|
{
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
2021-10-05 14:11:26 -05:00
|
|
|
std::chrono::steady_clock::now() - system_start_time)
|
2021-06-07 23:04:40 -05:00
|
|
|
.count();
|
|
|
|
}
|
|
|
|
|
2021-06-19 16:25:41 -05:00
|
|
|
static inline int64_t GetTicksUs()
|
2021-06-16 08:11:16 -05:00
|
|
|
{
|
|
|
|
return std::chrono::duration_cast<std::chrono::microseconds>(
|
2021-10-05 14:11:26 -05:00
|
|
|
std::chrono::steady_clock::now() - system_start_time)
|
2021-06-16 08:11:16 -05:00
|
|
|
.count();
|
|
|
|
}
|
|
|
|
|
2023-06-13 21:33:56 +10:00
|
|
|
static inline int64_t GetTicksDiff(const int64_t new_ticks, const int64_t old_ticks)
|
2021-06-07 23:04:40 -05:00
|
|
|
{
|
|
|
|
assert(new_ticks >= old_ticks);
|
2023-06-13 21:33:56 +10:00
|
|
|
return new_ticks - old_ticks;
|
2021-06-07 23:04:40 -05:00
|
|
|
}
|
|
|
|
|
2023-06-13 21:33:56 +10:00
|
|
|
static inline int64_t GetTicksSince(const int64_t old_ticks)
|
2021-06-07 23:04:40 -05:00
|
|
|
{
|
|
|
|
const auto now = GetTicks();
|
2021-06-16 08:11:16 -05:00
|
|
|
return GetTicksDiff(now, old_ticks);
|
|
|
|
}
|
|
|
|
|
2023-06-13 21:33:56 +10:00
|
|
|
static inline int64_t GetTicksUsSince(const int64_t old_ticks)
|
2021-06-16 08:11:16 -05:00
|
|
|
{
|
|
|
|
const auto now = GetTicksUs();
|
2021-06-07 23:04:40 -05:00
|
|
|
return GetTicksDiff(now, old_ticks);
|
|
|
|
}
|
|
|
|
|
2023-06-13 21:33:56 +10:00
|
|
|
static inline void Delay(const int64_t milliseconds)
|
2021-06-07 23:04:40 -05:00
|
|
|
{
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
|
|
|
|
}
|
|
|
|
|
2023-06-13 21:33:56 +10:00
|
|
|
static inline void DelayUs(const int64_t microseconds)
|
2021-06-16 13:58:42 -05:00
|
|
|
{
|
|
|
|
std::this_thread::sleep_for(std::chrono::microseconds(microseconds));
|
|
|
|
}
|
|
|
|
|
2002-07-27 13:08:48 +00:00
|
|
|
#endif
|