From d628375a18bf72326cacfc3e4ef681a88a0a9615 Mon Sep 17 00:00:00 2001 From: jaby Date: Thu, 13 Jun 2024 22:07:37 +0200 Subject: [PATCH] Reactive Callbacks and integrate them --- include/PSX/System/callbacks.hpp | 32 +++++++++++++ include/PSX/System/callbacks.hxx | 20 -------- .../BootLoader/boot_loader.hpp | 2 +- ...ks_internal.hxx => callbacks_internal.hpp} | 15 +++++- ...{callbacks_boot.cxx => callbacks_boot.cpp} | 7 +++ src/Library/src/BootLoader/start_boot.cpp | 2 +- src/Library/src/CD/cd.cpp | 11 ++++- src/Library/src/System/callbacks.cpp | 46 +++++++++++++++++++ src/Library/src/System/callbacks.cxx | 25 ---------- 9 files changed, 110 insertions(+), 50 deletions(-) create mode 100644 include/PSX/System/callbacks.hpp delete mode 100644 include/PSX/System/callbacks.hxx rename src/Library/internal-include/System/{callbacks_internal.hxx => callbacks_internal.hpp} (52%) rename src/Library/src/BootLoader/{callbacks_boot.cxx => callbacks_boot.cpp} (65%) create mode 100644 src/Library/src/System/callbacks.cpp delete mode 100644 src/Library/src/System/callbacks.cxx diff --git a/include/PSX/System/callbacks.hpp b/include/PSX/System/callbacks.hpp new file mode 100644 index 00000000..71764896 --- /dev/null +++ b/include/PSX/System/callbacks.hpp @@ -0,0 +1,32 @@ +#pragma once +#include "syscalls.hpp" + +namespace JabyEngine { + namespace Callback { + using Function = void (*)(); + + struct VSyncCallback { + static Function callback; + + static void install(Function function) { + VSyncCallback::callback = function; + } + + static void uninstall() { + VSyncCallback::install(nullptr); + } + }; + + struct DataReadyCallback { + static Function callback; + + static void install(Function function) { + DataReadyCallback::callback = function; + } + + static void uninstall() { + DataReadyCallback::install(nullptr); + } + }; + } +} \ No newline at end of file diff --git a/include/PSX/System/callbacks.hxx b/include/PSX/System/callbacks.hxx deleted file mode 100644 index 020c353f..00000000 --- a/include/PSX/System/callbacks.hxx +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "syscalls.hpp" - -namespace JabyEngine { - namespace [[deprecated("Callbacks are deprecated for now")]] Callback { - struct VSyncCallback { - using Function = void (*)(); - - static Function callback; - - static void install(Function function) { - VSyncCallback::callback = function; - } - - static void uninstall() { - VSyncCallback::install(nullptr); - } - }; - } -} \ No newline at end of file diff --git a/src/Library/internal-include/BootLoader/boot_loader.hpp b/src/Library/internal-include/BootLoader/boot_loader.hpp index 2398c3a9..49b5a6ea 100644 --- a/src/Library/internal-include/BootLoader/boot_loader.hpp +++ b/src/Library/internal-include/BootLoader/boot_loader.hpp @@ -8,7 +8,7 @@ namespace JabyEngine { void identify(); } - namespace [[deprecated("Callbacks are deprecated for now")]] Callbacks { + namespace Callbacks { void setup(); } diff --git a/src/Library/internal-include/System/callbacks_internal.hxx b/src/Library/internal-include/System/callbacks_internal.hpp similarity index 52% rename from src/Library/internal-include/System/callbacks_internal.hxx rename to src/Library/internal-include/System/callbacks_internal.hpp index a37980a1..992b8060 100644 --- a/src/Library/internal-include/System/callbacks_internal.hxx +++ b/src/Library/internal-include/System/callbacks_internal.hpp @@ -2,7 +2,7 @@ #include namespace JabyEngine { - namespace [[deprecated("Callbacks are deprecated for now")]] Callback { + namespace Callback { namespace internal { namespace VSync { static constexpr size_t StackSize = 64; @@ -16,6 +16,19 @@ namespace JabyEngine { Thread::execute_next(); } } + + namespace DataReady { + static constexpr size_t StackSize = 256; + + extern SysCall::ThreadHandle thread_handle; + extern uint32_t stack[StackSize]; + void routine(); + + static void execute() { + MainThread::prepare_if_main(DataReady::thread_handle); + Thread::execute_next(); + } + } } } } diff --git a/src/Library/src/BootLoader/callbacks_boot.cxx b/src/Library/src/BootLoader/callbacks_boot.cpp similarity index 65% rename from src/Library/src/BootLoader/callbacks_boot.cxx rename to src/Library/src/BootLoader/callbacks_boot.cpp index 7c5fb44b..c5e8273b 100644 --- a/src/Library/src/BootLoader/callbacks_boot.cxx +++ b/src/Library/src/BootLoader/callbacks_boot.cpp @@ -13,7 +13,14 @@ namespace JabyEngine { &InternalCallback::VSync::stack[InternalCallback::VSync::StackSize - 1], SysCall::get_gp() ); + InternalCallback::DataReady::thread_handle = SysCall::OpenThread( + InternalCallback::DataReady::routine, + &InternalCallback::DataReady::stack[InternalCallback::DataReady::StackSize - 1], + SysCall::get_gp() + ); + Thread::set_user_mode_for(InternalCallback::VSync::thread_handle); + Thread::set_user_mode_for(InternalCallback::DataReady::thread_handle); SysCall::ExitCriticalSection(); } } diff --git a/src/Library/src/BootLoader/start_boot.cpp b/src/Library/src/BootLoader/start_boot.cpp index 9af5db2e..4c85bae2 100644 --- a/src/Library/src/BootLoader/start_boot.cpp +++ b/src/Library/src/BootLoader/start_boot.cpp @@ -47,7 +47,7 @@ namespace JabyEngine { static constexpr auto DebugScale = 1.0; BIOS::identify(); - //Callbacks::setup(); + Callbacks::setup(); __debug_boot_color_at(::JabyEngine::GPU::Color24::Grey(), DebugX, DebugY, DebugScale); DMA::setup(); diff --git a/src/Library/src/CD/cd.cpp b/src/Library/src/CD/cd.cpp index 0d618589..289e9530 100644 --- a/src/Library/src/CD/cd.cpp +++ b/src/Library/src/CD/cd.cpp @@ -1,4 +1,5 @@ #include "../../internal-include/CD/cd_internal.hpp" +#include "../../internal-include/System/callbacks_internal.hpp" #include #include #include @@ -102,7 +103,8 @@ namespace JabyEngine { } static void handler(uint32_t) { - const auto old_status = CD_IO::IndexStatus.read(); + const auto old_status = CD_IO::IndexStatus.read(); + bool run_callback = false; CD_IO::PortIndex1::change_to(); const auto cur_irq = CD_IO::Interrupt::get_type(CD_IO::PortIndex1::InterruptFlag); @@ -112,6 +114,7 @@ namespace JabyEngine { if(current_state != State::XAMode) { switch(cur_irq) { case CD_IO::Interrupt::DataReady: { + run_callback = true; // Obtain sector content here auto* sector = sector_allocator.allocate_sector(); if(sector) { @@ -149,6 +152,10 @@ namespace JabyEngine { // No masking required because we can only write bit 0 - 2 CD_IO::IndexStatus.write(old_status); + + if(run_callback) { + Callback::internal::DataReady::execute(); + } SysCall::ReturnFromException(); } } @@ -203,7 +210,7 @@ namespace JabyEngine { const uint8_t mode = XAAudioSectorMode.raw | (double_speed ? DoubleSpeedBit : SingleSpeedBit); NewCommand::send(CD_IO::Command::SetMode, mode); - current_state = State::XAMode; + //current_state = State::XAMode; } } } diff --git a/src/Library/src/System/callbacks.cpp b/src/Library/src/System/callbacks.cpp new file mode 100644 index 00000000..ddacda94 --- /dev/null +++ b/src/Library/src/System/callbacks.cpp @@ -0,0 +1,46 @@ +#include "../../internal-include/System/callbacks_internal.hpp" +#include "../../internal-include/CD/cd_internal.hpp" +#include + +namespace JabyEngine { + namespace Callback { + Function VSyncCallback :: callback = nullptr; + Function DataReadyCallback :: callback = nullptr; + + namespace internal { + namespace VSync { + SysCall::ThreadHandle thread_handle = 0; + uint32_t stack[StackSize] = {0}; + + void routine() { + while(true) { + if(VSyncCallback::callback) { + VSyncCallback::callback(); + } + SysCall::EnterCriticalSection(); + MainThread::restore(); + } + } + } + + namespace DataReady { + SysCall::ThreadHandle thread_handle = 0; + uint32_t stack[StackSize]; + + void routine() { + while(true) { + if(DataReadyCallback::callback) { + DataReadyCallback::callback(); + } + + CD::internal::IRQ::resume_at0(CD::internal::BCDTimeStamp::from(222)); + CD::internal::NewCommand::send(CD_IO::Command::ReadS); + + SysCall::EnterCriticalSection(); + MainThread::restore(); + } + } + } + } + } +} \ No newline at end of file diff --git a/src/Library/src/System/callbacks.cxx b/src/Library/src/System/callbacks.cxx deleted file mode 100644 index 4a8e065a..00000000 --- a/src/Library/src/System/callbacks.cxx +++ /dev/null @@ -1,25 +0,0 @@ -#include "../../internal-include/System/callbacks_internal.hpp" -#include - -namespace JabyEngine { - namespace Callback { - VSyncCallback::Function VSyncCallback :: callback = nullptr; - - namespace internal { - namespace VSync { - SysCall::ThreadHandle thread_handle = 0; - uint32_t stack[StackSize] = {0}; - - void routine() { - while(true) { - if(VSyncCallback::callback) { - VSyncCallback::callback(); - } - SysCall::EnterCriticalSection(); - MainThread::restore(); - } - } - } - } - } -} \ No newline at end of file