Add busy loop code and make HighResTimer work on demand rather automatically or ifdefed
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#ifndef __JABYENGINE_HIGH_RES_TIMER_HPP__
|
||||
#define __JABYENGINE_HIGH_RES_TIMER_HPP__
|
||||
#include "../jabyengine_defines.h"
|
||||
#include <PSX/System/IOPorts/interrupt_io.hpp>
|
||||
#include <PSX/System/IOPorts/timer_io.hpp>
|
||||
|
||||
namespace JabyEngine {
|
||||
@@ -24,51 +25,57 @@ namespace JabyEngine {
|
||||
}
|
||||
};
|
||||
|
||||
#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;
|
||||
};
|
||||
|
||||
class HighResTime {
|
||||
public:
|
||||
class TimeStamp {
|
||||
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);
|
||||
uint16_t counter_10ms_value;
|
||||
uint16_t fraction;
|
||||
|
||||
static volatile uint16_t global_counter_10ms;
|
||||
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:
|
||||
HighResTime() = delete;
|
||||
~HighResTime() = delete;
|
||||
|
||||
static TimeStamp get_time_stamp() {
|
||||
return TimeStamp(HighResTime::global_counter_10ms, Timer_IO::Counter2.get_current_value());
|
||||
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;
|
||||
};
|
||||
#endif //JABYENGINE_USE_HIGH_PERCISION_TIMER
|
||||
|
||||
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 void enable() {
|
||||
Interrupt::enable_irq(Interrupt::Timer2);
|
||||
}
|
||||
|
||||
static void disable() {
|
||||
Interrupt::disable_irq(Interrupt::Timer2);
|
||||
}
|
||||
|
||||
static TimeStamp get_time_stamp() {
|
||||
return TimeStamp(HighResTime::global_counter_10ms, Timer_IO::Counter2.get_current_value());
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif //!__JABYENGINE_HIGH_RES_TIMER_HPP__
|
Reference in New Issue
Block a user