Reactive Callbacks and integrate them

This commit is contained in:
Jaby 2024-06-13 22:07:37 +02:00 committed by Jaby
parent 231dabfb3f
commit 77bf51d204
9 changed files with 110 additions and 50 deletions

View File

@ -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);
}
};
}
}

View File

@ -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);
}
};
}
}

View File

@ -8,7 +8,7 @@ namespace JabyEngine {
void identify(); void identify();
} }
namespace [[deprecated("Callbacks are deprecated for now")]] Callbacks { namespace Callbacks {
void setup(); void setup();
} }

View File

@ -2,7 +2,7 @@
#include <PSX/System/threads.hpp> #include <PSX/System/threads.hpp>
namespace JabyEngine { namespace JabyEngine {
namespace [[deprecated("Callbacks are deprecated for now")]] Callback { namespace Callback {
namespace internal { namespace internal {
namespace VSync { namespace VSync {
static constexpr size_t StackSize = 64; static constexpr size_t StackSize = 64;
@ -16,6 +16,19 @@ namespace JabyEngine {
Thread::execute_next(); 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();
}
}
} }
} }
} }

View File

@ -13,7 +13,14 @@ namespace JabyEngine {
&InternalCallback::VSync::stack[InternalCallback::VSync::StackSize - 1], &InternalCallback::VSync::stack[InternalCallback::VSync::StackSize - 1],
SysCall::get_gp() 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::VSync::thread_handle);
Thread::set_user_mode_for(InternalCallback::DataReady::thread_handle);
SysCall::ExitCriticalSection(); SysCall::ExitCriticalSection();
} }
} }

View File

@ -47,7 +47,7 @@ namespace JabyEngine {
static constexpr auto DebugScale = 1.0; static constexpr auto DebugScale = 1.0;
BIOS::identify(); BIOS::identify();
//Callbacks::setup(); Callbacks::setup();
__debug_boot_color_at(::JabyEngine::GPU::Color24::Grey(), DebugX, DebugY, DebugScale); __debug_boot_color_at(::JabyEngine::GPU::Color24::Grey(), DebugX, DebugY, DebugScale);
DMA::setup(); DMA::setup();

View File

@ -1,4 +1,5 @@
#include "../../internal-include/CD/cd_internal.hpp" #include "../../internal-include/CD/cd_internal.hpp"
#include "../../internal-include/System/callbacks_internal.hpp"
#include <PSX/System/IOPorts/dma_io.hpp> #include <PSX/System/IOPorts/dma_io.hpp>
#include <PSX/System/IOPorts/interrupt_io.hpp> #include <PSX/System/IOPorts/interrupt_io.hpp>
#include <PSX/System/syscalls.hpp> #include <PSX/System/syscalls.hpp>
@ -102,7 +103,8 @@ namespace JabyEngine {
} }
static void handler(uint32_t) { 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(); CD_IO::PortIndex1::change_to();
const auto cur_irq = CD_IO::Interrupt::get_type(CD_IO::PortIndex1::InterruptFlag); const auto cur_irq = CD_IO::Interrupt::get_type(CD_IO::PortIndex1::InterruptFlag);
@ -112,6 +114,7 @@ namespace JabyEngine {
if(current_state != State::XAMode) { if(current_state != State::XAMode) {
switch(cur_irq) { switch(cur_irq) {
case CD_IO::Interrupt::DataReady: { case CD_IO::Interrupt::DataReady: {
run_callback = true;
// Obtain sector content here // Obtain sector content here
auto* sector = sector_allocator.allocate_sector(); auto* sector = sector_allocator.allocate_sector();
if(sector) { if(sector) {
@ -149,6 +152,10 @@ namespace JabyEngine {
// No masking required because we can only write bit 0 - 2 // No masking required because we can only write bit 0 - 2
CD_IO::IndexStatus.write(old_status); CD_IO::IndexStatus.write(old_status);
if(run_callback) {
Callback::internal::DataReady::execute();
}
SysCall::ReturnFromException(); SysCall::ReturnFromException();
} }
} }
@ -203,7 +210,7 @@ namespace JabyEngine {
const uint8_t mode = XAAudioSectorMode.raw | (double_speed ? DoubleSpeedBit : SingleSpeedBit); const uint8_t mode = XAAudioSectorMode.raw | (double_speed ? DoubleSpeedBit : SingleSpeedBit);
NewCommand::send(CD_IO::Command::SetMode, mode); NewCommand::send(CD_IO::Command::SetMode, mode);
current_state = State::XAMode; //current_state = State::XAMode;
} }
} }
} }

View File

@ -0,0 +1,46 @@
#include "../../internal-include/System/callbacks_internal.hpp"
#include "../../internal-include/CD/cd_internal.hpp"
#include <PSX/System/callbacks.hpp>
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();
}
}
}
}
}
}

View File

@ -1,25 +0,0 @@
#include "../../internal-include/System/callbacks_internal.hpp"
#include <PSX/System/callbacks.hpp>
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();
}
}
}
}
}
}