diff --git a/configure.py b/configure.py index 74c1054bb..b670caf99 100644 --- a/configure.py +++ b/configure.py @@ -814,7 +814,7 @@ config.libs = [ Object(NonMatching, "JSystem/JUtility/JUTGamePad.cpp"), Object(NonMatching, "JSystem/JUtility/JUTException.cpp"), Object(NonMatching, "JSystem/JUtility/JUTDirectPrint.cpp"), - Object(NonMatching, "JSystem/JUtility/JUTAssert.cpp"), + Object(Matching, "JSystem/JUtility/JUTAssert.cpp"), Object(NonMatching, "JSystem/JUtility/JUTVideo.cpp"), Object(NonMatching, "JSystem/JUtility/JUTXfb.cpp"), Object(NonMatching, "JSystem/JUtility/JUTFader.cpp"), diff --git a/include/JSystem/JUtility/JUTAssert.h b/include/JSystem/JUtility/JUTAssert.h index d4f10e24e..73910d909 100644 --- a/include/JSystem/JUtility/JUTAssert.h +++ b/include/JSystem/JUtility/JUTAssert.h @@ -33,6 +33,9 @@ namespace JUTAssertion { void setMessageCount(int); }; -extern bool sAssertVisible; +extern "C" { + void setWarningMessage_f_va(u32 device, const char* file, int line, const char* msg, va_list args); + void setLogMessage_f_va(u32 device, const char* file, int line, const char* msg, va_list args); +} #endif /* JUTASSERT_H */ diff --git a/include/JSystem/JUtility/JUTConsole.h b/include/JSystem/JUtility/JUTConsole.h index 3915b9033..5ec6c776a 100644 --- a/include/JSystem/JUtility/JUTConsole.h +++ b/include/JSystem/JUtility/JUTConsole.h @@ -67,6 +67,7 @@ public: bool isVisible() const { return mVisible; } void setVisible(bool visible) { mVisible = visible; } + u8 getLineAttr(int param_0) { return mBuf[(field_0x20 + 2) * param_0]; } void setLineAttr(int param_0, u8 param_1) { mBuf[(field_0x20 + 2) * param_0] = param_1; } u8* getLinePtr(int param_0) const { return &mBuf[(field_0x20 + 2) * param_0] + 1; } int diffIndex(int param_0, int param_1) const { @@ -77,13 +78,12 @@ public: return diff += mMaxLines; } - int nextIndex(int param_0) const { - int index = param_0 + 1; - if (mMaxLines <= index) { - index = 0; - } + int prevIndex(int index) const { + return --index < 0 ? index = mMaxLines - 1 : index; + } - return index; + int nextIndex(int index) const { + return ++index >= mMaxLines ? 0 : index; } void scrollToLastLine() { scroll(mMaxLines); } @@ -94,7 +94,7 @@ private: private: /* 0x20 */ u32 field_0x20; - /* 0x24 */ u32 mMaxLines; + /* 0x24 */ int mMaxLines; /* 0x28 */ u8* mBuf; /* 0x2C */ bool field_0x2c; /* 0x30 */ int field_0x30; @@ -146,6 +146,7 @@ extern "C" JUTConsole* JUTGetWarningConsole(); extern "C" void JUTReportConsole_f_va(const char*, va_list); extern "C" void JUTReportConsole_f(const char*, ...); extern "C" void JUTWarningConsole(const char* message); +extern "C" void JUTWarningConsole_f_va(const char*, va_list); extern "C" void JUTWarningConsole_f(const char* message, ...); extern "C" void JUTReportConsole(const char* message); diff --git a/include/JSystem/JUtility/JUTDirectPrint.h b/include/JSystem/JUtility/JUTDirectPrint.h index bc17dab88..756d02c3d 100644 --- a/include/JSystem/JUtility/JUTDirectPrint.h +++ b/include/JSystem/JUtility/JUTDirectPrint.h @@ -21,8 +21,10 @@ public: /* 802E4240 */ static JUTDirectPrint* start(); + void* getFrameBuffer() { return field_0x00; } bool isActive() const { return field_0x00 != 0; } JUtility::TColor getCharColor() const { return mCharColor; } + void changeFrameBuffer(void *param_0) { changeFrameBuffer(param_0, mFrameBufferWidth, mFrameBufferHeight); } static JUTDirectPrint* getManager() { return sDirectPrint; } diff --git a/include/JSystem/JUtility/JUTException.h b/include/JSystem/JUtility/JUTException.h index 0380caf3c..f2f9b5aaf 100644 --- a/include/JSystem/JUtility/JUTException.h +++ b/include/JSystem/JUtility/JUTException.h @@ -56,7 +56,7 @@ public: }; /* 802E1D5C */ JUTException(JUTDirectPrint*); - /* 802E40EC */ virtual ~JUTException(); + /* 802E40EC */ virtual ~JUTException() {}; /* 802E22C4 */ void showFloatSub(int, f32); /* 802E2454 */ void showFloat(OSContext*); @@ -108,7 +108,7 @@ private: static OSMessageQueue sMessageQueue; static const char* sCpuExpName[17]; static JSUList sMapFileList; - static u8 sMessageBuffer[4 + 4 /* padding */]; + static OSMessage sMessageBuffer[1]; static JUTException* sErrorManager; static OSErrorHandler sPreUserCallback; static OSErrorHandler sPostUserCallback; @@ -128,10 +128,9 @@ private: /* 0x80 */ u32 mTraceSuppress; /* 0x84 */ u32 field_0x98; /* 0x88 */ u32 mPrintFlags; - /* 0x8C */ u32 mStackPointer; }; -STATIC_ASSERT(sizeof(JUTException) == 0x90); +STATIC_ASSERT(sizeof(JUTException) == 0x8C); struct JUTWarn { JUTWarn& operator<<(const char*) { return *this; } diff --git a/include/dolphin/gx/GXFrameBuf.h b/include/dolphin/gx/GXFrameBuf.h index 781a397f3..b65dd454a 100644 --- a/include/dolphin/gx/GXFrameBuf.h +++ b/include/dolphin/gx/GXFrameBuf.h @@ -8,6 +8,12 @@ extern "C" { #endif +extern GXRenderModeObj GXNtsc480IntDf; +extern GXRenderModeObj GXNtsc480Int; +extern GXRenderModeObj GXMpal480IntDf; +extern GXRenderModeObj GXPal528IntDf; +extern GXRenderModeObj GXEurgb60Hz480IntDf; + void GXSetDispCopySrc(u16 left, u16 top, u16 width, u16 height); void GXSetTexCopySrc(u16 left, u16 top, u16 width, u16 height); void GXSetDispCopyDst(u16 arg0, u16 arg1); diff --git a/src/JSystem/JUtility/JUTAssert.cpp b/src/JSystem/JUtility/JUTAssert.cpp index 59daa0746..056885bdf 100644 --- a/src/JSystem/JUtility/JUTAssert.cpp +++ b/src/JSystem/JUtility/JUTAssert.cpp @@ -4,69 +4,182 @@ // #include "JSystem/JUtility/JUTAssert.h" -#include "dolphin/types.h" +#include "JSystem/JUtility/JUTConsole.h" +#include "JSystem/JUtility/JUTDbPrint.h" +#include "JSystem/JUtility/JUTDirectPrint.h" +#include "MSL_C/stdio.h" +#include "dolphin/vi/vi.h" -/* 802C740C-802C7410 .text create__12JUTAssertionFv */ -void JUTAssertion::create() { - /* Nonmatching */ +namespace JUTAssertion { + namespace { + static char sMessageFileLine[64]; + static char sMessageString[96]; + static s32 sDisplayTime = -1; + static s32 sDevice = 3; + static bool mVisible = true; + static u32 sMessageLife; + static s32 sMessageOwner; + static u8 mSynchro; + } } +/* 802C740C-802C7410 .text create__12JUTAssertionFv */ +void JUTAssertion::create() {} + /* 802C7410-802C744C .text flush_subroutine__12JUTAssertionFv */ -void JUTAssertion::flush_subroutine() { - /* Nonmatching */ +u32 JUTAssertion::flush_subroutine() { + if (sMessageLife == 0) { + return 0; + } + + if (sMessageLife != -1) { + sMessageLife--; + } + + if (sMessageLife >= 5) { + return sMessageLife; + } + + return 0; } /* 802C744C-802C74B0 .text flushMessage__12JUTAssertionFv */ void JUTAssertion::flushMessage() { - /* Nonmatching */ + if (flush_subroutine() && mVisible == true) { + JUTDirectPrint::getManager()->drawString(16, 16, sMessageFileLine); + JUTDirectPrint::getManager()->drawString(16, 24, sMessageString); + } } /* 802C74B0-802C7690 .text flushMessage_dbPrint__12JUTAssertionFv */ void JUTAssertion::flushMessage_dbPrint() { - /* Nonmatching */ + if (flush_subroutine() && mVisible == true && JUTDbPrint::getManager() != NULL) { + JUTFont* font = JUTDbPrint::getManager()->getFont(); + if (font != NULL) { + u8 tmp = ((VIGetRetraceCount() & 0x3C) << 2) | 0xF; + font->setGX(); + font->setCharColor(JUtility::TColor(255, tmp, tmp, 255)); + font->drawString(30, 36, sMessageFileLine, true); + font->drawString(30, 54, sMessageString, true); + } + } } /* 802C7690-802C7698 .text getSDevice__12JUTAssertionFv */ -void JUTAssertion::getSDevice() { - /* Nonmatching */ +u32 JUTAssertion::getSDevice() { + return sDevice; } /* 802C7698-802C7788 .text setConfirmMessage__12JUTAssertionFUlPcibPCc */ -void JUTAssertion::setConfirmMessage(unsigned long, char*, int, bool, const char*) { - /* Nonmatching */ +void JUTAssertion::setConfirmMessage(u32 param_1, char* file, int line, bool param_4, const char* msg) { + if (param_4 == 1) { + return; + } + u32 r28 = sMessageLife; + if (r28 == 0 && (param_1 & 1) != 0) { + sMessageLife = sDisplayTime; + sMessageOwner = 2; + snprintf(sMessageFileLine, sizeof(sMessageFileLine) - 1, "FAILED [%s:%d]", file, line); + snprintf(sMessageString, sizeof(sMessageString) - 1, "%s", msg); + } + if ((r28 == 0 || mSynchro == 0) && (param_1 & 2) != 0) { + JUTWarningConsole_f("FAILED [%s:%d] %s\n", file, line, msg); + } } /* 802C7788-802C78E4 .text showAssert__12JUTAssertionFUlPCciPCc */ -void JUTAssertion::showAssert(unsigned long, const char*, int, const char*) { - /* Nonmatching */ +void JUTAssertion::showAssert(u32 device, const char* file, int line, const char* msg) { + OSReport("Failed assertion %s in \"%s\" on line %d\n", msg, file, line); + if ((device & 1) == 0) { + return; + } + if (!JUTDirectPrint::getManager()) { + return; + } + JUTDirectPrint::getManager()->erase(10, 0x10, 0x132, 0x18); + char buffer[0x100]; + snprintf(buffer, 95, "Failed assertion: %s:%d", file, line); + JUTDirectPrint::getManager()->drawString(0x10, 0x10, buffer); + snprintf(buffer, 95, "%s", msg); + JUTDirectPrint::getManager()->drawString(0x10, 0x18, buffer); + VISetNextFrameBuffer(JUTDirectPrint::getManager()->getFrameBuffer()); + VIWaitForRetrace(); + VIFlush(); + // busy loop for 2 seconds + OSTime var1 = OSGetTime(); + while (OSTicksToMilliseconds(OSGetTime() - var1) < 2000) {} } /* 802C78E4-802C79FC .text setWarningMessage_f_va */ -void setWarningMessage_f_va { - /* Nonmatching */ +void setWarningMessage_f_va(u32 device, const char* file, int line, const char* msg, va_list args) { + u32 messageLife = JUTAssertion::sMessageLife; + bool r26 = false; + if (messageLife == 0) { + if (device & 1) { + JUTAssertion::sMessageLife = JUTAssertion::sDisplayTime; + JUTAssertion::sMessageOwner = 3; + snprintf(JUTAssertion::sMessageFileLine, sizeof(JUTAssertion::sMessageFileLine) - 1, "WARNING [%s:%d]", file, line); + } + vsnprintf(JUTAssertion::sMessageString, sizeof(JUTAssertion::sMessageString) - 1, msg, args); + r26 = true; + } + if ((messageLife == 0 || JUTAssertion::mSynchro == 0) && (device & 2)) { + JUTWarningConsole_f("WARNING [%s:%d] ", file, line); + if (!r26) { + JUTWarningConsole_f_va(msg, args); + } else { + JUTWarningConsole(JUTAssertion::sMessageString); + } + JUTWarningConsole("\n"); + } } /* 802C79FC-802C7A7C .text setWarningMessage_f__12JUTAssertionFUlPciPCce */ -void JUTAssertion::setWarningMessage_f(unsigned long, char*, int, const char*, ...) { - /* Nonmatching */ +void JUTAssertion::setWarningMessage_f(u32 device, char* file, int line, const char* msg, ...) { + va_list args; + va_start(args, msg); + setWarningMessage_f_va(device, file, line, msg, args); + va_end(args); } /* 802C7A7C-802C7B94 .text setLogMessage_f_va */ -void setLogMessage_f_va { - /* Nonmatching */ +void setLogMessage_f_va(u32 device, const char* file, int line, const char* msg, va_list args) { + u32 messageLife = JUTAssertion::sMessageLife; + bool r26 = false; + if (messageLife == 0) { + if (device & 1) { + JUTAssertion::sMessageLife = JUTAssertion::sDisplayTime; + JUTAssertion::sMessageOwner = 4; + snprintf(JUTAssertion::sMessageFileLine, sizeof(JUTAssertion::sMessageFileLine) - 1, "[%s:%d]", file, line); + } + vsnprintf(JUTAssertion::sMessageString, sizeof(JUTAssertion::sMessageString) - 1, msg, args); + r26 = true; + } + if ((messageLife == 0 || JUTAssertion::mSynchro == 0) && (device & 2)) { + JUTReportConsole_f("[%s:%d] ", file, line); + if (!r26) { + JUTReportConsole_f_va(msg, args); + } else { + JUTReportConsole(JUTAssertion::sMessageString); + } + JUTReportConsole("\n"); + } } /* 802C7B94-802C7C14 .text setLogMessage_f__12JUTAssertionFUlPciPCce */ -void JUTAssertion::setLogMessage_f(unsigned long, char*, int, const char*, ...) { - /* Nonmatching */ +void JUTAssertion::setLogMessage_f(u32 device, char* file, int line, const char* msg, ...) { + va_list args; + va_start(args, msg); + setLogMessage_f_va(device, file, line, msg, args); + va_end(args); } /* 802C7C14-802C7C1C .text setVisible__12JUTAssertionFb */ -void JUTAssertion::setVisible(bool) { - /* Nonmatching */ +void JUTAssertion::setVisible(bool visible) { + mVisible = visible; } /* 802C7C1C-802C7C34 .text setMessageCount__12JUTAssertionFi */ -void JUTAssertion::setMessageCount(int) { - /* Nonmatching */ +void JUTAssertion::setMessageCount(int msg_count) { + sMessageLife = msg_count <= 0 ? 0 : msg_count; } diff --git a/src/JSystem/JUtility/JUTConsole.cpp b/src/JSystem/JUtility/JUTConsole.cpp index a2f7e64a7..febc5e286 100644 --- a/src/JSystem/JUtility/JUTConsole.cpp +++ b/src/JSystem/JUtility/JUTConsole.cpp @@ -4,100 +4,376 @@ // #include "JSystem/JUtility/JUTConsole.h" +#include "JSystem/J2DGraph/J2DOrthoGraph.h" +#include "JSystem/JKernel/JKRHeap.h" +#include "JSystem/JUtility/JUTAssert.h" +#include "JSystem/JUtility/JUTDirectPrint.h" +#include "JSystem/JUtility/JUTVideo.h" +#include "MSL_C/stdio.h" #include "dolphin/types.h" +JUTConsoleManager* JUTConsoleManager::sManager; + /* 802CA2FC-802CA3CC .text create__10JUTConsoleFUiUiP7JKRHeap */ -void JUTConsole::create(unsigned int, unsigned int, JKRHeap*) { - /* Nonmatching */ +JUTConsole* JUTConsole::create(unsigned int param_0, unsigned int maxLines, JKRHeap* p_heap) { + JUTConsoleManager* pManager = JUTConsoleManager::getManager(); + JUT_ASSERT(33, pManager != 0); + + void* buffer = JKRAllocFromHeap(p_heap, getObjectSizeFromBufferSize(param_0, maxLines), 0); + u8* tmpBuf = (u8*)buffer; + + JUTConsole* newConsole = new (tmpBuf) JUTConsole(param_0, maxLines, true); + newConsole->mBuf = tmpBuf + sizeof(JUTConsole); + newConsole->clear(); + + pManager->appendConsole(newConsole); + return newConsole; } /* 802CA3CC-802CA4C8 .text create__10JUTConsoleFUiPvUl */ -void JUTConsole::create(unsigned int, void*, unsigned long) { - /* Nonmatching */ +JUTConsole* JUTConsole::create(unsigned int param_0, void* buffer, u32 bufferSize) { + JUTConsoleManager* pManager = JUTConsoleManager::getManager(); + JUT_ASSERT(59, pManager != 0); + JUT_ASSERT(62, ( (u32)buffer & 0x3 ) == 0); + u32 maxLines = getLineFromObjectSize(bufferSize, param_0); + + JUTConsole* newConsole = new (buffer) JUTConsole(param_0, maxLines, false); + newConsole->mBuf = (u8*)buffer + sizeof(JUTConsole); + newConsole->clear(); + + pManager->appendConsole(newConsole); + return newConsole; } /* 802CA4C8-802CA5AC .text __ct__10JUTConsoleFUiUib */ -JUTConsole::JUTConsole(unsigned int, unsigned int, bool) { - /* Nonmatching */ +JUTConsole::JUTConsole(unsigned int param_0, unsigned int maxLines, bool param_2) { + field_0x2c = param_2; + field_0x20 = param_0; + mMaxLines = maxLines; + + mPositionX = 30; + mPositionY = 50; + mHeight = 20; + + if (mHeight > mMaxLines) { + mHeight = mMaxLines; + } + + mFont = NULL; + mVisible = true; + field_0x65 = false; + field_0x66 = false; + mOutput = 1; + + field_0x5c.set(0, 0, 0, 100); + field_0x60.set(0, 0, 0, 230); +} + +static void dummy1() { + OSReport("console != 0"); } /* 802CA5AC-802CA658 .text __dt__10JUTConsoleFv */ JUTConsole::~JUTConsole() { - /* Nonmatching */ + JUT_ASSERT(150, JUTConsoleManager::getManager()); + JUTConsoleManager::getManager()->removeConsole(this); } /* 802CA658-802CA668 .text getObjectSizeFromBufferSize__10JUTConsoleFUiUi */ -void JUTConsole::getObjectSizeFromBufferSize(unsigned int, unsigned int) { - /* Nonmatching */ +size_t JUTConsole::getObjectSizeFromBufferSize(unsigned int param_0, unsigned int maxLines) { + return (param_0 + 2) * maxLines + sizeof(JUTConsole); } /* 802CA668-802CA678 .text getLineFromObjectSize__10JUTConsoleFUlUi */ -void JUTConsole::getLineFromObjectSize(unsigned long, unsigned int) { - /* Nonmatching */ +size_t JUTConsole::getLineFromObjectSize(u32 bufferSize, unsigned int param_1) { + return (bufferSize - sizeof(JUTConsole)) / (param_1 + 2); } /* 802CA678-802CA6D4 .text clear__10JUTConsoleFv */ void JUTConsole::clear() { - /* Nonmatching */ + field_0x30 = 0; + field_0x34 = 0; + field_0x38 = 0; + field_0x3c = 0; + + for (u32 i = 0; i < mMaxLines; i++) { + setLineAttr(i, 0); + } + setLineAttr(0, -1); + *getLinePtr(0) = 0; } /* 802CA6D4-802CAC1C .text doDraw__10JUTConsoleCFQ210JUTConsole12EConsoleType */ -void JUTConsole::doDraw(JUTConsole::EConsoleType) const { +void JUTConsole::doDraw(JUTConsole::EConsoleType consoleType) const { /* Nonmatching */ + f32 font_yOffset; + s32 changeLine_1; + s32 changeLine_2; + + if (mVisible && (mFont != NULL || consoleType == CONSOLE_TYPE_2)) { + if (mHeight != 0) { + bool temp_r30 = consoleType == CONSOLE_TYPE_0; + font_yOffset = 2.0f + mFontSizeY; + + if (consoleType != CONSOLE_TYPE_2) { + if (JUTVideo::getManager() == NULL) { + J2DOrthoGraph ortho(0.0f, 0.0f, 640.0f, 480.0f, -1.0f, 1.0f); + ortho.setPort(); + } else { + J2DOrthoGraph ortho(0.0f, 0.0f, JUTVideo::getManager()->getFbWidth(), + JUTVideo::getManager()->getEfbHeight(), -1.0f, 1.0f); + ortho.setPort(); + } + + const JUtility::TColor* color; + if (temp_r30) { + color = &field_0x60; + } else { + color = &field_0x5c; + } + + J2DFillBox(mPositionX - 2, (int)(mPositionY - font_yOffset), + (int)((mFontSizeX * field_0x20) + 4.0f), (int)(font_yOffset * mHeight), + *color); + mFont->setGX(); + + if (temp_r30) { + s32 s = (diffIndex(field_0x30, field_0x38) - mHeight) + 1; + if (s <= 0) { + mFont->setCharColor(JUtility::TColor(255, 255, 255, 255)); + } else if (field_0x30 == field_0x34) { + mFont->setCharColor(JUtility::TColor(255, 230, 230, 255)); + } else { + mFont->setCharColor(JUtility::TColor(230, 230, 255, 255)); + } + } else { + mFont->setCharColor(JUtility::TColor(230, 230, 230, 255)); + } + } else { + JUTDirectPrint::getManager()->erase(mPositionX - 3, mPositionY - 2, + (field_0x20 * 6) + 6, + (int)(font_yOffset * mHeight) + 4); + } + + char* linePtr; + s32 curLine = field_0x30; + s32 yFactor = 0; + + do { + linePtr = (char*)getLinePtr(curLine); + + if ((u8)linePtr[-1] != NULL) { + if (consoleType != CONSOLE_TYPE_2) { + mFont->drawString_scale(mPositionX, ((yFactor * font_yOffset) + mPositionY), + mFontSizeX, mFontSizeY, linePtr, true); + } else { + JUTDirectPrint::getManager()->drawString( + mPositionX, ((yFactor * font_yOffset) + mPositionY), linePtr); + } + + changeLine_1 = curLine + 1; + yFactor += 1; + changeLine_2 = changeLine_1 & ~(-((s32)mMaxLines <= (s32)changeLine_1)); + curLine = changeLine_2; + } else { + break; + } + } while (yFactor < mHeight && changeLine_2 != field_0x34); + } + } } /* 802CAC1C-802CAC9C .text print_f__10JUTConsoleFPCce */ -void JUTConsole::print_f(const char*, ...) { - /* Nonmatching */ +void JUTConsole::print_f(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + JUTConsole_print_f_va_(this, fmt, args); + va_end(args); } /* 802CAC9C-802CAFB0 .text print__10JUTConsoleFPCc */ -void JUTConsole::print(const char*) { - /* Nonmatching */ +void JUTConsole::print(const char* param_0) { + if (mOutput & 2) { + OSReport("%s", param_0); + } + if (mOutput & 1) { + const u8* r29 = (const u8*)param_0; + u8* r28 = getLinePtr(field_0x38) + field_0x3c; + while (*r29) { + if (field_0x66 && field_0x34 == nextIndex(field_0x38)) { + break; + } + if (*r29 == '\n') { + r29++; + field_0x3c = field_0x20; + } else if (*r29 == '\t') { + r29++; + while (field_0x3c < field_0x20) { + *(r28++) = ' '; + field_0x3c++; + if (field_0x3c % 8 == 0) { + break; + } + } + } else if (mFont && mFont->isLeadByte(*r29)) { + if (field_0x3c + 1 < field_0x20) { + *(r28++) = *(r29++); + *(r28++) = *(r29++); + field_0x3c++; + field_0x3c++; + } else { + *(r28++) = 0; + field_0x3c++; + } + } else { + *(r28++) = *(r29++); + field_0x3c++; + } + + if (field_0x3c < field_0x20) { + continue; + } + *r28 = 0; + field_0x38 = nextIndex(field_0x38); + field_0x3c = 0; + setLineAttr(field_0x38, 0xff); + r28 = getLinePtr(field_0x38); + *r28 = 0; + int local_28 = diffIndex(field_0x30, field_0x38); + if (local_28 == mHeight) { + field_0x30 = nextIndex(field_0x30); + } + if (field_0x38 == field_0x34) { + field_0x34 = nextIndex(field_0x34); + } + if (field_0x38 == field_0x30) { + field_0x30 = nextIndex(field_0x30); + } + } + *r28 = 0; + } } /* 802CAFB0-802CB03C .text JUTConsole_print_f_va_ */ -void JUTConsole_print_f_va_ { - /* Nonmatching */ +void JUTConsole_print_f_va_(JUTConsole* console, const char* fmt, va_list args) { + JUT_ASSERT(561, console!=0); + char buf[1024]; + vsnprintf(buf, sizeof(buf), fmt, args); + console->print(buf); } /* 802CB03C-802CB19C .text dumpToTerminal__10JUTConsoleFUi */ -void JUTConsole::dumpToTerminal(unsigned int) { +void JUTConsole::dumpToTerminal(unsigned int param_0) { /* Nonmatching */ + if (param_0 == 0) { + return; + } + u32 r29 = field_0x34; + if (param_0 != -1) { + r29 = field_0x38; + for (int i = 0; i != param_0; i++) { + int r25 = prevIndex(r29); + if (getLineAttr(r25) == 0) { + break; + } + r29 = r25; + if (r25 == field_0x34) { + break; + } + } + } + + int r27 = 0; + OSReport("\n:::dump of console[%x]--------------------------------\n",this); + do { + u8* r28 = getLinePtr(r29); + u8 r24 = r28[-1]; + if (r24 == 0) { + break; + } + if (field_0x65) { + OSReport("[%03d] %s\n", r27, r28); + } else { + OSReport("%s\n", r28); + } + r29 = nextIndex(r29); + r27++; + } while (r27 != field_0x34); + OSReport(":::dump of console[%x] END----------------------------\n",this); } /* 802CB19C-802CB278 .text scroll__10JUTConsoleFi */ -void JUTConsole::scroll(int) { - /* Nonmatching */ +void JUTConsole::scroll(int scrollAmnt) { + if (scrollAmnt < 0) { + int diff = diffIndex(field_0x34, field_0x30); + if (scrollAmnt < -diff) { + scrollAmnt = -diff; + } + } else { + if (scrollAmnt > 0) { + int diff = diffIndex(field_0x34, field_0x38); + if (diff + 1 <= mHeight) { + scrollAmnt = 0; + } else { + diff = diffIndex(field_0x30, field_0x38); + if (scrollAmnt > (int)(diff - mHeight) + 1) { + scrollAmnt = (int)(diff - mHeight) + 1; + } + } + } + } + + field_0x30 += scrollAmnt; + if (field_0x30 < 0) { + field_0x30 += mMaxLines; + } + + if (field_0x30 >= mMaxLines) { + field_0x30 -= mMaxLines; + } } /* 802CB278-802CB29C .text getUsedLine__10JUTConsoleCFv */ -void JUTConsole::getUsedLine() const { - /* Nonmatching */ +int JUTConsole::getUsedLine() const { + return diffIndex(field_0x34, field_0x38); } /* 802CB29C-802CB2C0 .text getLineOffset__10JUTConsoleCFv */ -void JUTConsole::getLineOffset() const { - /* Nonmatching */ +int JUTConsole::getLineOffset() const { + return diffIndex(field_0x34, field_0x30); } /* 802CB2C0-802CB2E8 .text __ct__17JUTConsoleManagerFv */ JUTConsoleManager::JUTConsoleManager() { - /* Nonmatching */ + mActiveConsole = NULL; + mDirectConsole = NULL; +} + +static void dummy2() { + OSReport("console != this && console != 0"); + OSReport("\n:::dump of console[%x]----------------\n"); + OSReport(":::dump of console[%x] END------------\n"); } /* 802CB2E8-802CB380 .text createManager__17JUTConsoleManagerFP7JKRHeap */ -void JUTConsoleManager::createManager(JKRHeap*) { - /* Nonmatching */ +JUTConsoleManager* JUTConsoleManager::createManager(JKRHeap* pHeap) { + JUT_ASSERT(922, sManager == 0); + if (pHeap == NULL) { + pHeap = JKRHeap::sCurrentHeap; + } + + JUTConsoleManager* manager = new (pHeap, 0) JUTConsoleManager(); + sManager = manager; + return manager; } /* 802CB380-802CB4C4 .text appendConsole__17JUTConsoleManagerFP10JUTConsole */ -void JUTConsoleManager::appendConsole(JUTConsole*) { +void JUTConsoleManager::appendConsole(JUTConsole* console) { /* Nonmatching */ } /* 802CB4C4-802CB674 .text removeConsole__17JUTConsoleManagerFP10JUTConsole */ -void JUTConsoleManager::removeConsole(JUTConsole*) { +void JUTConsoleManager::removeConsole(JUTConsole* console) { /* Nonmatching */ } @@ -107,61 +383,105 @@ void JUTConsoleManager::draw() const { } /* 802CB740-802CB7B4 .text drawDirect__17JUTConsoleManagerCFb */ -void JUTConsoleManager::drawDirect(bool) const { - /* Nonmatching */ +void JUTConsoleManager::drawDirect(bool waitRetrace) const { + if (mDirectConsole != NULL) { + if (waitRetrace) { + s32 interrupt_status = OSEnableInterrupts(); + u32 retrace_count = VIGetRetraceCount(); + u32 new_count; + do { + new_count = VIGetRetraceCount(); + } while (retrace_count == new_count); + OSRestoreInterrupts(interrupt_status); + } + mDirectConsole->doDraw(JUTConsole::CONSOLE_TYPE_2); + } } /* 802CB7B4-802CB810 .text setDirectConsole__17JUTConsoleManagerFP10JUTConsole */ -void JUTConsoleManager::setDirectConsole(JUTConsole*) { - /* Nonmatching */ +void JUTConsoleManager::setDirectConsole(JUTConsole* p_console) { + if (mDirectConsole != NULL) { + appendConsole(mDirectConsole); + } + + if (p_console != NULL) { + removeConsole(p_console); + } + mDirectConsole = p_console; } +static JUTConsole* sReportConsole; + /* 802CB810-802CB818 .text JUTSetReportConsole */ -void JUTSetReportConsole { - /* Nonmatching */ +void JUTSetReportConsole(JUTConsole* p_console) { + sReportConsole = p_console; } /* 802CB818-802CB820 .text JUTGetReportConsole */ -void JUTGetReportConsole { - /* Nonmatching */ +JUTConsole* JUTGetReportConsole() { + return sReportConsole; } +static JUTConsole* sWarningConsole; + /* 802CB820-802CB828 .text JUTSetWarningConsole */ -void JUTSetWarningConsole { - /* Nonmatching */ +void JUTSetWarningConsole(JUTConsole* p_console) { + sWarningConsole = p_console; } /* 802CB828-802CB830 .text JUTGetWarningConsole */ -void JUTGetWarningConsole { - /* Nonmatching */ +JUTConsole* JUTGetWarningConsole() { + return sWarningConsole; } /* 802CB830-802CB8D0 .text JUTReportConsole_f_va */ -void JUTReportConsole_f_va { - /* Nonmatching */ +void JUTReportConsole_f_va(const char* fmt, va_list args) { + char buf[256]; + + if (JUTGetReportConsole() == NULL) { + vsnprintf(buf, sizeof(buf), fmt, args); + OSReport("%s", buf); + } else if (JUTGetReportConsole()->getOutput() & (JUTConsole::OUTPUT_CONSOLE | JUTConsole::OUTPUT_OSREPORT)) { + vsnprintf(buf, sizeof(buf), fmt, args); + JUTGetReportConsole()->print(buf); + } } /* 802CB8D0-802CB950 .text JUTReportConsole_f */ -void JUTReportConsole_f { - /* Nonmatching */ +void JUTReportConsole_f(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + JUTReportConsole_f_va(fmt, args); + va_end(args); } /* 802CB950-802CB984 .text JUTReportConsole */ -void JUTReportConsole { - /* Nonmatching */ +void JUTReportConsole(const char* message) { + JUTReportConsole_f("%s", message); } /* 802CB984-802CBA24 .text JUTWarningConsole_f_va */ -void JUTWarningConsole_f_va { - /* Nonmatching */ +void JUTWarningConsole_f_va(const char* fmt, va_list args) { + char buf[256]; + + if (JUTGetWarningConsole() == NULL) { + vsnprintf(buf, sizeof(buf), fmt, args); + OSReport("%s", buf); + } else if (JUTGetWarningConsole()->getOutput() & (JUTConsole::OUTPUT_CONSOLE | JUTConsole::OUTPUT_OSREPORT)) { + vsnprintf(buf, sizeof(buf), fmt, args); + JUTGetWarningConsole()->print(buf); + } } /* 802CBA24-802CBAA4 .text JUTWarningConsole_f */ -void JUTWarningConsole_f { - /* Nonmatching */ +void JUTWarningConsole_f(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + JUTReportConsole_f_va(fmt, args); + va_end(args); } /* 802CBAA4-802CBAD8 .text JUTWarningConsole */ -void JUTWarningConsole { - /* Nonmatching */ +void JUTWarningConsole(const char* message) { + JUTReportConsole_f("%s", message); } diff --git a/src/JSystem/JUtility/JUTException.cpp b/src/JSystem/JUtility/JUTException.cpp index 7e113e680..b32a961b9 100644 --- a/src/JSystem/JUtility/JUTException.cpp +++ b/src/JSystem/JUtility/JUTException.cpp @@ -4,154 +4,1007 @@ // #include "JSystem/JUtility/JUTException.h" -#include "dolphin/types.h" +#include "JSystem/JUtility/JUTConsole.h" +#include "JSystem/JUtility/JUTDirectFile.h" +#include "JSystem/JUtility/JUTDirectPrint.h" +#include "MSL_C/math.h" +#include "MSL_C/stdio.h" +#include "MSL_C/stdlib.h" +#include "dolphin/base/PPCArch.h" +#include "dolphin/gx/GX.h" +#include "dolphin/os/OS.h" +#include "dolphin/vi/vi.h" + +struct CallbackObject { + /* 0x00 */ OSErrorHandler callback; + /* 0x04 */ u16 error; + /* 0x06 */ u16 pad_0x06; + /* 0x08 */ OSContext* context; + /* 0x0C */ int param_3; + /* 0x10 */ int param_4; +}; + +void search_name_part(u8*, u8*, int); + +OSMessageQueue JUTException::sMessageQueue = {}; +static OSTime c3bcnt[4] = {0, 0, 0, 0}; +const char* JUTException::sCpuExpName[] = { + "SYSTEM RESET", + "MACHINE CHECK", + "DSI", + "ISI", + "EXTERNAL INTERRUPT", + "ALIGNMENT", + "PROGRAM", + "FLOATING POINT", + "DECREMENTER", + "SYSTEM CALL", + "TRACE", + "PERFORMACE MONITOR", + "BREAK POINT", + "SYSTEM INTERRUPT", + "THERMAL INTERRUPT", + "PROTECTION", + "FLOATING POINT", +}; +JUTException* JUTException::sErrorManager; +OSErrorHandler JUTException::sPreUserCallback; +OSErrorHandler JUTException::sPostUserCallback; /* 802C4AC8-802C4BAC .text __ct__12JUTExceptionFP14JUTDirectPrint */ -JUTException::JUTException(JUTDirectPrint*) { - /* Nonmatching */ +JUTException::JUTException(JUTDirectPrint* directPrint) : JKRThread(0x4000, 0x10, 0) { + mDirectPrint = directPrint; + OSSetErrorHandler(EXCEPTION_DSI, errorHandler); + OSSetErrorHandler(EXCEPTION_ISI, errorHandler); + OSSetErrorHandler(EXCEPTION_PROGRAM, errorHandler); + OSSetErrorHandler(EXCEPTION_ALIGNMENT, errorHandler); + OSSetErrorHandler(EXCEPTION_MEMORY_PROTECTION, errorHandler); + setFPException(0); + + sPreUserCallback = NULL; + sPostUserCallback = NULL; + + setGamePad(NULL); + + this->mPrintWaitTime0 = 10; + this->mPrintWaitTime1 = 10; + this->mTraceSuppress = -1; + this->field_0x98 = 0; + this->mPrintFlags = + JUT_PRINT_GPR | JUT_PRINT_GPR_MAP | JUT_PRINT_SRR0_MAP | JUT_PRINT_FLOAT | JUT_PRINT_STACK; } /* 802C4BAC-802C4C14 .text create__12JUTExceptionFP14JUTDirectPrint */ -void JUTException::create(JUTDirectPrint*) { - /* Nonmatching */ +JUTException* JUTException::create(JUTDirectPrint* directPrint) { + if (!sErrorManager) { + JKRHeap* systemHeap = JKRGetSystemHeap(); + sErrorManager = new (systemHeap, 0) JUTException(directPrint); + sErrorManager->resume(); + } + + return sErrorManager; } +OSMessage JUTException::sMessageBuffer[1] = {0}; + /* 802C4C14-802C4CEC .text run__12JUTExceptionFv */ -void JUTException::run() { - /* Nonmatching */ +void* JUTException::run() { + PPCMtmsr(PPCMfmsr() & ~0x0900); + OSInitMessageQueue(&sMessageQueue, sMessageBuffer, 1); + OSMessage message; + while (true) { + OSReceiveMessage(&sMessageQueue, &message, OS_MESSAGE_BLOCK); + CallbackObject* cb = (CallbackObject*)message; + OSErrorHandler callback = cb->callback; + u16 error = cb->error; + OSContext* context = cb->context; + int r24 = cb->param_3; + int r23 = cb->param_4; + mFrameMemory = (JUTExternalFB*)sErrorManager->mDirectPrint->getFrameBuffer(); + if (!sErrorManager->mDirectPrint->getFrameBuffer()) { + sErrorManager->createFB(); + } + if (callback) { + callback(error, context, r24, r23); + } + OSDisableInterrupts(); + sErrorManager->printContext(error, context, r24, r23); + } } +static CallbackObject exCallbackObject; +void* JUTException::sConsoleBuffer; +u32 JUTException::sConsoleBufferSize; +JUTConsole* JUTException::sConsole; +u32 JUTException::msr; +u32 JUTException::fpscr; + /* 802C4CEC-802C5084 .text errorHandler__12JUTExceptionFUsP9OSContextUlUl */ -void JUTException::errorHandler(unsigned short, OSContext*, unsigned long, unsigned long) { +void JUTException::errorHandler(OSError error, OSContext* context, u32 param_3, u32 param_4) { /* Nonmatching */ + if (error == 0x10) { + OSReport("\x1b[41;37m"); + OSReport(" FPE: 浮動小数点例外が発生しました。アドレスは %08x fpscr=%08x\n", context->srr0, context->fpscr); + u8 stack_38[0x20]; + u32 stack_3c; + u32 stack_40; + u32 stack_44; + u32 stack_48; + if (searchPartialModule(context->srr0, &stack_40, &stack_44, &stack_48, &stack_3c) == 1) { + search_name_part((u8*)stack_3c, stack_38, 0x20); + OSReport("%s:%x section:%d\n", stack_38, stack_48, stack_44); + } + u32 r31 = context->fpscr; + r31 &= ((context->fpscr & 0xf8) << 22) | 0x01f80700; + if (r31 & 0x20000000) { + context->fpscr &= ~0x80; + OSReport(" FPE: Invalid operation(無効な演算)\n"); + if (fpscr & 0x01000000) { + OSReport(" SNaN\n"); + } + if (fpscr & 0x00800000) { + OSReport(" Infinity - Infinity\n"); + } + if (fpscr & 0x00400000) { + OSReport(" Infinity / Infinity\n"); + } + if (fpscr & 0x00200000) { + OSReport(" 0 / 0\n"); + } + if (fpscr & 0x00100000) { + OSReport(" Infinity * 0\n"); + } + if (fpscr & 0x00080000) { + OSReport(" Invalid compare\n"); + } + if (fpscr & 0x0400) { + OSReport(" Software request\n"); + } + if (fpscr & 0x0200) { + OSReport(" Invalid square root\n"); + } + if (fpscr & 0x0100) { + OSReport(" Invalid integer convert\n"); + } + } + if (r31 & 0x10000000) { + context->fpscr &= ~0x40; + OSReport(" FPE: Overflow(オーバーフロー)\n"); + } + if (r31 & 0x08000000) { + context->fpscr &= ~0x20; + OSReport(" FPE: Underflow(アンダーフロー)\n"); + } + if (r31 & 0x04000000) { + context->fpscr &= ~0x10; + OSReport(" FPE: Zero division(0による割り算)\n"); + } + if (r31 & 0x02000000) { + context->fpscr &= ~0x08; + OSReport(" FPE: Inexact result(不正確な結果)\n"); + } + OSReport("\x1b[m"); + return; + } + + msr = PPCMfmsr(); + fpscr = context->fpscr; + OSFillFPUContext(context); + OSSetErrorHandler(error, NULL); + if (error == OS_ERROR_MEMORY_PROTECTION) { + OSProtectRange(0, NULL, 0, 3); + OSProtectRange(1, NULL, 0, 3); + OSProtectRange(2, NULL, 0, 3); + OSProtectRange(3, NULL, 0, 3); + } + + exCallbackObject.callback = sPreUserCallback; + exCallbackObject.error = error; + exCallbackObject.context = context; + exCallbackObject.param_3 = param_3; + exCallbackObject.param_4 = param_4; + + OSSendMessage(&sMessageQueue, &exCallbackObject, OS_MESSAGE_BLOCK); + OSEnableScheduler(); + OSYieldThread(); } /* 802C5084-802C50CC .text setFPException__12JUTExceptionFUl */ -void JUTException::setFPException(unsigned long) { - /* Nonmatching */ +void JUTException::setFPException(u32 fpscr_enable_bits) { + __OSFpscrEnableBits = fpscr_enable_bits; + if (fpscr_enable_bits) { + OSSetErrorHandler(EXCEPTION_FLOATING_POINT_EXCEPTION, errorHandler); + } else { + OSSetErrorHandler(EXCEPTION_FLOATING_POINT_EXCEPTION, NULL); + } } /* 802C50CC-802C525C .text showFloatSub__12JUTExceptionFif */ -void JUTException::showFloatSub(int, float) { - /* Nonmatching */ +void JUTException::showFloatSub(int index, f32 value) { + if (fpclassify(value) == FP_NAN) { + sConsole->print_f("F%02d: Nan ", index); + } else if (fpclassify(value) == FP_INFINITE) { + if (signbit(value)) { + sConsole->print_f("F%02d:+Inf ", index); + } else { + sConsole->print_f("F%02d:-Inf ", index); + } + } else if (value == 0.0f) { + sConsole->print_f("F%02d: 0.0 ", index); + } else { + sConsole->print_f("F%02d:%+.3E", index, value); + } } /* 802C525C-802C5380 .text showFloat__12JUTExceptionFP9OSContext */ -void JUTException::showFloat(OSContext*) { - /* Nonmatching */ +void JUTException::showFloat(OSContext* context) { + if (!sConsole) { + return; + } + + sConsole->print("-------------------------------- FPR\n"); + for (int i = 0; i < 10; i++) { + showFloatSub(i, context->fpr[i]); + sConsole->print(" "); + showFloatSub(i + 11, context->fpr[i + 11]); + sConsole->print(" "); + showFloatSub(i + 22, context->fpr[i + 22]); + sConsole->print("\n"); + } + showFloatSub(10, context->fpr[10]); + sConsole->print(" "); + showFloatSub(21, context->fpr[21]); + sConsole->print("\n"); } /* 802C5380-802C5440 .text searchPartialModule__12JUTExceptionFUlPUlPUlPUlPUl */ -void JUTException::searchPartialModule(unsigned long, unsigned long*, unsigned long*, unsigned long*, unsigned long*) { - /* Nonmatching */ +bool JUTException::searchPartialModule(u32 address, u32* module_id, u32* section_id, u32* section_offset, u32* name_offset) { + if (!address) { + return false; + } + + OSModuleInfo* module = *(OSModuleInfo**)0x800030C8; + while (module) { + OSSectionInfo* section = (OSSectionInfo*)module->info.sectionInfoOffset; + for (u32 i = 0; i < module->mNumSections; section = section + 1, i++) { + if (section->mSize) { + u32 addr = ALIGN_PREV(section->mOffset, 2); + if ((addr <= address) && (address < addr + section->mSize)) { + if (module_id) + *module_id = module->mId; + if (section_id) + *section_id = i; + if (section_offset) + *section_offset = address - addr; + if (name_offset) + *name_offset = module->mModuleNameOffset; + return true; + } + } + } + + module = (OSModuleInfo*)module->mNext; + } + + return false; } /* 802C5440-802C54B8 .text search_name_part__FPUcPUci */ -void search_name_part(unsigned char*, unsigned char*, int) { - /* Nonmatching */ +void search_name_part(u8* src, u8* dst, int dst_length) { + for (u8* p = src; *p; p++) { + if (*p == '\\') { + src = p; + } + } + + if (*src == '\\') { + src++; + } + + for (int i = 0; (*src != 0) && (i < dst_length);) { + if (*src == '.') + break; + *dst++ = *src++; + i++; + } + + *dst = '\0'; } /* 802C54B8-802C55BC .text showStack__12JUTExceptionFP9OSContext */ -void JUTException::showStack(OSContext*) { - /* Nonmatching */ +void JUTException::showStack(OSContext* context) { + if (!sConsole) { + return; + } + + u32 i; + u32* stackPointer; + sConsole->print("-------------------------------- TRACE\n"); + sConsole->print_f("Address: BackChain LR save\n"); + + for (i = 0, stackPointer = (u32*)context->gpr[1]; (stackPointer != NULL) && (stackPointer != (u32*)0xFFFFFFFF) && (i++ < 0x10);) { + if (i > mTraceSuppress) { + sConsole->print("Suppress trace.\n"); + return; + } + + sConsole->print_f("%08X: %08X %08X\n", stackPointer, stackPointer[0], stackPointer[1]); + showMapInfo_subroutine(stackPointer[1], false); + JUTConsoleManager* manager = JUTConsoleManager::sManager; + manager->drawDirect(true); + waitTime(mPrintWaitTime1); + stackPointer = (u32*)stackPointer[0]; + } } /* 802C55BC-802C586C .text showMainInfo__12JUTExceptionFUsP9OSContextUlUl */ -void JUTException::showMainInfo(unsigned short, OSContext*, unsigned long, unsigned long) { - /* Nonmatching */ +void JUTException::showMainInfo(u16 error, OSContext* context, u32 dsisr, u32 dar) { + if (!sConsole) { + return; + } + + sConsole->print_f("CONTEXT:%08XH (%s EXCEPTION)\n", context, sCpuExpName[error]); + + if (error == OS_ERROR_FLOATING_POINT_EXCEPTION) { + u32 flags = fpscr & (((fpscr & 0xf8) << 0x16) | 0x1f80700); + if ((flags & 0x20000000) != 0) { + sConsole->print_f(" FPE: Invalid operation\n"); + if ((fpscr & 0x1000000) != 0) { + sConsole->print_f(" SNaN\n"); + } + if ((fpscr & 0x800000) != 0) { + sConsole->print_f(" Infinity - Infinity\n"); + } + if ((fpscr & 0x400000) != 0) { + sConsole->print_f(" Infinity / Infinity\n"); + } + if ((fpscr & 0x200000) != 0) { + sConsole->print_f(" 0 / 0\n"); + } + if ((fpscr & 0x100000) != 0) { + sConsole->print_f(" Infinity * 0\n"); + } + if ((fpscr & 0x80000) != 0) { + sConsole->print_f(" Invalid compare\n"); + } + if ((fpscr & 0x400) != 0) { + sConsole->print_f(" Software request\n"); + } + if ((fpscr & 0x200) != 0) { + sConsole->print_f(" Invalid square root\n"); + } + if ((fpscr & 0x100) != 0) { + sConsole->print_f(" Invalid integer convert\n"); + } + } + if ((flags & 0x10000000) != 0) { + sConsole->print_f(" FPE: Overflow\n"); + } + if ((flags & 0x8000000) != 0) { + sConsole->print_f(" FPE: Underflow\n"); + } + if ((flags & 0x4000000) != 0) { + sConsole->print_f(" FPE: Zero division\n"); + } + if ((flags & 0x2000000) != 0) { + sConsole->print_f(" FPE: Inexact result\n"); + } + } + sConsole->print_f("SRR0: %08XH SRR1:%08XH\n", context->srr0, context->srr1); + sConsole->print_f("DSISR: %08XH DAR: %08XH\n", dsisr, dar); } /* 802C586C-802C592C .text showGPR__12JUTExceptionFP9OSContext */ -void JUTException::showGPR(OSContext*) { - /* Nonmatching */ +void JUTException::showGPR(OSContext* context) { + if (!sConsole) { + return; + } + + sConsole->print("-------------------------------- GPR\n"); + for (int i = 0; i < 10; i++) { + sConsole->print_f("R%02d:%08XH R%02d:%08XH R%02d:%08XH\n", i, context->gpr[i], i + 11, + context->gpr[i + 11], i + 22, context->gpr[i + 22]); + } + sConsole->print_f("R%02d:%08XH R%02d:%08XH\n", 10, context->gpr[10], 21, context->gpr[21]); } +JSUList JUTException::sMapFileList(false); + /* 802C592C-802C5A88 .text showMapInfo_subroutine__12JUTExceptionFUlb */ -void JUTException::showMapInfo_subroutine(unsigned long, bool) { - /* Nonmatching */ +bool JUTException::showMapInfo_subroutine(u32 address, bool begin_with_newline) { + if ((address < 0x80000000) || (0x82ffffff < address)) { + return false; + } + + u32 name_offset; + u32 module_id; + u32 section_id; + u32 section_offset; + u8 name_part[36]; + + const char* new_line = "\n"; + if (begin_with_newline == false) { + new_line = ""; + } + + bool result = + searchPartialModule(address, &module_id, §ion_id, §ion_offset, &name_offset); + if (result == true) { + search_name_part((u8*)name_offset, name_part, 32); + sConsole->print_f("%s %s:%x section:%d\n", new_line, name_part, section_offset, section_id); + begin_with_newline = false; + } + + JSUListIterator last = sMapFileList.getEnd(); + JSUListIterator first = sMapFileList.getFirst(); + if (first != last) { + u32 out_addr; + u32 out_size; + char out_line[256]; + + if (result == true) { + result = + queryMapAddress((char*)name_part, section_offset, section_id, &out_addr, &out_size, + out_line, ARRAY_SIZE(out_line), true, begin_with_newline); + } else { + result = queryMapAddress(NULL, address, -1, &out_addr, &out_size, out_line, + ARRAY_SIZE(out_line), true, begin_with_newline); + } + + if (result == true) { + return true; + } + } + + return false; } /* 802C5A88-802C5B94 .text showGPRMap__12JUTExceptionFP9OSContext */ -void JUTException::showGPRMap(OSContext*) { - /* Nonmatching */ +void JUTException::showGPRMap(OSContext* context) { + if (!sConsole) { + return; + } + + bool found_address_register = false; + sConsole->print("-------------------------------- GPRMAP\n"); + + for (int i = 0; i < 31; i++) { + u32 address = context->gpr[i]; + + if (address >= 0x80000000 && 0x83000000 - 1 >= address) { + found_address_register = true; + + sConsole->print_f("R%02d: %08XH", i, address); + if (!showMapInfo_subroutine(address, true)) { + sConsole->print(" no information\n"); + } + JUTConsoleManager::sManager->drawDirect(true); + waitTime(mPrintWaitTime1); + } + } + + if (!found_address_register) { + sConsole->print(" no register which seem to address.\n"); + } } /* 802C5B94-802C5C58 .text showSRR0Map__12JUTExceptionFP9OSContext */ -void JUTException::showSRR0Map(OSContext*) { - /* Nonmatching */ +void JUTException::showSRR0Map(OSContext* context) { + if (!sConsole) { + return; + } + + sConsole->print("-------------------------------- SRR0MAP\n"); + u32 address = context->srr0; + if (address >= 0x80000000 && 0x83000000 - 1 >= address) { + sConsole->print_f("SRR0: %08XH", address); + if (showMapInfo_subroutine(address, true) == false) { + sConsole->print(" no information\n"); + } + JUTConsoleManager::getManager()->drawDirect(true); + } } /* 802C5C58-802C5D00 .text printDebugInfo__12JUTExceptionFQ212JUTException9EInfoPageUsP9OSContextUlUl */ -void JUTException::printDebugInfo(JUTException::EInfoPage, unsigned short, OSContext*, unsigned long, unsigned long) { - /* Nonmatching */ +void JUTException::printDebugInfo(JUTException::EInfoPage page, OSError error, OSContext* context, u32 param_3, u32 param_4) { + switch (page) { + case EINFO_PAGE_GPR: + return showGPR(context); + case EINFO_PAGE_FLOAT: + showFloat(context); + if (sConsole) { + sConsole->print_f(" MSR:%08XH\t FPSCR:%08XH\n", msr, fpscr); + } + break; + case EINFO_PAGE_STACK: + return showStack(context); + case EINFO_PAGE_GPR_MAP: + return showGPRMap(context); + case EINFO_PAGE_SSR0_MAP: + return showSRR0Map(context); + } } /* 802C5D00-802C5D3C .text isEnablePad__12JUTExceptionCFv */ -void JUTException::isEnablePad() const { - /* Nonmatching */ +bool JUTException::isEnablePad() const { + if (mGamePad == (JUTGamePad*)0xFFFFFFFF) + return true; + + if (mGamePadPort >= 0) + return true; + + return mGamePad; } /* 802C5D3C-802C62A8 .text readPad__12JUTExceptionFPUlPUl */ -void JUTException::readPad(unsigned long*, unsigned long*) { - /* Nonmatching */ +bool JUTException::readPad(u32* out_trigger, u32* out_button) { + bool result = false; + OSTime start_time = OSGetTime(); + OSTime ms; + do { + OSTime end_time = OSGetTime(); + OSTime ticks = end_time - start_time; + ms = ticks / (OS_TIMER_CLOCK / 1000); + } while (ms < 0x32); + + if (mGamePad == (JUTGamePad*)0xffffffff) { + JUTGamePad gamePad0(JUTGamePad::Port_1); + JUTGamePad gamePad1(JUTGamePad::Port_2); + JUTGamePad gamePad2(JUTGamePad::Port_3); + JUTGamePad gamePad3(JUTGamePad::Port_4); + JUTGamePad::read(); + + c3bcnt[0] = + (gamePad0.isPushing3ButtonReset() ? (c3bcnt[0] != 0 ? c3bcnt[0] : OSGetTime()) : 0); + c3bcnt[1] = + (gamePad1.isPushing3ButtonReset() ? (c3bcnt[1] != 0 ? c3bcnt[1] : OSGetTime()) : 0); + c3bcnt[2] = + (gamePad2.isPushing3ButtonReset() ? (c3bcnt[2] != 0 ? c3bcnt[2] : OSGetTime()) : 0); + c3bcnt[3] = + (gamePad3.isPushing3ButtonReset() ? (c3bcnt[3] != 0 ? c3bcnt[3] : OSGetTime()) : 0); + + OSTime resetTime0 = (c3bcnt[0] != 0) ? (OSGetTime() - c3bcnt[0]) : 0; + OSTime resetTime1 = (c3bcnt[1] != 0) ? (OSGetTime() - c3bcnt[1]) : 0; + OSTime resetTime2 = (c3bcnt[2] != 0) ? (OSGetTime() - c3bcnt[2]) : 0; + OSTime resetTime3 = (c3bcnt[3] != 0) ? (OSGetTime() - c3bcnt[3]) : 0; + + gamePad0.checkResetCallback(resetTime0); + gamePad1.checkResetCallback(resetTime1); + gamePad2.checkResetCallback(resetTime2); + gamePad3.checkResetCallback(resetTime3); + + if (out_trigger) { + *out_trigger = gamePad0.getTrigger() | gamePad1.getTrigger() | gamePad2.getTrigger() | + gamePad3.getTrigger(); + } + if (out_button) { + *out_button = gamePad0.getButton() | gamePad1.getButton() | gamePad2.getButton() | + gamePad3.getButton(); + } + + result = true; + } else if (mGamePadPort >= 0) { + JUTGamePad gamePad(mGamePadPort); + OSTime& gamePadTime = c3bcnt[0]; + gamePadTime = + (gamePad.isPushing3ButtonReset() ? (gamePadTime != 0 ? gamePadTime : OSGetTime()) : 0); + + OSTime resetTime = (gamePadTime != 0) ? (OSGetTime() - gamePadTime) : 0; + gamePad.checkResetCallback(resetTime); + + JUTGamePad::read(); + if (out_trigger) { + *out_trigger = gamePad.getTrigger(); + } + if (out_button) { + *out_button = gamePad.getButton(); + } + + result = true; + } else if (mGamePad) { + JUTGamePad::read(); + if (out_trigger) { + *out_trigger = mGamePad->getTrigger(); + } + if (out_button) { + *out_button = mGamePad->getButton(); + } + + result = true; + } + + return result; } /* 802C62A8-802C6724 .text printContext__12JUTExceptionFUsP9OSContextUlUl */ -void JUTException::printContext(unsigned short, OSContext*, unsigned long, unsigned long) { - /* Nonmatching */ +void JUTException::printContext(OSError error, OSContext* context, u32 dsisr, u32 dar) { + bool is_pad_enabled = isEnablePad() == 0; + if (!sErrorManager->mDirectPrint->isActive()) { + return; + } + + if (!sConsole) { + return; + } + + sConsole->print_f("******** EXCEPTION OCCURRED! ********\nFrameMemory:%XH\n", getFrameMemory()); + + int post_callback_executed = false; + while (true) { + showMainInfo(error, context, dsisr, dar); + + JUTConsoleManager::sManager->drawDirect(true); + waitTime(mPrintWaitTime0); + + if ((mPrintFlags & JUT_PRINT_GPR) != 0) { + printDebugInfo(EINFO_PAGE_GPR, error, context, dsisr, dar); + JUTConsoleManager::sManager->drawDirect(true); + waitTime(mPrintWaitTime0); + } + if ((mPrintFlags & JUT_PRINT_SRR0_MAP) != 0) { + printDebugInfo(EINFO_PAGE_SSR0_MAP, error, context, dsisr, dar); + JUTConsoleManager::sManager->drawDirect(true); + waitTime(mPrintWaitTime0); + } + if ((mPrintFlags & JUT_PRINT_GPR_MAP) != 0) { + printDebugInfo(EINFO_PAGE_GPR_MAP, error, context, dsisr, dar); + JUTConsoleManager::sManager->drawDirect(true); + waitTime(mPrintWaitTime0); + } + if ((mPrintFlags & JUT_PRINT_FLOAT) != 0) { + printDebugInfo(EINFO_PAGE_FLOAT, error, context, dsisr, dar); + JUTConsoleManager::sManager->drawDirect(true); + waitTime(mPrintWaitTime0); + } + if ((mPrintFlags & JUT_PRINT_STACK) != 0) { + printDebugInfo(EINFO_PAGE_STACK, error, context, dsisr, dar); + JUTConsoleManager::sManager->drawDirect(true); + waitTime(mPrintWaitTime1); + } + + sConsole->print("--------------------------------\n"); + JUTConsoleManager::sManager->drawDirect(true); + + if (post_callback_executed == 0 && sPostUserCallback) { + BOOL enable = OSEnableInterrupts(); + post_callback_executed = true; + (*sPostUserCallback)(error, context, dsisr, dar); + OSRestoreInterrupts(enable); + } + + if (this->field_0x98 == 0 || !is_pad_enabled) { + break; + } + + sConsole->setOutput(sConsole->getOutput() & 1); + } + + if (!is_pad_enabled) { + OSEnableInterrupts(); + + u32 button; + u32 trigger; + + int down = 0; + int up = 0; + do { + readPad(&trigger, &button); + + bool draw = false; + if (trigger == 0x100) { + sConsole->scrollToLastLine(); + draw = true; + } + + if (trigger == 0x200) { + sConsole->scrollToFirstLine(); + draw = true; + } + + if (button == 8) { + JUTConsole* console = sConsole; + up = (down < 3) ? -1 : ((down < 5) ? -2 : ((down < 7) ? -4 : -8)); + + console->scroll(up); + draw = true; + up = 0; + down++; + } else if (button == 4) { + JUTConsole* console = sConsole; + down = (up < 3) ? 1 : ((up < 5) ? 2 : ((up < 7) ? 4 : 8)); + + console->scroll(down); + draw = true; + down = 0; + up++; + } else { + down = 0; + up = 0; + } + + if (draw == true) { + u32 start = VIGetRetraceCount(); + while (start == VIGetRetraceCount()) + ; + JUTConsoleManager::sManager->drawDirect(true); + } + + waitTime(30); + } while (true); + } + + while (true) { + sConsole->scrollToFirstLine(); + JUTConsoleManager::sManager->drawDirect(true); + waitTime(2000); + + int line_offset; + int used_line; + u32 height; + next: + for (u32 i = sConsole->getHeight(); i > 0; i--) { + sConsole->scroll(1); + JUTConsoleManager::sManager->drawDirect(true); + + height = sConsole->getHeight(); + JUTConsole* console = sConsole; + line_offset = console->getLineOffset(); + used_line = console->getUsedLine(); + if ((used_line - height) + 1U <= line_offset) + break; + waitTime(20); + } + + waitTime(3000); + height = sConsole->getHeight(); + JUTConsole* console = sConsole; + line_offset = console->getLineOffset(); + used_line = console->getUsedLine(); + if ((used_line - height) + 1U <= line_offset) { + continue; + } + goto next; + } } /* 802C6724-802C67AC .text waitTime__12JUTExceptionFl */ -void JUTException::waitTime(long) { - /* Nonmatching */ +void JUTException::waitTime(s32 timeout_ms) { + if (timeout_ms) { + OSTime start_time = OSGetTime(); + OSTime ms; + do { + OSTime end_time = OSGetTime(); + OSTime ticks = end_time - start_time; + ms = ticks / (OS_TIMER_CLOCK / 1000); + } while (ms < timeout_ms); + } } /* 802C67AC-802C6868 .text createFB__12JUTExceptionFv */ void JUTException::createFB() { - /* Nonmatching */ + GXRenderModeObj* renderMode = &GXNtsc480Int; + void* end = (void*)OSGetArenaHi(); + u16 width = ALIGN_NEXT(renderMode->fb_width, 16); + u16 height = renderMode->xfb_height; + u32 pixel_count = width * height; + u32 size = pixel_count * 2; + + void* begin = (void*)ALIGN_PREV((u32)end - size, 32); + void* object = (void*)ALIGN_PREV((s32)begin - sizeof(JUTExternalFB), 32); + new (object) JUTExternalFB(renderMode, GX_GM_1_7, begin, size); + + mDirectPrint->changeFrameBuffer(object); + VIConfigure(renderMode); + VISetNextFrameBuffer(begin); + VISetBlack(FALSE); + VIFlush(); + + mFrameMemory = (JUTExternalFB*)object; } /* 802C6868-802C6878 .text setPreUserCallback__12JUTExceptionFPFUsP9OSContextUlUl_v */ -void JUTException::setPreUserCallback(void (*)(unsigned short, OSContext*, unsigned long, unsigned long)) { - /* Nonmatching */ +OSErrorHandler JUTException::setPreUserCallback(OSErrorHandler callback) { + OSErrorHandler previous = sPreUserCallback; + sPreUserCallback = callback; + return previous; } /* 802C6878-802C6888 .text setPostUserCallback__12JUTExceptionFPFUsP9OSContextUlUl_v */ -void JUTException::setPostUserCallback(void (*)(unsigned short, OSContext*, unsigned long, unsigned long)) { - /* Nonmatching */ +OSErrorHandler JUTException::setPostUserCallback(OSErrorHandler callback) { + OSErrorHandler previous = sPostUserCallback; + sPostUserCallback = callback; + return previous; } /* 802C6888-802C691C .text appendMapFile__12JUTExceptionFPCc */ -void JUTException::appendMapFile(const char*) { - /* Nonmatching */ +void JUTException::appendMapFile(const char* path) { + if (!path) { + return; + } + + JSUListIterator iterator; + for (iterator = sMapFileList.getFirst(); iterator != sMapFileList.getEnd(); ++iterator) { + if (strcmp(path, iterator->mPath) == 0) { + return; + } + } + + JUTExMapFile* mapFile = new JUTExMapFile((char*)path); + sMapFileList.append(&mapFile->mLink); } /* 802C691C-802C6A0C .text queryMapAddress__12JUTExceptionFPcUllPUlPUlPcUlbb */ -void JUTException::queryMapAddress(char*, unsigned long, long, unsigned long*, unsigned long*, char*, unsigned long, bool, bool) { - /* Nonmatching */ +bool JUTException::queryMapAddress(char* mapPath, u32 address, s32 section_id, u32* out_addr, u32* out_size, char* out_line, u32 line_length, bool print, bool begin_with_newline) { + if (mapPath) { + char buffer[80]; + snprintf(buffer, sizeof(buffer), "/maps/%s.map", mapPath); + if (queryMapAddress_single(buffer, address, section_id, out_addr, out_size, out_line, + line_length, print, begin_with_newline) == true) + { + return true; + } + } else if (sMapFileList.getFirst() != sMapFileList.getEnd()) { + if (queryMapAddress_single(sMapFileList.getFirst()->getObject()->mPath, address, -1, + out_addr, out_size, out_line, line_length, print, + begin_with_newline) == true) + { + return true; + } + } + + return false; } /* 802C6A0C-802C6D60 .text queryMapAddress_single__12JUTExceptionFPcUllPUlPUlPcUlbb */ -void JUTException::queryMapAddress_single(char*, unsigned long, long, unsigned long*, unsigned long*, char*, unsigned long, bool, bool) { +bool JUTException::queryMapAddress_single(char* mapPath, u32 address, s32 section_id, u32* out_addr, u32* out_size, char* out_line, u32 line_length, bool print, bool begin_with_newline) { /* Nonmatching */ + if (!mapPath) { + return false; + } + + char section_name[16]; + char buffer[0x200]; + JUTDirectFile file; + int i = 0; + if (!file.fopen(mapPath)) { + return false; + } + + int result = 0; + do { + char* src = buffer; + int found_section = 0; + do { + i++; + while (true) { + while (true) { + int length = file.fgets(buffer, ARRAY_SIZE(buffer)); + if (length < 0) + goto next_section; + if (buffer[0] == '.') + break; + } + + char* dst = section_name; + int i = 0; + char* src = buffer + 1; + for (; *src != '\0'; i++, dst++, src++) { + *dst = *src; + if (*src == ' ' || i == 0xf) + break; + } + + section_name[i] = 0; + if (*src == 0) + break; + + if (src[1] == 's' && src[2] == 'e' && src[3] == 'c' && src[4] == 't') { + found_section = true; + break; + } + } + if ((found_section & 0xFF) == 0) + goto end; + } while (section_id >= 0 && section_id != i); + next_section:; + + u32 addr; + int size; + do { + int length; + do { + length = file.fgets(buffer, ARRAY_SIZE(buffer)); + if (length <= 4) + goto next_symbol; + } while ((length < 28) || (buffer[28] != '4')); + + addr = strtol(buffer + 19, NULL, 16); + addr = ((buffer[18] - '0') << 28) | addr; + size = strtol(buffer + 11, NULL, 16); + } while (addr > address || address >= addr + size); + + if (out_addr) + *out_addr = addr; + + if (out_size) + *out_size = size; + + if (out_line) { + src = buffer + 0x1e; + char* dst = out_line; + u32 length = 0; + for (; length < line_length - 1; src++) { + u32 ch = *(u8*)src; + if (ch < ' ' && ch != '\t') + break; + if (((int)ch == ' ' || ch == '\t') && (length != 0)) { + if (dst[-1] != ' ') { + *dst = ' '; + dst++; + length++; + } + } else { + *dst = ch; + dst++; + length++; + } + } + if (length != 0 && dst[-1] == ' ') { + dst--; + } + *dst = 0; + if (print) { + if (begin_with_newline) { + sConsole->print("\n"); + } + sConsole->print_f(" [%08X]: .%s [%08X: %XH]\n %s\n", address, section_name, addr, size, out_line); + begin_with_newline = false; + } + } + + result = true; + + next_symbol:; + } while (section_id >= 0 && section_id != i); + + if (print && begin_with_newline) { + sConsole->print("\n"); + } + +end: + file.fclose(); + bool bresult = (result & 0xff) != 0; + return bresult; } /* 802C6D60-802C6E40 .text createConsole__12JUTExceptionFPvUl */ -void JUTException::createConsole(void*, unsigned long) { - /* Nonmatching */ +void JUTException::createConsole(void* console_buffer, u32 console_buffer_size) { + if (!console_buffer || !console_buffer_size) { + return; + } + + u32 lines = JUTConsole::getLineFromObjectSize(console_buffer_size, 0x32); + if (lines != 0) { + sConsoleBuffer = console_buffer; + sConsoleBufferSize = console_buffer_size; + sConsole = JUTConsole::create(0x32, console_buffer, console_buffer_size); + + JUTConsoleManager* manager = JUTConsoleManager::sManager; + manager->setDirectConsole(sConsole); + + sConsole->setFontSize(10.0, 6.0); + sConsole->setPosition(12, 40); + sConsole->setHeight(23); + sConsole->setVisible(true); + sConsole->setOutput(JUTConsole::OUTPUT_OSREPORT | JUTConsole::OUTPUT_CONSOLE); + } } /* 802C6E40-802C6E60 .text __ct__13JUTExternalFBFP16_GXRenderModeObj8_GXGammaPvUl */ -JUTExternalFB::JUTExternalFB(_GXRenderModeObj*, _GXGamma, void*, unsigned long) { - /* Nonmatching */ -} - -/* 802C6E60-802C6EC0 .text __dt__12JUTExceptionFv */ -JUTException::~JUTException() { - /* Nonmatching */ -} - -/* 802C6F08-802C6F5C .text __dt__39JSUListFv */ -JSUList::~JSUList() { - /* Nonmatching */ +JUTExternalFB::JUTExternalFB(GXRenderModeObj* renderMode, GXGamma gamma, void* buffer, u32 size) { + mRenderMode = renderMode; + mSize = size; + field_0x0C = 1; + mGamma = gamma; + field_0x10 = false; }