2014-02-01 16:32:20 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: GPL - See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS Virtual DOS Machine
|
2015-09-18 17:01:49 +00:00
|
|
|
* FILE: subsystems/mvdm/ntvdm/clock.c
|
2014-02-01 16:32:20 +00:00
|
|
|
* PURPOSE: Clock for VDM
|
|
|
|
* PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
|
|
|
|
* Hermes Belusca-Maito (hermes.belusca@sfr.fr)
|
|
|
|
*/
|
|
|
|
|
2015-10-04 11:49:28 +00:00
|
|
|
/* INCLUDES *******************************************************************/
|
|
|
|
|
2015-10-03 19:17:55 +00:00
|
|
|
#include "ntvdm.h"
|
|
|
|
|
2014-02-01 16:32:20 +00:00
|
|
|
#define NDEBUG
|
2015-10-03 19:17:55 +00:00
|
|
|
#include <debug.h>
|
2014-02-01 16:32:20 +00:00
|
|
|
|
2015-10-04 11:49:28 +00:00
|
|
|
#include "emulator.h"
|
|
|
|
#include "clock.h"
|
|
|
|
|
|
|
|
#include "cpu/cpu.h"
|
|
|
|
#include "hardware/cmos.h"
|
|
|
|
#include "hardware/ps2.h"
|
|
|
|
#include "hardware/pit.h"
|
|
|
|
#include "hardware/video/svga.h"
|
|
|
|
#include "hardware/mouse.h"
|
|
|
|
|
2014-02-01 16:32:20 +00:00
|
|
|
/* DEFINES ********************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Activate IPS_DISPLAY if you want to display the
|
2015-04-17 00:20:39 +00:00
|
|
|
* number of instructions per second.
|
2014-02-01 16:32:20 +00:00
|
|
|
*/
|
|
|
|
// #define IPS_DISPLAY
|
|
|
|
|
|
|
|
/* Processor speed */
|
2015-04-17 18:36:45 +00:00
|
|
|
#define STEPS_PER_CYCLE 1024
|
2014-02-01 16:32:20 +00:00
|
|
|
|
|
|
|
/* VARIABLES ******************************************************************/
|
|
|
|
|
2015-04-17 00:20:39 +00:00
|
|
|
static LIST_ENTRY Timers;
|
2014-11-11 15:49:56 +00:00
|
|
|
static LARGE_INTEGER StartPerfCount, Frequency;
|
2015-04-18 01:39:29 +00:00
|
|
|
// static ULONG StartTickCount;
|
|
|
|
static LARGE_INTEGER Counter;
|
|
|
|
static ULONG CurrentTickCount;
|
2015-06-09 19:22:52 +00:00
|
|
|
static ULONGLONG LastCycles = 0ULL;
|
2015-04-18 01:39:29 +00:00
|
|
|
static PHARDWARE_TIMER IpsTimer;
|
2014-09-16 00:51:15 +00:00
|
|
|
|
2016-08-27 21:55:34 +00:00
|
|
|
ULONGLONG CurrentCycleCount = 0ULL;
|
|
|
|
ULONGLONG CurrentIps = 20000000ULL; // 20 MIPS is a good estimate
|
|
|
|
|
2015-04-17 00:20:39 +00:00
|
|
|
/* PRIVATE FUNCTIONS **********************************************************/
|
2014-02-01 16:32:20 +00:00
|
|
|
|
2015-06-09 19:22:52 +00:00
|
|
|
static VOID FASTCALL IpsCallback(ULONGLONG ElapsedTime)
|
2015-04-17 00:20:39 +00:00
|
|
|
{
|
2016-08-27 23:05:51 +00:00
|
|
|
#ifdef IPS_DISPLAY
|
2016-08-27 21:55:34 +00:00
|
|
|
static INT NumCalls = 0;
|
2016-08-27 23:05:51 +00:00
|
|
|
#endif
|
2021-09-13 01:33:14 +00:00
|
|
|
|
2016-08-27 21:55:34 +00:00
|
|
|
ULONGLONG NewIps = 10ULL * (CurrentCycleCount - LastCycles) / ElapsedTime;
|
|
|
|
CurrentIps = (CurrentIps + NewIps) >> 1;
|
2015-06-09 19:22:52 +00:00
|
|
|
|
|
|
|
#ifdef IPS_DISPLAY
|
2016-08-27 21:55:34 +00:00
|
|
|
NumCalls++;
|
|
|
|
if (NumCalls == 10)
|
|
|
|
{
|
|
|
|
DPRINT1("NTVDM: %I64u Instructions Per Second\n", CurrentIps);
|
|
|
|
NumCalls = 0;
|
|
|
|
}
|
2014-02-01 16:32:20 +00:00
|
|
|
#endif
|
|
|
|
|
2016-08-27 21:55:34 +00:00
|
|
|
LastCycles = CurrentCycleCount;
|
2015-06-09 19:22:52 +00:00
|
|
|
}
|
|
|
|
|
2014-02-01 16:32:20 +00:00
|
|
|
/* PUBLIC FUNCTIONS ***********************************************************/
|
|
|
|
|
|
|
|
VOID ClockUpdate(VOID)
|
|
|
|
{
|
2014-09-30 23:47:23 +00:00
|
|
|
extern BOOLEAN CpuRunning;
|
2014-02-01 16:32:20 +00:00
|
|
|
UINT i;
|
2015-04-17 00:20:39 +00:00
|
|
|
PLIST_ENTRY Entry;
|
2015-08-01 18:56:44 +00:00
|
|
|
PHARDWARE_TIMER Timer;
|
2014-11-11 15:49:56 +00:00
|
|
|
|
|
|
|
while (VdmRunning && CpuRunning)
|
|
|
|
{
|
2015-04-18 01:39:29 +00:00
|
|
|
/* Get the current counters */
|
2015-04-18 00:57:50 +00:00
|
|
|
/// DWORD_PTR oldmask = SetThreadAffinityMask(GetCurrentThread(), 0);
|
2015-04-18 01:39:29 +00:00
|
|
|
CurrentTickCount = GetTickCount();
|
2015-04-18 00:57:50 +00:00
|
|
|
NtQueryPerformanceCounter(&Counter, NULL);
|
|
|
|
/// SetThreadAffinityMask(GetCurrentThread(), oldmask);
|
2015-04-17 00:20:39 +00:00
|
|
|
|
|
|
|
/* Continue CPU emulation */
|
|
|
|
for (i = 0; VdmRunning && CpuRunning && (i < STEPS_PER_CYCLE); i++)
|
|
|
|
{
|
|
|
|
CpuStep();
|
2016-08-27 21:55:34 +00:00
|
|
|
++CurrentCycleCount;
|
2015-04-17 00:20:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-01 18:56:44 +00:00
|
|
|
Entry = Timers.Flink;
|
|
|
|
while (Entry != &Timers)
|
2015-04-17 00:20:39 +00:00
|
|
|
{
|
|
|
|
ULONGLONG Ticks = (ULONGLONG)-1;
|
2015-08-01 18:56:44 +00:00
|
|
|
|
|
|
|
Timer = CONTAINING_RECORD(Entry, HARDWARE_TIMER, Link);
|
|
|
|
Entry = Entry->Flink;
|
2015-04-17 00:20:39 +00:00
|
|
|
|
|
|
|
ASSERT((Timer->EnableCount > 0) && (Timer->Flags & HARDWARE_TIMER_ENABLED));
|
|
|
|
|
|
|
|
if (Timer->Delay)
|
|
|
|
{
|
|
|
|
if (Timer->Flags & HARDWARE_TIMER_PRECISE)
|
|
|
|
{
|
|
|
|
/* Use the performance counter for precise timers */
|
|
|
|
if (Counter.QuadPart <= Timer->LastTick.QuadPart) continue;
|
|
|
|
Ticks = (Counter.QuadPart - Timer->LastTick.QuadPart) / Timer->Delay;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Use the regular tick count for normal timers */
|
|
|
|
if (CurrentTickCount <= Timer->LastTick.LowPart) continue;
|
|
|
|
Ticks = (CurrentTickCount - Timer->LastTick.LowPart) / (ULONG)Timer->Delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ticks == 0) continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer->Callback(Ticks);
|
|
|
|
|
|
|
|
if (Timer->Flags & HARDWARE_TIMER_ONESHOT)
|
|
|
|
{
|
|
|
|
/* Disable this timer */
|
|
|
|
DisableHardwareTimer(Timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the time of the last timer tick */
|
|
|
|
Timer->LastTick.QuadPart += Ticks * Timer->Delay;
|
|
|
|
}
|
2015-05-22 21:21:43 +00:00
|
|
|
|
|
|
|
/* Yield execution to other threads */
|
2015-05-23 02:08:50 +00:00
|
|
|
// FIXME: Disabled because it causes timing issues (slowdowns).
|
|
|
|
// NtYieldExecution();
|
2014-02-01 16:32:20 +00:00
|
|
|
}
|
2015-04-17 00:20:39 +00:00
|
|
|
}
|
2014-02-01 16:32:20 +00:00
|
|
|
|
2015-04-18 00:29:14 +00:00
|
|
|
PHARDWARE_TIMER CreateHardwareTimer(ULONG Flags, ULONGLONG Delay, PHARDWARE_TIMER_PROC Callback)
|
2015-04-17 00:20:39 +00:00
|
|
|
{
|
|
|
|
PHARDWARE_TIMER Timer;
|
2015-05-22 21:21:43 +00:00
|
|
|
|
2015-08-01 18:56:44 +00:00
|
|
|
Timer = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*Timer));
|
2015-04-17 00:20:39 +00:00
|
|
|
if (Timer == NULL) return NULL;
|
|
|
|
|
|
|
|
Timer->Flags = Flags & ~HARDWARE_TIMER_ENABLED;
|
|
|
|
Timer->EnableCount = 0;
|
|
|
|
Timer->Callback = Callback;
|
2015-08-01 18:56:44 +00:00
|
|
|
Timer->LastTick.QuadPart = 0;
|
2015-04-18 00:29:14 +00:00
|
|
|
SetHardwareTimerDelay(Timer, Delay);
|
2015-04-17 00:20:39 +00:00
|
|
|
|
|
|
|
if (Flags & HARDWARE_TIMER_ENABLED) EnableHardwareTimer(Timer);
|
|
|
|
return Timer;
|
|
|
|
}
|
2014-02-01 16:32:20 +00:00
|
|
|
|
2015-04-17 00:20:39 +00:00
|
|
|
VOID EnableHardwareTimer(PHARDWARE_TIMER Timer)
|
|
|
|
{
|
|
|
|
/* Increment the count */
|
|
|
|
Timer->EnableCount++;
|
2014-02-01 16:32:20 +00:00
|
|
|
|
2015-04-17 00:20:39 +00:00
|
|
|
/* Check if the count is above 0 but the timer isn't enabled */
|
|
|
|
if ((Timer->EnableCount > 0) && !(Timer->Flags & HARDWARE_TIMER_ENABLED))
|
2014-02-01 16:32:20 +00:00
|
|
|
{
|
2015-04-17 00:20:39 +00:00
|
|
|
if (Timer->Flags & HARDWARE_TIMER_PRECISE)
|
|
|
|
{
|
|
|
|
NtQueryPerformanceCounter(&Timer->LastTick, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Timer->LastTick.LowPart = GetTickCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer->Flags |= HARDWARE_TIMER_ENABLED;
|
|
|
|
InsertTailList(&Timers, &Timer->Link);
|
2014-02-01 16:32:20 +00:00
|
|
|
}
|
2015-04-17 00:20:39 +00:00
|
|
|
}
|
2014-02-01 16:32:20 +00:00
|
|
|
|
2015-04-17 00:20:39 +00:00
|
|
|
VOID DisableHardwareTimer(PHARDWARE_TIMER Timer)
|
|
|
|
{
|
|
|
|
/* Decrement the count */
|
|
|
|
Timer->EnableCount--;
|
2014-02-01 16:32:20 +00:00
|
|
|
|
2015-04-17 00:20:39 +00:00
|
|
|
/* Check if the count is 0 or less but the timer is enabled */
|
|
|
|
if ((Timer->EnableCount <= 0) && (Timer->Flags & HARDWARE_TIMER_ENABLED))
|
2014-02-01 16:32:20 +00:00
|
|
|
{
|
2015-04-17 00:20:39 +00:00
|
|
|
/* Disable the timer */
|
|
|
|
Timer->Flags &= ~HARDWARE_TIMER_ENABLED;
|
|
|
|
RemoveEntryList(&Timer->Link);
|
2014-02-01 16:32:20 +00:00
|
|
|
}
|
2015-04-17 00:20:39 +00:00
|
|
|
}
|
2014-02-01 16:32:20 +00:00
|
|
|
|
2015-04-17 00:20:39 +00:00
|
|
|
VOID SetHardwareTimerDelay(PHARDWARE_TIMER Timer, ULONGLONG NewDelay)
|
|
|
|
{
|
|
|
|
if (Timer->Flags & HARDWARE_TIMER_PRECISE)
|
2014-09-16 00:51:15 +00:00
|
|
|
{
|
2015-04-17 00:20:39 +00:00
|
|
|
/* Convert the delay from nanoseconds to performance counter ticks */
|
|
|
|
Timer->Delay = (NewDelay * Frequency.QuadPart + 500000000ULL) / 1000000000ULL;
|
2014-09-16 00:51:15 +00:00
|
|
|
}
|
2015-04-17 00:20:39 +00:00
|
|
|
else
|
2014-02-01 16:32:20 +00:00
|
|
|
{
|
2015-04-17 23:58:01 +00:00
|
|
|
Timer->Delay = NewDelay / 1000000ULL;
|
2014-02-01 16:32:20 +00:00
|
|
|
}
|
2015-04-17 00:20:39 +00:00
|
|
|
}
|
2014-02-01 16:32:20 +00:00
|
|
|
|
2015-04-17 00:20:39 +00:00
|
|
|
VOID DestroyHardwareTimer(PHARDWARE_TIMER Timer)
|
|
|
|
{
|
2015-05-05 08:07:08 +00:00
|
|
|
if (Timer)
|
|
|
|
{
|
|
|
|
if (Timer->Flags & HARDWARE_TIMER_ENABLED) RemoveEntryList(&Timer->Link);
|
|
|
|
RtlFreeHeap(RtlGetProcessHeap(), 0, Timer);
|
|
|
|
}
|
2015-04-17 00:20:39 +00:00
|
|
|
}
|
2014-02-01 16:32:20 +00:00
|
|
|
|
2015-04-17 00:20:39 +00:00
|
|
|
BOOLEAN ClockInitialize(VOID)
|
|
|
|
{
|
|
|
|
InitializeListHead(&Timers);
|
2014-02-01 16:32:20 +00:00
|
|
|
|
|
|
|
/* Initialize the performance counter (needed for hardware timers) */
|
2014-11-11 15:49:56 +00:00
|
|
|
/* Find the starting performance */
|
|
|
|
NtQueryPerformanceCounter(&StartPerfCount, &Frequency);
|
|
|
|
if (Frequency.QuadPart == 0)
|
2014-02-01 16:32:20 +00:00
|
|
|
{
|
|
|
|
wprintf(L"FATAL: Performance counter not available\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-11-11 15:49:56 +00:00
|
|
|
/* Find the starting tick count */
|
2015-04-18 01:39:29 +00:00
|
|
|
// StartTickCount = GetTickCount();
|
2014-02-01 16:32:20 +00:00
|
|
|
|
2016-08-27 21:55:34 +00:00
|
|
|
IpsTimer = CreateHardwareTimer(HARDWARE_TIMER_ENABLED, HZ_TO_NS(10), IpsCallback);
|
2015-04-17 00:20:39 +00:00
|
|
|
if (IpsTimer == NULL)
|
|
|
|
{
|
2015-06-09 19:22:52 +00:00
|
|
|
wprintf(L"FATAL: Cannot create IPS timer.\n");
|
2015-04-17 00:20:39 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-02-01 16:32:20 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|