Support HighResolutionTimer in a better fashion

This commit is contained in:
Jaby 2023-01-10 20:36:28 +01:00
parent 2b34d105c8
commit 3bd3a57801
8 changed files with 152 additions and 129 deletions

View File

@ -29,8 +29,14 @@ namespace JabyEngine {
static constexpr auto CounterTargetValue = BitRange<uint32_t>::from_to(0, 15);
};
struct __no_align CounterValue : public ComplexBitMap<uint32_t> {
__io_port_inherit_complex_bit_map(CounterValue);
static constexpr auto Value = BitRange<uint32_t>::from_to(0, 15);
};
struct __no_align Counter {
IOPort<uint32_t> value;
IOPort<CounterValue> value;
IOPort<CounterMode> mode;
IOPort<CounterTarget> target;
private:

View File

@ -0,0 +1,75 @@
#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.value.read().get_value(Timer_IO::CounterValue::Value));
}
};
#endif //JABYENGINE_USE_HIGH_PERCISION_TIMER
}
#endif //!__JABYENGINE_HIGH_RES_TIMER_HPP__

View File

@ -1,45 +0,0 @@
#ifndef __JABYENGINE_TIMER_HPP__
#define __JABYENGINE_TIMER_HPP__
#include "../jabyengine_defines.h"
namespace JabyEngine {
class GlobalTime {
public:
class TimeStamp {
private:
size_t value;
constexpr TimeStamp(size_t value) : value(value) {}
public:
#ifdef JABYENGINE_USE_HIGH_PERCISION_TIMER
constexpr size_t microseconds_to(const TimeStamp& ts) const {
return (ts.value - this->value)*100;
}
constexpr size_t milliseconds_to(const TimeStamp& ts) const {
return microseconds_to(ts)/1000;
}
#else
constexpr size_t milliseconds_to(const TimeStamp& ts) const {
return (ts.value - this->value)*10;
}
#endif //JABYENGINE_USE_HIGH_PERCISION_TIMER
friend class GlobalTime;
};
private:
static volatile size_t global_counter;
public:
GlobalTime() = delete;
~GlobalTime() = delete;
static TimeStamp get_time_stamp() {
return TimeStamp(GlobalTime::global_counter);
}
};
}
#endif //!__JABYENGINE_TIMER_HPP__

View File

