74 lines
3.2 KiB
C++
74 lines
3.2 KiB
C++
#ifndef __JABYENGINE_HIGH_RES_TIMER_HPP__
|
|
#define __JABYENGINE_HIGH_RES_TIMER_HPP__
|
|
#include "../jabyengine_defines.h"
|
|
#include <PSX/System/IOPorts/timer_io.hpp>
|
|
|
|
namespace JabyEngine {
|
|
struct CPUTicks {
|
|
static constexpr double Frequency_Hz = 33868800.0;
|
|
static constexpr double Frequency_Hz_Div8 = (Frequency_Hz/8.0);
|
|
|
|
template<typename T>
|
|
static constexpr T ticks_per_ns(double CPU_Frequency_Hz, double time = 1.0) {
|
|
return static_cast<T>((time/CPU_Frequency_Hz)*1000.0*1000.0*1000.0);
|
|
}
|
|
|
|
template<typename T>
|
|
static constexpr T ticks_per_us(double CPU_Frequency_Hz, double time = 1.0) {
|
|
return static_cast<T>(((time*1000.0)/ticks_per_ns<double>(CPU_Frequency_Hz)));
|
|
}
|
|
|
|
template<typename T>
|
|
static constexpr T ticks_per_ms(double CPU_Frequency_Hz, double time = 1.0) {
|
|
return static_cast<T>(((time*1000.0*1000.0)/ticks_per_ns<double>(CPU_Frequency_Hz)));
|
|
}
|
|
};
|
|
|
|
#ifdef JABYENGINE_USE_HIGH_PERCISION_TIMER
|
|
class HighResTime {
|
|
public:
|
|
class TimeStamp {
|
|
private:
|
|
uint16_t counter_10ms_value;
|
|
uint16_t fraction;
|
|
|
|
constexpr TimeStamp(uint16_t counter_10ms_value, uint16_t fraction) : counter_10ms_value(counter_10ms_value), fraction(fraction) {
|
|
}
|
|
|
|
constexpr static size_t to_us(uint16_t counter_10ms_value, uint16_t fraction) {
|
|
return counter_10ms_value*(10*1000) + ((fraction/HighResTime::TicksFor100us)*100);
|
|
}
|
|
|
|
constexpr static size_t to_ms(uint16_t counter_10ms_value, uint16_t fraction) {
|
|
return counter_10ms_value*10 + (fraction/HighResTime::TicksFor1ms);
|
|
}
|
|
|
|
public:
|
|
constexpr size_t microseconds_to(const TimeStamp& end) const {
|
|
return TimeStamp::to_us((end.counter_10ms_value - this->counter_10ms_value), (end.fraction - this->fraction));
|
|
}
|
|
|
|
constexpr size_t milliseconds_to(const TimeStamp& end) const {
|
|
return TimeStamp::to_ms((end.counter_10ms_value - this->counter_10ms_value), (end.fraction - this->fraction));
|
|
}
|
|
friend class HighResTime;
|
|
};
|
|
|
|
private:
|
|
static constexpr uint16_t TicksFor100us = CPUTicks::ticks_per_us<uint16_t>(CPUTicks::Frequency_Hz_Div8, 100.0);
|
|
static constexpr uint16_t TicksFor1ms = CPUTicks::ticks_per_ms<uint16_t>(CPUTicks::Frequency_Hz_Div8, 1.0);
|
|
static constexpr uint16_t TicksFor10ms = CPUTicks::ticks_per_ms<uint16_t>(CPUTicks::Frequency_Hz_Div8, 10.0);
|
|
|
|
static volatile uint16_t global_counter_10ms;
|
|
|
|
public:
|
|
HighResTime() = delete;
|
|
~HighResTime() = delete;
|
|
|
|
static TimeStamp get_time_stamp() {
|
|
return TimeStamp(HighResTime::global_counter_10ms, Timer_IO::Counter2.get_current_value());
|
|
}
|
|
};
|
|
#endif //JABYENGINE_USE_HIGH_PERCISION_TIMER
|
|
}
|
|
#endif //!__JABYENGINE_HIGH_RES_TIMER_HPP__
|