From 5023f3aa70d066078c95293f9c447db40025a878 Mon Sep 17 00:00:00 2001 From: Jcw87 Date: Mon, 18 Sep 2023 13:52:15 -0700 Subject: [PATCH] m_Do_printf (#53) --- include/dolphin/os/OS.h | 11 --- include/m_Do/m_Do_printf.h | 22 +++++ src/DynamicLink.cpp | 2 +- src/d/d_resorce.cpp | 2 +- src/m_Do/m_Do_hostIO.cpp | 2 +- src/m_Do/m_Do_machine.cpp | 2 +- src/m_Do/m_Do_main.cpp | 2 +- src/m_Do/m_Do_printf.cpp | 164 ++++++++++++++++++++++++++++++------- 8 files changed, 163 insertions(+), 44 deletions(-) create mode 100644 include/m_Do/m_Do_printf.h diff --git a/include/dolphin/os/OS.h b/include/dolphin/os/OS.h index 21bbc4979..d57e44d5e 100644 --- a/include/dolphin/os/OS.h +++ b/include/dolphin/os/OS.h @@ -78,20 +78,9 @@ BOOL OSIsThreadSuspended(OSThread* thread); u32 OSGetConsoleType(void); -void OSAttention(const char* msg, ...); void OSPanic(const char* file, s32 line, const char* fmt, ...); void OSReport(const char* fmt, ...); -void OSReport_Error(const char* fmt, ...); -void OSReport_FatalError(const char* fmt, ...); -void OSReport_System(const char* fmt, ...); -void OSReport_Warning(const char* fmt, ...); -void OSReportDisable(void); -void OSReportEnable(void); -void OSReportForceEnableOff(void); -void OSReportForceEnableOn(void); void OSVReport(const char* format, va_list list); -void OSVAttention(const char* fmt, va_list args); -void OSReportInit(void); extern u8 __OSReport_disable; extern u8 __OSReport_Error_disable; diff --git a/include/m_Do/m_Do_printf.h b/include/m_Do/m_Do_printf.h new file mode 100644 index 000000000..e92986917 --- /dev/null +++ b/include/m_Do/m_Do_printf.h @@ -0,0 +1,22 @@ +#ifndef M_DO_M_DO_PRINTF_H +#define M_DO_M_DO_PRINTF_H + +#include "Runtime.PPCEABI.H/__va_arg.h" +#include "m_Do/m_Do_main.h" + +extern "C" { + void OSReportDisable(); + void OSReportEnable(); + void OSReportForceEnableOn(); + void OSReportForceEnableOff(); + void OSVReport(const char* format, va_list list); + void OSReport(const char* fmt, ...); + void OSReport_FatalError(const char* fmt, ...); + void OSReport_Error(const char* fmt, ...); + void OSReport_Warning(const char* fmt, ...); + void OSPanic(const char* file, s32 line, const char* fmt, ...); +} + +void OSReportInit(); + +#endif /* M_DO_M_DO_PRINTF_H */ diff --git a/src/DynamicLink.cpp b/src/DynamicLink.cpp index a9d3cd5bb..2402654b0 100644 --- a/src/DynamicLink.cpp +++ b/src/DynamicLink.cpp @@ -14,7 +14,7 @@ #include "REL/executor.h" #include "m_Do/m_Do_dvd_thread.h" #include "m_Do/m_Do_ext.h" -#include "dolphin/os/OS.h" +#include "m_Do/m_Do_printf.h" DynamicModuleControlBase* DynamicModuleControlBase::mFirst; DynamicModuleControlBase* DynamicModuleControlBase::mLast; diff --git a/src/d/d_resorce.cpp b/src/d/d_resorce.cpp index f28564500..be3cd5f49 100644 --- a/src/d/d_resorce.cpp +++ b/src/d/d_resorce.cpp @@ -4,7 +4,7 @@ // #include "d_resorce.h" -#include "dolphin/types.h" +#include "m_Do/m_Do_printf.h" /* 8006D804-8006D824 .text __ct__11dRes_info_cFv */ dRes_info_c::dRes_info_c() diff --git a/src/m_Do/m_Do_hostIO.cpp b/src/m_Do/m_Do_hostIO.cpp index cdc0feb12..97087fe58 100644 --- a/src/m_Do/m_Do_hostIO.cpp +++ b/src/m_Do/m_Do_hostIO.cpp @@ -4,8 +4,8 @@ // #include "m_Do/m_Do_hostIO.h" -#include "dolphin/os/OS.h" #include "MSL_C/string.h" +#include "m_Do/m_Do_printf.h" mDoHIO_root_c mDoHIO_root; diff --git a/src/m_Do/m_Do_machine.cpp b/src/m_Do/m_Do_machine.cpp index 8b6567a61..fc2e7e542 100644 --- a/src/m_Do/m_Do_machine.cpp +++ b/src/m_Do/m_Do_machine.cpp @@ -20,7 +20,6 @@ #include "SSystem/SComponent/c_math.h" #include "dolphin/base/PPCArch.h" #include "dolphin/gx/GXStruct.h" -#include "dolphin/os/OS.h" #include "dolphin/vi/vi.h" #include "m_Do/m_Do_DVDError.h" #include "m_do/m_Do_MemCard.h" @@ -29,6 +28,7 @@ #include "m_Do/m_Do_ext.h" #include "m_Do/m_Do_machine_exception.h" #include "m_Do/m_Do_main.h" +#include "m_Do/m_Do_printf.h" namespace mDoMch { static u8 mDebugFill; diff --git a/src/m_Do/m_Do_main.cpp b/src/m_Do/m_Do_main.cpp index 6d23c1655..5a251f01d 100644 --- a/src/m_Do/m_Do_main.cpp +++ b/src/m_Do/m_Do_main.cpp @@ -15,7 +15,6 @@ #include "MSL_C/string.h" #include "c/c_dylink.h" #include "d/d_com_inf_game.h" -#include "dolphin/os/OS.h" #include "f_ap/f_ap_game.h" #include "m_Do/m_Do_MemCard.h" #include "m_Do/m_Do_Reset.h" @@ -24,6 +23,7 @@ #include "m_Do/m_Do_dvd_thread.h" #include "m_Do/m_Do_graphic.h" #include "m_Do/m_Do_machine.h" +#include "m_Do/m_Do_printf.h" class JUTGamePad; diff --git a/src/m_Do/m_Do_printf.cpp b/src/m_Do/m_Do_printf.cpp index 760c24463..ddaa62e4d 100644 --- a/src/m_Do/m_Do_printf.cpp +++ b/src/m_Do/m_Do_printf.cpp @@ -4,79 +4,187 @@ // #include "m_Do/m_Do_printf.h" -#include "dolphin/types.h" +#include "MSL_C/stdio.h" +#include "dolphin/base/PPCArch.h" +#include "dolphin/os/OS.h" + +u8 __OSReport_disable; +u8 __OSReport_Error_disable; +u8 __OSReport_Warning_disable; +u8 __OSReport_enable; /* 800065DC-80006640 .text OSGetCallerPC */ -void OSGetCallerPC { - /* Nonmatching */ +extern "C" void* OSGetCallerPC(int param_0) { + u32* stack = (u32*)OSGetStackPointer(); + for (u32 i = 0; i <= param_0; i++) { + stack = (u32*)stack[0]; + if (stack == 0 || u32(stack) == 0xffffffff) { + return NULL; + } + } + return (void*)stack[1]; } /* 80006640-800066B0 .text OSGetActiveThreadID */ -void OSGetActiveThreadID { - /* Nonmatching */ +extern "C" int OSGetActiveThreadID(OSThread* thread) { + OSThread* r31; + int id = -1; + BOOL enable = OSDisableInterrupts(); + for (r31 = OS_THREAD_QUEUE.head; r31; r31 = r31->active_threads_link.next, id++) { + if (r31 == thread) { + break; + } + } + OSRestoreInterrupts(enable); + return r31 ? id : -1; } /* 800066B0-80006770 .text search_partial_address */ -void search_partial_address { +extern "C" int search_partial_address(void* param_0, int* param_1, int* param_2, int* param_3, int* param_4) { /* Nonmatching */ } /* 80006770-800067D0 .text convert_partial_address */ -void convert_partial_address { +extern "C" int convert_partial_address(void* param_0) { /* Nonmatching */ } /* 800067D0-800067DC .text OSReportDisable */ -void OSReportDisable { - /* Nonmatching */ +void OSReportDisable() { + __OSReport_disable = true; } /* 800067DC-800067E8 .text OSReportEnable */ -void OSReportEnable { - /* Nonmatching */ +void OSReportEnable() { + __OSReport_disable = false; } /* 800067E8-800067F4 .text OSReportForceEnableOn */ -void OSReportForceEnableOn { - /* Nonmatching */ +void OSReportForceEnableOn() { + __OSReport_enable = true; } /* 800067F4-80006800 .text OSReportForceEnableOff */ -void OSReportForceEnableOff { - /* Nonmatching */ +void OSReportForceEnableOff() { + __OSReport_enable = false; } +static OSThread* __OSReport_MonopolyThread; +static u8 print_threadID; +static u8 print_callerPC; +static u8 print_callerPCLevel = 3; +static u32 print_counts; +static u32 print_errors; +static u32 print_warings; +static bool print_initialized; + /* 80006800-80006818 .text OSReportInit__Fv */ void OSReportInit() { - /* Nonmatching */ + if (print_initialized) { + return; + } + print_initialized = true; } /* 80006818-80006950 .text OSVReport */ -void OSVReport { - /* Nonmatching */ +void OSVReport(const char* fmt, va_list args) { + if (!print_initialized) { + OSReportInit(); + } + if (__OSReport_enable || !__OSReport_disable) { + OSThread* currentThread = OSGetCurrentThread(); + if (currentThread && currentThread->state != OS_THREAD_STATE_RUNNING) { + currentThread = NULL; + } + if (__OSReport_MonopolyThread == NULL || __OSReport_MonopolyThread == currentThread) { + if (print_threadID) { + printf("[%x]", OSGetActiveThreadID(currentThread)); + } + if (print_callerPC) { + printf("[PC:"); + for (u32 i = 3; i <= print_callerPCLevel; i++) { + printf(" %08x", convert_partial_address(OSGetCallerPC(i))); + } + printf("]"); + } + vprintf(fmt, args); + print_counts++; + } + } } /* 80006950-800069D0 .text OSReport */ -void OSReport { - /* Nonmatching */ +void OSReport(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + OSVReport(fmt, args); + va_end(args); } /* 800069D0-80006A9C .text OSReport_FatalError */ -void OSReport_FatalError { - /* Nonmatching */ +void OSReport_FatalError(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + + OSReportForceEnableOn(); + OSReport("\x1B[41;37m***** FATAL ERROR *****\n"); + OSVReport(fmt, args); + OSReport("***** FATAL ERROR *****\n\x1B[m"); + OSReportForceEnableOff(); + print_errors++; + + va_end(args); } /* 80006A9C-80006B74 .text OSReport_Error */ -void OSReport_Error { - /* Nonmatching */ +void OSReport_Error(const char* fmt, ...) { + print_errors++; + if (!__OSReport_Error_disable) { + va_list args; + va_start(args, fmt); + OSReportForceEnableOn(); + printf("\x1B[41;37m"); + OSVReport(fmt, args); + printf("\x1B[m"); + OSReportForceEnableOff(); + va_end(args); + } } /* 80006B74-80006C4C .text OSReport_Warning */ -void OSReport_Warning { - /* Nonmatching */ +void OSReport_Warning(const char* fmt, ...) { + print_warings++; + if (!__OSReport_Warning_disable) { + va_list args; + va_start(args, fmt); + OSReportForceEnableOn(); + OSReport("\x1B[43;30m"); + OSVReport(fmt, args); + OSReport("\x1B[m"); + OSReportForceEnableOff(); + va_end(args); + } } /* 80006C4C-80006D84 .text OSPanic */ -void OSPanic { - /* Nonmatching */ +void OSPanic(const char* file, s32 line, const char* fmt, ...) { + va_list args; + u32 i; + u32* p; + u32* tmp; + + OSDisableInterrupts(); + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); + OSReport(" in \"%s\" on line %d.\n", file, line); + + OSReport("\nAddress: Back Chain LR Save\n"); + for (i = 0, p = (u32*)OSGetStackPointer(); p && (u32)p != 0xFFFFFFFF && i++ < 16; p = (u32*)*p) { + OSReport("0x%08x: 0x%08x 0x%08x\n", p, p[0], p[1]); + } + + tmp = (u32*)0x1234567; // ?????? + *tmp = 0x1234567; + PPCHalt(); }