@ -1,7 +1,7 @@
#include "BootLoader/boot_loader.hpp"
#include <PSX/System/IOPorts/dMa_io.hpp>
#include <PSX/Timer/timer.hpp>
#include <PSX/Timer/high_res_timer.hpp>
#include <stdio.h>
namespace JabyEngine {
@ -16,11 +16,11 @@ namespace JabyEngine {
SPU::stop_voices();
Timer::setup();
const auto start = GlobalTime::get_time_stamp();
const auto start = HighResTime::get_time_stamp();
printf("Start...\n");
GPU::setup();
GPU::display_logo();
const auto end = GlobalTime::get_time_stamp();
const auto end = HighResTime::get_time_stamp();
printf("GPU setup took %ims %ius\n", start.milliseconds_to(end), start.microseconds_to(end));
//Pause??

View File

@ -1,51 +1,37 @@
#include <PSX/System/IOPorts/interrupt_io.hpp>
#include <PSX/System/IOPorts/timer_io.hpp>
#include <PSX/System/syscalls.h>
#include <PSX/jabyengine_config.hpp>
#ifdef JABYENGINE_USE_HIGH_PERCISION_TIMER
#include <PSX/System/IOPorts/interrupt_io.hpp>
#include <PSX/System/syscalls.h>
#define private public
#include <PSX/Timer/high_res_timer.hpp>
#undef private
#include <GPU/gpu.hpp>
#include <stdio.h>
namespace JabyEngine {
namespace Timer {
extern InterrupCallback IRQCallback;
void setup() {
using namespace Timer_IO;
namespace JabyEngine {
namespace Timer {
extern InterrupCallback IRQCallback;
static constexpr auto Mode = CounterMode::with(CounterMode::FreeRun, Counter2::SyncMode::Freerun, CounterMode::ResetAfterTarget, CounterMode::IRQAtTarget, CounterMode::IRQEveryTime, CounterMode::IRQPulse, Counter2::Source::System_Clock_Div_8);
static constexpr double CPU_Frequency_Hz = 33868800.0;
static constexpr double CPU_Frequency_Hz_Div8 = (CPU_Frequency_Hz/8.0);
Interrupt::disable_irq(Interrupt::Timer2);
template<typename T>
static constexpr T NS_Per_Tick(double CPU_Frequency_Hz, double time = 1.0) {
return static_cast<T>((time/CPU_Frequency_Hz)*1000.0*1000.0*1000.0);
}
__syscall_EnterCriticalSection();
__syscall_SysEnqIntRP(Timer2Irq, &IRQCallback);
__syscall_ExitCriticalSection();
template<typename T>
static constexpr T US_Per_Tick(double CPU_Frequency_Hz, double time = 1.0) {
return static_cast<T>(((time*1000.0)/NS_Per_Tick<double>(CPU_Frequency_Hz)));
}
Counter2.target.write(CounterTarget::CounterTargetValue.with(HighResTime::TicksFor10ms));
Counter2.mode.write(Mode);
template<typename T>
static constexpr T MS_Per_Tick(double CPU_Frequency_Hz, double time = 1.0) {
return static_cast<T>(((time*1000.0*1000.0)/NS_Per_Tick<double>(CPU_Frequency_Hz)));
}
void setup() {
using namespace Timer_IO;
static constexpr auto Mode = CounterMode::with(CounterMode::FreeRun, Counter2::SyncMode::Freerun, CounterMode::ResetAfterTarget, CounterMode::IRQAtTarget, CounterMode::IRQEveryTime, CounterMode::IRQPulse, Counter2::Source::System_Clock_Div_8);
#ifdef JABYENGINE_USE_HIGH_PERCISION_TIMER
static constexpr uint16_t Target = US_Per_Tick<uint16_t>(CPU_Frequency_Hz, 100.0);
#else
static constexpr uint16_t Target = MS_Per_Tick<uint16_t>(CPU_Frequency_Hz_Div8, 10.0);
#endif //#ifdef JABYENGINE_USE_HIGH_PERCISION_TIMER
Interrupt::disable_irq(Interrupt::Timer2);
__syscall_EnterCriticalSection();
__syscall_SysEnqIntRP(Timer2Irq, &IRQCallback);
__syscall_ExitCriticalSection();
Counter2.target.write(CounterTarget::CounterTargetValue.with(Target));
Counter2.mode.write(Mode);
Interrupt::enable_irq(Interrupt::Timer2);
Interrupt::enable_irq(Interrupt::Timer2);
}
}
}
}
#else
namespace JabyEngine {
namespace Timer {
void setup() {
}
}
}
#endif //JABYENGINE_USE_HIGH_PERCISION_TIMER

View File

@ -0,0 +1,38 @@
#include <PSX/jabyengine_config.hpp>
#ifdef JABYENGINE_USE_HIGH_PERCISION_TIMER
#define private public
#include <PSX/System/IOPorts/interrupt_io.hpp>
#include <PSX/System/IOPorts/timer_io.hpp>
#include <PSX/System/syscalls.h>
#include <PSX/Timer/high_res_timer.hpp>
#undef private
namespace JabyEngine {
volatile uint16_t HighResTime :: global_counter_10ms = 0;
namespace Timer {
static InterruptVerifierResult interrupt_verifier() {
if(Interrupt::is_irq(Interrupt::Timer2)) {
return InterruptVerifierResult::ExecuteHandler;
}
else {
return InterruptVerifierResult::SkipHandler;
}
}
static void interrupt_handler(uint32_t) {
HighResTime::global_counter_10ms = HighResTime::global_counter_10ms + 1;
Interrupt::ack_irq(Interrupt::Timer2);
__syscall_ReturnFromException();
}
InterrupCallback IRQCallback = {
.next = nullptr,
.handler_function = reinterpret_cast<InterruptHandler>(interrupt_handler),
.verifier_function = interrupt_verifier
};
}
}
#endif //JABYENGINE_USE_HIGH_PERCISION_TIMER

View File

@ -1,35 +0,0 @@
#define private public
#include <PSX/System/IOPorts/interrupt_io.hpp>
#include <PSX/System/IOPorts/timer_io.hpp>
#include <PSX/System/syscalls.h>
#include <PSX/Timer/timer.hpp>
#undef private
namespace JabyEngine {
volatile size_t GlobalTime :: global_counter = 0;
namespace Timer {
static InterruptVerifierResult interrupt_verifier() {
if(Interrupt::is_irq(Interrupt::Timer2)) {
return InterruptVerifierResult::ExecuteHandler;
}
else {
return InterruptVerifierResult::SkipHandler;
}
}
static void interrupt_handler(uint32_t) {
GlobalTime::global_counter = GlobalTime::global_counter + 1;
Interrupt::ack_irq(Interrupt::Timer2);
__syscall_ReturnFromException();
}
InterrupCallback IRQCallback = {
.next = nullptr,
.handler_function = reinterpret_cast<InterruptHandler>(interrupt_handler),
.verifier_function = interrupt_verifier
};
}
}

View File

@ -1,8 +1,6 @@
#include "../include/BootLoader/boot_loader.hpp"
#include <stdio.h>
#include <PSX/Timer/timer.hpp>
namespace JabyEngine {
static NextRoutine execute(NextRoutine routine) {
// Support currently only direct call