Support High Percision Counter

This commit is contained in:
Jaby 2023-01-09 22:27:10 +01:00
parent 4a09d57e5c
commit 2b34d105c8
7 changed files with 37 additions and 17 deletions

View File

@ -32,7 +32,7 @@ namespace JabyEngine {
return Status.read().is_bit_set(irq); return Status.read().is_bit_set(irq);
} }
static void ack_irg(Bit<uint32_t> irq) { static void ack_irq(Bit<uint32_t> irq) {
Status.write(Status.read().clear_bit(irq)); Status.write(Status.read().clear_bit(irq));
} }

View File

@ -12,9 +12,19 @@ namespace JabyEngine {
constexpr TimeStamp(size_t value) : value(value) {} constexpr TimeStamp(size_t value) : value(value) {}
public: 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 { constexpr size_t milliseconds_to(const TimeStamp& ts) const {
return (ts.value - this->value)*10; return (ts.value - this->value)*10;
} }
#endif //JABYENGINE_USE_HIGH_PERCISION_TIMER
friend class GlobalTime; friend class GlobalTime;
}; };

View File

@ -0,0 +1,8 @@
#ifndef __JABYENGINE_CONFIG_HPP__
#define __JABYENGINE_CONFIG_HPP__
#ifdef USE_CUSTOM_CONFIG
#include <jabyengine_custom_config.hpp>
#else
#define JABYENGINE_USE_HIGH_PERCISION_TIMER
#endif //USE_CUSTOM_CONFIG
#endif //!__JABYENGINE_CONFIG_HPP__

View File

@ -1,5 +1,6 @@
#ifndef __JABYENGINE_DEFINES__H__ #ifndef __JABYENGINE_DEFINES__H__
#define __JABYENGINE_DEFINES__H__ #define __JABYENGINE_DEFINES__H__
#include "jabyengine_config.hpp"
#include "../stddef.h" #include "../stddef.h"
#define __keep __attribute__((used)) #define __keep __attribute__((used))

View File

@ -21,7 +21,7 @@ namespace JabyEngine {
GPU::setup(); GPU::setup();
GPU::display_logo(); GPU::display_logo();
const auto end = GlobalTime::get_time_stamp(); const auto end = GlobalTime::get_time_stamp();
printf("GPU setup took %ims\n", start.milliseconds_to(end)); printf("GPU setup took %ims %ius\n", start.milliseconds_to(end), start.microseconds_to(end));
//Pause?? //Pause??

View File

@ -10,29 +10,32 @@ namespace JabyEngine {
extern InterrupCallback IRQCallback; extern InterrupCallback IRQCallback;
static constexpr double CPU_Frequency_Hz = 33868800.0; static constexpr double CPU_Frequency_Hz = 33868800.0;
static constexpr double CPU_Frequncey_Hz_Div8 = (CPU_Frequency_Hz/8.0); static constexpr double CPU_Frequency_Hz_Div8 = (CPU_Frequency_Hz/8.0);
template<typename T> template<typename T>
static constexpr T NS_Per_Tick(double CPU_Frequency_Hz) { static constexpr T NS_Per_Tick(double CPU_Frequency_Hz, double time = 1.0) {
return static_cast<T>((1.0/CPU_Frequency_Hz)*1000.0*1000.0*1000.0); return static_cast<T>((time/CPU_Frequency_Hz)*1000.0*1000.0*1000.0);
} }
template<typename T> template<typename T>
static constexpr T US_Per_Tick(double CPU_Frequency_Hz) { static constexpr T US_Per_Tick(double CPU_Frequency_Hz, double time = 1.0) {
return static_cast<T>((1000.0/NS_Per_Tick<double>(CPU_Frequency_Hz))); return static_cast<T>(((time*1000.0)/NS_Per_Tick<double>(CPU_Frequency_Hz)));
} }
template<typename T> template<typename T>
static constexpr T MS_Per_Tick(double CPU_Frequency_Hz) { static constexpr T MS_Per_Tick(double CPU_Frequency_Hz, double time = 1.0) {
return static_cast<T>(((1000.0*1000.0)/NS_Per_Tick<double>(CPU_Frequency_Hz))); return static_cast<T>(((time*1000.0*1000.0)/NS_Per_Tick<double>(CPU_Frequency_Hz)));
} }
void setup() { void setup() {
using namespace Timer_IO; 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); 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 uint16_t Target = MS_Per_Tick<uint16_t>(CPU_Frequncey_Hz_Div8)*10; #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); Interrupt::disable_irq(Interrupt::Timer2);
__syscall_EnterCriticalSection(); __syscall_EnterCriticalSection();

View File

@ -9,7 +9,7 @@ namespace JabyEngine {
volatile size_t GlobalTime :: global_counter = 0; volatile size_t GlobalTime :: global_counter = 0;
namespace Timer { namespace Timer {
InterruptVerifierResult interrupt_verifier() { static InterruptVerifierResult interrupt_verifier() {
if(Interrupt::is_irq(Interrupt::Timer2)) { if(Interrupt::is_irq(Interrupt::Timer2)) {
return InterruptVerifierResult::ExecuteHandler; return InterruptVerifierResult::ExecuteHandler;
} }
@ -19,18 +19,16 @@ namespace JabyEngine {
} }
} }
uint32_t interrupt_handler(uint32_t) { static void interrupt_handler(uint32_t) {
GlobalTime::global_counter = GlobalTime::global_counter + 1; GlobalTime::global_counter = GlobalTime::global_counter + 1;
Interrupt::ack_irg(Interrupt::Timer2); Interrupt::ack_irq(Interrupt::Timer2);
__syscall_ReturnFromException(); __syscall_ReturnFromException();
// v to make GCC happy
return 0;
} }
InterrupCallback IRQCallback = { InterrupCallback IRQCallback = {
.next = nullptr, .next = nullptr,
.handler_function = interrupt_handler, .handler_function = reinterpret_cast<InterruptHandler>(interrupt_handler),
.verifier_function = interrupt_verifier .verifier_function = interrupt_verifier
}; };
} }