Refactoring to enable future time scaling, coordinated clocks, etc.

This commit is contained in:
Ben Vanik
2015-05-26 22:20:46 -07:00
parent 05f2577fb7
commit 8244409501
19 changed files with 213 additions and 57 deletions

96
src/xenia/base/clock.cc Normal file
View File

@@ -0,0 +1,96 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2015 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include "xenia/base/clock.h"
#include "xenia/base/platform.h"
namespace xe {
double guest_time_scalar_ = 1.0;
uint64_t guest_tick_frequency_ = Clock::host_tick_frequency();
uint64_t guest_system_time_base_ = Clock::QueryHostSystemTime();
uint64_t Clock::host_tick_frequency() {
static LARGE_INTEGER frequency = {0};
if (!frequency.QuadPart) {
QueryPerformanceFrequency(&frequency);
}
return frequency.QuadPart;
}
uint64_t Clock::QueryHostTickCount() {
LARGE_INTEGER counter;
uint64_t time = 0;
if (QueryPerformanceCounter(&counter)) {
time = counter.QuadPart;
}
return time;
}
uint64_t Clock::QueryHostSystemTime() {
FILETIME t;
GetSystemTimeAsFileTime(&t);
return (uint64_t(t.dwHighDateTime) << 32) | t.dwLowDateTime;
}
uint32_t Clock::QueryHostUptimeMillis() { return ::GetTickCount(); }
double Clock::guest_time_scalar() { return guest_time_scalar_; }
void Clock::set_guest_time_scalar(double scalar) {
guest_time_scalar_ = scalar;
}
uint64_t Clock::guest_tick_frequency() { return guest_tick_frequency_; }
void Clock::set_guest_tick_frequency(uint64_t frequency) {
guest_tick_frequency_ = frequency;
}
uint64_t Clock::guest_system_time_base() { return guest_system_time_base_; }
void Clock::set_guest_system_time_base(uint64_t time_base) {
guest_system_time_base_ = time_base;
}
uint64_t Clock::QueryGuestTickCount() {
// TODO(benvanik): adjust.
return QueryHostTickCount();
}
uint64_t Clock::QueryGuestSystemTime() {
// TODO(benvanik): adjust.
return QueryHostSystemTime();
}
uint32_t Clock::QueryGuestUptimeMillis() {
// TODO(benvanik): adjust.
return QueryHostUptimeMillis();
}
uint32_t Clock::ScaleGuestDurationMillis(uint32_t guest_ms) {
// TODO(benvanik): adjust.
// if -1, return -1
// if 0, return 0
return guest_ms;
}
int64_t Clock::ScaleGuestDurationFileTime(int64_t guest_file_time) {
// TODO(benvanik): adjust.
// negative = relative times
// positive = absolute times
return guest_file_time;
}
void Clock::ScaleGuestDurationTimeval(long* tv_sec, long* tv_usec) {
// TODO(benvanik): adjust.
}
} // namespace xe

60
src/xenia/base/clock.h Normal file
View File

@@ -0,0 +1,60 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2015 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef XENIA_BASE_CLOCK_H_
#define XENIA_BASE_CLOCK_H_
#include <cstdint>
namespace xe {
class Clock {
public:
// Host ticks-per-second.
static uint64_t host_tick_frequency();
// Queries the current host tick count.
static uint64_t QueryHostTickCount();
// Host time, in FILETIME format.
static uint64_t QueryHostSystemTime();
// Queries the milliseconds since the host began.
static uint32_t QueryHostUptimeMillis();
// Guest time scalar.
static double guest_time_scalar();
// Sets the guest time scalar, adjusting tick and wall clock speed.
// Ex: 1x=normal, 2x=double speed, 1/2x=half speed.
static void set_guest_time_scalar(double scalar);
// Guest ticks-per-second.
static uint64_t guest_tick_frequency();
// Sets the guest ticks-per-second.
static void set_guest_tick_frequency(uint64_t frequency);
// Time based used for the guest system time.
static uint64_t guest_system_time_base();
// Sets the guest time base, used for computing the system time.
// By default this is the current system time.
static void set_guest_system_time_base(uint64_t time_base);
// Queries the current guest tick count, accounting for frequency adjustment
// and scaling.
static uint64_t QueryGuestTickCount();
// Queries the guest time, in FILETIME format, accounting for scaling.
static uint64_t QueryGuestSystemTime();
// Queries the milliseconds since the guest began, accounting for scaling.
static uint32_t QueryGuestUptimeMillis();
// Scales a time duration in milliseconds, from guest time.
static uint32_t ScaleGuestDurationMillis(uint32_t guest_ms);
// Scales a time duration in 100ns ticks like FILETIME, from guest time.
static int64_t ScaleGuestDurationFileTime(int64_t guest_file_time);
// Scales a time duration represented as a timeval, from guest time.
static void ScaleGuestDurationTimeval(long* tv_sec, long* tv_usec);
};
} // namespace xe
#endif // XENIA_BASE_CLOCK_H_

View File

@@ -43,10 +43,6 @@ class Fence {
std::atomic<bool> signaled_;
};
// Gets the current high-performance tick count.
uint64_t ticks();
uint64_t ticks_per_second();
// TODO(benvanik): processor info API.
// Gets a stable thread-specific ID, but may not be. Use for informative

View File

@@ -14,23 +14,6 @@
namespace xe {
namespace threading {
uint64_t ticks() {
LARGE_INTEGER counter;
uint64_t time = 0;
if (QueryPerformanceCounter(&counter)) {
time = counter.QuadPart;
}
return time;
}
uint64_t ticks_per_second() {
static LARGE_INTEGER freq = {0};
if (!freq.QuadPart) {
QueryPerformanceFrequency(&freq);
}
return freq.QuadPart;
}
uint32_t current_thread_id() {
return static_cast<uint32_t>(GetCurrentThreadId());
}