match JFramework

This commit is contained in:
SwareJonge
2025-06-16 22:11:30 +02:00
parent 5b865cb165
commit b791f26f44
16 changed files with 1262 additions and 14 deletions
+1 -1
View File
@@ -618,7 +618,7 @@ config.libs = [
JSystemLib(
"JFramework",
[
Object(NonMatching, "JSystem/JFramework/JFWDisplay.cpp"),
Object(Matching, "JSystem/JFramework/JFWDisplay.cpp"),
Object(NonMatching, "JSystem/JFramework/JFWSystem.cpp"),
],
),
+164
View File
@@ -0,0 +1,164 @@
#ifndef _JSYSTEM_JFW_JFWDISPLAY_H
#define _JSYSTEM_JFW_JFWDISPLAY_H
#include <dolphin/gx.h>
#include <dolphin/mtx.h>
#include "JSystem/JKernel/JKRHeap.h"
#include "JSystem/JUtility/JUTDirectPrint.h"
#include "JSystem/JUtility/JUTFader.h"
#include "JSystem/JUtility/JUTProcBar.h"
#include "JSystem/JUtility/JUTVideo.h"
#include "JSystem/JUtility/JUTXfb.h"
#include "JSystem/JUtility/TColor.h"
#include "types.h"
typedef void (*JFWDisplayUnkFunc)(void);
extern GC_Mtx e_mtx;
class JFWAlarm : public OSAlarm { // everything here seems to be auto inlined or unused
public:
// Contructor and Destructor are both present in map but unused, not sure if it belongs here
JFWAlarm() {}
~JFWAlarm() {}
void createAlarm() { OSCreateAlarm(this); }
void cancelAlarm() { OSCancelAlarm(this); }
OSThread *getThread() const { return mThread; }
void setThread(OSThread *thread) { mThread = thread; }
static JSUList<JFWAlarm> sList;//
public:
/* 0x28 */ OSThread *mThread;
};
class JFWDisplay
{
public:
enum EDrawDone
{
/* 0x0 */ UNK_METHOD_0 = 0,
/* 0x1 */ UNK_METHOD_1 = 1
};
static JFWDisplay *createManager(const _GXRenderModeObj *, JKRHeap *, JUTXfb::EXfbNumber, bool); // 0x80015bfc
void waitBlanking(int); // 0x8001684c
void threadSleep(s64); // 0x800169fc
void clearEfb_init(); // 0x80016ab8
void clearEfb(); // 0x80016b2c
void clearEfb(_GXColor); // 0x80016b58
void clearEfb(int, int, int, int, _GXColor); // 0x80016b9c
void calcCombinationRatio(); // 0x80016f0c
void ctor_subroutine(const GXRenderModeObj *, bool);
JFWDisplay(const GXRenderModeObj *, JKRHeap *, JUTXfb::EXfbNumber, bool);
static void destroyManager();
void prepareCopyDisp();
void drawendXfb_single();
void exchangeXfb_double();
void exchangeXfb_triple();
void copyXfb_triple();
void preGX();
void endGX();
void* changeToSingleXfb(int);
void* changeToDoubleXfb();
// UNUSED
JFWDisplay(void *, bool);
JFWDisplay(void *, void *, bool);
JFWDisplay(void *, void *, void *, bool);
void createManager(const _GXRenderModeObj *, void *, bool);
void createManager(const _GXRenderModeObj *, void *, void *, bool);
void createManager(const _GXRenderModeObj *, void *, void *, void *, bool);
void deleteToSingleXfb(int);
void deleteToSingleXfb(void *);
void addToDoubleXfb(void *, bool);
void addToDoubleXfb(JKRHeap *);
void clearAllXfb();
s32 frameToTick(float);
static void setForOSResetSystem();
// Virtual functions
virtual void beginRender(); // 0x80015e0c
virtual void endRender(); // 0x8001633c
virtual void endFrame(); // 0x8001669c
virtual ~JFWDisplay(); // 0x80015b80
static JFWDisplay *getManager() { return sManager; }
int getEfbHeight() const {
return JUTVideo::getManager()->getEfbHeight();
}
int getEfbWidth() const {
return JUTVideo::getManager()->getFbWidth();
}
JUTFader *getFader() const { return mFader; }
void setFader(JUTFader * fader) { mFader = fader; }
bool startFadeOut(int duration) {
if (mFader != nullptr) {
return mFader->startFadeOut(duration);
}
return true;
}
bool startFadeIn(int duration) {
if (mFader != nullptr) {
return mFader->startFadeIn(duration);
}
return true;
}
void setTickRate(u32 rate) {
mTickRate = rate;
mFrameRate = 0;
}
JUtility::TColor getClearColor() const {
return mClearColor;
}
void setClearColor(u8 r, u8 g, u8 b, u8 a) {
mClearColor.set(r, g, b, a);
}
void setClearColor(JUtility::TColor color)
{
mClearColor = color;
}
void setFBAlpha(bool enable) {
mEnableAlpha = enable;
}
bool getFBAlpha()
{
return mEnableAlpha;
}
static JFWDisplay *sManager; // 0x80415718
private:
JUTFader *mFader; // 04
const GXRenderModeObj *mRMode; // 08
JUtility::TColor mClearColor; // 0c
u32 mZClear; // 10
JUTXfb *mXfb; // 14
u16 mGamma; // 18
EDrawDone mDrawDoneMethod; // 1c
u16 mFrameRate; // 20
u32 mTickRate; // 24
bool mEnableAlpha; // 28
u16 mClamp; // 2a
f32 mCombinationRatio; // 2c
u32 mStartTick; // 30, tick of when the frame starts rendering
u32 mFrameTime; // 34, time it took to render a frame/amount of ticks beginRender took
u32 mVideoFrameTime; // 38, time between mStartTick and the last Video Tick
s16 mDrawingXfbNo; // 3c
bool mIsSingleXfb; // 3e
};
#endif
+70
View File
@@ -0,0 +1,70 @@
#ifndef _JSYSTEM_JFW_JFWSYSTEM_H
#define _JSYSTEM_JFW_JFWSYSTEM_H
#include <dolphin/gx.h>
#include "JSystem/JKernel/JKRHeap.h"
#include "JSystem/JKernel/JKRThread.h"
#include "JSystem/JUtility/JUTAssertion.h"
#include "JSystem/JUtility/JUTConsole.h"
#include "JSystem/JUtility/JUTDbPrint.h"
#include "JSystem/JUtility/JUTFont.h"
#include "types.h"
// Maybe these are namespaces?
struct JFWSystem
{
struct CSetUpParam
{
static int maxStdHeaps;
static u32 sysHeapSize;
static u32 fifoBufSize;
static u32 aramAudioBufSize;
static u32 aramGraphBufSize;
static s32 streamPriority;
static s32 decompPriority;
static s32 aPiecePriority;
static const ResFONT *systemFontRes;
static const _GXRenderModeObj *renderMode;
static u32 exConsoleBufferSize;
};
static void firstInit();
static void init();
static JKRHeap *rootHeap;
static JKRHeap *systemHeap;
static JKRThread *mainThread;
static JUTDbPrint *debugPrint;
static JUTFont *systemFont;
static JUTConsoleManager *systemConsoleManager;
static JUTConsole *systemConsole;
static bool sInitCalled;
// No idea how they formatted this lol
static void setMaxStdHeap(int stdHeaps) {
JUT_ASSERT(sInitCalled == 0);
CSetUpParam::maxStdHeaps = stdHeaps; }
static void setSysHeapSize(u32 heapSize) {
JUT_ASSERT(sInitCalled == 0);
CSetUpParam::sysHeapSize = heapSize; }
static void setFifoBufSize(u32 bufSize) {
JUT_ASSERT(sInitCalled == 0);
CSetUpParam::fifoBufSize = bufSize; }
// Inlines for Aram
static void setAramAudioBufSize(u32 bufSize) {
JUT_ASSERT(sInitCalled == 0);
CSetUpParam::aramAudioBufSize = bufSize; }
static void setAramGraphBufSize(u32 bufSize) {
JUT_ASSERT(sInitCalled == 0);
CSetUpParam::aramGraphBufSize = bufSize; }
// probably some more inlines for other variables, not used by MKDD
static void setRenderMode(const _GXRenderModeObj * rmode) {
#line 80
JUT_ASSERT(sInitCalled == 0);
CSetUpParam::renderMode = rmode; }
};
#endif
+1
View File
@@ -1,6 +1,7 @@
#ifndef _JSYSTEM_JGEOMETRY_H
#define _JSYSTEM_JGEOMETRY_H
#include <dolphin/mtx.h>
#include "types.h"
#ifdef __cplusplus
+1 -1
View File
@@ -3,6 +3,6 @@
#include "JSystem/JKernel/JKRAram.h"
#include "JSystem/JMacro.h"
//#include "JSystem/JUtility/JUTException.h"
#include "JSystem/JUtility/JUTException.h"
#endif
+48
View File
@@ -2,6 +2,54 @@
#define _JSYSTEM_JUT_JUTDBPRINT_H
#ifdef __cplusplus
#include "JSystem/JUtility/TColor.h"
#include "JSystem/JUtility/JUTFont.h"
#include "JSystem/JKernel/JKRHeap.h"
struct JUTDbPrintList
{
JUTDbPrintList *mNext; // _00
s16 mX; // _04
s16 mY; // _06
s16 mDuration; // _08
s16 mLen; // _0A
u8 mStr; // _0C
};
struct JUTDbPrint
{
JUTDbPrint(JUTFont *, JKRHeap *); // unused/inlined
~JUTDbPrint(); // unused/inlined
JUTFont *changeFont(JUTFont *);
void flush();
void flush(int, int, int, int);
void drawString(int, int, int, const u8 *);
// Unused/inlined:
void enter(int, int, int, const char *, int);
void print(int, int, const char *, ...);
void print(int, int, int, const char *, ...);
void reset();
void setVisible(bool visible) { mVisible = visible; }
JUTFont *getFont() const { return mFont; }
JUTDbPrintList *getList() const { return mList; }
static JUTDbPrint *start(JUTFont *, JKRHeap *);
static JUTDbPrint *getManager() { return sDebugPrint; }
static JUTDbPrint *sDebugPrint;
JUTDbPrintList *mList; // _00
JUTFont *mFont; // _04
JUtility::TColor mColor; // _08
bool mVisible; // _0C
JKRHeap *mHeap; // _10
};
extern "C" {
#endif
+1 -1
View File
@@ -40,7 +40,7 @@ private:
void *mFramebuffer; // _00
u16 mFbWidth; // _04
u16 mFbHeight; // _06
u16 mStride; // _08, aligned width?
u16 mStride; // _08
size_t mFbSize; // _0C
u8 _10[0x4]; // _10 - unknown
u16 *mFrameMemory; // _14
+149
View File
@@ -0,0 +1,149 @@
#ifndef _JSYSTEM_JUT_JUTEXCEPTION_H
#define _JSYSTEM_JUT_JUTEXCEPTION_H
#include <dolphin/os.h>
#include "JSystem/JKernel/JKRThread.h"
#include "JSystem/JSupport/JSUList.h"
#include "JSystem/JUtility/JUTGamePad.h"
#include "JSystem/JUtility/JUTXfb.h"
#include "types.h"
struct JUTConsole;
struct JUTDirectPrint;
typedef void (*JUTErrorHandler)(s32 error, OSContext *context, u32 dsisr, u32 dar);
enum ExPrintFlags
{
EXPRINTFLAG_GPR = 0x1,
EXPRINTFLAG_GPRMap = 0x2,
EXPRINTFLAG_SRR0Map = 0x4,
EXPRINTFLAG_Float = 0x8,
EXPRINTFLAG_Stack = 0x10,
EXPRINTFLAG_All = 0x1F,
};
/**
* @size{0xA4}
*/
struct JUTException : public JKRThread
{
enum EInfoPage
{
INFOPAGE_GPR = 1,
INFOPAGE_Float = 2,
INFOPAGE_Stack = 3,
INFOPAGE_GPRMap = 4,
INFOPAGE_SRR0Map = 5,
};
// size: 0x14
struct JUTExMapFile
{
inline JUTExMapFile(const char *fileName)
: mLink(this)
{
mFileName = (char *)fileName;
}
char *mFileName; // _00
JSULink<JUTExMapFile> mLink; // _04
};
/** @fabricated */
struct ExCallbackObject
{
JUTErrorHandler mErrorHandler; // _00
s32 mError; // _04
OSContext *mContext; // _08
u32 _0C; // _0C
u32 _10; // _10
};
JUTException(JUTDirectPrint *); // unused/inlined
virtual ~JUTException(){}; // _08 (weak)
virtual void *run(); // _0C
void showFloat(OSContext *);
void showStack(OSContext *);
void showMainInfo(u16, OSContext *, u32, u32);
bool showMapInfo_subroutine(u32, bool);
void showGPRMap(OSContext *);
void printDebugInfo(JUTException::EInfoPage, u16, OSContext *, u32, u32);
bool readPad(u32 *, u32 *);
void printContext(u16, OSContext *, u32, u32);
void createFB();
static void waitTime(long);
static JUTErrorHandler setPreUserCallback(JUTErrorHandler);
static void appendMapFile(const char *);
static bool queryMapAddress(char *, u32, long, u32 *, u32 *, char *, u32, bool, bool);
static bool queryMapAddress_single(char *, u32, long, u32 *, u32 *, char *, u32, bool, bool);
static JUTException *create(JUTDirectPrint *);
static void createConsole(void *buffer, u32 bufferSize);
static void panic_f(const char *file, int line, const char *msg, ...);
static void errorHandler(u16, OSContext *, u32, u32);
static void setFPException(u32);
static bool searchPartialModule(u32, u32 *, u32 *, u32 *, u32 *);
// unused/inlined:
static void panic_f_va(const char *, int, const char *, va_list);
static JUTErrorHandler setPostUserCallback(JUTErrorHandler);
// Inline
static void panic(const char *file, int line, const char *msg) {
panic_f(file, line, "%s", msg);
}
void showFloatSub(int, f32);
void showGPR(OSContext *);
void showSRR0Map(OSContext *);
bool isEnablePad() const;
u32 getFpscr();
void setFpscr(u32);
void enableFpuException();
void disableFpuException();
JUTExternalFB *getFrameMemory() const { return mFrameMemory; }
void setTraceSuppress(u32 supress) { mTraceSuppress = supress; }
void setGamePad(JUTGamePad *gamePad)
{
mGamePad = gamePad;
mPadPort = JUTGamePad::Port_Invalid;
}
static JUTException *getManager() { return sErrorManager; }
static JUTConsole *getConsole() { return sConsole; }
static JUTConsole *sConsole;
static void *sConsoleBuffer;
static size_t sConsoleBufferSize;
static JUTException *sErrorManager;
static OSMessageQueue sMessageQueue;
static void *sMessageBuffer[1];
static JUTErrorHandler sPreUserCallback;
static JUTErrorHandler sPostUserCallback;
static u32 msr;
static u32 fpscr;
static const char *sCpuExpName[32];
static JSUList<JUTExMapFile> sMapFileList;
// _00 = VTBL
// _00-_7C = JKRThread
JUTExternalFB *mFrameMemory; // _7C
JUTDirectPrint *mDirectPrint; // _80
JUTGamePad *mGamePad; // _84
JUTGamePad::EPadPort mPadPort; // _88
int mPrintWaitTime0; // _8C
int mPrintWaitTime1; // _90
u32 mTraceSuppress; // _94
u32 _98; // _98
u32 mPrintFlags; // _9C, see ExPrintFlags enum
u32 mStackPointer; // _A0
};
#endif
+49
View File
@@ -0,0 +1,49 @@
#ifndef _JUTFADER_H
#define _JUTFADER_H
#include "JSystem/JGeometry.h"
#include "JSystem/JUtility/TColor.h"
#include "JSystem/JUtility/JUTVideo.h"
#include "types.h"
struct JUTFader
{
enum EStatus
{
Status_Out = 0,
Status_In = 1,
Status_FadingIn = 2,
Status_FadingOut = 3
};
JUTFader(int, int, int, int, JUtility::TColor);
virtual ~JUTFader() {} // _08 (weak)
virtual bool startFadeIn(int duration); // _0C
virtual bool startFadeOut(int duration); // _10
virtual void draw(); // _14
void control();
// unused/inlined:
void start(int);
void setStatus(EStatus, int);
void setResetState(bool reset) { mResetting = reset; }
EStatus getStatus() const { return mStatus; }
void setColor(JUtility::TColor color) { mColor.set(color.r, color.g, color.b, mColor.a); }
// _00 VTBL
EStatus mStatus; // _04 - current status
u16 mTicksTarget; // _08 - ticks (calls to control()) to run a fade in/out for
u16 mTicksRun; // _0A - ticks the current fade has run
bool mResetting; // _0C - probably exclusive to mkdd, holds true when starting the fadeout for reset, sets back to false when fading in
u8 _0D[3]; // padding
JUtility::TColor mColor; // _10 - color of fade
JGeometry::TBox2f mViewBox; // _14 - ortho box to render within
int mEStatus; // _24 - ???
EStatus _28; // _28 - ???
};
#endif
+43
View File
@@ -0,0 +1,43 @@
#ifndef _JSYSTEM_JUT_JUTGRAPHFIFO_H
#define _JSYSTEM_JUT_JUTGRAPHFIFO_H
#include "types.h"
#include <dolphin/gx.h>
struct JUTGraphFifo
{
JUTGraphFifo(u32);
virtual ~JUTGraphFifo(); // _08
void becomeCurrent();
void setBreakPt();
void getGpStatus()
{
GXGetGPStatus((GXBool*)&mGpStatus[0], (GXBool*)&mGpStatus[1], (GXBool*)&mGpStatus[2], (GXBool*)&mGpStatus[3],
(GXBool*)&mGpStatus[4]);
}
bool isGPActive()
{
getGpStatus();
return mGpStatus[2] == false;
}
void save() { GXSaveCPUFifo(this->mFifo); }
static JUTGraphFifo *sCurrentFifo;
static GXBool mGpStatus[5];
static bool sInitiated;
// _00 = VTBL
GXFifoObj* mFifo; // _04
void* mBase; // _08
u32 mSize; // _0C
u8 _10[0xC]; // _10
};
inline void JUTCreateFifo(u32 bufSize) { new JUTGraphFifo(bufSize); }
#endif
+8 -11
View File
@@ -8,46 +8,43 @@
#include "dolphin/vi.h"
#ifdef __cplusplus
/**
* @size{0x58}
*/
typedef u8 (*Pattern)[2];
struct JUTVideo
{
JUTVideo(const _GXRenderModeObj *);
JUTVideo(const GXRenderModeObj*);
virtual ~JUTVideo(); // _08
static JUTVideo *createManager(const _GXRenderModeObj *);
static JUTVideo *createManager(const GXRenderModeObj*);
static void destroyManager();
static void preRetraceProc(unsigned long);
static void postRetraceProc(unsigned long);
static void drawDoneCallback();
u16 getEfbHeight() const { return mRenderModeObj->efbHeight; }
u16 getFbWidth() const { return mRenderModeObj->fbWidth; }
u32 getEfbHeight() const { return mRenderModeObj->efbHeight; }
u32 getXfbHeight() const { return mRenderModeObj->xfbHeight; }
u32 getFbWidth() const { return (u16)mRenderModeObj->fbWidth; } // cast required for callDirectDraw
void getBounds(u16& width, u16& height) const {
width = getFbWidth();
height = getEfbHeight();
}
_GXRenderModeObj *getRenderMode() const { return mRenderModeObj; }
u16 getXfbHeight() const { return mRenderModeObj->xfbHeight; }
GXRenderModeObj *getRenderMode() const { return mRenderModeObj; }
u32 isAntiAliasing() const { return mRenderModeObj->aa; }
Pattern getSamplePattern() const { return mRenderModeObj->sample_pattern; }
u8 *getVFilter() const { return mRenderModeObj->vfilter; }
OSMessageQueue *getMessageQueue() { return &mMessageQueue; };
static void drawDoneStart();
static void dummyNoDrawWait();
void setRenderMode(const _GXRenderModeObj *);
void setRenderMode(const GXRenderModeObj*);
void waitRetraceIfNeed(); // blr, global
VIRetraceCallback setPostRetraceCallback(VIRetraceCallback);
// Unused/inlined:
void getDrawWait();
VIRetraceCallback setPreRetraceCallback(VIRetraceCallback);
void getPixelAspect(const _GXRenderModeObj *);
void getPixelAspect(const GXRenderModeObj*);
void getPixelAspect() const;
// Static inline gets
+84
View File
@@ -0,0 +1,84 @@
#ifndef _JSYSTEM_JUT_JUTXFB_H
#define _JSYSTEM_JUT_JUTXFB_H
#include <dolphin/gx.h>
#include <JSystem/JKernel/JKRHeap.h>
#include "types.h"
struct JUTExternalFB
{
JUTExternalFB(GXRenderModeObj *, GXGamma, void *, u32);
GXRenderModeObj *mRenderModeObj; // _00
u32 mSize; // _04
u8 _08[4]; // _08
u16 _0C; // _0C
u16 mGamma; // _0E, treat as GXGamma
bool _10; // _10
};
class JUTXfb
{
public:
enum EXfbNumber
{
Unset = 0,
SingleBuffer = 1,
DoubleBuffer = 2,
TripleBuffer = 3
};
void clearIndex();
void common_init(int);
JUTXfb(const GXRenderModeObj*, JKRHeap*, JUTXfb::EXfbNumber);
~JUTXfb();
void delXfb(int);
static JUTXfb *createManager(const GXRenderModeObj* rmode, JKRHeap*, JUTXfb::EXfbNumber);
static void destroyManager();
void initiate(u16, u16, JKRHeap*, JUTXfb::EXfbNumber);
u32 accumeXfbSize();
int getBufferNum() const { return mBufferNum; }
int getDrawnXfbIndex() const { return mDrawnXfbIndex; }
s16 getDrawingXfbIndex() const { return mDrawingXfbIndex; }
s16 getDisplayingXfbIndex() const { return mDisplayingXfbIndex; }
int getSDrawingFlag() const { return mSDrawingFlag; }
void *getXfb(int index) const {
return mBuffer[index];
}
void *getDrawnXfb() const {
return (mDrawnXfbIndex >= 0) ? mBuffer[mDrawnXfbIndex] : nullptr;
}
void *getDrawingXfb() const {
return (mDrawingXfbIndex >= 0) ? mBuffer[mDrawingXfbIndex] : nullptr;
}
void *getDisplayingXfb() const {
return (mDisplayingXfbIndex >= 0) ? mBuffer[mDisplayingXfbIndex] : nullptr;
}
void setBufferNum(int num) { mBufferNum = num; }
void setDisplayingXfbIndex(s16 index) { mDisplayingXfbIndex = index; }
void setSDrawingFlag(s32 flag) { mSDrawingFlag = flag; }
void setDrawnXfbIndex(s16 index) { mDrawnXfbIndex = index; }
void setDrawingXfbIndex(s16 index) { mDrawingXfbIndex = index; }
static JUTXfb *getManager() { return sManager; }
private:
static JUTXfb *sManager;
private:
void *mBuffer[3]; // 00
bool mXfbAllocated[3]; // 0c
int mBufferNum; // 10
s16 mDrawingXfbIndex; // 14
s16 mDrawnXfbIndex; // 16
s16 mDisplayingXfbIndex; // 18
s32 mSDrawingFlag; // 1a
};
#endif
+2
View File
@@ -76,6 +76,8 @@ u32 VIGetRetraceCount();
u32 VIGetDTVStatus();
void VIFlush();
void VISetNextFrameBuffer(void *fb);
#ifdef __cplusplus
};
#endif
@@ -0,0 +1,548 @@
#include <libc/string.h>
#include <dolphin/vi.h>
#include "JSystem/J2D/J2DGrafContext.h"
#include "JSystem/JUtility/JUTAssertion.h"
#include "JSystem/JUtility/JUTConsole.h"
#include "JSystem/JUtility/JUTDbPrint.h"
#include "JSystem/JUtility/JUTVideo.h"
#include "JSystem/JUtility/JUTProcBar.h"
#include "JSystem/JFramework/JFWDisplay.h"
// Sources: https://github.com/zeldaret/tp/blob/master/libs/JSystem/JFramework/JFWDisplay.cpp
// https://github.com/kiwi515/ogws/blob/master/src/egg/core/eggAsyncDisplay.cpp
// gpHang: https://github.com/valentinaslover/paper-mar/blob/master/source/sdk/DEMOInit.c#L280
GC_Mtx e_mtx = {{1.0f, 0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f, 0.0f}};
JFWDisplay* JFWDisplay::sManager;
extern void JFWThreadAlarmHandler(OSAlarm*, OSContext*);
extern void JFWGXAbortAlarmHandler(OSAlarm*, OSContext*);
void waitForTick(u32, u16);
void diagnoseGpHang();
inline void JFWDrawDoneAlarm() {
OSAlarm alarm;
OSCreateAlarm(&alarm);
OSSetAlarm(&alarm, (OS_TIMER_CLOCK), JFWGXAbortAlarmHandler);
GXDrawDone();
OSCancelAlarm(&alarm);
}
void JFWDisplay::ctor_subroutine(const GXRenderModeObj* rmode, bool enableAlpha) {
mEnableAlpha = enableAlpha;
mClamp = GX_CLAMP_TOP | GX_CLAMP_BOTTOM;
mClearColor.set(0, 0, 0, 0);
mZClear = 0xFFFFFF;
mRMode = (rmode) ? rmode : JUTVideo::sManager->getRenderMode();
mGamma = 0;
mFader = nullptr;
mFrameRate = 1;
mTickRate = 0;
mCombinationRatio = 0.0f;
mFrameTime = 0;
mStartTick = OSGetTick();
mVideoFrameTime = 0;
mDrawingXfbNo = 0;
mIsSingleXfb = false;
mDrawDoneMethod = UNK_METHOD_0;
clearEfb_init();
JUTProcBar::create();
JUTProcBar::clear();
}
JFWDisplay::JFWDisplay(const _GXRenderModeObj* rmode, JKRHeap* heap, JUTXfb::EXfbNumber bufferCount, bool p3) {
ctor_subroutine(rmode, p3);
mXfb = JUTXfb::createManager(rmode, heap, bufferCount);
}
JFWDisplay::~JFWDisplay() {
waitBlanking(2);
JUTProcBar::destroy();
JUTXfb::destroyManager();
}
JFWDisplay *JFWDisplay::createManager(const GXRenderModeObj* rmode, JKRHeap* heap, JUTXfb::EXfbNumber bufferCount, bool p4) {
JUT_CONFIRM_MESSAGE(sManager == 0);
if (sManager == nullptr)
sManager = new JFWDisplay(rmode, heap, bufferCount, p4);
return sManager;
}
void JFWDisplay::destroyManager() {
JUT_CONFIRM_MESSAGE(sManager);
delete sManager;
sManager = nullptr;
}
void callDirectDraw() {
JUTChangeFrameBuffer(JUTXfb::getManager()->getDrawingXfb(),
JUTVideo::getManager()->getEfbHeight(),
JUTVideo::getManager()->getFbWidth());
JUTAssertion::flushMessage();
}
void JFWDisplay::prepareCopyDisp() {
GXRenderModeObj* rmode = JUTVideo::getManager()->getRenderMode();
u16 width = (u16)JUTVideo::getManager()->getFbWidth();
u16 height = (u16)JUTVideo::getManager()->getEfbHeight();
u16 xfbHeight = (u16)JUTVideo::getManager()->getXfbHeight();
GXSetCopyClear(mClearColor, mZClear);
GXSetDispCopySrc(0, 0, width, height);
GXSetDispCopyDst(width, xfbHeight);
GXSetDispCopyYScale(xfbHeight / (f32)height);
VIFlush();
GXSetCopyFilter((GXBool)rmode->aa, rmode->sample_pattern, GX_ENABLE, rmode->vfilter);
GXSetCopyClamp((GXFBClamp)mClamp);
GXSetDispCopyGamma((GXGamma)mGamma);
GXSetZMode(GX_ENABLE, GX_LEQUAL, GX_ENABLE);
if (mEnableAlpha) {
GXSetAlphaUpdate(GX_ENABLE);
}
}
void JFWDisplay::drawendXfb_single() {
JUTXfb *manager = JUTXfb::getManager();
if (manager->getDrawingXfbIndex() >= 0) {
prepareCopyDisp();
JFWDrawDoneAlarm();
GXFlush();
manager->setDrawnXfbIndex(manager->getDrawingXfbIndex());
}
}
void JFWDisplay::exchangeXfb_double() {
JUTXfb *xfbMng = JUTXfb::getManager();
if (xfbMng->getDrawnXfbIndex() == xfbMng->getDisplayingXfbIndex()) {
if (xfbMng->getDrawingXfbIndex() >= 0)
{
prepareCopyDisp();
GXCopyDisp(xfbMng->getDrawingXfb(), GX_TRUE);
if (mDrawDoneMethod == UNK_METHOD_0) {
GXDrawDone();
JUTVideo::dummyNoDrawWait();
}
else {
JUTVideo::drawDoneStart();
}
if (mDrawDoneMethod == UNK_METHOD_0) {
callDirectDraw();
}
}
int cur_xfb_index = xfbMng->getDrawingXfbIndex();
xfbMng->setDrawnXfbIndex(cur_xfb_index);
xfbMng->setDrawingXfbIndex(cur_xfb_index >= 0 ? cur_xfb_index ^ 1 : 0);
}
else {
clearEfb(JUtility::TColor(0, 0, 0, 0xff));
if (xfbMng->getDrawingXfbIndex() < 0) {
xfbMng->setDrawingXfbIndex(0);
}
}
}
void JFWDisplay::exchangeXfb_triple() {
JUTXfb *xfbMng = JUTXfb::getManager();
if (xfbMng->getDrawingXfbIndex() >= 0) {
callDirectDraw();
}
xfbMng->setDrawnXfbIndex(xfbMng->getDrawingXfbIndex());
s16 drawing_idx = xfbMng->getDrawingXfbIndex() + 1;
do {
if (drawing_idx >= 3 || drawing_idx < 0) {
drawing_idx = 0;
}
} while (drawing_idx == xfbMng->getDisplayingXfbIndex());
xfbMng->setDrawingXfbIndex(drawing_idx);
}
void JFWDisplay::copyXfb_triple() {
JUTXfb *xfbMng = JUTXfb::getManager();
if (xfbMng->getDrawingXfbIndex() >= 0) {
prepareCopyDisp();
GXCopyDisp(xfbMng->getDrawingXfb(), GX_TRUE);
GXPixModeSync();
}
}
void JFWDisplay::preGX() {
GXInvalidateTexAll();
GXInvalidateVtxCache();
if (mRMode->aa) {
GXSetPixelFmt(GX_PF_RGB565_Z16, GX_ZC_LINEAR);
GXSetDither(GX_ENABLE);
}
else {
if (mEnableAlpha) {
GXSetPixelFmt(GX_PF_RGBA6_Z24, GX_ZC_LINEAR);
GXSetDither(GX_ENABLE);
}
else {
GXSetPixelFmt(GX_PF_RGB8_Z24, GX_ZC_LINEAR);
GXSetDither(GX_DISABLE);
}
}
}
void JFWDisplay::endGX() {
u32 width = JUTVideo::getManager()->getFbWidth();
u32 height = JUTVideo::getManager()->getEfbHeight();
J2DOrthoGraph ortho(0.0f, 0.0f, width, height, -1.0f, 1.0f);
if (mFader != nullptr) {
ortho.setPort();
mFader->control();
}
JUTDbPrint::getManager()->flush();
if (JUTConsoleManager::getManager() != nullptr) {
JUTConsoleManager::getManager()->draw();
}
ortho.setPort();
JUTProcBar::getManager()->draw();
if (mDrawDoneMethod != UNK_METHOD_0 || JUTXfb::getManager()->getBufferNum() == 1) {
JUTAssertion::flushMessage_dbPrint();
}
GXFlush();
}
void JFWDisplay::beginRender() {
JUTProcBar::getManager()->wholeLoopEnd();
JUTProcBar::getManager()->wholeLoopStart();
JUTProcBar::getManager()->idleStart();
waitForTick(mTickRate, mFrameRate);
JUTVideo::getManager()->waitRetraceIfNeed();
u32 tick = OSGetTick();
mFrameTime = tick - mStartTick;
mStartTick = tick;
mVideoFrameTime = mStartTick - JUTVideo::getVideoLastTick();
JUTProcBar::getManager()->idleEnd();
JUTProcBar::getManager()->gpStart();
JUTXfb * xfbMgr = JUTXfb::getManager();
switch (xfbMgr->getBufferNum()) {
case 1:
if (xfbMgr->getSDrawingFlag() != 2) {
xfbMgr->setSDrawingFlag(1);
clearEfb(JUtility::TColor(0, 0, 0, 0xff));
}
else {
xfbMgr->setSDrawingFlag(1);
}
xfbMgr->setDrawingXfbIndex(mDrawingXfbNo);
break;
case 2:
exchangeXfb_double();
break;
case 3:
exchangeXfb_triple();
break;
default:
break;
}
preGX();
}
void JFWDisplay::endRender() {
endGX();
switch (JUTXfb::getManager()->getBufferNum())
{
case 1:
drawendXfb_single();
case 2:
break;
case 3:
copyXfb_triple();
default:
break;
}
JUTProcBar::getManager()->cpuStart();
calcCombinationRatio();
}
void JFWDisplay::endFrame() {
JUTProcBar::getManager()->cpuEnd();
JUTProcBar::getManager()->gpWaitStart();
switch (JUTXfb::getManager()->getBufferNum()) {
case 1:
break;
case 2:
JFWDrawDoneAlarm();
GXFlush();
break;
case 3:
JFWDrawDoneAlarm();
GXFlush();
break;
default:
break;
}
JUTProcBar::getManager()->gpWaitEnd();
JUTProcBar::getManager()->gpEnd();
static u32 prevFrame = VIGetRetraceCount();
u32 retrace_cnt = VIGetRetraceCount();
JUTProcBar::getManager()->setCostFrame(retrace_cnt - prevFrame);
prevFrame = retrace_cnt;
}
void JFWDisplay::waitBlanking(int p1) {
while (p1-- > 0) {
waitForTick(mTickRate, mFrameRate);
}
}
void waitForTick(u32 p1, u16 p2) {
if (p1 != 0) {
static s64 nextTick = OSGetTime();
s64 time = OSGetTime();
while (time < nextTick) {
JFWDisplay::getManager()->threadSleep((nextTick - time));
time = OSGetTime();
}
nextTick = time + p1;
}
else {
static u32 nextCount = VIGetRetraceCount();
u32 uVar1 = (p2 == 0) ? 1 : p2;
OSMessage msg;
do {
if (!OSReceiveMessage(JUTVideo::getManager()->getMessageQueue(), &msg, OS_MESSAGE_BLOCK)) {
msg = 0;
}
} while (((int)msg - (int)nextCount) < 0);
nextCount = (int)msg + uVar1;
}
}
void JFWThreadAlarmHandler(OSAlarm* p_alarm, OSContext* p_ctx) {
JFWAlarm *alarm = static_cast<JFWAlarm *>(p_alarm);
OSResumeThread(alarm->getThread());
}
void JFWDisplay::threadSleep(s64 time) {
JFWAlarm alarm;
alarm.createAlarm();
alarm.setThread(OSGetCurrentThread());
s32 status = OSDisableInterrupts();
OSSetAlarm(&alarm, time, JFWThreadAlarmHandler);
OSSuspendThread(alarm.getThread());
OSRestoreInterrupts(status);
}
static GXTexObj clear_z_tobj;
static u8 clear_z_TX[] __attribute__((aligned(32))) = {
0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
void* JFWDisplay::changeToSingleXfb(int index) {
JUTXfb* xfb = JUTXfb::getManager();
s16 xfbNo = !index ? 1 : 0;
// Check if xfb is single buffered
if (xfb->getBufferNum() != 2) {
return nullptr;
}
if (mIsSingleXfb) {
return nullptr;
}
VIWaitForRetrace();
if (xfbNo != xfb->getDisplayingXfbIndex()) {
u32 xfbSize = xfb->accumeXfbSize();
DCInvalidateRange(xfb->getDrawingXfb(), xfbSize);
memcpy(xfb->getDrawingXfb(), xfb->getDisplayingXfb(), xfbSize);
DCStoreRange(xfb->getDrawingXfb(), xfbSize);
xfb->setDrawnXfbIndex(xfb->getDrawingXfbIndex());
VISetNextFrameBuffer(xfb->getDrawingXfb());
VIFlush();
VIWaitForRetrace();
}
xfb->setSDrawingFlag(99);
xfb->setBufferNum(1);
mDrawingXfbNo = xfbNo;
mIsSingleXfb = true;
return xfb->getXfb(index);
}
void* JFWDisplay::changeToDoubleXfb() {
JUTXfb* xfb = JUTXfb::getManager();
// Check if xfb is not single buffered already
if (!mIsSingleXfb) {
return nullptr;
}
VIWaitForRetrace();
if (xfb->getSDrawingFlag() != 0) {
VIWaitForRetrace();
}
s16 xfbNo = !mDrawingXfbNo ? 1 : 0;
xfb->setDrawnXfbIndex(mDrawingXfbNo);
xfb->setDisplayingXfbIndex(xfbNo);
xfb->setDrawingXfbIndex(xfbNo);
xfb->setBufferNum(2);
mIsSingleXfb = false;
return xfb->getXfb(xfbNo);
}
void JFWDisplay::clearEfb_init() {
GXInitTexObj(&clear_z_tobj, &clear_z_TX, 4, 4, GX_TF_Z24X8, GX_REPEAT, GX_REPEAT, GX_FALSE);
GXInitTexObjLOD(&clear_z_tobj, GX_NEAR, GX_NEAR, 0.0f, 0.0f, 0.0f, GX_FALSE, GX_FALSE,
GX_ANISO_1);
}
void JFWDisplay::clearEfb(GXColor color) {
Mtx44 mtx;
u16 height = mRMode->efbHeight;
u16 width = mRMode->fbWidth;
C_MTXOrtho(mtx, 0.0f, height, 0.0f, width, 0.0f, 1.0f);
GXSetProjection(mtx, GX_ORTHOGRAPHIC);
GXSetViewport(0.0f, 0.0f, width, height, 0.0f, 1.0f);
GXSetScissor(0, 0, width, height);
GXLoadPosMtxImm(e_mtx, GX_PNMTX0);
GXSetCurrentMtx(0);
GXClearVtxDesc();
GXSetVtxDesc(GX_VA_POS, GX_DIRECT);
GXSetVtxDesc(GX_VA_TEX0, GX_DIRECT);
GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_CLR_RGB, GX_RGBX8, 0);
GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_CLR_RGBA, GX_RGB565, 0);
GXSetNumChans(0);
GXSetChanCtrl(GX_COLOR0A0, GX_DISABLE, GX_SRC_REG, GX_SRC_REG, GX_LIGHT_NULL, GX_DF_NONE, GX_AF_NONE);
GXSetChanCtrl(GX_COLOR1A1, GX_DISABLE, GX_SRC_REG, GX_SRC_REG, GX_LIGHT_NULL, GX_DF_NONE, GX_AF_NONE);
GXSetNumTexGens(1);
GXSetTexCoordGen2(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, 60, GX_DISABLE, 125);
GXLoadTexObj(&clear_z_tobj, GX_TEXMAP0);
GXSetNumTevStages(1);
GXSetTevColor(GX_TEVREG0, color);
GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR_NULL);
GXSetTevColorIn(GX_TEVSTAGE0, GX_CC_ZERO, GX_CC_ZERO, GX_CC_ZERO, GX_CC_C0);
GXSetTevColorOp(GX_TEVSTAGE0, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_ENABLE, GX_TEVPREV);
GXSetTevAlphaIn(GX_TEVSTAGE0, GX_CA_ZERO, GX_CA_ZERO, GX_CA_ZERO, GX_CA_A0);
GXSetTevAlphaOp(GX_TEVSTAGE0, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_ENABLE, GX_TEVPREV);
GXSetAlphaCompare(GX_ALWAYS, 0, GX_AOP_OR, GX_ALWAYS, 0);
GXSetZTexture(GX_ZT_REPLACE, GX_TF_Z24X8, 0);
GXSetZCompLoc(GX_DISABLE);
GXSetBlendMode(GX_BM_NONE, GX_BL_ZERO, GX_BL_ZERO, GX_LO_NOOP);
if (mEnableAlpha) {
GXSetAlphaUpdate(GX_ENABLE);
GXSetDstAlpha(GX_ENABLE, 0);
}
GXSetZMode(GX_ENABLE, GX_ALWAYS, GX_ENABLE);
GXSetCullMode(GX_CULL_BACK);
GXBegin(GX_QUADS, GX_VTXFMT0, 4);
GXPosition2u16(0, 0);
GXTexCoord2u8(0, 0);
GXPosition2u16(0 + width, 0);
GXTexCoord2u8(1, 0);
GXPosition2u16(0 + width, 0 + height);
GXTexCoord2u8(1, 1);
GXPosition2u16(0, 0 + height);
GXTexCoord2u8(0, 1);
GXEnd();
GXSetZTexture(GX_ZT_DISABLE, GX_TF_Z24X8, 0);
GXSetZCompLoc(GX_ENABLE);
if (mEnableAlpha) {
GXSetDstAlpha(GX_DISABLE, 0);
}
}
void JFWDisplay::calcCombinationRatio() {
u32 vidInterval = JUTVideo::getVideoInterval();
s32 unk30 = mFrameTime * 2;
s32 i = vidInterval;
for (; i < unk30; i += vidInterval) {
}
s32 tmp = (i - unk30) - mVideoFrameTime;
if (tmp < 0) {
tmp += vidInterval;
}
mCombinationRatio = (f32)tmp / (f32)mFrameTime;
if (mCombinationRatio > 1.0f) {
mCombinationRatio = 1.0f;
}
}
void JFWGXAbortAlarmHandler(OSAlarm *param_0, OSContext *param_1) {
diagnoseGpHang();
GXAbortFrame();
GXSetDrawDone();
}
void diagnoseGpHang() {
u32 xfTop0, xfBot0, suRdy0, r0Rdy0;
u32 xfTop1, xfBot1, suRdy1, r0Rdy1;
u32 xfTopD, xfBotD, suRdyD, r0RdyD;
GXBool readIdle, cmdIdle, junk;
GXReadXfRasMetric(&xfBot0, &xfTop0, &r0Rdy0, &suRdy0);
GXReadXfRasMetric(&xfBot1, &xfTop1, &r0Rdy1, &suRdy1);
xfTopD = (xfTop1 - xfTop0) == 0;
xfBotD = (xfBot1 - xfBot0) == 0;
suRdyD = (suRdy1 - suRdy0) > 0;
r0RdyD = (r0Rdy1 - r0Rdy0) > 0;
GXGetGPStatus(&junk, &junk, &readIdle, &cmdIdle, &junk);
OSReport("GP status %d%d%d%d%d%d --> ", readIdle, cmdIdle, xfTopD, xfBotD, suRdyD, r0RdyD);
if (!xfBotD && suRdyD)
OSReport("GP hang due to XF stall bug.\n");
else if (!xfTopD && xfBotD && suRdyD)
OSReport("GP hang due to unterminated primitive.\n");
else if (!cmdIdle && xfTopD && xfBotD && suRdyD)
OSReport("GP hang due to illegal instruction.\n");
else if (readIdle && cmdIdle && xfTopD && xfBotD && suRdyD && r0RdyD)
OSReport("GP appears to be not hung (waiting for input).\n");
else
OSReport("GP is in unknown state.\n");
}
@@ -0,0 +1,93 @@
#include "dolphin/dvd.h"
#include "dolphin/gx.h"
#include "dolphin/os.h"
#include "JSystem/JKernel/JKRAram.h"
#include "JSystem/JKernel/JKRHeap.h"
#include "JSystem/JKernel/JKRExpHeap.h"
#include "JSystem/JKernel/JKRThread.h"
#include "JSystem/JUtility/JUTConsole.h"
#include "JSystem/JUtility/JUTAssertion.h"
#include "JSystem/JUtility/JUTDbPrint.h"
#include "JSystem/JUtility/JUTDirectPrint.h"
#include "JSystem/JUtility/JUTException.h"
#include "JSystem/JUtility/JUTFont.h"
#include "JSystem/JUtility/JUTGamePad.h"
#include "JSystem/JUtility/JUTGraphFifo.h"
#include "JSystem/JUtility/JUTVideo.h"
#include "JSystem/JFramework/JFWSystem.h"
int JFWSystem::CSetUpParam::maxStdHeaps = 2;
u32 JFWSystem::CSetUpParam::sysHeapSize = 0x400000;
u32 JFWSystem::CSetUpParam::fifoBufSize = 0x40000;
u32 JFWSystem::CSetUpParam::aramAudioBufSize = 0x800000;
u32 JFWSystem::CSetUpParam::aramGraphBufSize = 0x600000;
s32 JFWSystem::CSetUpParam::streamPriority = 8;
s32 JFWSystem::CSetUpParam::decompPriority = 7;
s32 JFWSystem::CSetUpParam::aPiecePriority = 6;
const ResFONT *JFWSystem::CSetUpParam::systemFontRes = &JUTResFONT_Ascfont_fix12;
const _GXRenderModeObj *JFWSystem::CSetUpParam::renderMode = &GXNtsc480IntDf;
u32 JFWSystem::CSetUpParam::exConsoleBufferSize = 0x24FC;
JKRHeap *JFWSystem::rootHeap;
JKRHeap *JFWSystem::systemHeap;
JKRThread *JFWSystem::mainThread;
JUTDbPrint *JFWSystem::debugPrint;
JUTFont *JFWSystem::systemFont;
JUTConsoleManager *JFWSystem::systemConsoleManager;
JUTConsole *JFWSystem::systemConsole;
bool JFWSystem::sInitCalled;
void JFWSystem::firstInit()
{
#line 80
JUT_ASSERT(rootHeap == 0);
OSInit();
DVDInit();
rootHeap = JKRExpHeap::createRoot(CSetUpParam::maxStdHeaps, false);
systemHeap = JKRExpHeap::create(CSetUpParam::sysHeapSize, rootHeap, false);
}
void JFWSystem::init()
{
#line 101
JUT_ASSERT(sInitCalled == false);
if (rootHeap == 0)
firstInit();
sInitCalled = true;
JKRAram::create(CSetUpParam::aramAudioBufSize, CSetUpParam::aramGraphBufSize, CSetUpParam::streamPriority, CSetUpParam::decompPriority,
CSetUpParam::aPiecePriority);
mainThread = new JKRThread(OSGetCurrentThread(), 4);
JUTVideo::createManager(CSetUpParam::renderMode);
JUTCreateFifo(CSetUpParam::fifoBufSize);
JUTGamePad::init();
JUTDirectPrint *directPrint = JUTDirectPrint::start();
JUTAssertion::create();
JUTException::create(directPrint);
systemFont = new JUTResFont(CSetUpParam::systemFontRes, nullptr);
debugPrint = JUTDbPrint::start(nullptr, nullptr);
debugPrint->changeFont(systemFont);
systemConsoleManager = JUTConsoleManager::createManager(nullptr);
systemConsole = JUTConsole::create(60, 200, nullptr);
systemConsole->setFont(systemFont);
if (CSetUpParam::renderMode->efbHeight < 300)
{
systemConsole->setFontSize(systemFont->getWidth() * 0.85f, systemFont->getHeight() * 0.5f);
systemConsole->setPosition(20, 25);
}
else
{
systemConsole->setFontSize(systemFont->getWidth() * 0.85f, systemFont->getHeight());
systemConsole->setPosition(20, 50);
}
systemConsole->setHeight(25);
systemConsole->setVisible(false);
systemConsole->setOutput(JUTConsole::OUTPUT_OSREPORT | JUTConsole::OUTPUT_CONSOLE);
JUTSetReportConsole(systemConsole);
JUTSetWarningConsole(systemConsole);
void *mem = systemHeap->alloc(CSetUpParam::exConsoleBufferSize, 4);
JUTException::createConsole(mem, CSetUpParam::exConsoleBufferSize);
}