From ea2b776fb0d64930a9d30b987240c39416cc57bb Mon Sep 17 00:00:00 2001 From: Jaby Date: Thu, 9 May 2024 23:15:04 +0200 Subject: [PATCH] Somehow works?! --- include/PSX/System/syscalls.hpp | 50 ++++++++++++ src/Library/src/CD/cd.cpp | 133 ++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) diff --git a/include/PSX/System/syscalls.hpp b/include/PSX/System/syscalls.hpp index df0694a5..987b948b 100644 --- a/include/PSX/System/syscalls.hpp +++ b/include/PSX/System/syscalls.hpp @@ -45,6 +45,34 @@ namespace JabyEngine { extern const Version version; } + struct TCB { + uint32_t status; + uint32_t unused; + uint32_t reg[32]; + uint32_t epc; + uint32_t hi; + uint32_t lo; + uint32_t sr; + uint32_t cause; + uint32_t unused2[6]; + }; + + struct PCB { + TCB* current_tcb; + }; + + static __always_inline TCB* get_tcb_of_th(uint32_t id) { + return reinterpret_cast((*reinterpret_cast(0x110)) + (id*4)); + } + + static __always_inline void set_tcb(TCB* adr) { + reinterpret_cast(0x108)->current_tcb = adr; + } + + static __always_inline TCB& get_current_tcb() { + return *reinterpret_cast(0x108)->current_tcb; + } + namespace SysCall { static constexpr const uint32_t Table_A = 0xA0; static constexpr const uint32_t Table_B = 0xB0; @@ -92,6 +120,28 @@ namespace JabyEngine { return __syscall_function_cast(Table_A, void*(*)(void*, const void*, size_t))(dst, src, len); } + static __always_inline uint32_t OpenTh(void (*thread_func)(), uint32_t* stack_ptr, uint32_t* gp) { + register uint32_t FuncID asm("t1") = 0x0E; + __asm__ volatile("" : "=r"(FuncID) : "r"(FuncID)); + + return __syscall_function_cast(Table_B, uint32_t(*)(void(*)(), uint32_t*, uint32_t*))(thread_func, stack_ptr, gp); + } + + static __always_inline uint32_t ChangeTh(uint32_t thread) { + register uint32_t FuncID asm("t1") = 0x10; + __asm__ volatile("" : "=r"(FuncID) : "r"(FuncID)); + + return __syscall_function_cast(Table_B, uint32_t(*)(uint32_t))(thread); + } + + static __always_inline int changeThreadSubFunction(uint32_t address) { + register int n asm("a0") = 3; + register int tcb asm("a1") = address; + register int r asm("v0"); + __asm__ volatile("syscall\n" : "=r"(r) : "r"(n), "r"(tcb) : "memory"); + return r; +} + static __always_inline void InitPad(uint8_t *portA, uint32_t portASize, uint8_t *portB, uint32_t portBSize) { register uint32_t FuncID asm("t1") = 0x12; __asm__ volatile("" : "=r"(FuncID) : "r"(FuncID)); diff --git a/src/Library/src/CD/cd.cpp b/src/Library/src/CD/cd.cpp index 49e4874f..ba41bf14 100644 --- a/src/Library/src/CD/cd.cpp +++ b/src/Library/src/CD/cd.cpp @@ -2,8 +2,96 @@ #include #include #include +#include +struct Registers { + union { + struct { + uint32_t r0, at, v0, v1, a0, a1, a2, a3; + uint32_t t0, t1, t2, t3, t4, t5, t6, t7; + uint32_t s0, s1, s2, s3, s4, s5, s6, s7; + uint32_t t8, t9, k0, k1, gp, sp, fp, ra; + } n; + uint32_t r[32]; + } GPR; + uint32_t returnPC; + uint32_t hi, lo; + uint32_t SR; + uint32_t Cause; +}; + +struct Thread { + uint32_t flags, flags2; + struct Registers registers; + uint32_t unknown[9]; +}; + +struct Process { + struct Thread* thread; +}; + +struct Globals { + /* 100 */ struct HandlersStorage* handlersArray; + /* 104 */ uint32_t handlersArraySize; + /* 108 */ struct Process* processes; + /* 10c */ uint32_t processBlockSize; + /* 110 */ struct Thread* threads; + /* 114 */ uint32_t threadBlockSize; + /* 118 */ uint32_t xxx_06; + /* 11c */ uint32_t xxx_07; + /* 120 */ struct EventInfo* events; + /* 124 */ uint32_t eventsSize; + /* 128 */ uint32_t xxx_0a; + /* 12c */ uint32_t xxx_0b; + /* 130 */ uint32_t xxx_0c; + /* 134 */ uint32_t xxx_0d; + /* 138 */ uint32_t xxx_0e; + /* 13c */ uint32_t xxx_0f; + /* 140 */ struct File* files; + /* 144 */ uint32_t filesSize; + /* 148 */ uint32_t xxx_12; + /* 14c */ uint32_t xxx_13; + /* 150 */ struct Device* devices; + /* 154 */ struct Device* devicesEnd; + /* 158 */ uint32_t xxx_16; + /* 15c */ uint32_t xxx_17; + /* 160 */ uint32_t xxx_18; + /* 164 */ uint32_t xxx_19; + /* 168 */ uint32_t xxx_1a; + /* 16c */ uint32_t xxx_1b; + /* 170 */ uint32_t xxx_1c; + /* 174 */ uint32_t xxx_1d; + /* 178 */ uint32_t xxx_1e; + /* 17c */ uint32_t xxx_1f; +}; + +Globals *__globals; namespace JabyEngine { + struct ToT { + unsigned long *head; + long size; +}; + +struct TCBH { + TCB *entry; /* NULL */ + long flag; +}; + +TCBH *exec_TCB; +TCB *mainFlow; + +inline unsigned short getSystemTableNumber(uint32_t thread) +{ + return (thread & 0xFFFF); +} + +inline TCB* getTCB(uint32_t thread) +{ + return (TCB*) (((ToT*)0x100)[2]).head + getSystemTableNumber(thread); +} + +int changeThread(int threadId) { return SysCall::changeThreadSubFunction((uint32_t)&__globals->threads[threadId & 0xffff]); } + namespace CD { namespace internal { static constexpr auto AudioSectorMode = CD_IO::Mode::from(CD_IO::Mode::SingleSpeed, CD_IO::Mode::AutoPauseTrack, CD_IO::Mode::CDDA); @@ -12,6 +100,9 @@ namespace JabyEngine { static SysCall::InterruptVerifierResult interrupt_verifier(); static uint32_t interrupt_handler(uint32_t); + static uint32_t stack[1024]; + static uint32_t thread = 0; + static SectorBufferAllocator sector_allocator; static uint32_t cur_lba; static uint32_t dst_lba; @@ -108,6 +199,17 @@ namespace JabyEngine { } break; case CD_IO::Interrupt::DataEnd: { + + //set_tcb(get_tcb_of_th(thread)); + + /*printf("EPC: 0x%p; SR: 0x%p\n", get_current_tcb().reg[31], get_current_tcb().sr); + get_current_tcb().reg[31] = (uint32_t)(void(*)())[]() { + printf("Wuff! Now crash!\n"); + while(true); + }; + printf("EPC: 0x%p; SR: 0x%p\n", get_current_tcb().reg[31], get_current_tcb().sr); while(true);*/ + //SysCall::ReturnFromException(); + CD_IO::PortIndex0::change_to(); Command::send(CD_IO::Command::SetLoc, static_cast(0x0), static_cast(0x09), static_cast(0x0)); @@ -127,6 +229,11 @@ namespace JabyEngine { // No masking required because we can only write bit 0 - 2 CD_IO::IndexStatus.write(old_status); Interrupt::ack_irq(Interrupt::CDROM); + + if(thread != 0) { + //changeThread(thread); + exec_TCB->entry = reinterpret_cast(&__globals->threads[thread & 0xffff]); + } SysCall::ReturnFromException(); __builtin_unreachable(); } @@ -150,7 +257,33 @@ namespace JabyEngine { } } + + void enable_CDDA() { + if(thread == 0) { + __globals = reinterpret_cast(0x100); + ToT *table = (ToT*)0x100; + exec_TCB = (TCBH*)table[1].head; + mainFlow = exec_TCB->entry; + + SysCall::EnterCriticalSection(); + thread = SysCall::OpenTh([]() { + printf("Hallo!\n"); + printf("Me: 0x%p - 0x%p\n", get_tcb_of_th(thread), &get_current_tcb()); + + exec_TCB->entry = mainFlow; + SysCall::ReturnFromException(); + //changeThread(0); + }, reinterpret_cast(0x80010000), reinterpret_cast(mainFlow->reg[28])); + SysCall::ExitCriticalSection(); + + printf("Raw: %x, TCB: %i\n", thread, thread & 0xFFFF); + //__globals->threads[thread & 0xffff].registers.SR = 0x404; + + //mainFlow = exec_TCB->entry; + //changeThread(thread); + } + Command::wait_completed(); CD_IO::PortIndex0::change_to(); Command::send_wait(CD_IO::Command::SetMode, AudioSectorMode);