Add busy loop code and make HighResTimer work on demand rather automatically or ifdefed
This commit is contained in:
parent
646897f83e
commit
31d6062d05
|
@ -1,6 +1,7 @@
|
||||||
#ifndef __JABYENGINE_HIGH_RES_TIMER_HPP__
|
#ifndef __JABYENGINE_HIGH_RES_TIMER_HPP__
|
||||||
#define __JABYENGINE_HIGH_RES_TIMER_HPP__
|
#define __JABYENGINE_HIGH_RES_TIMER_HPP__
|
||||||
#include "../jabyengine_defines.h"
|
#include "../jabyengine_defines.h"
|
||||||
|
#include <PSX/System/IOPorts/interrupt_io.hpp>
|
||||||
#include <PSX/System/IOPorts/timer_io.hpp>
|
#include <PSX/System/IOPorts/timer_io.hpp>
|
||||||
|
|
||||||
namespace JabyEngine {
|
namespace JabyEngine {
|
||||||
|
@ -24,51 +25,57 @@ namespace JabyEngine {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef JABYENGINE_USE_HIGH_PERCISION_TIMER
|
class HighResTime {
|
||||||
class HighResTime {
|
public:
|
||||||
public:
|
class TimeStamp {
|
||||||
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:
|
private:
|
||||||
static constexpr uint16_t TicksFor100us = CPUTicks::ticks_per_us<uint16_t>(CPUTicks::Frequency_Hz_Div8, 100.0);
|
uint16_t counter_10ms_value;
|
||||||
static constexpr uint16_t TicksFor1ms = CPUTicks::ticks_per_ms<uint16_t>(CPUTicks::Frequency_Hz_Div8, 1.0);
|
uint16_t fraction;
|
||||||
static constexpr uint16_t TicksFor10ms = CPUTicks::ticks_per_ms<uint16_t>(CPUTicks::Frequency_Hz_Div8, 10.0);
|
|
||||||
|
|
||||||
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:
|
public:
|
||||||
HighResTime() = delete;
|
constexpr size_t microseconds_to(const TimeStamp& end) const {
|
||||||
~HighResTime() = delete;
|
return TimeStamp::to_us((end.counter_10ms_value - this->counter_10ms_value), (end.fraction - this->fraction));
|
||||||
|
|
||||||
static TimeStamp get_time_stamp() {
|
|
||||||
return TimeStamp(HighResTime::global_counter_10ms, Timer_IO::Counter2.get_current_value());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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__
|
#endif //!__JABYENGINE_HIGH_RES_TIMER_HPP__
|
|
@ -1,8 +0,0 @@
|
||||||
#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__
|
|
|
@ -1,6 +1,5 @@
|
||||||
#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 __used __attribute__((used))
|
#define __used __attribute__((used))
|
||||||
|
|
10
lib/Makefile
10
lib/Makefile
|
@ -46,11 +46,7 @@ CCFLAGS_debug += -O0
|
||||||
|
|
||||||
CXXFLAGS += -fno-exceptions -fno-rtti
|
CXXFLAGS += -fno-exceptions -fno-rtti
|
||||||
|
|
||||||
USE_FUNCTION_SECTIONS ?= true
|
CCFLAGS += -mno-gpopt -fomit-frame-pointer -ffunction-sections
|
||||||
ifeq ($(USE_FUNCTION_SECTIONS),true)
|
|
||||||
CCFLAGS += -ffunction-sections
|
|
||||||
endif
|
|
||||||
CCFLAGS += -mno-gpopt -fomit-frame-pointer
|
|
||||||
CCFLAGS += -fno-builtin -fno-strict-aliasing -Wno-attributes
|
CCFLAGS += -fno-builtin -fno-strict-aliasing -Wno-attributes
|
||||||
CCFLAGS += -std=c++20
|
CCFLAGS += -std=c++20
|
||||||
CCFLAGS += $(ARCHFLAGS)
|
CCFLAGS += $(ARCHFLAGS)
|
||||||
|
@ -76,11 +72,11 @@ OBJS = $(addprefix $(OUTPUT_DIR)/,$(addsuffix .o, $(subst ..,!super,$(basename $
|
||||||
#Compiling rule
|
#Compiling rule
|
||||||
$(OUTPUT_DIR)/%.o: %.s
|
$(OUTPUT_DIR)/%.o: %.s
|
||||||
@mkdir -p $(dir $@)
|
@mkdir -p $(dir $@)
|
||||||
$(CC) -c $(DEPS) -o $@ $(CCFLAGS) $(CCFLAGS) $<
|
$(CC) -c $(DEPS) -o $@ $(CCFLAGS) $<
|
||||||
|
|
||||||
$(OUTPUT_DIR)/%.o: %.c
|
$(OUTPUT_DIR)/%.o: %.c
|
||||||
@mkdir -p $(dir $@)
|
@mkdir -p $(dir $@)
|
||||||
$(CC) -c $(DEPS) -o $@ $(CCFLAGS) $(CCFLAGS) $<
|
$(CC) -c $(DEPS) -o $@ $(CCFLAGS) $<
|
||||||
|
|
||||||
$(OUTPUT_DIR)/%.o: %.cpp
|
$(OUTPUT_DIR)/%.o: %.cpp
|
||||||
@mkdir -p $(dir $@)
|
@mkdir -p $(dir $@)
|
||||||
|
|
|
@ -10,8 +10,7 @@ CCFLAGS += -Iinclude -I../../include
|
||||||
CCFLAGS += -save-temps=obj
|
CCFLAGS += -save-temps=obj
|
||||||
|
|
||||||
include ../../lib/Wildcard.mk
|
include ../../lib/Wildcard.mk
|
||||||
SRCS = $(call rwildcard, src, c cpp)
|
SRCS = $(call rwildcard, src, c cpp s)
|
||||||
SRCS += src/syscall_printf.asm
|
|
||||||
|
|
||||||
include ../../lib/Makefile
|
include ../../lib/Makefile
|
||||||
LIB_DIR = ../../lib/$(CONFIG_NAME)
|
LIB_DIR = ../../lib/$(CONFIG_NAME)
|
||||||
|
|
|
@ -21,15 +21,10 @@ namespace JabyEngine {
|
||||||
CD::setup();
|
CD::setup();
|
||||||
Timer::setup();
|
Timer::setup();
|
||||||
|
|
||||||
const auto start = HighResTime::get_time_stamp();
|
|
||||||
printf("Start...\n");
|
|
||||||
GPU::setup();
|
GPU::setup();
|
||||||
GPU::display_logo();
|
GPU::display_logo();
|
||||||
const auto end = HighResTime::get_time_stamp();
|
|
||||||
printf("GPU setup took %ims %ius\n", start.milliseconds_to(end), start.microseconds_to(end));
|
|
||||||
|
|
||||||
//Pause??
|
//Pause??
|
||||||
|
|
||||||
SPU::setup();
|
SPU::setup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,46 +1,34 @@
|
||||||
#include <PSX/jabyengine_config.hpp>
|
#include <PSX/System/IOPorts/interrupt_io.hpp>
|
||||||
#ifdef JABYENGINE_USE_HIGH_PERCISION_TIMER
|
#include <PSX/System/syscalls.h>
|
||||||
#include <PSX/System/IOPorts/interrupt_io.hpp>
|
#define private public
|
||||||
#include <PSX/System/syscalls.h>
|
#include <PSX/Timer/high_res_timer.hpp>
|
||||||
#define private public
|
#undef private
|
||||||
#include <PSX/Timer/high_res_timer.hpp>
|
|
||||||
#undef private
|
|
||||||
|
|
||||||
namespace JabyEngine {
|
namespace JabyEngine {
|
||||||
|
namespace Timer {
|
||||||
|
extern InterrupCallback IRQCallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace boot {
|
||||||
namespace Timer {
|
namespace Timer {
|
||||||
extern InterrupCallback IRQCallback;
|
using namespace JabyEngine::Timer;
|
||||||
}
|
|
||||||
|
void setup() {
|
||||||
|
using namespace Timer_IO;
|
||||||
|
|
||||||
namespace boot {
|
static constexpr auto Mode = CounterMode_t::from(CounterMode_t::FreeRun, Counter2_v::SyncMode::FreeRun, CounterMode_t::ResetAfterTarget, CounterMode_t::IRQAtTarget, CounterMode_t::IRQEveryTime, CounterMode_t::IRQPulse, Counter2_v::Source::System_Clock_Div_8);
|
||||||
namespace Timer {
|
|
||||||
using namespace JabyEngine::Timer;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
using namespace Timer_IO;
|
|
||||||
|
|
||||||
static constexpr auto Mode = CounterMode_t::from(CounterMode_t::FreeRun, Counter2_v::SyncMode::FreeRun, CounterMode_t::ResetAfterTarget, CounterMode_t::IRQAtTarget, CounterMode_t::IRQEveryTime, CounterMode_t::IRQPulse, Counter2_v::Source::System_Clock_Div_8);
|
// We disable the IRQ here so it can be enabled by user demand later
|
||||||
|
// Having the interrupt fire every 10ms will slow us down slightly so we only do it on demand
|
||||||
|
Interrupt::disable_irq(Interrupt::Timer2);
|
||||||
|
|
||||||
Interrupt::disable_irq(Interrupt::Timer2);
|
__syscall_EnterCriticalSection();
|
||||||
|
__syscall_SysEnqIntRP(Timer2Irq, &IRQCallback);
|
||||||
|
__syscall_ExitCriticalSection();
|
||||||
|
|
||||||
__syscall_EnterCriticalSection();
|
Counter2.set_target_value(HighResTime::TicksFor10ms);
|
||||||
__syscall_SysEnqIntRP(Timer2Irq, &IRQCallback);
|
Counter2.set_mode(Mode);
|
||||||
__syscall_ExitCriticalSection();
|
|
||||||
|
|
||||||
Counter2.set_target_value(HighResTime::TicksFor10ms);
|
|
||||||
Counter2.set_mode(Mode);
|
|
||||||
|
|
||||||
Interrupt::enable_irq(Interrupt::Timer2);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
}
|
||||||
namespace JabyEngine {
|
|
||||||
namespace boot {
|
|
||||||
namespace Timer {
|
|
||||||
void setup() {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif //JABYENGINE_USE_HIGH_PERCISION_TIMER
|
|
|
@ -1,38 +1,34 @@
|
||||||
#include <PSX/jabyengine_config.hpp>
|
#define private public
|
||||||
#ifdef JABYENGINE_USE_HIGH_PERCISION_TIMER
|
#include <PSX/System/IOPorts/timer_io.hpp>
|
||||||
#define private public
|
#include <PSX/System/syscalls.h>
|
||||||
#include <PSX/System/IOPorts/interrupt_io.hpp>
|
#include <PSX/Timer/high_res_timer.hpp>
|
||||||
#include <PSX/System/IOPorts/timer_io.hpp>
|
#undef private
|
||||||
#include <PSX/System/syscalls.h>
|
|
||||||
#include <PSX/Timer/high_res_timer.hpp>
|
|
||||||
#undef private
|
|
||||||
|
|
||||||
namespace JabyEngine {
|
namespace JabyEngine {
|
||||||
volatile uint16_t HighResTime :: global_counter_10ms = 0;
|
volatile uint16_t HighResTime :: global_counter_10ms = 0;
|
||||||
|
|
||||||
namespace Timer {
|
namespace Timer {
|
||||||
static 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;
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
return InterruptVerifierResult::SkipHandler;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void interrupt_handler(uint32_t) {
|
else {
|
||||||
HighResTime::global_counter_10ms = HighResTime::global_counter_10ms + 1;
|
return InterruptVerifierResult::SkipHandler;
|
||||||
|
|
||||||
Interrupt::ack_irq(Interrupt::Timer2);
|
|
||||||
__syscall_ReturnFromException();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
InterrupCallback IRQCallback = {
|
|
||||||
.next = nullptr,
|
|
||||||
.handler_function = reinterpret_cast<InterruptHandler>(interrupt_handler),
|
|
||||||
.verifier_function = interrupt_verifier
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
.set noreorder
|
||||||
|
.section .text, "ax", @progbits
|
||||||
|
.align 2
|
||||||
|
.global busy_loop
|
||||||
|
.type busy_loop, @function
|
||||||
|
|
||||||
|
busy_loop:
|
||||||
|
sw $a0, 0($sp)
|
||||||
|
lw $v0, 0($sp)
|
||||||
|
lw $v1, 0($sp)
|
||||||
|
nop
|
||||||
|
addiu $v1, -1
|
||||||
|
beqz $v0, early_exit
|
||||||
|
sw $v1, 0($sp)
|
||||||
|
|
||||||
|
busy_loop_loop:
|
||||||
|
lw $v0, 0($sp)
|
||||||
|
lw $v1, 0($sp)
|
||||||
|
nop
|
||||||
|
addiu $v1, -1
|
||||||
|
bnez $v0, busy_loop_loop
|
||||||
|
sw $v1, 0($sp)
|
||||||
|
|
||||||
|
early_exit:
|
||||||
|
jr $ra
|
||||||
|
nop
|
|
@ -1,6 +1,6 @@
|
||||||
.set push
|
.set push
|
||||||
.set noreorder
|
.set noreorder
|
||||||
#.section .ramtext, "ax", @progbits
|
.section .text, "ax", @progbits
|
||||||
.align 2
|
.align 2
|
||||||
.global __syscall_printf
|
.global __syscall_printf
|
||||||
.type __syscall_printf, @function
|
.type __syscall_printf, @function
|
||||||
|
|
Loading…
Reference in New Issue