From c60b97a75e6f5f98ebb3fc4101c7764862fc9f37 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sun, 8 Jan 2023 16:20:30 +0100 Subject: [PATCH] Use GlobalTime --- include/PSX/System/IOPorts/interrupt_io.hpp | 4 +++ include/PSX/System/IOPorts/timer_io.hpp | 5 +-- include/PSX/Timer/timer.hpp | 4 +-- src/Library/include/GPU/gpu.hpp | 3 ++ src/Library/src/BootLoader/start_boot.cpp | 7 ++++ src/Library/src/BootLoader/timer_boot.cpp | 37 ++++++++++++++++++--- src/Library/src/Timer/timer.cpp | 5 ++- src/Library/src/startup.cpp | 2 ++ 8 files changed, 55 insertions(+), 12 deletions(-) diff --git a/include/PSX/System/IOPorts/interrupt_io.hpp b/include/PSX/System/IOPorts/interrupt_io.hpp index 02007979..864ef63e 100644 --- a/include/PSX/System/IOPorts/interrupt_io.hpp +++ b/include/PSX/System/IOPorts/interrupt_io.hpp @@ -36,6 +36,10 @@ namespace JabyEngine { Status.write(Status.read().clear_bit(irq)); } + static void disable_irq(Bit irq) { + Mask.write(Mask.read().clear_bit(irq)); + } + static void enable_irq(Bit irq) { Mask.write(Mask.read().set_bit(irq)); } diff --git a/include/PSX/System/IOPorts/timer_io.hpp b/include/PSX/System/IOPorts/timer_io.hpp index 0fec7860..5c6695ad 100644 --- a/include/PSX/System/IOPorts/timer_io.hpp +++ b/include/PSX/System/IOPorts/timer_io.hpp @@ -32,10 +32,11 @@ namespace JabyEngine { }; struct __no_align Timer { + IOPort value; IOPort mode; IOPort target; private: - uint32_t _unused[2]; + uint32_t _unused[1]; }; namespace Counter0 { @@ -86,7 +87,7 @@ namespace JabyEngine { }; } - __declare_io_port_global_array(Timer, Counter, 0x1F801104, 3); + __declare_io_port_global_array(Timer, Counter, 0x1F801100, 3); static_assert(sizeof(Timer) == 0x10); } } diff --git a/include/PSX/Timer/timer.hpp b/include/PSX/Timer/timer.hpp index f60271cb..6b0c8723 100644 --- a/include/PSX/Timer/timer.hpp +++ b/include/PSX/Timer/timer.hpp @@ -13,14 +13,14 @@ namespace JabyEngine { public: constexpr size_t milliseconds_to(const TimeStamp& ts) const { - return (ts.value - this->value); + return (ts.value - this->value)*10; } friend class GlobalTime; }; private: - static size_t global_counter; + static volatile size_t global_counter; public: GlobalTime() = delete; diff --git a/src/Library/include/GPU/gpu.hpp b/src/Library/include/GPU/gpu.hpp index 66e2396a..b259fed1 100644 --- a/src/Library/include/GPU/gpu.hpp +++ b/src/Library/include/GPU/gpu.hpp @@ -4,6 +4,8 @@ #include #include +#include + namespace JabyEngine { namespace GPU { namespace Screen { @@ -94,6 +96,7 @@ namespace JabyEngine { } static void set_src(uintptr_t adr) { + printf("Source: 0x%p\n", adr); ::JabyEngine::DMA::GPU.adr.write(::JabyEngine::DMA::MADR::MemoryAdr.with(static_cast(adr))); } diff --git a/src/Library/src/BootLoader/start_boot.cpp b/src/Library/src/BootLoader/start_boot.cpp index 0c044cb4..cfdda265 100644 --- a/src/Library/src/BootLoader/start_boot.cpp +++ b/src/Library/src/BootLoader/start_boot.cpp @@ -1,5 +1,7 @@ #include "BootLoader/boot_loader.hpp" #include + +#include #include namespace JabyEngine { @@ -14,8 +16,13 @@ namespace JabyEngine { SPU::stop_voices(); Timer::setup(); + const auto start = GlobalTime::get_time_stamp(); + printf("Start...\n"); GPU::setup(); GPU::display_logo(); + const auto end = GlobalTime::get_time_stamp(); + printf("GPU setup took %ims\n", start.milliseconds_to(end)); + //Pause?? SPU::setup(); diff --git a/src/Library/src/BootLoader/timer_boot.cpp b/src/Library/src/BootLoader/timer_boot.cpp index 8c987b71..2f5757d3 100644 --- a/src/Library/src/BootLoader/timer_boot.cpp +++ b/src/Library/src/BootLoader/timer_boot.cpp @@ -2,19 +2,46 @@ #include #include +#include +#include + namespace JabyEngine { namespace Timer { extern InterrupCallback IRQCallback; - void setup() { - static constexpr auto Mode = CounterMode::with(CounterMode::FreeRun, Counter2::SyncMode::Freerun, CounterMode::ResetAfterTarget, CounterMode::IRQAtTarget, CounterMode::IRQEveryTime, Counter2::Source::System_Clock_Div_8); - static constexpr uint16_t Target = 0x1234; + static constexpr double CPU_Frequency_Hz = 33868800.0; + static constexpr double CPU_Frequncey_Hz_Div8 = (CPU_Frequency_Hz/8.0); - //Counter[2].target.ref().set(CounterTarget::CounterTargetValue.with(Target)); + template + static constexpr T NS_Per_Tick(double CPU_Frequency_Hz) { + return static_cast((1.0/CPU_Frequency_Hz)*1000.0*1000.0*1000.0); + } + + template + static constexpr T US_Per_Tick(double CPU_Frequency_Hz) { + return static_cast((1000.0/NS_Per_Tick(CPU_Frequency_Hz))); + } + + template + static constexpr T MS_Per_Tick(double CPU_Frequency_Hz) { + return static_cast(((1000.0*1000.0)/NS_Per_Tick(CPU_Frequency_Hz))); + } + + void setup() { + 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(CPU_Frequncey_Hz_Div8)*10; + + printf("Timer2 Target: %i\n", Target); + + Interrupt::disable_irq(Interrupt::Timer2); __syscall_EnterCriticalSection(); - __syscall_SysEnqIntRP(Timer2Irq, &IRQCallback); + __syscall_SysEnqIntRP(Timer2Irq, &IRQCallback); __syscall_ExitCriticalSection(); + + Counter[2].target.write(CounterTarget::CounterTargetValue.with(Target)); + Counter[2].mode.write(Mode); + Interrupt::enable_irq(Interrupt::Timer2); } } diff --git a/src/Library/src/Timer/timer.cpp b/src/Library/src/Timer/timer.cpp index 98fefa2e..1e45d9ba 100644 --- a/src/Library/src/Timer/timer.cpp +++ b/src/Library/src/Timer/timer.cpp @@ -1,16 +1,15 @@ #define private public #include +#include #include #include #undef private namespace JabyEngine { - size_t GlobalTime :: global_counter = 0; + volatile size_t GlobalTime :: global_counter = 0; namespace Timer { InterruptVerifierResult interrupt_verifier() { - const auto irq_status = Interrupt::Status.read(); - if(Interrupt::is_irq(Interrupt::Timer2)) { return InterruptVerifierResult::ExecuteHandler; } diff --git a/src/Library/src/startup.cpp b/src/Library/src/startup.cpp index cfd0d651..bc016c9a 100644 --- a/src/Library/src/startup.cpp +++ b/src/Library/src/startup.cpp @@ -1,6 +1,8 @@ #include "../include/BootLoader/boot_loader.hpp" #include +#include + namespace JabyEngine { static NextRoutine execute(NextRoutine routine) { // Support currently only direct call