From 9fdd81e3de5e01b2f5cb07e8a5b8ecc5c15cebbf Mon Sep 17 00:00:00 2001 From: Dean Southwood Date: Fri, 27 Sep 2024 12:06:46 +1000 Subject: [PATCH] Link most of dvd, mtx --- .vscode/settings.json | 3 +- configure.py | 31 +- include/Dolphin/dvd.h | 1 + include/Dolphin/mtx.h | 33 +- include/Dolphin/os.h | 6 +- include/Dolphin/vec.h | 17 +- src/dvd/dvd.c | 4079 +++++++++++------------------------------ src/dvd/dvderror.c | 37 +- src/dvd/dvdfs.c | 1319 +++++-------- src/dvd/dvdlow.c | 1463 ++++----------- src/dvd/dvdqueue.c | 238 +-- src/dvd/fstload.c | 235 +-- src/mtx/mtx.c | 107 +- src/mtx/mtx44.c | 172 +- src/mtx/vec.c | 72 +- 15 files changed, 2286 insertions(+), 5527 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 7d17dba3..c252fb1c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -18,6 +18,7 @@ "*.cp": "cpp", "__mem.h": "c", "db.h": "c", - "math.h": "c" + "math.h": "c", + "dvd.h": "c" } } diff --git a/configure.py b/configure.py index ba15b200..c61fb2b3 100755 --- a/configure.py +++ b/configure.py @@ -224,6 +224,12 @@ "-inline auto", ] +# Dolphin mtx library flags +cflags_mtx = [ + *cflags_base, + "-fp_contract off", +] + config.linker_version = "GC/1.2.5" @@ -811,23 +817,26 @@ def DolphinLib(lib_name: str, objects: List[Object]) -> Dict[str, Any]: Object(Matching, "db/db.c"), ] ), - DolphinLib( - "mtx", - [ + { + "lib": "mtx", + "mw_version": "GC/1.2.5n", + "cflags": cflags_mtx, + "progress_category": "sdk", + "objects": [ Object(NonMatching, "mtx/mtx.c"), - Object(NonMatching, "mtx/mtx44.c"), - Object(NonMatching, "mtx/vec.c"), + Object(Matching, "mtx/mtx44.c"), + Object(Matching, "mtx/vec.c"), ] - ), + }, DolphinLib( "dvd", [ - Object(NonMatching, "dvd/dvdlow.c"), - Object(NonMatching, "dvd/dvdfs.c"), - Object(NonMatching, "dvd/dvd.c"), - Object(NonMatching, "dvd/dvdqueue.c"), + Object(Matching, "dvd/dvdlow.c"), + Object(Matching, "dvd/dvdfs.c"), + Object(Matching, "dvd/dvd.c"), + Object(Matching, "dvd/dvdqueue.c"), Object(NonMatching, "dvd/dvderror.c"), - Object(NonMatching, "dvd/fstload.c"), + Object(Matching, "dvd/fstload.c"), ] ), DolphinLib( diff --git a/include/Dolphin/dvd.h b/include/Dolphin/dvd.h index 66bf9dcf..943e21a3 100644 --- a/include/Dolphin/dvd.h +++ b/include/Dolphin/dvd.h @@ -140,6 +140,7 @@ BOOL DVDChangeDir(char* dirName); s32 DVDConvertPathToEntrynum(char* path); // Other disk functions. +void __DVDLowSetWAType(u32 type, u32 location); s32 DVDGetTransferredSize(DVDFileInfo* fileInfo); DVDDiskID* DVDGetCurrentDiskID(); BOOL DVDCompareDiskID(DVDDiskID* id1, DVDDiskID* id2); diff --git a/include/Dolphin/mtx.h b/include/Dolphin/mtx.h index c0f9bfe2..a80a32ad 100644 --- a/include/Dolphin/mtx.h +++ b/include/Dolphin/mtx.h @@ -28,43 +28,20 @@ typedef struct Quaternion { ////// PAIRED SINGLE MATRIX FUNCTIONS ////// void PSMTXIdentity(Mtx mtx); -void PSMTXCopy(const Mtx src, Mtx dest); void PSMTXConcat(const Mtx A, const Mtx B, Mtx concat); - void PSMTXTranspose(const Mtx src, Mtx xPose); u32 PSMTXInverse(const Mtx src, Mtx inv); - -void __PSMTXRotAxisRadInternal(Mtx mtx, const Vec* axis, f32 sinA, f32 cosA); -void PSMTXRotRad(Mtx mtx, char axis, f32 angle); -void PSMTXRotTrig(Mtx mtx, char axis, f32 sinA, f32 cosA); -void PSMTXRotAxisRad(Mtx mtx, const Vec* axis, f32 angle); - -void PSMTXTrans(Mtx mtx, f32 xT, f32 yT, f32 zT); -void PSMTXTransApply(const Mtx src, Mtx dest, f32 xT, f32 yT, f32 zT); - -void PSMTXScale(Mtx mtx, f32 xS, f32 yS, f32 zS); -void PSMTXScaleApply(const Mtx src, Mtx dest, f32 xS, f32 yS, f32 zS); -void PSMTXQuat(Mtx mtx, const PSQuaternion* quat); - -//////////////////////////////////////////// - -//// PAIRED SINGLE MATRIX VEC FUNCTIONS //// -void PSMTXMultVec(const Mtx, const Vec*, Vec*); -void PSMTXMultVecSR(const Mtx, const Vec*, Vec*); -void PSMTXMultVecArraySR(const Mtx, f32*, f32*, f32*); - //////////////////////////////////////////// /////////// MATRIX44 FUNCTIONS //////////// -void PSMTX44Copy(Mtx44 src, Mtx44 dest); -void C_MTXPerspective(Mtx44 mtx, f32 fovY, f32 aspect, f32 n, f32 f); -void C_MTXOrtho(Mtx44 mtx, f32 t, f32 b, f32 l, f32 r, f32 n, f32 f); +void MTXPerspective(Mtx44 mtx, f32 fovY, f32 aspect, f32 n, f32 f); +void MTXOrtho(Mtx44 mtx, f32 t, f32 b, f32 l, f32 r, f32 n, f32 f); //////////////////////////////////////////// ///////// CODED C MATRIX FUNCTIONS ///////// -void C_MTXLookAt(Mtx, const Vec*, const Vec*, const Vec*); -void C_MTXLightPerspective(Mtx mtx, f32 fovY, f32 aspect, f32 scaleS, f32 scaleT, f32 transS, f32 transT); -void C_MTXLightOrtho(Mtx mtx, f32 t, f32 b, f32 l, f32 r, f32 scaleS, f32 scaleT, f32 transS, f32 transT); +void MTXTrans(Mtx m, f32 xT, f32 yT, f32 zT); +void MTXScale(Mtx m, f32 xS, f32 yS, f32 zS); +void MTXLightPerspective(Mtx m, f32 fovY, f32 aspect, f32 scaleS, f32 scaleT, f32 transS, f32 transT); //////////////////////////////////////////// ////////////// MATRIX INLINES ////////////// diff --git a/include/Dolphin/os.h b/include/Dolphin/os.h index 442ba1a3..33d40e17 100644 --- a/include/Dolphin/os.h +++ b/include/Dolphin/os.h @@ -50,12 +50,8 @@ void OSInit(); void OSReport(const char* message, ...); void OSPanic(const char* file, int line, const char* message, ...); -#define OSError(...) OSPanic(__FILE__, __LINE__, __VA_ARGS__) -#ifndef MATCHING -#define OSErrorLine(line, ...) OSError(__VA_ARGS__) -#else +#define OSError(...) OSPanic(__FILE__, __LINE__, __VA_ARGS__) #define OSErrorLine(line, ...) OSPanic(__FILE__, line, __VA_ARGS__) -#endif // Other OS functions. void OSRegisterVersion(const char*); diff --git a/include/Dolphin/vec.h b/include/Dolphin/vec.h index 2852e53a..51a0ab49 100644 --- a/include/Dolphin/vec.h +++ b/include/Dolphin/vec.h @@ -19,24 +19,11 @@ typedef struct SVec { s16 z; } SVec; -void PSVECAdd(const Vec*, const Vec*, Vec*); -void PSVECSubtract(const Vec*, const Vec*, Vec*); -void PSVECNormalize(const Vec*, Vec*); -f32 PSVECMag(const Vec*); -void PSVECCrossProduct(const Vec*, const Vec*, Vec*); +f32 PSVECSquareMag(Vec*); +f32 VECMag(Vec*); #ifdef __cplusplus } #endif -// lfs f1,0(r3) -// lfs f0,4(r3) -// fmuls f1,f1,f1 -// lfs f2,8(r3) -// fmuls f0,f0,f0 -// fmuls f2,f2,f2 -// fadds f0,f1,f0 -// fadds f1,f2,f0 -// blr - #endif diff --git a/src/dvd/dvd.c b/src/dvd/dvd.c index d7979908..f8450018 100644 --- a/src/dvd/dvd.c +++ b/src/dvd/dvd.c @@ -1,84 +1,102 @@ +#include "Dolphin/os.h" +#include "Dolphin/hw_regs.h" #include "types.h" +#include "Dolphin/dvd.h" +#include "mem.h" + +// forward declarations for local functions, as needed: +static void AlarmHandler(OSAlarm* alarm, OSContext* context); +static void cbForCancelSync(s32 result, DVDCommandBlock* block); +static void cbForStateBusy(u32 p1); +static void cbForStateCheckID1(u32 p1); +static void cbForStateCheckID2(u32 p1); +static void cbForStateCheckID2a(u32 p1); +static void cbForStateCheckID3(u32 p1); +static void cbForStateCoverClosed(u32 p1); +static void cbForStateMotorStopped(u32 p1); +static void cbForStateReadingFST(u32 p1); +static void cbForStateGettingError(u32 p1); +static void cbForStateGoToRetry(u32 p1); +static void cbForUnrecoveredError(u32 p1); +static void cbForUnrecoveredErrorRetry(u32 p1); +static void cbForCancelStreamSync(s32 result, DVDCommandBlock* block); +static void defaultOptionalCommandChecker(DVDCommandBlock* block, DVDLowCallback cb); +static void stateBusy(DVDCommandBlock*); +static void stateCheckID2(DVDCommandBlock*); +static void stateCheckID2a(DVDCommandBlock*); +static void stateCheckID3(DVDCommandBlock*); +static void stateCoverClosed(); +static void stateCoverClosed_CMD(DVDCommandBlock*); +static void stateGettingError(); +static void stateGoToRetry(); +static void stateMotorStopped(); +static void stateReady(); +static void stateTimeout(); + +// Functions from other files. +void __DVDInterruptHandler(__OSInterrupt interrupt, OSContext* context); +DVDCommandBlock* __DVDPopWaitingQueue(); + +typedef void (*stateFunc)(DVDCommandBlock* block); +stateFunc LastState; + +static u8 tmpBuffer[0x80] ATTRIBUTE_ALIGN(32); +static DVDCommandBlock* executing; +static DVDDiskID* currID; +static OSBootInfo* bootInfo; +static BOOL autoInvalidation = TRUE; +static volatile BOOL PauseFlag = FALSE; +static volatile BOOL PausingFlag = FALSE; +static volatile BOOL AutoFinishing = FALSE; +static volatile BOOL FatalErrorFlag = FALSE; +static vu32 CurrCommand; +static vu32 Canceling = FALSE; +static DVDCBCallback CancelCallback; +static vu32 ResumeFromHere = 0; +static vu32 CancelLastError; +static vu32 LastError; +static vs32 NumInternalRetry = 0; +static volatile BOOL ResetRequired; +static volatile BOOL FirstTimeInBootrom = FALSE; + +static DVDCommandBlock DummyCommandBlock; +static OSAlarm ResetAlarm; + +static BOOL DVDInitialized = FALSE; /* * --INFO-- * Address: 801FF964 * Size: 0000FC */ -void DVDInit(void) -{ - /* - .loc_0x0: - mflr r0 - lis r3, 0x802F - stw r0, 0x4(r1) - stwu r1, -0x10(r1) - stw r31, 0xC(r1) - stw r30, 0x8(r1) - subi r30, r3, 0x77E8 - lwz r0, 0x32F8(r13) - cmpwi r0, 0 - bne- .loc_0xE4 - bl -0x98CC - li r31, 0x1 - stw r31, 0x32F8(r13) - bl -0xA18 - bl 0x2568 - bl -0x1874 - lis r0, 0x8000 - lis r3, 0x8020 - stw r0, 0x32C0(r13) - subi r4, r3, 0x1E94 - stw r0, 0x32BC(r13) - li r3, 0x15 - bl -0x69F4 - li r3, 0x400 - bl -0x6620 - addi r3, r13, 0x32B0 - bl -0x3EF8 - lis r3, 0xCC00 - li r0, 0x2A - stw r0, 0x6000(r3) - li r0, 0 - stw r0, 0x6004(r3) - lwz r3, 0x32C0(r13) - addi r3, r3, 0x20 - lwz r3, 0x0(r3) - addis r0, r3, 0x1AE0 - cmplwi r0, 0x7C22 - bne- .loc_0xB8 - addi r3, r30, 0 - crclr 6, 0x6 - bl -0x8174 - addi r3, r30, 0x18 - crclr 6, 0x6 - bl -0x8180 - bl 0x2918 - b .loc_0xE4 - - .loc_0xB8: - subis r0, r3, 0xD15 - cmplwi r0, 0xEA5E - bne- .loc_0xD4 - addi r3, r30, 0x24 - crclr 6, 0x6 - bl -0x81A0 - b .loc_0xE4 - - .loc_0xD4: - stw r31, 0x32F4(r13) - addi r3, r30, 0x40 - crclr 6, 0x6 - bl -0x81B4 - - .loc_0xE4: - lwz r0, 0x14(r1) - lwz r31, 0xC(r1) - lwz r30, 0x8(r1) - mtlr r0 - addi r1, r1, 0x10 - blr - */ +void DVDInit() +{ + if (DVDInitialized) { + return; + } + + OSInitAlarm(); + DVDInitialized = TRUE; + __DVDFSInit(); + __DVDClearWaitingQueue(); + __DVDInitWA(); + bootInfo = (OSBootInfo*)OSPhysicalToCached(0x0000); + currID = &(bootInfo->DVDDiskID); + __OSSetInterruptHandler(21, __DVDInterruptHandler); + __OSUnmaskInterrupts(0x400); + OSInitThreadQueue(&__DVDThreadQueue); + __DIRegs[DI_STATUS] = 42; + __DIRegs[DI_COVER_STATUS] = 0; + if (bootInfo->magic == OS_BOOTINFO_MAGIC_JTAG) { + OSReport("app booted via JTAG\n"); + OSReport("load fst\n"); + __fstLoad(); + } else if (bootInfo->magic == 0xD15EA5E) { + OSReport("app booted from bootrom\n"); + } else { + FirstTimeInBootrom = TRUE; + OSReport("bootrom\n"); + } } /* @@ -86,32 +104,11 @@ void DVDInit(void) * Address: 801FFA60 * Size: 000054 */ -void stateReadingFST(void) +static void stateReadingFST() { - /* - .loc_0x0: - mflr r0 - lis r3, 0x8020 - stw r0, 0x4(r1) - subi r0, r3, 0x5A0 - lis r3, 0x803D - stwu r1, -0x8(r1) - addi r5, r3, 0x3100 - lis r3, 0x8020 - stw r0, 0x32FC(r13) - subi r6, r3, 0x54C - lwz r7, 0x32C0(r13) - lwz r4, 0x8(r5) - lwz r3, 0x38(r7) - addi r0, r4, 0x1F - lwz r5, 0x4(r5) - rlwinm r4,r0,0,0,26 - bl -0x13BC - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ + LastState = (stateFunc)stateReadingFST; + + DVDLowRead(bootInfo->FSTLocation, OSRoundUp32B(((u32*)tmpBuffer)[2]), ((u32*)tmpBuffer)[1], cbForStateReadingFST); } /* @@ -119,51 +116,24 @@ void stateReadingFST(void) * Address: 801FFAB4 * Size: 000080 */ -void cbForStateReadingFST(void) +static void cbForStateReadingFST(u32 p1) { - /* - .loc_0x0: - mflr r0 - cmplwi r3, 0x10 - stw r0, 0x4(r1) - stwu r1, -0x8(r1) - bne- .loc_0x28 - lwz r3, 0x32B8(r13) - li r0, -0x1 - stw r0, 0xC(r3) - bl 0x108 - b .loc_0x70 - - .loc_0x28: - rlwinm. r0,r3,0,31,31 - beq- .loc_0x6C - lis r3, 0x803D - lwz r4, 0x32B8(r13) - addi r0, r3, 0x3180 - li r3, 0 - stw r0, 0x32B8(r13) - stw r3, 0x32EC(r13) - stw r3, 0xC(r4) - lwz r12, 0x28(r4) - cmplwi r12, 0 - beq- .loc_0x64 - mtlr r12 - li r3, 0 - blrl - - .loc_0x64: - bl 0xE10 - b .loc_0x70 - - .loc_0x6C: - bl 0x158 - - .loc_0x70: - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ + DVDCommandBlock* cmdBlock; + if (p1 == 0x10) { + executing->state = -1; + stateTimeout(); + } else if ((p1 & 1) != 0) { + NumInternalRetry = 0; + cmdBlock = executing; + executing = &DummyCommandBlock; + cmdBlock->state = 0; + if (cmdBlock->callback) { + (cmdBlock->callback)(0, cmdBlock); + } + stateReady(); + } else { + stateGettingError(); + } } /* @@ -171,63 +141,30 @@ void cbForStateReadingFST(void) * Address: 801FFB34 * Size: 0000A8 */ -void cbForStateError(void) +static void cbForStateError(u32 intType) { - /* - .loc_0x0: - mflr r0 - cmplwi r3, 0x10 - stw r0, 0x4(r1) - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - bne- .loc_0x2C - lwz r3, 0x32B8(r13) - li r0, -0x1 - stw r0, 0xC(r3) - bl .loc_0xA8 - b .loc_0x94 - - .loc_0x2C: - lis r3, 0x803D - lwz r31, 0x32B8(r13) - addi r0, r3, 0x3180 - li r3, 0x1 - stw r0, 0x32B8(r13) - stw r3, 0x32D0(r13) - lwz r12, 0x28(r31) - cmplwi r12, 0 - beq- .loc_0x60 - mtlr r12 - addi r4, r31, 0 - li r3, -0x1 - blrl - - .loc_0x60: - lwz r0, 0x32D8(r13) - cmplwi r0, 0 - beq- .loc_0x90 - lwz r12, 0x32DC(r13) - li r0, 0 - stw r0, 0x32D8(r13) - cmplwi r12, 0 - beq- .loc_0x90 - mtlr r12 - addi r4, r31, 0 - li r3, 0 - blrl + DVDCommandBlock* finished; - .loc_0x90: - bl 0xD64 + if (intType == 16) { + executing->state = -1; + stateTimeout(); + return; + } - .loc_0x94: - lwz r0, 0x1C(r1) - lwz r31, 0x14(r1) - addi r1, r1, 0x18 - mtlr r0 - blr + FatalErrorFlag = TRUE; + finished = executing; + executing = &DummyCommandBlock; + if (finished->callback) { + (finished->callback)(-1, finished); + } - .loc_0xA8: - */ + if (Canceling) { + Canceling = FALSE; + if (CancelCallback) + (CancelCallback)(0, finished); + } + + stateReady(); } /* @@ -235,8 +172,10 @@ void cbForStateError(void) * Address: ........ * Size: 00002C */ -void stateError(void) +static void stateError(u32 error) { + __DVDStoreErrorCode(error); + DVDLowStopMotor(cbForStateError); // UNUSED FUNCTION } @@ -245,54 +184,11 @@ void stateError(void) * Address: 801FFBDC * Size: 00009C */ -void stateTimeout(void) +static void stateTimeout() { - /* - .loc_0x0: - mflr r0 - lis r3, 0x123 - stw r0, 0x4(r1) - addi r3, r3, 0x4568 - stwu r1, -0x10(r1) - stw r31, 0xC(r1) - bl 0x2508 - bl 0x1CD4 - lis r3, 0x803D - lwz r31, 0x32B8(r13) - addi r0, r3, 0x3180 - li r3, 0x1 - stw r0, 0x32B8(r13) - stw r3, 0x32D0(r13) - lwz r12, 0x28(r31) - cmplwi r12, 0 - beq- .loc_0x54 - mtlr r12 - addi r4, r31, 0 - li r3, -0x1 - blrl - - .loc_0x54: - lwz r0, 0x32D8(r13) - cmplwi r0, 0 - beq- .loc_0x84 - lwz r12, 0x32DC(r13) - li r0, 0 - stw r0, 0x32D8(r13) - cmplwi r12, 0 - beq- .loc_0x84 - mtlr r12 - addi r4, r31, 0 - li r3, 0 - blrl - - .loc_0x84: - bl 0xCC8 - lwz r0, 0x14(r1) - lwz r31, 0xC(r1) - addi r1, r1, 0x10 - mtlr r0 - blr - */ + __DVDStoreErrorCode(0x1234568); + DVDReset(); + cbForStateError(0); } /* @@ -300,92 +196,44 @@ void stateTimeout(void) * Address: 801FFC78 * Size: 000028 */ -void stateGettingError(void) -{ - /* - .loc_0x0: - mflr r0 - lis r3, 0x8020 - stw r0, 0x4(r1) - subi r3, r3, 0x2AC - stwu r1, -0x8(r1) - bl -0x1120 - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ -} +static void stateGettingError() { DVDLowRequestError(cbForStateGettingError); } /* * --INFO-- * Address: 801FFCA0 * Size: 0000B4 */ -void CategorizeError(void) +static u32 CategorizeError(u32 error) { - /* - .loc_0x0: - subis r0, r3, 0x2 - cmplwi r0, 0x400 - bne- .loc_0x18 - stw r3, 0x32E8(r13) - li r3, 0x1 - blr + if (error == 0x20400) { + LastError = error; + return 1; + } - .loc_0x18: - rlwinm r4,r3,0,8,31 - subis r0, r4, 0x6 - cmplwi r0, 0x2800 - beq- .loc_0x40 - subis r0, r4, 0x2 - cmplwi r0, 0x3A00 - beq- .loc_0x40 - subis r0, r4, 0xB - cmplwi r0, 0x5A01 - bne- .loc_0x48 - - .loc_0x40: - li r3, 0 - blr + error &= 0xffffff; - .loc_0x48: - lwz r3, 0x32EC(r13) - addi r0, r3, 0x1 - stw r0, 0x32EC(r13) - lwz r0, 0x32EC(r13) - cmpwi r0, 0x2 - bne- .loc_0x84 - lwz r0, 0x32E8(r13) - cmplw r4, r0 - bne- .loc_0x78 - stw r4, 0x32E8(r13) - li r3, 0x1 - blr - - .loc_0x78: - stw r4, 0x32E8(r13) - li r3, 0x2 - blr + if ((error == 0x62800) || (error == 0x23a00) || (error == 0xb5a01)) { + return 0; + } - .loc_0x84: - subis r0, r4, 0x3 - stw r4, 0x32E8(r13) - cmplwi r0, 0x1100 - beq- .loc_0xA4 - lwz r3, 0x32B8(r13) - lwz r0, 0x8(r3) - cmplwi r0, 0x5 - bne- .loc_0xAC + ++NumInternalRetry; + if (NumInternalRetry == 2) { + if (error == LastError) { + LastError = error; + return 1; + } else { + LastError = error; + return 2; + } + } else { + LastError = error; - .loc_0xA4: - li r3, 0x2 - blr - - .loc_0xAC: - li r3, 0x3 - blr - */ + if ((error == 0x31100) || (executing->command == 5)) { + return 2; + } else { + return 3; + } + } } /* @@ -393,8 +241,26 @@ void CategorizeError(void) * Address: ........ * Size: 00009C */ -void CheckCancel(void) +static BOOL CheckCancel(u32 resume) { + DVDCommandBlock* finished; + + if (Canceling) { + ResumeFromHere = resume; + Canceling = FALSE; + + finished = executing; + executing = &DummyCommandBlock; + + finished->state = 10; + if (finished->callback) + (*finished->callback)(-3, finished); + if (CancelCallback) + (CancelCallback)(0, finished); + stateReady(); + return TRUE; + } + return FALSE; // UNUSED FUNCTION } @@ -403,214 +269,84 @@ void CheckCancel(void) * Address: 801FFD54 * Size: 000294 */ -void cbForStateGettingError(void) -{ - /* - .loc_0x0: - mflr r0 - cmplwi r3, 0x10 - stw r0, 0x4(r1) - stwu r1, -0x20(r1) - stw r31, 0x1C(r1) - stw r30, 0x18(r1) - stw r29, 0x14(r1) - stw r28, 0x10(r1) - bne- .loc_0x4C - lwz r4, 0x32B8(r13) - li r0, -0x1 - lis r3, 0x123 - stw r0, 0xC(r4) - addi r3, r3, 0x4568 - bl 0x2370 - bl 0x1B3C - li r3, 0 - bl -0x264 - b .loc_0x274 - - .loc_0x4C: - rlwinm. r0,r3,0,30,30 - beq- .loc_0x7C - lwz r4, 0x32B8(r13) - li r0, -0x1 - lis r3, 0x123 - stw r0, 0xC(r4) - addi r3, r3, 0x4567 - bl 0x2340 - lis r3, 0x8020 - subi r3, r3, 0x4CC - bl -0x12E8 - b .loc_0x274 - - .loc_0x7C: - lis r3, 0xCC00 - lwz r29, 0x6020(r3) - addi r3, r29, 0 - rlwinm r28,r29,0,0,7 - bl -0x140 - addi r31, r3, 0 - cmplwi r31, 0x1 - bne- .loc_0xC0 - lwz r4, 0x32B8(r13) - li r0, -0x1 - addi r3, r29, 0 - stw r0, 0xC(r4) - bl 0x22FC - lis r3, 0x8020 - subi r3, r3, 0x4CC - bl -0x132C - b .loc_0x274 - - .loc_0xC0: - subi r0, r31, 0x2 - cmplwi r0, 0x1 - bgt- .loc_0xD4 - li r4, 0 - b .loc_0x114 - - .loc_0xD4: - subis r0, r28, 0x100 - cmplwi r0, 0 - bne- .loc_0xE8 - li r4, 0x4 - b .loc_0x114 - - .loc_0xE8: - subis r0, r28, 0x200 - cmplwi r0, 0 - bne- .loc_0xFC - li r4, 0x6 - b .loc_0x114 - - .loc_0xFC: - subis r0, r28, 0x300 - cmplwi r0, 0 - bne- .loc_0x110 - li r4, 0x3 - b .loc_0x114 - - .loc_0x110: - li r4, 0x5 - - .loc_0x114: - lwz r0, 0x32D8(r13) - cmplwi r0, 0 - beq- .loc_0x188 - lis r3, 0x803D - stw r4, 0x32E0(r13) - li r0, 0 - lwz r30, 0x32B8(r13) - addi r3, r3, 0x3180 - stw r0, 0x32D8(r13) - li r0, 0xA - stw r3, 0x32B8(r13) - stw r0, 0xC(r30) - lwz r12, 0x28(r30) - cmplwi r12, 0 - beq- .loc_0x160 - mtlr r12 - addi r4, r30, 0 - li r3, -0x3 - blrl - - .loc_0x160: - lwz r12, 0x32DC(r13) - cmplwi r12, 0 - beq- .loc_0x17C - mtlr r12 - addi r4, r30, 0 - li r3, 0 - blrl - - .loc_0x17C: - bl 0xA58 - li r0, 0x1 - b .loc_0x18C - - .loc_0x188: - li r0, 0 - - .loc_0x18C: - cmpwi r0, 0 - bne- .loc_0x274 - cmplwi r31, 0x2 - bne- .loc_0x1AC - mr r3, r29 - bl 0x2208 - bl 0x1F0 - b .loc_0x274 - - .loc_0x1AC: - cmplwi r31, 0x3 - bne- .loc_0x1F0 - rlwinm r3,r29,0,8,31 - subis r0, r3, 0x3 - cmplwi r0, 0x1100 - bne- .loc_0x1DC - lwz r5, 0x32B8(r13) - lis r3, 0x8020 - subi r4, r3, 0x18 - lwz r3, 0x10(r5) - bl -0x15AC - b .loc_0x274 - - .loc_0x1DC: - lwz r12, 0x32FC(r13) - lwz r3, 0x32B8(r13) - mtlr r12 - blrl - b .loc_0x274 - - .loc_0x1F0: - subis r0, r28, 0x100 - cmplwi r0, 0 - bne- .loc_0x210 - lwz r3, 0x32B8(r13) - li r0, 0x5 - stw r0, 0xC(r3) - bl 0x8C0 - b .loc_0x274 - - .loc_0x210: - subis r0, r28, 0x200 - cmplwi r0, 0 - bne- .loc_0x230 - lwz r3, 0x32B8(r13) - li r0, 0x3 - stw r0, 0xC(r3) - bl 0x734 - b .loc_0x274 - - .loc_0x230: - subis r0, r28, 0x300 - cmplwi r0, 0 - bne- .loc_0x250 - lwz r3, 0x32B8(r13) - li r0, 0x4 - stw r0, 0xC(r3) - bl 0x880 - b .loc_0x274 - - .loc_0x250: - lwz r4, 0x32B8(r13) - li r0, -0x1 - lis r3, 0x123 - stw r0, 0xC(r4) - addi r3, r3, 0x4567 - bl 0x2144 - lis r3, 0x8020 - subi r3, r3, 0x4CC - bl -0x14E4 - - .loc_0x274: - lwz r0, 0x24(r1) - lwz r31, 0x1C(r1) - lwz r30, 0x18(r1) - mtlr r0 - lwz r29, 0x14(r1) - lwz r28, 0x10(r1) - addi r1, r1, 0x20 - blr - */ +static void cbForStateGettingError(u32 intType) +{ + u32 error; + u32 status; + u32 errorCategory; + u32 resume; + + if (intType == 16) { + executing->state = -1; + stateTimeout(); + return; + } + + if (intType & 2) { + executing->state = -1; + stateError(0x1234567); + return; + } + + error = __DIRegs[DI_MM_BUF]; + status = error & 0xff000000; + + errorCategory = CategorizeError(error); + + if (errorCategory == 1) { + executing->state = -1; + stateError(error); + return; + } + + if ((errorCategory == 2) || (errorCategory == 3)) { + resume = 0; + } else { + if (status == 0x01000000) + resume = 4; + else if (status == 0x02000000) + resume = 6; + else if (status == 0x03000000) + resume = 3; + else + resume = 5; + } + + if (CheckCancel(resume)) + return; + + if (errorCategory == 2) { + __DVDStoreErrorCode(error); + stateGoToRetry(); + return; + } + + if (errorCategory == 3) { + if ((error & 0x00ffffff) == 0x00031100) { + DVDLowSeek(executing->offset, cbForUnrecoveredError); + } else { + LastState(executing); + } + return; + } + + if (status == 0x01000000) { + executing->state = 5; + stateMotorStopped(); + return; + } else if (status == 0x02000000) { + executing->state = 3; + stateCoverClosed(); + return; + } else if (status == 0x03000000) { + executing->state = 4; + stateMotorStopped(); + return; + } else { + executing->state = -1; + stateError(0x1234567); + return; + } } /* @@ -618,52 +354,48 @@ void cbForStateGettingError(void) * Address: 801FFFE8 * Size: 000068 */ -void cbForUnrecoveredError(void) +static void cbForUnrecoveredError(u32 p1) { - /* - .loc_0x0: - mflr r0 - cmplwi r3, 0x10 - stw r0, 0x4(r1) - stwu r1, -0x8(r1) - bne- .loc_0x3C - lwz r4, 0x32B8(r13) - li r0, -0x1 - lis r3, 0x123 - stw r0, 0xC(r4) - addi r3, r3, 0x4568 - bl 0x20EC - bl 0x18B8 - li r3, 0 - bl -0x4E8 - b .loc_0x58 - - .loc_0x3C: - rlwinm. r0,r3,0,31,31 - beq- .loc_0x4C - bl 0xBC - b .loc_0x58 + if (p1 == 16) { + executing->state = -1; + stateTimeout(); + return; + } - .loc_0x4C: - lis r3, 0x8020 - addi r3, r3, 0x50 - bl -0x14D0 + if (p1 & 1) { + stateGoToRetry(); + return; + } - .loc_0x58: - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ + DVDLowRequestError(cbForUnrecoveredErrorRetry); } +#pragma dont_inline on /* * --INFO-- * Address: 80200050 * Size: 000098 */ -void cbForUnrecoveredErrorRetry(void) +void cbForUnrecoveredErrorRetry(u32 p1) { + if (p1 == 0x10) { + executing->state = -1; + __DVDStoreErrorCode(0x1234568); + DVDReset(); + cbForStateError(0); + return; + } + + executing->state = -1; + + if ((p1 & 2) != 0) { + __DVDStoreErrorCode(0x1234567); + DVDLowStopMotor(cbForStateError); + return; + } + + __DVDStoreErrorCode(__DIRegs[DI_MM_BUF]); + DVDLowStopMotor(cbForStateError); /* .loc_0x0: mflr r0 @@ -712,143 +444,44 @@ void cbForUnrecoveredErrorRetry(void) blr */ } +#pragma dont_inline reset /* * --INFO-- * Address: 802000E8 * Size: 000028 */ -void stateGoToRetry(void) -{ - /* - .loc_0x0: - mflr r0 - lis r3, 0x8020 - stw r0, 0x4(r1) - addi r3, r3, 0x110 - stwu r1, -0x8(r1) - bl -0x161C - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ -} +void stateGoToRetry() { DVDLowStopMotor(cbForStateGoToRetry); } /* * --INFO-- * Address: 80200110 * Size: 000158 */ -void cbForStateGoToRetry(void) +void cbForStateGoToRetry(u32 p1) { - /* - .loc_0x0: - mflr r0 - cmplwi r3, 0x10 - stw r0, 0x4(r1) - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - bne- .loc_0x40 - lwz r4, 0x32B8(r13) - li r0, -0x1 - lis r3, 0x123 - stw r0, 0xC(r4) - addi r3, r3, 0x4568 - bl 0x1FC0 - bl 0x178C - li r3, 0 - bl -0x614 - b .loc_0x144 + if (p1 == 16) { + executing->state = -1; + stateTimeout(); + return; + } - .loc_0x40: - rlwinm. r0,r3,0,30,30 - beq- .loc_0x70 - lwz r4, 0x32B8(r13) - li r0, -0x1 - lis r3, 0x123 - stw r0, 0xC(r4) - addi r3, r3, 0x4567 - bl 0x1F90 - lis r3, 0x8020 - subi r3, r3, 0x4CC - bl -0x1698 - b .loc_0x144 - - .loc_0x70: - li r0, 0 - stw r0, 0x32EC(r13) - lwz r0, 0x32D4(r13) - cmplwi r0, 0x4 - beq- .loc_0xA8 - lwz r0, 0x32D4(r13) - cmplwi r0, 0x5 - beq- .loc_0xA8 - lwz r0, 0x32D4(r13) - cmplwi r0, 0xD - beq- .loc_0xA8 - lwz r0, 0x32D4(r13) - cmplwi r0, 0xF - bne- .loc_0xB0 - - .loc_0xA8: - li r0, 0x1 - stw r0, 0x32F0(r13) - - .loc_0xB0: - lwz r0, 0x32D8(r13) - cmplwi r0, 0 - beq- .loc_0x128 - li r0, 0x2 - lwz r31, 0x32B8(r13) - lis r3, 0x803D - stw r0, 0x32E0(r13) - addi r0, r3, 0x3180 - li r3, 0 - stw r0, 0x32B8(r13) - li r0, 0xA - stw r3, 0x32D8(r13) - stw r0, 0xC(r31) - lwz r12, 0x28(r31) - cmplwi r12, 0 - beq- .loc_0x100 - mtlr r12 - addi r4, r31, 0 - li r3, -0x3 - blrl - - .loc_0x100: - lwz r12, 0x32DC(r13) - cmplwi r12, 0 - beq- .loc_0x11C - mtlr r12 - addi r4, r31, 0 - li r3, 0 - blrl + if (p1 & 2) { + executing->state = -1; + stateError(0x1234567); + return; + } - .loc_0x11C: - bl 0x6FC - li r0, 0x1 - b .loc_0x12C + NumInternalRetry = 0; - .loc_0x128: - li r0, 0 + if ((CurrCommand == 4) || (CurrCommand == 5) || (CurrCommand == 13) || (CurrCommand == 15)) { + ResetRequired = TRUE; + } - .loc_0x12C: - cmpwi r0, 0 - bne- .loc_0x144 - lwz r3, 0x32B8(r13) - li r0, 0xB - stw r0, 0xC(r3) - bl 0x5CC - - .loc_0x144: - lwz r0, 0x1C(r1) - lwz r31, 0x14(r1) - addi r1, r1, 0x18 - mtlr r0 - blr - */ + if (!CheckCancel(2)) { + executing->state = 11; + stateMotorStopped(); + } } /* @@ -856,80 +489,29 @@ void cbForStateGoToRetry(void) * Address: 80200268 * Size: 0000E4 */ -void stateCheckID(void) -{ - /* - .loc_0x0: - mflr r0 - lis r3, 0x803D - stw r0, 0x4(r1) - stwu r1, -0x10(r1) - stw r31, 0xC(r1) - addi r31, r3, 0x3100 - lwz r0, 0x32D4(r13) - cmpwi r0, 0x3 - beq- .loc_0x28 - b .loc_0x94 - - .loc_0x28: - lwz r4, 0x32B8(r13) - addi r3, r31, 0 - li r5, 0x1C - lwz r4, 0x24(r4) - bl 0x15ECC - cmpwi r3, 0 - beq- .loc_0x54 - lis r3, 0x8020 - addi r3, r3, 0x3B8 - bl -0x17D4 - b .loc_0xD0 - - .loc_0x54: - lwz r3, 0x32BC(r13) - addi r4, r31, 0 - li r5, 0x20 - bl -0x1FCEB4 - lwz r4, 0x32B8(r13) - li r0, 0x1 - addi r3, r31, 0 - stw r0, 0xC(r4) - li r4, 0x20 - bl -0x9728 - lis r4, 0x8020 - lwz r3, 0x32B8(r13) - addi r0, r4, 0x380 - stw r0, 0x32FC(r13) - bl 0x8C - b .loc_0xD0 - - .loc_0x94: - lwz r4, 0x32BC(r13) - addi r3, r31, 0 - li r5, 0x20 - bl 0x15E64 - cmpwi r3, 0 - beq- .loc_0xBC - lis r3, 0x8020 - addi r3, r3, 0x3B8 - bl -0x183C - b .loc_0xD0 - - .loc_0xBC: - lis r4, 0x8020 - lwz r3, 0x32B8(r13) - addi r0, r4, 0x34C - stw r0, 0x32FC(r13) - bl .loc_0xE4 - - .loc_0xD0: - lwz r0, 0x14(r1) - lwz r31, 0xC(r1) - addi r1, r1, 0x10 - mtlr r0 - blr - - .loc_0xE4: - */ +static void stateCheckID() +{ + switch (CurrCommand) { + case 3: + if (memcmp(tmpBuffer, executing->id, 0x1C) != FALSE) { + DVDLowStopMotor(cbForStateCheckID1); + } else { + memcpy(currID, tmpBuffer, sizeof(DVDDiskID)); + executing->state = 1; + DCInvalidateRange(tmpBuffer, sizeof(DVDBB2)); + LastState = stateCheckID2; + stateCheckID2(executing); + } + break; + default: + if (memcmp(tmpBuffer, currID, sizeof(DVDDiskID)) != 0) { + DVDLowStopMotor(cbForStateCheckID1); + } else { + LastState = stateCheckID3; + stateCheckID3(executing); + } + break; + } } /* @@ -937,143 +519,40 @@ void stateCheckID(void) * Address: 8020034C * Size: 000034 */ -void stateCheckID3(void) -{ - /* - .loc_0x0: - mflr r0 - lis r3, 0x8020 - stw r0, 0x4(r1) - addi r5, r3, 0x570 - stwu r1, -0x8(r1) - lwz r4, 0x32BC(r13) - lbz r3, 0x8(r4) - li r4, 0xA - bl -0x15B4 - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ -} +static void stateCheckID3(DVDCommandBlock* cmdBlock) { DVDLowAudioBufferConfig(currID->streaming, 10, cbForStateCheckID3); } /* * --INFO-- * Address: 80200380 * Size: 000038 */ -void stateCheckID2(void) -{ - /* - .loc_0x0: - mflr r0 - lis r3, 0x803D - stw r0, 0x4(r1) - lis r4, 0x8020 - addi r6, r4, 0x4CC - stwu r1, -0x8(r1) - addi r3, r3, 0x3100 - li r4, 0x20 - li r5, 0x420 - bl -0x1CC0 - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ -} +static void stateCheckID2(DVDCommandBlock* block) { DVDLowRead(tmpBuffer, OSRoundUp32B(sizeof(DVDBB2)), 0x420, cbForStateCheckID2); } /* * --INFO-- * Address: 802003B8 * Size: 000114 */ -void cbForStateCheckID1(void) +void cbForStateCheckID1(u32 p1) { - /* - .loc_0x0: - mflr r0 - cmplwi r3, 0x10 - stw r0, 0x4(r1) - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - bne- .loc_0x40 - lwz r4, 0x32B8(r13) - li r0, -0x1 - lis r3, 0x123 - stw r0, 0xC(r4) - addi r3, r3, 0x4568 - bl 0x1D18 - bl 0x14E4 - li r3, 0 - bl -0x8BC - b .loc_0x100 + if (p1 == 16) { + executing->state = -1; + stateTimeout(); + return; + } - .loc_0x40: - rlwinm. r0,r3,0,30,30 - beq- .loc_0x70 - lwz r4, 0x32B8(r13) - li r0, -0x1 - lis r3, 0x123 - stw r0, 0xC(r4) - addi r3, r3, 0x4567 - bl 0x1CE8 - lis r3, 0x8020 - subi r3, r3, 0x4CC - bl -0x1940 - b .loc_0x100 - - .loc_0x70: - li r4, 0 - stw r4, 0x32EC(r13) - lwz r0, 0x32D8(r13) - cmplwi r0, 0 - beq- .loc_0xE8 - li r0, 0x1 - lwz r31, 0x32B8(r13) - stw r0, 0x32E0(r13) - lis r3, 0x803D - addi r3, r3, 0x3180 - stw r4, 0x32D8(r13) - li r0, 0xA - stw r3, 0x32B8(r13) - stw r0, 0xC(r31) - lwz r12, 0x28(r31) - cmplwi r12, 0 - beq- .loc_0xC4 - mtlr r12 - addi r4, r31, 0 - li r3, -0x3 - blrl - - .loc_0xC4: - lwz r12, 0x32DC(r13) - cmplwi r12, 0 - beq- .loc_0xE0 - mtlr r12 - addi r4, r31, 0 - li r3, 0 - blrl + if (p1 & 2) { + executing->state = -1; + stateError(0x1234567); + return; + } - .loc_0xE0: - bl 0x490 - li r4, 0x1 + NumInternalRetry = 0; - .loc_0xE8: - cmpwi r4, 0 - bne- .loc_0x100 - lwz r3, 0x32B8(r13) - li r0, 0x6 - stw r0, 0xC(r3) - bl 0x368 - - .loc_0x100: - lwz r0, 0x1C(r1) - lwz r31, 0x14(r1) - addi r1, r1, 0x18 - mtlr r0 - blr - */ + if (!CheckCancel(1)) { + executing->state = 6; + stateMotorStopped(); + } } /* @@ -1081,58 +560,21 @@ void cbForStateCheckID1(void) * Address: 802004CC * Size: 0000A4 */ -void cbForStateCheckID2(void) +void cbForStateCheckID2(u32 p1) { - /* - .loc_0x0: - mflr r0 - cmplwi r3, 0x10 - stw r0, 0x4(r1) - stwu r1, -0x8(r1) - bne- .loc_0x3C - lwz r4, 0x32B8(r13) - li r0, -0x1 - lis r3, 0x123 - stw r0, 0xC(r4) - addi r3, r3, 0x4568 - bl 0x1C08 - bl 0x13D4 - li r3, 0 - bl -0x9CC - b .loc_0x94 - - .loc_0x3C: - rlwinm. r0,r3,0,31,31 - beq- .loc_0x88 - lis r3, 0x8020 - lwz r7, 0x32C0(r13) - subi r0, r3, 0x5A0 - li r4, 0 - stw r0, 0x32FC(r13) - lis r3, 0x803D - addi r5, r3, 0x3100 - stw r4, 0x32EC(r13) - lis r3, 0x8020 - lwz r4, 0x8(r5) - subi r6, r3, 0x54C - lwz r3, 0x38(r7) - addi r0, r4, 0x1F - lwz r5, 0x4(r5) - rlwinm r4,r0,0,0,26 - bl -0x1E68 - b .loc_0x94 + if (p1 == 16) { + executing->state = -1; + stateTimeout(); + return; + } - .loc_0x88: - lis r3, 0x8020 - subi r3, r3, 0x2AC - bl -0x19F0 + if (p1 & 1) { + NumInternalRetry = 0; + stateReadingFST(); - .loc_0x94: - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ + } else { + stateGettingError(); + } } /* @@ -1140,86 +582,25 @@ void cbForStateCheckID2(void) * Address: 80200570 * Size: 0000FC */ -void cbForStateCheckID3(void) +static void cbForStateCheckID3(u32 p1) { - /* - .loc_0x0: - mflr r0 - cmplwi r3, 0x10 - stw r0, 0x4(r1) - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - bne- .loc_0x40 - lwz r4, 0x32B8(r13) - li r0, -0x1 - lis r3, 0x123 - stw r0, 0xC(r4) - addi r3, r3, 0x4568 - bl 0x1B60 - bl 0x132C - li r3, 0 - bl -0xA74 - b .loc_0xE8 - - .loc_0x40: - rlwinm. r0,r3,0,31,31 - beq- .loc_0xDC - li r4, 0 - stw r4, 0x32EC(r13) - lwz r0, 0x32D8(r13) - cmplwi r0, 0 - beq- .loc_0xBC - stw r4, 0x32E0(r13) - lis r3, 0x803D - lwz r31, 0x32B8(r13) - addi r3, r3, 0x3180 - stw r4, 0x32D8(r13) - li r0, 0xA - stw r3, 0x32B8(r13) - stw r0, 0xC(r31) - lwz r12, 0x28(r31) - cmplwi r12, 0 - beq- .loc_0x98 - mtlr r12 - addi r4, r31, 0 - li r3, -0x3 - blrl - - .loc_0x98: - lwz r12, 0x32DC(r13) - cmplwi r12, 0 - beq- .loc_0xB4 - mtlr r12 - addi r4, r31, 0 - li r3, 0 - blrl - - .loc_0xB4: - bl 0x304 - li r4, 0x1 + if (p1 == 16) { + executing->state = -1; + stateTimeout(); + return; + } - .loc_0xBC: - cmpwi r4, 0 - bne- .loc_0xE8 - lwz r3, 0x32B8(r13) - li r0, 0x1 - stw r0, 0xC(r3) - lwz r3, 0x32B8(r13) - bl 0x52C - b .loc_0xE8 + if (p1 & 1) { + NumInternalRetry = 0; - .loc_0xDC: - lis r3, 0x8020 - subi r3, r3, 0x2AC - bl -0x1AE8 + if (!CheckCancel(0)) { + executing->state = 1; + stateBusy(executing); + } - .loc_0xE8: - lwz r0, 0x1C(r1) - lwz r31, 0x14(r1) - addi r1, r1, 0x18 - mtlr r0 - blr - */ + } else { + stateGettingError(); + } } /* @@ -1227,28 +608,12 @@ void cbForStateCheckID3(void) * Address: 8020066C * Size: 000044 */ -void AlarmHandler(void) +static void AlarmHandler(OSAlarm* alarm, OSContext* context) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x8(r1) - bl 0x1254 - lis r3, 0x803D - addi r3, r3, 0x3100 - li r4, 0x20 - bl -0x9AD0 - lis r4, 0x8020 - lwz r3, 0x32B8(r13) - addi r0, r4, 0x77C - stw r0, 0x32FC(r13) - bl 0xE0 - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ + DVDReset(); + DCInvalidateRange(tmpBuffer, sizeof(DVDDiskID)); + LastState = stateCoverClosed_CMD; + stateCoverClosed_CMD(executing); } /* @@ -1256,72 +621,28 @@ void AlarmHandler(void) * Address: 802006B0 * Size: 0000CC */ -void stateCoverClosed(void) +static void stateCoverClosed() { - /* - .loc_0x0: - mflr r0 - lis r3, 0x803D - stw r0, 0x4(r1) - stwu r1, -0x10(r1) - stw r31, 0xC(r1) - addi r31, r3, 0x3100 - lwz r0, 0x32D4(r13) - cmpwi r0, 0xD - beq- .loc_0x48 - bge- .loc_0x3C - cmpwi r0, 0x6 - bge- .loc_0x78 - cmpwi r0, 0x4 - bge- .loc_0x48 - b .loc_0x78 - - .loc_0x3C: - cmpwi r0, 0xF - beq- .loc_0x48 - b .loc_0x78 - - .loc_0x48: - bl 0x180C - lwz r4, 0x32B8(r13) - addi r0, r31, 0x80 - stw r0, 0x32B8(r13) - lwz r12, 0x28(r4) - cmplwi r12, 0 - beq- .loc_0x70 - mtlr r12 - li r3, -0x4 - blrl - - .loc_0x70: - bl 0x208 - b .loc_0xB8 - - .loc_0x78: - bl 0x11A4 - addi r3, r31, 0xB0 - bl -0xA624 - lis r3, 0x8000 - lwz r0, 0xF8(r3) - lis r4, 0x1062 - lis r3, 0x8020 - rlwinm r0,r0,30,2,31 - addi r4, r4, 0x4DD3 - mulhwu r0, r4, r0 - rlwinm r0,r0,26,6,31 - mulli r6, r0, 0x47E - addi r7, r3, 0x66C - addi r3, r31, 0xB0 - li r5, 0 - bl -0xA3FC - - .loc_0xB8: - lwz r0, 0x14(r1) - lwz r31, 0xC(r1) - addi r1, r1, 0x10 - mtlr r0 - blr - */ + DVDCommandBlock* cmdBlock; + switch (CurrCommand) { + case 4: + case 5: + case 13: + case 15: + __DVDClearWaitingQueue(); + cmdBlock = executing; + executing = &DummyCommandBlock; + if (cmdBlock->callback) { + (cmdBlock->callback)(-4, cmdBlock); + } + stateReady(); + break; + default: + DVDReset(); + OSCreateAlarm(&ResetAlarm); + OSSetAlarm(&ResetAlarm, OSMillisecondsToTicks(1150), AlarmHandler); + break; + } } /* @@ -1329,69 +650,27 @@ void stateCoverClosed(void) * Address: 8020077C * Size: 000030 */ -void stateCoverClosed_CMD(void) -{ - /* - .loc_0x0: - mflr r0 - lis r3, 0x803D - stw r0, 0x4(r1) - lis r4, 0x8020 - addi r3, r3, 0x3100 - stwu r1, -0x8(r1) - addi r4, r4, 0x7AC - bl -0x1D5C - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ -} +void stateCoverClosed_CMD(DVDCommandBlock* cmdBlock) { DVDLowReadDiskID(tmpBuffer, cbForStateCoverClosed); } /* * --INFO-- * Address: 802007AC * Size: 000070 */ -void cbForStateCoverClosed(void) +void cbForStateCoverClosed(u32 p1) { - /* - .loc_0x0: - mflr r0 - cmplwi r3, 0x10 - stw r0, 0x4(r1) - stwu r1, -0x8(r1) - bne- .loc_0x3C - lwz r4, 0x32B8(r13) - li r0, -0x1 - lis r3, 0x123 - stw r0, 0xC(r4) - addi r3, r3, 0x4568 - bl 0x1928 - bl 0x10F4 - li r3, 0 - bl -0xCAC - b .loc_0x60 + if (p1 == 16) { + executing->state = -1; + stateTimeout(); + return; + } - .loc_0x3C: - rlwinm. r0,r3,0,31,31 - beq- .loc_0x54 - li r0, 0 - stw r0, 0x32EC(r13) - bl -0x590 - b .loc_0x60 - - .loc_0x54: - lis r3, 0x8020 - subi r3, r3, 0x2AC - bl -0x1C9C - - .loc_0x60: - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ + if (p1 & 1) { + NumInternalRetry = 0; + stateCheckID(); + } else { + stateGettingError(); + } } /* @@ -1399,102 +678,18 @@ void cbForStateCoverClosed(void) * Address: 8020081C * Size: 000028 */ -void stateMotorStopped(void) -{ - /* - .loc_0x0: - mflr r0 - lis r3, 0x8020 - stw r0, 0x4(r1) - addi r3, r3, 0x844 - stwu r1, -0x8(r1) - bl -0x1E20 - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ -} +static void stateMotorStopped() { DVDLowWaitCoverClose(cbForStateMotorStopped); } /* * --INFO-- * Address: 80200844 * Size: 0000E4 */ -void cbForStateMotorStopped(void) +void cbForStateMotorStopped(u32 p1) { - /* - .loc_0x0: - mflr r0 - lis r3, 0xCC00 - stw r0, 0x4(r1) - li r0, 0 - lis r4, 0x803D - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - addi r31, r4, 0x3100 - stw r0, 0x6004(r3) - li r0, 0x3 - lwz r3, 0x32B8(r13) - stw r0, 0xC(r3) - lwz r0, 0x32D4(r13) - cmpwi r0, 0xD - beq- .loc_0x60 - bge- .loc_0x54 - cmpwi r0, 0x6 - bge- .loc_0x90 - cmpwi r0, 0x4 - bge- .loc_0x60 - b .loc_0x90 - - .loc_0x54: - cmpwi r0, 0xF - beq- .loc_0x60 - b .loc_0x90 - - .loc_0x60: - bl 0x1660 - lwz r4, 0x32B8(r13) - addi r0, r31, 0x80 - stw r0, 0x32B8(r13) - lwz r12, 0x28(r4) - cmplwi r12, 0 - beq- .loc_0x88 - mtlr r12 - li r3, -0x4 - blrl - - .loc_0x88: - bl .loc_0xE4 - b .loc_0xD0 - - .loc_0x90: - bl 0xFF8 - addi r3, r31, 0xB0 - bl -0xA7D0 - lis r3, 0x8000 - lwz r0, 0xF8(r3) - lis r4, 0x1062 - lis r3, 0x8020 - rlwinm r0,r0,30,2,31 - addi r4, r4, 0x4DD3 - mulhwu r0, r4, r0 - rlwinm r0,r0,26,6,31 - mulli r6, r0, 0x47E - addi r7, r3, 0x66C - addi r3, r31, 0xB0 - li r5, 0 - bl -0xA5A8 - - .loc_0xD0: - lwz r0, 0x1C(r1) - lwz r31, 0x14(r1) - addi r1, r1, 0x18 - mtlr r0 - blr - - .loc_0xE4: - */ + __DIRegs[DI_COVER_STATUS] = 0; + executing->state = 3; + stateCoverClosed(); } /* @@ -1502,848 +697,333 @@ void cbForStateMotorStopped(void) * Address: 80200928 * Size: 000248 */ -void stateReady(void) -{ - /* - .loc_0x0: - mflr r0 - lis r3, 0x803D - stw r0, 0x4(r1) - stwu r1, -0x10(r1) - stw r31, 0xC(r1) - addi r31, r3, 0x3100 - bl 0x1704 - cmpwi r3, 0 - bne- .loc_0x30 - li r0, 0 - stw r0, 0x32B8(r13) - b .loc_0x234 - - .loc_0x30: - lwz r0, 0x32C4(r13) - cmpwi r0, 0 - beq- .loc_0x50 - li r3, 0x1 - li r0, 0 - stw r3, 0x32C8(r13) - stw r0, 0x32B8(r13) - b .loc_0x234 - - .loc_0x50: - bl 0x162C - lwz r0, 0x32D0(r13) - stw r3, 0x32B8(r13) - cmpwi r0, 0 - beq- .loc_0x9C - lwz r3, 0x32B8(r13) - li r4, -0x1 - addi r0, r31, 0x80 - stw r4, 0xC(r3) - lwz r4, 0x32B8(r13) - stw r0, 0x32B8(r13) - lwz r12, 0x28(r4) - cmplwi r12, 0 - beq- .loc_0x94 - mtlr r12 - li r3, -0x1 - blrl - - .loc_0x94: - bl .loc_0x0 - b .loc_0x234 - - .loc_0x9C: - lwz r4, 0x32B8(r13) - lwz r0, 0x8(r4) - stw r0, 0x32D4(r13) - lwz r0, 0x32E0(r13) - cmplwi r0, 0 - beq- .loc_0x224 - lwz r0, 0x32E0(r13) - cmplwi r0, 0x7 - bgt- .loc_0x218 - lis r3, 0x802F - subi r3, r3, 0x779C - rlwinm r0,r0,2,0,29 - lwzx r0, r3, r0 - mtctr r0 - bctr - li r0, 0x6 - lis r3, 0x8020 - stw r0, 0xC(r4) - addi r3, r3, 0x844 - bl -0x2000 - b .loc_0x218 - li r0, 0xB - lis r3, 0x8020 - stw r0, 0xC(r4) - addi r3, r3, 0x844 - bl -0x2018 - b .loc_0x218 - li r0, 0x4 - lis r3, 0x8020 - stw r0, 0xC(r4) - addi r3, r3, 0x844 - bl -0x2030 - b .loc_0x218 - li r0, 0x7 - lis r3, 0x8020 - stw r0, 0xC(r4) - addi r3, r3, 0x844 - bl -0x2048 - b .loc_0x218 - li r0, 0x5 - lis r3, 0x8020 - stw r0, 0xC(r4) - addi r3, r3, 0x844 - bl -0x2060 - b .loc_0x218 - li r0, 0x3 - stw r0, 0xC(r4) - lwz r0, 0x32D4(r13) - cmpwi r0, 0xD - beq- .loc_0x188 - bge- .loc_0x17C - cmpwi r0, 0x6 - bge- .loc_0x1B8 - cmpwi r0, 0x4 - bge- .loc_0x188 - b .loc_0x1B8 - - .loc_0x17C: - cmpwi r0, 0xF - beq- .loc_0x188 - b .loc_0x1B8 - - .loc_0x188: - bl 0x1454 - lwz r4, 0x32B8(r13) - addi r0, r31, 0x80 - stw r0, 0x32B8(r13) - lwz r12, 0x28(r4) - cmplwi r12, 0 - beq- .loc_0x1B0 - mtlr r12 - li r3, -0x4 - blrl - - .loc_0x1B0: - bl .loc_0x0 - b .loc_0x218 - - .loc_0x1B8: - bl 0xDEC - addi r3, r31, 0xB0 - bl -0xA9DC - lis r3, 0x8000 - lwz r0, 0xF8(r3) - lis r4, 0x1062 - lis r3, 0x8020 - rlwinm r0,r0,30,2,31 - addi r4, r4, 0x4DD3 - mulhwu r0, r4, r0 - rlwinm r0,r0,26,6,31 - mulli r6, r0, 0x47E - addi r7, r3, 0x66C - addi r3, r31, 0xB0 - li r5, 0 - bl -0xA7B4 - b .loc_0x218 - li r0, -0x1 - stw r0, 0xC(r4) - lwz r3, 0x32E4(r13) - bl 0x15CC - lis r3, 0x8020 - subi r3, r3, 0x4CC - bl -0x205C - - .loc_0x218: - li r0, 0 - stw r0, 0x32E0(r13) - b .loc_0x234 - - .loc_0x224: - li r0, 0x1 - stw r0, 0xC(r4) - lwz r3, 0x32B8(r13) - bl .loc_0x248 - - .loc_0x234: - lwz r0, 0x14(r1) - lwz r31, 0xC(r1) - addi r1, r1, 0x10 - mtlr r0 - blr - - .loc_0x248: - */ -} - -/* - * --INFO-- - * Address: 80200B70 - * Size: 0002C0 - */ -void stateBusy(void) -{ - /* - .loc_0x0: - mflr r0 - lis r4, 0x8020 - stw r0, 0x4(r1) - addi r0, r4, 0xB70 - mr r7, r3 - stwu r1, -0x8(r1) - stw r0, 0x32FC(r13) - lwz r0, 0x8(r3) - cmplwi r0, 0xF - bgt- .loc_0x2B0 - lis r3, 0x802F - subi r3, r3, 0x777C - rlwinm r0,r0,2,0,29 - lwzx r0, r3, r0 - mtctr r0 - bctr - lis r3, 0xCC00 - lwz r0, 0x6004(r3) - addi r5, r3, 0x6000 - lis r3, 0x8020 - stw r0, 0x4(r5) - li r0, 0x20 - addi r4, r3, 0xE30 - stw r0, 0x1C(r7) - lwz r3, 0x18(r7) - bl -0x2198 - b .loc_0x2B0 - lis r3, 0xCC00 - addi r3, r3, 0x6000 - lwz r0, 0x4(r3) - lis r4, 0x8 - stw r0, 0x4(r3) - lwz r3, 0x20(r7) - lwz r0, 0x14(r7) - sub r0, r0, r3 - cmplw r0, r4 - ble- .loc_0x98 - b .loc_0x9C - - .loc_0x98: - mr r4, r0 - - .loc_0x9C: - stw r4, 0x1C(r7) - lis r3, 0x8020 - addi r6, r3, 0xE30 - lwz r5, 0x20(r7) - lwz r3, 0x18(r7) - lwz r0, 0x10(r7) - add r3, r3, r5 - lwz r4, 0x1C(r7) - add r5, r0, r5 - bl -0x254C - b .loc_0x2B0 - lis r3, 0xCC00 - lwz r0, 0x6004(r3) - addi r5, r3, 0x6000 - lis r3, 0x8020 - stw r0, 0x4(r5) - addi r4, r3, 0xE30 - lwz r3, 0x10(r7) - bl -0x22D8 - b .loc_0x2B0 - lis r3, 0x8020 - addi r3, r3, 0xE30 - bl -0x2184 - b .loc_0x2B0 - lis r3, 0x8020 - addi r3, r3, 0xE30 - bl -0x2194 - b .loc_0x2B0 - lis r3, 0xCC00 - addi r3, r3, 0x6000 - lwz r0, 0x4(r3) - stw r0, 0x4(r3) - lwz r0, 0x32CC(r13) - cmpwi r0, 0 - beq- .loc_0x148 - lwz r5, 0x32B8(r13) - li r0, 0 - lis r3, 0x8020 - stw r0, 0x1C(r5) - addi r4, r3, 0xE30 - li r3, 0 - bl -0x1F84 - b .loc_0x2B0 - - .loc_0x148: - lwz r4, 0x32B8(r13) - li r0, 0x1 - lis r3, 0x8020 - stw r0, 0x1C(r4) - addi r6, r3, 0xE30 - li r3, 0 - lwz r4, 0x14(r7) - lwz r5, 0x10(r7) - bl -0x2044 - b .loc_0x2B0 - lis r3, 0xCC00 - lwz r0, 0x6004(r3) - addi r4, r3, 0x6000 - lis r3, 0x8020 - stw r0, 0x4(r4) - addi r6, r3, 0xE30 - lis r3, 0x1 - li r4, 0 - li r5, 0 - bl -0x2070 - b .loc_0x2B0 - lis r3, 0xCC00 - lwz r0, 0x6004(r3) - addi r4, r3, 0x6000 - lis r3, 0x8020 - stw r0, 0x4(r4) - li r0, 0x1 - addi r6, r3, 0xE30 - stw r0, 0x32CC(r13) - li r3, 0 - li r4, 0 - li r5, 0 - bl -0x20A4 - b .loc_0x2B0 - lis r3, 0xCC00 - lwz r0, 0x6004(r3) - addi r5, r3, 0x6000 - lis r3, 0x8020 - stw r0, 0x4(r5) - addi r4, r3, 0xE30 - li r3, 0 - bl -0x2030 - b .loc_0x2B0 - lis r3, 0xCC00 - lwz r0, 0x6004(r3) - addi r5, r3, 0x6000 - lis r3, 0x8020 - stw r0, 0x4(r5) - addi r4, r3, 0xE30 - lis r3, 0x1 - bl -0x2054 - b .loc_0x2B0 - lis r3, 0xCC00 - lwz r0, 0x6004(r3) - addi r5, r3, 0x6000 - lis r3, 0x8020 - stw r0, 0x4(r5) - addi r4, r3, 0xE30 - lis r3, 0x2 - bl -0x2078 - b .loc_0x2B0 - lis r3, 0xCC00 - lwz r0, 0x6004(r3) - addi r5, r3, 0x6000 - lis r3, 0x8020 - stw r0, 0x4(r5) - addi r4, r3, 0xE30 - lis r3, 0x3 - bl -0x209C - b .loc_0x2B0 - lis r3, 0xCC00 - lwz r0, 0x6004(r3) - addi r4, r3, 0x6000 - lis r3, 0x8020 - stw r0, 0x4(r4) - addi r5, r3, 0xE30 - lwz r3, 0x10(r7) - lwz r4, 0x14(r7) - bl -0x2038 - b .loc_0x2B0 - lis r3, 0xCC00 - lwz r0, 0x6004(r3) - addi r5, r3, 0x6000 - lis r3, 0x8020 - stw r0, 0x4(r5) - li r0, 0x20 - addi r4, r3, 0xE30 - stw r0, 0x1C(r7) - lwz r3, 0x18(r7) - bl -0x2224 - - .loc_0x2B0: - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ -} - -/* - * --INFO-- - * Address: 80200E30 - * Size: 0005D4 - */ -void cbForStateBusy(void) +void stateReady() { - /* - .loc_0x0: - mflr r0 - cmplwi r3, 0x10 - stw r0, 0x4(r1) - lis r4, 0x803D - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - addi r31, r4, 0x3100 - stw r30, 0x10(r1) - bne- .loc_0x4C - lwz r4, 0x32B8(r13) - li r0, -0x1 - lis r3, 0x123 - stw r0, 0xC(r4) - addi r3, r3, 0x4568 - bl 0x1294 - bl 0xA60 - li r3, 0 - bl -0x1340 - b .loc_0x5BC - - .loc_0x4C: - lwz r0, 0x32D4(r13) - cmplwi r0, 0x3 - beq- .loc_0x64 - lwz r0, 0x32D4(r13) - cmplwi r0, 0xF - bne- .loc_0x14C - - .loc_0x64: - rlwinm. r0,r3,0,30,30 - beq- .loc_0x94 - lwz r4, 0x32B8(r13) - li r0, -0x1 - lis r3, 0x123 - stw r0, 0xC(r4) - addi r3, r3, 0x4567 - bl 0x124C - lis r3, 0x8020 - subi r3, r3, 0x4CC - bl -0x23DC - b .loc_0x5BC - - .loc_0x94: - li r0, 0 - stw r0, 0x32EC(r13) - lwz r0, 0x32D4(r13) - cmplwi r0, 0xF - bne- .loc_0xB0 - li r0, 0x1 - stw r0, 0x32F0(r13) - - .loc_0xB0: - lwz r0, 0x32D8(r13) - cmplwi r0, 0 - beq- .loc_0x124 - li r0, 0x7 - lwz r30, 0x32B8(r13) - stw r0, 0x32E0(r13) - addi r3, r31, 0x80 - li r0, 0 - stw r0, 0x32D8(r13) - li r0, 0xA - stw r3, 0x32B8(r13) - stw r0, 0xC(r30) - lwz r12, 0x28(r30) - cmplwi r12, 0 - beq- .loc_0xFC - mtlr r12 - addi r4, r30, 0 - li r3, -0x3 - blrl - - .loc_0xFC: - lwz r12, 0x32DC(r13) - cmplwi r12, 0 - beq- .loc_0x118 - mtlr r12 - addi r4, r30, 0 - li r3, 0 - blrl + DVDCommandBlock* finished; - .loc_0x118: - bl -0x620 - li r0, 0x1 - b .loc_0x128 + if (!__DVDCheckWaitingQueue()) { + executing = (DVDCommandBlock*)nullptr; + return; + } - .loc_0x124: - li r0, 0 + if (PauseFlag) { + PausingFlag = TRUE; + executing = (DVDCommandBlock*)nullptr; + return; + } - .loc_0x128: - cmpwi r0, 0 - bne- .loc_0x5BC - lwz r4, 0x32B8(r13) - li r0, 0x7 - lis r3, 0x8020 - stw r0, 0xC(r4) - addi r3, r3, 0x844 - bl -0x2564 - b .loc_0x5BC - - .loc_0x14C: - lwz r0, 0x32D4(r13) - cmplwi r0, 0x1 - beq- .loc_0x17C - lwz r0, 0x32D4(r13) - cmplwi r0, 0x4 - beq- .loc_0x17C - lwz r0, 0x32D4(r13) - cmplwi r0, 0x5 - beq- .loc_0x17C - lwz r0, 0x32D4(r13) - cmplwi r0, 0xE - bne- .loc_0x1A0 - - .loc_0x17C: - lwz r6, 0x32B8(r13) - lis r4, 0xCC00 - addi r4, r4, 0x6000 - lwz r4, 0x18(r4) - lwz r0, 0x1C(r6) - lwz r5, 0x20(r6) - sub r0, r0, r4 - add r0, r5, r0 - stw r0, 0x20(r6) - - .loc_0x1A0: - rlwinm. r0,r3,0,28,28 - beq- .loc_0x204 - lwz r30, 0x32B8(r13) - addi r3, r31, 0x80 - li r0, 0 - stw r0, 0x32D8(r13) - li r0, 0xA - stw r3, 0x32B8(r13) - stw r0, 0xC(r30) - lwz r12, 0x28(r30) - cmplwi r12, 0 - beq- .loc_0x1E0 - mtlr r12 - addi r4, r30, 0 - li r3, -0x3 - blrl - - .loc_0x1E0: - lwz r12, 0x32DC(r13) - cmplwi r12, 0 - beq- .loc_0x1FC - mtlr r12 - addi r4, r30, 0 - li r3, 0 - blrl - - .loc_0x1FC: - bl -0x704 - b .loc_0x5BC - - .loc_0x204: - rlwinm. r0,r3,0,31,31 - beq- .loc_0x48C - li r4, 0 - stw r4, 0x32EC(r13) - lwz r0, 0x32D8(r13) - cmplwi r0, 0 - beq- .loc_0x27C - stw r4, 0x32E0(r13) - addi r3, r31, 0x80 - lwz r30, 0x32B8(r13) - li r0, 0xA - stw r4, 0x32D8(r13) - stw r3, 0x32B8(r13) - stw r0, 0xC(r30) - lwz r12, 0x28(r30) - cmplwi r12, 0 - beq- .loc_0x258 - mtlr r12 - addi r4, r30, 0 - li r3, -0x3 - blrl - - .loc_0x258: - lwz r12, 0x32DC(r13) - cmplwi r12, 0 - beq- .loc_0x274 - mtlr r12 - addi r4, r30, 0 - li r3, 0 - blrl - - .loc_0x274: - bl -0x77C - li r4, 0x1 - - .loc_0x27C: - cmpwi r4, 0 - bne- .loc_0x5BC - lwz r0, 0x32D4(r13) - cmplwi r0, 0x1 - beq- .loc_0x2B4 - lwz r0, 0x32D4(r13) - cmplwi r0, 0x4 - beq- .loc_0x2B4 - lwz r0, 0x32D4(r13) - cmplwi r0, 0x5 - beq- .loc_0x2B4 - lwz r0, 0x32D4(r13) - cmplwi r0, 0xE - bne- .loc_0x304 - - .loc_0x2B4: - lwz r3, 0x32B8(r13) - lwz r4, 0x20(r3) - lwz r0, 0x14(r3) - cmplw r4, r0 - beq- .loc_0x2D0 - bl -0x588 - b .loc_0x5BC - - .loc_0x2D0: - addi r0, r31, 0x80 - stw r0, 0x32B8(r13) - li r0, 0 - addi r4, r3, 0 - stw r0, 0xC(r3) - lwz r12, 0x28(r3) - cmplwi r12, 0 - beq- .loc_0x2FC - lwz r3, 0x20(r4) - mtlr r12 - blrl - - .loc_0x2FC: - bl -0x804 - b .loc_0x5BC - - .loc_0x304: - lwz r0, 0x32D4(r13) - cmplwi r0, 0x9 - beq- .loc_0x334 - lwz r0, 0x32D4(r13) - cmplwi r0, 0xA - beq- .loc_0x334 - lwz r0, 0x32D4(r13) - cmplwi r0, 0xB - beq- .loc_0x334 - lwz r0, 0x32D4(r13) - cmplwi r0, 0xC - bne- .loc_0x398 - - .loc_0x334: - lwz r0, 0x32D4(r13) - cmplwi r0, 0xB - beq- .loc_0x34C - lwz r0, 0x32D4(r13) - cmplwi r0, 0xA - bne- .loc_0x35C - - .loc_0x34C: - lis r3, 0xCC00 - lwz r0, 0x6020(r3) - rlwinm r3,r0,2,0,29 - b .loc_0x368 + executing = __DVDPopWaitingQueue(); - .loc_0x35C: - lis r3, 0xCC00 - addi r3, r3, 0x6000 - lwz r3, 0x20(r3) + if (FatalErrorFlag) { + executing->state = -1; + finished = executing; + executing = &DummyCommandBlock; + if (finished->callback) { + (finished->callback)(-1, finished); + } + stateReady(); + return; + } - .loc_0x368: - lwz r4, 0x32B8(r13) - addi r5, r31, 0x80 - li r0, 0 - stw r5, 0x32B8(r13) - stw r0, 0xC(r4) - lwz r12, 0x28(r4) - cmplwi r12, 0 - beq- .loc_0x390 - mtlr r12 - blrl - - .loc_0x390: - bl -0x898 - b .loc_0x5BC - - .loc_0x398: - lwz r0, 0x32D4(r13) - cmplwi r0, 0x6 - bne- .loc_0x458 - lwz r4, 0x32B8(r13) - addi r5, r4, 0x1C - lwz r0, 0x1C(r4) - cmplwi r0, 0 - bne- .loc_0x428 - lis r3, 0xCC00 - lwz r0, 0x6020(r3) - rlwinm. r0,r0,0,31,31 - beq- .loc_0x3F8 - addi r0, r31, 0x80 - stw r0, 0x32B8(r13) - li r0, 0x9 - stw r0, 0xC(r4) - lwz r12, 0x28(r4) - cmplwi r12, 0 - beq- .loc_0x3F0 - mtlr r12 - li r3, -0x2 - blrl - - .loc_0x3F0: - bl -0x8F8 - b .loc_0x5BC - - .loc_0x3F8: - li r0, 0 - stw r0, 0x32CC(r13) - li r0, 0x1 - lis r3, 0x8020 - stw r0, 0x0(r5) - addi r6, r3, 0xE30 - li r3, 0 - lwz r5, 0x32B8(r13) - lwz r4, 0x14(r5) - lwz r5, 0x10(r5) - bl -0x25BC - b .loc_0x5BC - - .loc_0x428: - addi r0, r31, 0x80 - stw r0, 0x32B8(r13) - li r0, 0 - stw r0, 0xC(r4) - lwz r12, 0x28(r4) - cmplwi r12, 0 - beq- .loc_0x450 - mtlr r12 - li r3, 0 - blrl + CurrCommand = executing->command; - .loc_0x450: - bl -0x958 - b .loc_0x5BC + if (ResumeFromHere) { + switch (ResumeFromHere) { + case 1: + executing->state = 6; + stateMotorStopped(); + break; - .loc_0x458: - lwz r4, 0x32B8(r13) - addi r3, r31, 0x80 - li r0, 0 - stw r3, 0x32B8(r13) - stw r0, 0xC(r4) - lwz r12, 0x28(r4) - cmplwi r12, 0 - beq- .loc_0x484 - mtlr r12 - li r3, 0 - blrl + case 2: + executing->state = 11; + stateMotorStopped(); + break; - .loc_0x484: - bl -0x98C - b .loc_0x5BC + case 3: + executing->state = 4; + stateMotorStopped(); + break; - .loc_0x48C: - lwz r0, 0x32D4(r13) - cmplwi r0, 0xE - bne- .loc_0x4C0 - lwz r4, 0x32B8(r13) - li r0, -0x1 - lis r3, 0x123 - stw r0, 0xC(r4) - addi r3, r3, 0x4567 - bl 0xE20 - lis r3, 0x8020 - subi r3, r3, 0x4CC - bl -0x2808 - b .loc_0x5BC - - .loc_0x4C0: - lwz r0, 0x32D4(r13) - cmplwi r0, 0x1 - beq- .loc_0x4F0 - lwz r0, 0x32D4(r13) - cmplwi r0, 0x4 - beq- .loc_0x4F0 - lwz r0, 0x32D4(r13) - cmplwi r0, 0x5 - beq- .loc_0x4F0 - lwz r0, 0x32D4(r13) - cmplwi r0, 0xE - bne- .loc_0x5B0 - - .loc_0x4F0: - lwz r30, 0x32B8(r13) - lwz r3, 0x20(r30) - lwz r0, 0x14(r30) - cmplw r3, r0 - bne- .loc_0x5B0 - lwz r0, 0x32D8(r13) - cmplwi r0, 0 - beq- .loc_0x570 - li r4, 0 - stw r4, 0x32E0(r13) - addi r3, r31, 0x80 - li r0, 0xA - stw r4, 0x32D8(r13) - stw r3, 0x32B8(r13) - stw r0, 0xC(r30) - lwz r12, 0x28(r30) - cmplwi r12, 0 - beq- .loc_0x548 - mtlr r12 - addi r4, r30, 0 - li r3, -0x3 - blrl - - .loc_0x548: - lwz r12, 0x32DC(r13) - cmplwi r12, 0 - beq- .loc_0x564 - mtlr r12 - addi r4, r30, 0 - li r3, 0 - blrl + case 7: + executing->state = 7; + stateMotorStopped(); + break; - .loc_0x564: - bl -0xA6C - li r0, 0x1 - b .loc_0x574 + case 4: + executing->state = 5; + stateMotorStopped(); + break; - .loc_0x570: - li r0, 0 + case 6: + executing->state = 3; + stateCoverClosed(); + break; - .loc_0x574: - cmpwi r0, 0 - bne- .loc_0x5BC - lwz r4, 0x32B8(r13) - addi r3, r31, 0x80 - li r0, 0 - stw r3, 0x32B8(r13) - stw r0, 0xC(r4) - lwz r12, 0x28(r4) - cmplwi r12, 0 - beq- .loc_0x5A8 - lwz r3, 0x20(r4) - mtlr r12 - blrl - - .loc_0x5A8: - bl -0xAB0 - b .loc_0x5BC - - .loc_0x5B0: - lis r3, 0x8020 - subi r3, r3, 0x2AC - bl -0x287C + case 5: + executing->state = -1; + stateError(CancelLastError); + break; + } - .loc_0x5BC: - lwz r0, 0x1C(r1) - lwz r31, 0x14(r1) - lwz r30, 0x10(r1) - mtlr r0 - addi r1, r1, 0x18 - blr - */ + ResumeFromHere = 0; + + } else { + executing->state = 1; + stateBusy(executing); + } +} + +// idk why this file insists on doing this in the opposite order +#define DVDMIN(a, b) (((a) > (b)) ? (b) : (a)) + +/* + * --INFO-- + * Address: 80200B70 + * Size: 0002C0 + */ +void stateBusy(DVDCommandBlock* block) +{ + DVDCommandBlock* finished; + LastState = stateBusy; + switch (block->command) { + case 5: + __DIRegs[DI_COVER_STATUS] = __DIRegs[DI_COVER_STATUS]; + block->currTransferSize = sizeof(DVDDiskID); + DVDLowReadDiskID(block->addr, cbForStateBusy); + break; + case 1: + case 4: + __DIRegs[DI_COVER_STATUS] = __DIRegs[DI_COVER_STATUS]; + block->currTransferSize = DVDMIN(block->length - block->transferredSize, 0x80000); + DVDLowRead((void*)((u8*)block->addr + block->transferredSize), block->currTransferSize, block->offset + block->transferredSize, + cbForStateBusy); + break; + + case 2: + __DIRegs[DI_COVER_STATUS] = __DIRegs[DI_COVER_STATUS]; + DVDLowSeek(block->offset, cbForStateBusy); + break; + + case 3: + DVDLowStopMotor(cbForStateBusy); + break; + + case 15: + DVDLowStopMotor(cbForStateBusy); + break; + + case 6: + __DIRegs[DI_COVER_STATUS] = __DIRegs[DI_COVER_STATUS]; + if (AutoFinishing) { + executing->currTransferSize = 0; + DVDLowRequestAudioStatus(0, cbForStateBusy); + } else { + executing->currTransferSize = 1; + DVDLowAudioStream(0, block->length, block->offset, cbForStateBusy); + } + break; + + case 7: + __DIRegs[DI_COVER_STATUS] = __DIRegs[DI_COVER_STATUS]; + DVDLowAudioStream(0x10000, 0, 0, cbForStateBusy); + break; + + case 8: + __DIRegs[DI_COVER_STATUS] = __DIRegs[DI_COVER_STATUS]; + AutoFinishing = TRUE; + DVDLowAudioStream(0, 0, 0, cbForStateBusy); + break; + + case 9: + __DIRegs[DI_COVER_STATUS] = __DIRegs[DI_COVER_STATUS]; + DVDLowRequestAudioStatus(0, cbForStateBusy); + break; + + case 10: + __DIRegs[DI_COVER_STATUS] = __DIRegs[DI_COVER_STATUS]; + DVDLowRequestAudioStatus(0x10000, cbForStateBusy); + break; + + case 11: + __DIRegs[DI_COVER_STATUS] = __DIRegs[DI_COVER_STATUS]; + DVDLowRequestAudioStatus(0x20000, cbForStateBusy); + break; + + case 12: + __DIRegs[DI_COVER_STATUS] = __DIRegs[DI_COVER_STATUS]; + DVDLowRequestAudioStatus(0x30000, cbForStateBusy); + break; + + case 13: + __DIRegs[DI_COVER_STATUS] = __DIRegs[DI_COVER_STATUS]; + DVDLowAudioBufferConfig(block->offset, block->length, cbForStateBusy); + break; + + case 14: + __DIRegs[DI_COVER_STATUS] = __DIRegs[DI_COVER_STATUS]; + block->currTransferSize = sizeof(DVDDriveInfo); + DVDLowInquiry(block->addr, cbForStateBusy); + break; + } +} + +/* + * --INFO-- + * Address: 80200E30 + * Size: 0005D4 + */ +void cbForStateBusy(u32 p1) +{ + DVDCommandBlock* finished; + + if (p1 == 16) { + executing->state = -1; + stateTimeout(); + return; + } + + if ((CurrCommand == 3) || (CurrCommand == 15)) { + if (p1 & 2) { + executing->state = -1; + stateError(0x1234567); + return; + } + + NumInternalRetry = 0; + + if (CurrCommand == 15) { + ResetRequired = TRUE; + } + + if (CheckCancel(7)) { + return; + } + + executing->state = 7; + stateMotorStopped(); + return; + } + + if ((CurrCommand == 1) || (CurrCommand == 4) || (CurrCommand == 5) || (CurrCommand == 14)) { + executing->transferredSize += executing->currTransferSize - __DIRegs[6]; + } + + if (p1 & 8) { + Canceling = FALSE; + finished = executing; + executing = &DummyCommandBlock; + + finished->state = 10; + if (finished->callback) { + (*finished->callback)(-3, finished); + } + if (CancelCallback) { + (CancelCallback)(0, finished); + } + stateReady(); + + return; + } + + if (p1 & 1) { + NumInternalRetry = 0; + + if (CheckCancel(0)) + return; + + if ((CurrCommand == 1) || (CurrCommand == 4) || (CurrCommand == 5) || (CurrCommand == 14)) { + if (executing->transferredSize != executing->length) { + stateBusy(executing); + return; + } + + finished = executing; + executing = &DummyCommandBlock; + + finished->state = 0; + if (finished->callback) { + (finished->callback)((s32)finished->transferredSize, finished); + } + stateReady(); + } else if ((CurrCommand == 9) || (CurrCommand == 10) || (CurrCommand == 11) || (CurrCommand == 12)) { + s32 result; + + if ((CurrCommand == 11) || (CurrCommand == 10)) { + result = (s32)(__DIRegs[DI_MM_BUF] << 2); + } else { + result = (s32)__DIRegs[DI_MM_BUF]; + } + finished = executing; + executing = &DummyCommandBlock; + + finished->state = 0; + if (finished->callback) { + (finished->callback)(result, finished); + } + stateReady(); + } else if (CurrCommand == 6) { + if (executing->currTransferSize == 0) { + if (__DIRegs[DI_MM_BUF] & 1) { + finished = executing; + executing = &DummyCommandBlock; + + finished->state = 9; + if (finished->callback) { + (finished->callback)(-2, finished); + } + stateReady(); + } else { + AutoFinishing = FALSE; + executing->currTransferSize = 1; + DVDLowAudioStream(0, executing->length, executing->offset, cbForStateBusy); + } + } else { + finished = executing; + executing = &DummyCommandBlock; + + finished->state = 0; + if (finished->callback) { + (finished->callback)(0, finished); + } + stateReady(); + } + } else { + finished = executing; + executing = &DummyCommandBlock; + + finished->state = 0; + if (finished->callback) { + (finished->callback)(0, finished); + } + stateReady(); + } + + } else { + if (CurrCommand == 14) { + executing->state = -1; + stateError(0x01234567); + return; + } + + if ((CurrCommand == 1 || CurrCommand == 4 || CurrCommand == 5 || CurrCommand == 14) + && (executing->transferredSize == executing->length)) { + + if (CheckCancel(0)) { + return; + } + finished = executing; + executing = &DummyCommandBlock; + + finished->state = 0; + if (finished->callback) { + (finished->callback)((s32)finished->transferredSize, finished); + } + stateReady(); + return; + } + + stateGettingError(); + } } /* @@ -2351,64 +1031,27 @@ void cbForStateBusy(void) * Address: 80201404 * Size: 0000BC */ -void issueCommand(void) +static BOOL issueCommand(s32 prio, DVDCommandBlock* block) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x20(r1) - stw r31, 0x1C(r1) - stw r30, 0x18(r1) - addi r30, r4, 0 - stw r29, 0x14(r1) - addi r29, r3, 0 - lwz r0, 0x2A18(r13) - cmpwi r0, 0 - beq- .loc_0x58 - lwz r3, 0x8(r30) - cmplwi r3, 0x1 - beq- .loc_0x4C - subi r0, r3, 0x4 - cmplwi r0, 0x1 - ble- .loc_0x4C - cmplwi r3, 0xE - bne- .loc_0x58 - - .loc_0x4C: - lwz r3, 0x18(r30) - lwz r4, 0x14(r30) - bl -0xA8A0 - - .loc_0x58: - bl -0x84E0 - li r0, 0x2 - stw r0, 0xC(r30) - addi r31, r3, 0 - addi r3, r29, 0 - addi r4, r30, 0 - bl 0xAC8 - lwz r0, 0x32B8(r13) - addi r30, r3, 0 - cmplwi r0, 0 - bne- .loc_0x94 - lwz r0, 0x32C4(r13) - cmpwi r0, 0 - bne- .loc_0x94 - bl -0xB6C - - .loc_0x94: - mr r3, r31 - bl -0x84F8 - lwz r0, 0x24(r1) - mr r3, r30 - lwz r31, 0x1C(r1) - lwz r30, 0x18(r1) - mtlr r0 - lwz r29, 0x14(r1) - addi r1, r1, 0x20 - blr - */ + BOOL level; + BOOL result; + + if (autoInvalidation && (block->command == 1 || block->command == 4 || block->command == 5 || block->command == 14)) { + DCInvalidateRange(block->addr, block->length); + } + + level = OSDisableInterrupts(); + + block->state = 2; + result = __DVDPushWaitingQueue(prio, block); + + if ((executing == (DVDCommandBlock*)nullptr) && (PauseFlag == FALSE)) { + stateReady(); + } + + OSRestoreInterrupts(level); + + return result; } /* @@ -2416,72 +1059,18 @@ void issueCommand(void) * Address: 802014C0 * Size: 0000DC */ -void DVDReadAbsAsyncPrio(void) +BOOL DVDReadAbsAsyncPrio(DVDCommandBlock* block, void* addr, s32 length, s32 offset, DVDCBCallback callback, s32 prio) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - li r0, 0x1 - stwu r1, -0x38(r1) - stw r31, 0x34(r1) - addi r31, r8, 0 - stw r30, 0x30(r1) - stw r29, 0x2C(r1) - addi r29, r3, 0 - stw r0, 0x8(r3) - li r0, 0 - stw r4, 0x18(r3) - stw r5, 0x14(r3) - stw r6, 0x10(r3) - stw r0, 0x20(r3) - stw r7, 0x28(r3) - lwz r0, 0x2A18(r13) - cmpwi r0, 0 - beq- .loc_0x78 - lwz r3, 0x8(r29) - cmplwi r3, 0x1 - beq- .loc_0x6C - subi r0, r3, 0x4 - cmplwi r0, 0x1 - ble- .loc_0x6C - cmplwi r3, 0xE - bne- .loc_0x78 + BOOL idle; + block->command = 1; + block->addr = addr; + block->length = length; + block->offset = offset; + block->transferredSize = 0; + block->callback = callback; - .loc_0x6C: - lwz r3, 0x18(r29) - lwz r4, 0x14(r29) - bl -0xA97C - - .loc_0x78: - bl -0x85BC - li r0, 0x2 - stw r0, 0xC(r29) - addi r30, r3, 0 - addi r3, r31, 0 - addi r4, r29, 0 - bl 0x9EC - lwz r0, 0x32B8(r13) - addi r31, r3, 0 - cmplwi r0, 0 - bne- .loc_0xB4 - lwz r0, 0x32C4(r13) - cmpwi r0, 0 - bne- .loc_0xB4 - bl -0xC48 - - .loc_0xB4: - mr r3, r30 - bl -0x85D4 - lwz r0, 0x3C(r1) - mr r3, r31 - lwz r31, 0x34(r1) - lwz r30, 0x30(r1) - mtlr r0 - lwz r29, 0x2C(r1) - addi r1, r1, 0x38 - blr - */ + idle = issueCommand(prio, block); + return idle; } /* @@ -2499,69 +1088,18 @@ void DVDSeekAbsAsyncPrio(void) * Address: 8020159C * Size: 0000D0 */ -void DVDReadAbsAsyncForBS(void) +BOOL DVDReadAbsAsyncForBS(DVDCommandBlock* block, void* addr, s32 length, s32 offset, DVDCBCallback callback) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - li r0, 0x4 - stwu r1, -0x30(r1) - stw r31, 0x2C(r1) - addi r31, r3, 0 - stw r30, 0x28(r1) - stw r0, 0x8(r3) - li r0, 0 - stw r4, 0x18(r3) - stw r5, 0x14(r3) - stw r6, 0x10(r3) - stw r0, 0x20(r3) - stw r7, 0x28(r3) - lwz r0, 0x2A18(r13) - cmpwi r0, 0 - beq- .loc_0x70 - lwz r3, 0x8(r31) - cmplwi r3, 0x1 - beq- .loc_0x64 - subi r0, r3, 0x4 - cmplwi r0, 0x1 - ble- .loc_0x64 - cmplwi r3, 0xE - bne- .loc_0x70 - - .loc_0x64: - lwz r3, 0x18(r31) - lwz r4, 0x14(r31) - bl -0xAA50 - - .loc_0x70: - bl -0x8690 - li r0, 0x2 - stw r0, 0xC(r31) - addi r30, r3, 0 - addi r4, r31, 0 - li r3, 0x2 - bl 0x918 - lwz r0, 0x32B8(r13) - addi r31, r3, 0 - cmplwi r0, 0 - bne- .loc_0xAC - lwz r0, 0x32C4(r13) - cmpwi r0, 0 - bne- .loc_0xAC - bl -0xD1C - - .loc_0xAC: - mr r3, r30 - bl -0x86A8 - lwz r0, 0x34(r1) - mr r3, r31 - lwz r31, 0x2C(r1) - lwz r30, 0x28(r1) - mtlr r0 - addi r1, r1, 0x30 - blr - */ + BOOL idle; + block->command = 4; + block->addr = addr; + block->length = length; + block->offset = offset; + block->transferredSize = 0; + block->callback = callback; + + idle = issueCommand(2, block); + return idle; } /* @@ -2569,70 +1107,19 @@ void DVDReadAbsAsyncForBS(void) * Address: 8020166C * Size: 0000D4 */ -void DVDReadDiskID(void) +BOOL DVDReadDiskID(DVDCommandBlock* block, DVDDiskID* diskID, DVDCBCallback callback) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - li r0, 0x5 - stwu r1, -0x28(r1) - stw r31, 0x24(r1) - addi r31, r3, 0 - stw r30, 0x20(r1) - stw r0, 0x8(r3) - li r3, 0x20 - li r0, 0 - stw r4, 0x18(r31) - stw r3, 0x14(r31) - stw r0, 0x10(r31) - stw r0, 0x20(r31) - stw r5, 0x28(r31) - lwz r0, 0x2A18(r13) - cmpwi r0, 0 - beq- .loc_0x74 - lwz r3, 0x8(r31) - cmplwi r3, 0x1 - beq- .loc_0x68 - subi r0, r3, 0x4 - cmplwi r0, 0x1 - ble- .loc_0x68 - cmplwi r3, 0xE - bne- .loc_0x74 - - .loc_0x68: - lwz r3, 0x18(r31) - lwz r4, 0x14(r31) - bl -0xAB24 - - .loc_0x74: - bl -0x8764 - li r0, 0x2 - stw r0, 0xC(r31) - addi r30, r3, 0 - addi r4, r31, 0 - li r3, 0x2 - bl 0x844 - lwz r0, 0x32B8(r13) - addi r31, r3, 0 - cmplwi r0, 0 - bne- .loc_0xB0 - lwz r0, 0x32C4(r13) - cmpwi r0, 0 - bne- .loc_0xB0 - bl -0xDF0 - - .loc_0xB0: - mr r3, r30 - bl -0x877C - lwz r0, 0x2C(r1) - mr r3, r31 - lwz r31, 0x24(r1) - lwz r30, 0x20(r1) - mtlr r0 - addi r1, r1, 0x28 - blr - */ + BOOL idle; + block->command = 5; + block->addr = diskID; + block->length = sizeof(DVDDiskID); + + block->offset = 0; + block->transferredSize = 0; + block->callback = callback; + + idle = issueCommand(2, block); + return idle; } /* @@ -2640,66 +1127,16 @@ void DVDReadDiskID(void) * Address: 80201740 * Size: 0000C4 */ -void DVDPrepareStreamAbsAsync(void) +BOOL DVDPrepareStreamAbsAsync(DVDCommandBlock* block, u32 length, u32 offset, DVDCBCallback callback) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - li r0, 0x6 - stwu r1, -0x28(r1) - stw r31, 0x24(r1) - addi r31, r3, 0 - stw r30, 0x20(r1) - stw r0, 0x8(r3) - stw r4, 0x14(r3) - stw r5, 0x10(r3) - stw r6, 0x28(r3) - lwz r0, 0x2A18(r13) - cmpwi r0, 0 - beq- .loc_0x64 - lwz r3, 0x8(r31) - cmplwi r3, 0x1 - beq- .loc_0x58 - subi r0, r3, 0x4 - cmplwi r0, 0x1 - ble- .loc_0x58 - cmplwi r3, 0xE - bne- .loc_0x64 - - .loc_0x58: - lwz r3, 0x18(r31) - lwz r4, 0x14(r31) - bl -0xABE8 - - .loc_0x64: - bl -0x8828 - li r0, 0x2 - stw r0, 0xC(r31) - addi r30, r3, 0 - addi r4, r31, 0 - li r3, 0x1 - bl 0x780 - lwz r0, 0x32B8(r13) - addi r31, r3, 0 - cmplwi r0, 0 - bne- .loc_0xA0 - lwz r0, 0x32C4(r13) - cmpwi r0, 0 - bne- .loc_0xA0 - bl -0xEB4 - - .loc_0xA0: - mr r3, r30 - bl -0x8840 - lwz r0, 0x2C(r1) - mr r3, r31 - lwz r31, 0x24(r1) - lwz r30, 0x20(r1) - mtlr r0 - addi r1, r1, 0x28 - blr - */ + BOOL idle; + block->command = 6; + block->length = length; + block->offset = offset; + block->callback = callback; + + idle = issueCommand(1, block); + return idle; } /* @@ -2707,8 +1144,13 @@ void DVDPrepareStreamAbsAsync(void) * Address: ........ * Size: 0000BC */ -void DVDCancelStreamAsync(void) +BOOL DVDCancelStreamAsync(DVDCommandBlock* block, DVDCBCallback callback) { + BOOL idle; + block->command = 7; + block->callback = callback; + idle = issueCommand(1, block); + return idle; // UNUSED FUNCTION } @@ -2717,63 +1159,34 @@ void DVDCancelStreamAsync(void) * Address: 80201804 * Size: 0000A0 */ -void DVDCancelStream(void) +BOOL DVDCancelStream(DVDCommandBlock* block) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - li r0, 0x7 - stwu r1, -0x20(r1) - stw r31, 0x1C(r1) - stw r30, 0x18(r1) - addi r30, r3, 0 - lis r3, 0x8020 - stw r0, 0x8(r30) - addi r0, r3, 0x18A4 - addi r4, r30, 0 - stw r0, 0x28(r30) - li r3, 0x1 - bl -0x434 - cmpwi r3, 0 - bne- .loc_0x48 - li r3, -0x1 - b .loc_0x88 + BOOL result; + s32 state; + BOOL enabled; + s32 retVal; - .loc_0x48: - bl -0x88D0 - mr r31, r3 + result = DVDCancelStreamAsync(block, cbForCancelStreamSync); - .loc_0x50: - lwz r3, 0xC(r30) - addi r0, r3, 0x1 - cmplwi r0, 0x1 - ble- .loc_0x68 - cmpwi r3, 0xA - bne- .loc_0x70 + if (result == FALSE) { + return -1; + } - .loc_0x68: - lwz r30, 0x20(r30) - b .loc_0x7C + enabled = OSDisableInterrupts(); - .loc_0x70: - addi r3, r13, 0x32B0 - bl -0x4EBC - b .loc_0x50 + while (TRUE) { + state = ((volatile DVDCommandBlock*)block)->state; - .loc_0x7C: - mr r3, r31 - bl -0x88E0 - mr r3, r30 + if (state == 0 || state == -1 || state == 10) { + retVal = (s32)block->transferredSize; + break; + } - .loc_0x88: - lwz r0, 0x24(r1) - lwz r31, 0x1C(r1) - lwz r30, 0x18(r1) - mtlr r0 - addi r1, r1, 0x20 - blr - */ + OSSleepThread(&__DVDThreadQueue); + } + + OSRestoreInterrupts(enabled); + return retVal; } /* @@ -2781,21 +1194,10 @@ void DVDCancelStream(void) * Address: 802018A4 * Size: 000028 */ -void cbForCancelStreamSync(void) +static void cbForCancelStreamSync(s32 result, DVDCommandBlock* block) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x8(r1) - stw r3, 0x20(r4) - addi r3, r13, 0x32B0 - bl -0x4E10 - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ + block->transferredSize = (u32)result; + OSWakeupThread(&__DVDThreadQueue); } /* @@ -2803,7 +1205,7 @@ void cbForCancelStreamSync(void) * Address: ........ * Size: 0000BC */ -void DVDStopStreamAtEndAsync(void) +static BOOL DVDStopStreamAtEndAsync(DVDCommandBlock* block, DVDCBCallback callback) { // UNUSED FUNCTION } @@ -2973,7 +1375,7 @@ void DVDChangeDiskAsyncForBS(void) * Address: ........ * Size: 0000D0 */ -void DVDChangeDiskAsync(void) +BOOL DVDChangeDiskAsync(DVDCommandBlock*, DVDDiskID*, DVDCBCallback) { // UNUSED FUNCTION } @@ -2983,7 +1385,7 @@ void DVDChangeDiskAsync(void) * Address: ........ * Size: 0000C8 */ -void DVDChangeDisk(void) +s32 DVDChangeDisk(DVDCommandBlock*, DVDDiskID*) { // UNUSED FUNCTION } @@ -3003,7 +1405,7 @@ void cbForChangeDiskSync(void) * Address: ........ * Size: 0000D0 */ -void DVDInquiryAsync(void) +BOOL DVDInquiryAsync(DVDCommandBlock* block, DVDDriveInfo* info, DVDCBCallback callback) { // UNUSED FUNCTION } @@ -3033,28 +1435,13 @@ void cbForInquirySync(void) * Address: 802018CC * Size: 000044 */ -void DVDReset(void) +void DVDReset() { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x8(r1) - bl -0x2A84 - lis r3, 0xCC00 - li r0, 0x2A - stw r0, 0x6000(r3) - addi r4, r3, 0x6000 - li r0, 0 - lwz r3, 0x6004(r3) - stw r3, 0x4(r4) - stw r0, 0x32F0(r13) - stw r0, 0x32E0(r13) - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ + DVDLowReset(); + __DIRegs[DI_STATUS] = 42; + __DIRegs[DI_COVER_STATUS] = __DIRegs[DI_COVER_STATUS]; + ResetRequired = FALSE; + ResumeFromHere = 0; } /* @@ -3072,8 +1459,17 @@ void DVDResetRequired(void) * Address: ........ * Size: 00004C */ -void DVDGetCommandBlockStatus(void) -{ +s32 DVDGetCommandBlockStatus(const DVDCommandBlock* block) +{ + int interrupts = OSDisableInterrupts(); + int result; + if (block->state == 3) { + result = 1; + } else { + result = block->state; + } + OSRestoreInterrupts(interrupts); + return result; // UNUSED FUNCTION } @@ -3082,66 +1478,27 @@ void DVDGetCommandBlockStatus(void) * Address: 80201910 * Size: 0000AC */ -void DVDGetDriveStatus(void) +s32 DVDGetDriveStatus() { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - stw r30, 0x10(r1) - bl -0x89A8 - lwz r0, 0x32D0(r13) - addi r30, r3, 0 - cmpwi r0, 0 - beq- .loc_0x30 - li r31, -0x1 - b .loc_0x88 - - .loc_0x30: - lwz r0, 0x32C8(r13) - cmpwi r0, 0 - beq- .loc_0x44 - li r31, 0x8 - b .loc_0x88 - - .loc_0x44: - lwz r31, 0x32B8(r13) - cmplwi r31, 0 - bne- .loc_0x58 - li r31, 0 - b .loc_0x88 - - .loc_0x58: - lis r3, 0x803D - addi r0, r3, 0x3180 - cmplw r31, r0 - bne- .loc_0x70 - li r31, 0 - b .loc_0x88 - - .loc_0x70: - bl -0x8A04 - lwz r31, 0xC(r31) - cmpwi r31, 0x3 - bne- .loc_0x84 - li r31, 0x1 - - .loc_0x84: - bl -0x89F0 - - .loc_0x88: - mr r3, r30 - bl -0x89F8 - lwz r0, 0x1C(r1) - mr r3, r31 - lwz r31, 0x14(r1) - lwz r30, 0x10(r1) - mtlr r0 - addi r1, r1, 0x18 - blr - */ + int interrupts = OSDisableInterrupts(); + int result; + if (FatalErrorFlag != FALSE) { + result = DVD_STATE_FATAL_ERROR; + } else { + if (PausingFlag != FALSE) { + result = DVD_STATE_PAUSING; + } else { + if (executing == nullptr) { + result = DVD_STATE_END; + } else if (executing == &DummyCommandBlock) { + result = DVD_STATE_END; + } else { + result = DVDGetCommandBlockStatus((struct DVDCommandBlock*)executing); + } + } + } + OSRestoreInterrupts(interrupts); + return result; } /* @@ -3149,7 +1506,7 @@ void DVDGetDriveStatus(void) * Address: ........ * Size: 000010 */ -void DVDSetAutoInvalidation(void) +int DVDSetAutoInvalidation(int newValue) { // UNUSED FUNCTION } @@ -3159,9 +1516,15 @@ void DVDSetAutoInvalidation(void) * Address: ........ * Size: 00003C */ -void DVDPause(void) +static void DVDPause() { - // UNUSED FUNCTION + BOOL level; + level = OSDisableInterrupts(); + PauseFlag = TRUE; + if (executing == (DVDCommandBlock*)NULL) { + PausingFlag = TRUE; + } + OSRestoreInterrupts(level); } /* @@ -3169,9 +1532,16 @@ void DVDPause(void) * Address: ........ * Size: 000050 */ -void DVDResume(void) +void DVDResume() { - // UNUSED FUNCTION + BOOL level; + level = OSDisableInterrupts(); + PauseFlag = FALSE; + if (PausingFlag) { + PausingFlag = FALSE; + stateReady(); + } + OSRestoreInterrupts(level); } /* @@ -3179,201 +1549,99 @@ void DVDResume(void) * Address: 802019BC * Size: 000270 */ -void DVDCancelAsync(void) -{ - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x20(r1) - stw r31, 0x1C(r1) - stw r30, 0x18(r1) - addi r30, r4, 0 - stw r29, 0x14(r1) - addi r29, r3, 0 - bl -0x8A60 - lwz r4, 0xC(r29) - addi r31, r3, 0 - addi r0, r4, 0x1 - cmplwi r0, 0xC - bgt- .loc_0x248 - lis r3, 0x802F - subi r3, r3, 0x773C - rlwinm r0,r0,2,0,29 - lwzx r0, r3, r0 - mtctr r0 - bctr - cmplwi r30, 0 - beq- .loc_0x248 - addi r12, r30, 0 - mtlr r12 - addi r4, r29, 0 - li r3, 0 - blrl - b .loc_0x248 - lwz r0, 0x32D8(r13) - cmplwi r0, 0 - beq- .loc_0x8C - mr r3, r31 - bl -0x8A98 - li r3, 0 - b .loc_0x254 - - .loc_0x8C: - li r0, 0x1 - stw r30, 0x32DC(r13) - stw r0, 0x32D8(r13) - lwz r0, 0x8(r29) - cmplwi r0, 0x4 - beq- .loc_0xAC - cmplwi r0, 0x1 - bne- .loc_0x248 - - .loc_0xAC: - bl -0x2B58 - b .loc_0x248 - mr r3, r29 - bl 0x628 - li r0, 0xA - stw r0, 0xC(r29) - lwz r12, 0x28(r29) - cmplwi r12, 0 - beq- .loc_0xE0 - mtlr r12 - addi r4, r29, 0 - li r3, -0x3 - blrl - - .loc_0xE0: - cmplwi r30, 0 - beq- .loc_0x248 - addi r12, r30, 0 - mtlr r12 - addi r4, r29, 0 - li r3, 0 - blrl - b .loc_0x248 - lwz r0, 0x8(r29) - cmpwi r0, 0xD - beq- .loc_0x130 - bge- .loc_0x124 - cmpwi r0, 0x6 - bge- .loc_0x150 - cmpwi r0, 0x4 - bge- .loc_0x130 - b .loc_0x150 - - .loc_0x124: - cmpwi r0, 0xF - beq- .loc_0x130 - b .loc_0x150 - - .loc_0x130: - cmplwi r30, 0 - beq- .loc_0x248 - addi r12, r30, 0 - mtlr r12 - addi r4, r29, 0 - li r3, 0 - blrl - b .loc_0x248 - - .loc_0x150: - lwz r0, 0x32D8(r13) - cmplwi r0, 0 - beq- .loc_0x16C - mr r3, r31 - bl -0x8B78 - li r3, 0 - b .loc_0x254 - - .loc_0x16C: - li r0, 0x1 - stw r30, 0x32DC(r13) - stw r0, 0x32D8(r13) - b .loc_0x248 - bl -0x2C14 - lis r4, 0x8020 - addi r0, r4, 0x844 - cmplw r3, r0 - beq- .loc_0x1A0 - mr r3, r31 - bl -0x8BAC - li r3, 0 - b .loc_0x254 - - .loc_0x1A0: - lwz r0, 0xC(r29) - cmpwi r0, 0x4 - bne- .loc_0x1B4 - li r0, 0x3 - stw r0, 0x32E0(r13) - - .loc_0x1B4: - lwz r0, 0xC(r29) - cmpwi r0, 0x5 - bne- .loc_0x1C8 - li r0, 0x4 - stw r0, 0x32E0(r13) - - .loc_0x1C8: - lwz r0, 0xC(r29) - cmpwi r0, 0x6 - bne- .loc_0x1DC - li r0, 0x1 - stw r0, 0x32E0(r13) - - .loc_0x1DC: - lwz r0, 0xC(r29) - cmpwi r0, 0xB - bne- .loc_0x1F0 - li r0, 0x2 - stw r0, 0x32E0(r13) - - .loc_0x1F0: - lwz r0, 0xC(r29) - cmpwi r0, 0x7 - bne- .loc_0x204 - li r0, 0x7 - stw r0, 0x32E0(r13) - - .loc_0x204: - li r0, 0xA - stw r0, 0xC(r29) - lwz r12, 0x28(r29) - cmplwi r12, 0 - beq- .loc_0x228 - mtlr r12 - addi r4, r29, 0 - li r3, -0x3 - blrl - - .loc_0x228: - cmplwi r30, 0 - beq- .loc_0x244 - addi r12, r30, 0 - mtlr r12 - addi r4, r29, 0 - li r3, 0 - blrl - - .loc_0x244: - bl -0x12D8 - - .loc_0x248: - mr r3, r31 - bl -0x8C64 - li r3, 0x1 - - .loc_0x254: - lwz r0, 0x24(r1) - lwz r31, 0x1C(r1) - lwz r30, 0x18(r1) - mtlr r0 - lwz r29, 0x14(r1) - addi r1, r1, 0x20 - blr - */ +BOOL DVDCancelAsync(DVDCommandBlock* block, DVDCBCallback callback) +{ + BOOL enabled; + DVDLowCallback old; + + enabled = OSDisableInterrupts(); + + switch (block->state) { + case -1: + case 0: + case 10: + if (callback) + (*callback)(0, block); + break; + + case 1: + if (Canceling) { + OSRestoreInterrupts(enabled); + return FALSE; + } + + Canceling = TRUE; + CancelCallback = callback; + if (block->command == 4 || block->command == 1) { + DVDLowBreak(); + } + break; + + case 2: + __DVDDequeueWaitingQueue(block); + block->state = 10; + if (block->callback) + (block->callback)(-3, block); + if (callback) + (*callback)(0, block); + break; + + case 3: + switch (block->command) { + case 5: + case 4: + case 13: + case 15: + if (callback) + (*callback)(0, block); + break; + + default: + if (Canceling) { + OSRestoreInterrupts(enabled); + return FALSE; + } + Canceling = TRUE; + CancelCallback = callback; + break; + } + break; + + case 4: + case 5: + case 6: + case 7: + case 11: + old = DVDLowClearCallback(); + if (old != cbForStateMotorStopped) { + OSRestoreInterrupts(enabled); + return FALSE; + } + + if (block->state == 4) + ResumeFromHere = 3; + if (block->state == 5) + ResumeFromHere = 4; + if (block->state == 6) + ResumeFromHere = 1; + if (block->state == 11) + ResumeFromHere = 2; + if (block->state == 7) + ResumeFromHere = 7; + + block->state = 10; + if (block->callback) { + (block->callback)(-3, block); + } + if (callback) { + (callback)(0, block); + } + stateReady(); + break; + } + + OSRestoreInterrupts(enabled); + return TRUE; } /* @@ -3381,64 +1649,41 @@ void DVDCancelAsync(void) * Address: 80201C2C * Size: 0000AC */ -void DVDCancel(void) +s32 DVDCancel(DVDCommandBlock* block) { - /* - .loc_0x0: - mflr r0 - lis r4, 0x8020 - stw r0, 0x4(r1) - addi r4, r4, 0x1CD8 - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - stw r30, 0x10(r1) - addi r30, r3, 0 - bl -0x290 - cmpwi r3, 0 - bne- .loc_0x34 - li r3, -0x1 - b .loc_0x94 - - .loc_0x34: - bl -0x8CE4 - mr r31, r3 + BOOL result; + s32 state; + u32 command; + BOOL enabled; - .loc_0x3C: - lwz r3, 0xC(r30) - addi r0, r3, 0x1 - cmplwi r0, 0x1 - ble- .loc_0x88 - cmpwi r3, 0xA - beq- .loc_0x88 - cmpwi r3, 0x3 - bne- .loc_0x7C - lwz r3, 0x8(r30) - subi r0, r3, 0x4 - cmplwi r0, 0x1 - ble- .loc_0x88 - cmplwi r3, 0xD - beq- .loc_0x88 - cmplwi r3, 0xF - beq- .loc_0x88 - - .loc_0x7C: - addi r3, r13, 0x32B0 - bl -0x52F0 - b .loc_0x3C + result = DVDCancelAsync(block, cbForCancelSync); - .loc_0x88: - mr r3, r31 - bl -0x8D14 - li r3, 0 + if (result == FALSE) { + return -1; + } - .loc_0x94: - lwz r0, 0x1C(r1) - lwz r31, 0x14(r1) - lwz r30, 0x10(r1) - mtlr r0 - addi r1, r1, 0x18 - blr - */ + enabled = OSDisableInterrupts(); + + for (;;) { + state = ((volatile DVDCommandBlock*)block)->state; + + if ((state == 0) || (state == -1) || (state == 10)) { + break; + } + + if (state == 3) { + command = ((volatile DVDCommandBlock*)block)->command; + + if ((command == 4) || (command == 5) || (command == 13) || (command == 15)) { + break; + } + } + + OSSleepThread(&__DVDThreadQueue); + } + + OSRestoreInterrupts(enabled); + return 0; } /* @@ -3446,29 +1691,37 @@ void DVDCancel(void) * Address: 80201CD8 * Size: 000024 */ -void cbForCancelSync(void) -{ - /* - .loc_0x0: - mflr r0 - addi r3, r13, 0x32B0 - stw r0, 0x4(r1) - stwu r1, -0x8(r1) - bl -0x5240 - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ -} +static void cbForCancelSync(s32 result, DVDCommandBlock* block) { OSWakeupThread(&__DVDThreadQueue); } /* * --INFO-- * Address: ........ * Size: 0000EC */ -void DVDCancelAllAsync(void) +static BOOL DVDCancelAllAsync(DVDCBCallback callback) { + BOOL enabled; + DVDCommandBlock* p; + BOOL retVal; + + enabled = OSDisableInterrupts(); + DVDPause(); + + while ((p = __DVDPopWaitingQueue()) != 0) { + DVDCancelAsync(p, NULL); + } + + if (executing) + retVal = DVDCancelAsync(executing, callback); + else { + retVal = TRUE; + if (callback) + (*callback)(0, NULL); + } + + DVDResume(); + OSRestoreInterrupts(enabled); + return retVal; // UNUSED FUNCTION } @@ -3477,7 +1730,7 @@ void DVDCancelAllAsync(void) * Address: ........ * Size: 000138 */ -void DVDCancelAll(void) +s32 DVDCancelAll() { // UNUSED FUNCTION } @@ -3497,98 +1750,67 @@ void cbForCancelAllSync(void) * Address: 80201CFC * Size: 000008 */ -void DVDGetCurrentDiskID(void) -{ - /* - .loc_0x0: - lis r3, 0x8000 - blr - */ -} +DVDDiskID* DVDGetCurrentDiskID() { return (DVDDiskID*)OSPhysicalToCached(0); } /* * --INFO-- * Address: 80201D04 * Size: 0000E4 */ -void DVDCheckDisk(void) -{ - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x10(r1) - stw r31, 0xC(r1) - bl -0x8D98 - lwz r0, 0x32D0(r13) - cmpwi r0, 0 - beq- .loc_0x28 - li r4, -0x1 - b .loc_0x6C - - .loc_0x28: - lwz r0, 0x32C8(r13) - cmpwi r0, 0 - beq- .loc_0x3C - li r4, 0x8 - b .loc_0x6C - - .loc_0x3C: - lwz r5, 0x32B8(r13) - cmplwi r5, 0 - bne- .loc_0x50 - li r4, 0 - b .loc_0x6C - - .loc_0x50: - lis r4, 0x803D - addi r0, r4, 0x3180 - cmplw r5, r0 - bne- .loc_0x68 - li r4, 0 - b .loc_0x6C - - .loc_0x68: - lwz r4, 0xC(r5) - - .loc_0x6C: - addi r0, r4, 0x1 - cmplwi r0, 0xC - bgt- .loc_0xC8 - lis r4, 0x802F - subi r4, r4, 0x7708 - rlwinm r0,r0,2,0,29 - lwzx r0, r4, r0 - mtctr r0 - bctr - li r31, 0x1 - b .loc_0xC8 - li r31, 0 - b .loc_0xC8 - lis r4, 0xCC00 - addi r4, r4, 0x6000 - lwz r4, 0x4(r4) - rlwinm. r0,r4,30,31,31 - bne- .loc_0xBC - rlwinm. r0,r4,0,31,31 - beq- .loc_0xC4 - - .loc_0xBC: - li r31, 0 - b .loc_0xC8 - - .loc_0xC4: - li r31, 0x1 - - .loc_0xC8: - bl -0x8E28 - lwz r0, 0x14(r1) - mr r3, r31 - lwz r31, 0xC(r1) - addi r1, r1, 0x10 - mtlr r0 - blr - */ +BOOL DVDCheckDisk() +{ + BOOL enabled; + s32 result; + s32 state; + u32 coverReg; + + enabled = OSDisableInterrupts(); + + if (FatalErrorFlag) { + state = -1; + } else if (PausingFlag) { + state = 8; + } else { + if (executing == (DVDCommandBlock*)NULL) { + state = 0; + } else if (executing == &DummyCommandBlock) { + state = 0; + } else { + state = executing->state; + } + } + + switch (state) { + case 1: + case 9: + case 10: + case 2: + result = TRUE; + break; + + case -1: + case 11: + case 7: + case 3: + case 4: + case 5: + case 6: + result = FALSE; + break; + + case 0: + case 8: + coverReg = __DIRegs[1]; + if (((coverReg >> 2) & 1) || (coverReg & 1)) { + result = FALSE; + } else { + result = TRUE; + } + } + + OSRestoreInterrupts(enabled); + + return result; } /* @@ -3596,100 +1818,23 @@ void DVDCheckDisk(void) * Address: 80201DE8 * Size: 00011C */ -void __DVDPrepareResetAsync(void) +void __DVDPrepareResetAsync(DVDCBCallback callback) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x20(r1) - stw r31, 0x1C(r1) - stw r30, 0x18(r1) - mr r30, r3 - stw r29, 0x14(r1) - bl -0x8E88 - mr r29, r3 - bl .loc_0x11C - lwz r0, 0x32D8(r13) - cmplwi r0, 0 - beq- .loc_0x3C - stw r30, 0x32DC(r13) - b .loc_0xF8 + BOOL enabled; - .loc_0x3C: - lwz r3, 0x32B8(r13) - cmplwi r3, 0 - beq- .loc_0x50 - li r0, 0 - stw r0, 0x28(r3) - - .loc_0x50: - bl -0x8EBC - mr r31, r3 - bl -0x8EC4 - lwz r0, 0x32B8(r13) - li r4, 0x1 - stw r4, 0x32C4(r13) - cmplwi r0, 0 - bne- .loc_0x74 - stw r4, 0x32C8(r13) - - .loc_0x74: - bl -0x8EB8 - b .loc_0x84 - - .loc_0x7C: - li r4, 0 - bl -0x4AC - - .loc_0x84: - bl 0x138 - cmplwi r3, 0 - bne+ .loc_0x7C - lwz r3, 0x32B8(r13) - cmplwi r3, 0 - beq- .loc_0xA8 - mr r4, r30 - bl -0x4CC - b .loc_0xC4 - - .loc_0xA8: - cmplwi r30, 0 - beq- .loc_0xC4 - addi r12, r30, 0 - mtlr r12 - li r3, 0 - li r4, 0 - blrl - - .loc_0xC4: - bl -0x8F30 - li r4, 0 - stw r4, 0x32C4(r13) - mr r30, r3 - lwz r0, 0x32C8(r13) - cmpwi r0, 0 - beq- .loc_0xE8 - stw r4, 0x32C8(r13) - bl -0x15A4 - - .loc_0xE8: - mr r3, r30 - bl -0x8F30 - mr r3, r31 - bl -0x8F38 - - .loc_0xF8: - mr r3, r29 - bl -0x8F40 - lwz r0, 0x24(r1) - lwz r31, 0x1C(r1) - lwz r30, 0x18(r1) - mtlr r0 - lwz r29, 0x14(r1) - addi r1, r1, 0x20 - blr + enabled = OSDisableInterrupts(); - .loc_0x11C: - */ + __DVDClearWaitingQueue(); + + if (Canceling) { + CancelCallback = callback; + } else { + if (executing) { + executing->callback = nullptr; + } + + DVDCancelAllAsync(callback); + } + + OSRestoreInterrupts(enabled); } diff --git a/src/dvd/dvderror.c b/src/dvd/dvderror.c index 68c7efbc..203b48df 100644 --- a/src/dvd/dvderror.c +++ b/src/dvd/dvderror.c @@ -1,12 +1,25 @@ -#include "types.h" +#include "Dolphin/dvd.h" +#include "Dolphin/os.h" + +u32 ErrorTable[16] = { 0x00000000, 0x00023A00, 0x00062800, 0x00030200, 0x00031100, 0x00052000, 0x00052001, 0x00052100, + 0x00052400, 0x00052401, 0x00052402, 0x000B5A01, 0x00056300, 0x00020401, 0x00020400, 0x00040800 }; /* * --INFO-- * Address: ........ * Size: 0000E4 */ -void ErrorCode2Num(void) +u8 ErrorCode2Num(u32 errorCode) { + int i; + + for (i = 0; i < 16; i++) { + if (errorCode == ErrorTable[i]) { + return i; + } + } + + return 29; // UNUSED FUNCTION } @@ -15,8 +28,22 @@ void ErrorCode2Num(void) * Address: ........ * Size: 000128 */ -void Convert(void) +u8 Convert(u32 errCode) { + u8 storedCode; + u32 upperByte; + if (errCode == 0x1234567) { + return -1; + } else if (errCode == 0x1234568) { + return -2; + } + + upperByte = errCode >> 0x18; + storedCode = ErrorCode2Num(errCode & 0xffffff); + if (errCode >> 0x18 >= 6) { + upperByte = 6; + } + return storedCode + upperByte * 30; // UNUSED FUNCTION } @@ -25,8 +52,10 @@ void Convert(void) * Address: 802020FC * Size: 000158 */ -void __DVDStoreErrorCode(void) +void __DVDStoreErrorCode(u32 errCode) { + __OSLockSramEx()->dvdErrorCode = Convert(errCode); + __OSUnlockSramEx(TRUE); /* .loc_0x0: mflr r0 diff --git a/src/dvd/dvdfs.c b/src/dvd/dvdfs.c index 58a22b79..82a33d0f 100644 --- a/src/dvd/dvdfs.c +++ b/src/dvd/dvdfs.c @@ -1,38 +1,75 @@ -#include "types.h" +#include "Dolphin/dvd.h" +#include "Dolphin/os.h" + +typedef struct FSTEntry FSTEntry; + +struct FSTEntry { + uint isDirAndStringOff; + uint parentOrPosition; + uint nextEntryOrLength; +}; + +static OSBootInfo* BootInfo; +static FSTEntry* FstStart; +static char* FstStringStart; +static u32 MaxEntryNum; +static u32 currentDirectory = 0; +OSThreadQueue __DVDThreadQueue; +u32 __DVDLongFileNameFlag = 0; + +static void cbForReadAsync(s32 result, DVDCommandBlock* block); +static void cbForReadSync(s32 result, DVDCommandBlock* block); +static void cbForSeekAsync(s32 result, DVDCommandBlock* block); +static void cbForSeekSync(s32 result, DVDCommandBlock* block); +static void cbForPrepareStreamAsync(s32 result, DVDCommandBlock* block); +static void cbForPrepareStreamSync(s32 result, DVDCommandBlock* block); + +#ifndef offsetof +#define offsetof(type, memb) ((u32) & ((type*)0)->memb) +#endif /* * --INFO-- * Address: 801FEF80 * Size: 000038 */ -void __DVDFSInit(void) +void __DVDFSInit() { - /* - .loc_0x0: - lis r3, 0x8000 - stw r3, 0x3298(r13) - lwz r0, 0x38(r3) - stw r0, 0x329C(r13) - lwz r3, 0x329C(r13) - cmplwi r3, 0 - beqlr- - lwz r0, 0x8(r3) - stw r0, 0x32A4(r13) - lwz r0, 0x32A4(r13) - mulli r0, r0, 0xC - add r0, r3, r0 - stw r0, 0x32A0(r13) - blr - */ + BootInfo = (OSBootInfo*)OSPhysicalToCached(0); + FstStart = (FSTEntry*)BootInfo->FSTLocation; + + if (FstStart) { + MaxEntryNum = FstStart[0].nextEntryOrLength; + FstStringStart = (char*)&(FstStart[MaxEntryNum]); + } } +/* For convenience */ +#define entryIsDir(i) (((FstStart[i].isDirAndStringOff & 0xff000000) == 0) ? FALSE : TRUE) +#define stringOff(i) (FstStart[i].isDirAndStringOff & ~0xff000000) +#define parentDir(i) (FstStart[i].parentOrPosition) +#define nextDir(i) (FstStart[i].nextEntryOrLength) +#define filePosition(i) (FstStart[i].parentOrPosition) +#define fileLength(i) (FstStart[i].nextEntryOrLength) + /* * --INFO-- * Address: ........ * Size: 00009C */ -void isSame(void) +static BOOL isSame(const char* path, const char* string) { + while ((u8)*string != '\0') { + if (tolower((u8)*path++) != tolower((u8)*string++)) { + return FALSE; + } + } + + if (((u8)*path == '/') || ((u8)*path == '\0')) { + return TRUE; + } + + return FALSE; // UNUSED FUNCTION } @@ -41,273 +78,104 @@ void isSame(void) * Address: 801FEFB8 * Size: 0002E0 */ -void DVDConvertPathToEntrynum(void) +s32 DVDConvertPathToEntrynum(char* pathPtr) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x48(r1) - stmw r20, 0x18(r1) - mr r23, r3 - lis r3, 0x802F - addi r25, r23, 0 - subi r31, r3, 0x7BA0 - lwz r28, 0x32A8(r13) - - .loc_0x24: - lbz r0, 0x0(r23) - cmplwi r0, 0 - bne- .loc_0x38 - mr r3, r28 - b .loc_0x2CC - - .loc_0x38: - cmplwi r0, 0x2F - bne- .loc_0x4C - li r28, 0 - addi r23, r23, 0x1 - b .loc_0x24 - - .loc_0x4C: - cmplwi r0, 0x2E - bne- .loc_0xC0 - lbz r0, 0x1(r23) - cmplwi r0, 0x2E - bne- .loc_0xA0 - lbz r0, 0x2(r23) - cmplwi r0, 0x2F - bne- .loc_0x84 - mulli r3, r28, 0xC - lwz r4, 0x329C(r13) - addi r0, r3, 0x4 - lwzx r28, r4, r0 - addi r23, r23, 0x3 - b .loc_0x24 - - .loc_0x84: - cmplwi r0, 0 - bne- .loc_0xC0 - mulli r0, r28, 0xC - lwz r3, 0x329C(r13) - add r3, r3, r0 - lwz r3, 0x4(r3) - b .loc_0x2CC - - .loc_0xA0: - cmplwi r0, 0x2F - bne- .loc_0xB0 - addi r23, r23, 0x2 - b .loc_0x24 - - .loc_0xB0: - cmplwi r0, 0 - bne- .loc_0xC0 - mr r3, r28 - b .loc_0x2CC - - .loc_0xC0: - lwz r0, 0x32AC(r13) - cmplwi r0, 0 - bne- .loc_0x16C - addi r20, r23, 0 - li r4, 0 - li r3, 0 - b .loc_0x11C - - .loc_0xDC: - cmplwi r0, 0x2E - bne- .loc_0x10C - sub r0, r20, r23 - cmpwi r0, 0x8 - bgt- .loc_0xF8 - cmpwi r4, 0x1 - bne- .loc_0x100 - - .loc_0xF8: - li r3, 0x1 - b .loc_0x130 - - .loc_0x100: - addi r24, r20, 0x1 - li r4, 0x1 - b .loc_0x118 - - .loc_0x10C: - cmplwi r0, 0x20 - bne- .loc_0x118 - li r3, 0x1 - - .loc_0x118: - addi r20, r20, 0x1 - - .loc_0x11C: - lbz r0, 0x0(r20) - cmplwi r0, 0 - beq- .loc_0x130 - cmplwi r0, 0x2F - bne+ .loc_0xDC - - .loc_0x130: - cmpwi r4, 0x1 - bne- .loc_0x148 - sub r0, r20, r24 - cmpwi r0, 0x3 - ble- .loc_0x148 - li r3, 0x1 - - .loc_0x148: - cmpwi r3, 0 - beq- .loc_0x18C - addi r5, r31, 0 - crclr 6, 0x6 - addi r6, r25, 0 - addi r3, r13, 0x2A10 - li r4, 0x175 - bl -0x780C - b .loc_0x18C - - .loc_0x16C: - mr r20, r23 - b .loc_0x178 - - .loc_0x174: - addi r20, r20, 0x1 - - .loc_0x178: - lbz r0, 0x0(r20) - cmplwi r0, 0 - beq- .loc_0x18C - cmplwi r0, 0x2F - bne+ .loc_0x174 - - .loc_0x18C: - lbz r0, 0x0(r20) - cmplwi r0, 0 - bne- .loc_0x1A0 - li r30, 0 - b .loc_0x1A4 - - .loc_0x1A0: - li r30, 0x1 - - .loc_0x1A4: - mulli r29, r28, 0xC - sub r27, r20, r23 - addi r26, r28, 0x1 - b .loc_0x290 - - .loc_0x1B4: - mulli r28, r26, 0xC - lwzx r4, r3, r28 - rlwinm. r0,r4,0,0,7 - bne- .loc_0x1CC - li r0, 0 - b .loc_0x1D0 - - .loc_0x1CC: - li r0, 0x1 - - .loc_0x1D0: - cmpwi r0, 0 - bne- .loc_0x1E0 - cmpwi r30, 0x1 - beq- .loc_0x258 - - .loc_0x1E0: - lwz r3, 0x32A0(r13) - rlwinm r0,r4,0,8,31 - addi r21, r23, 0 - add r20, r3, r0 - b .loc_0x224 - - .loc_0x1F4: - lbz r3, 0x0(r20) - addi r20, r20, 0x1 - bl 0x16C28 - lbz r0, 0x0(r21) - addi r22, r3, 0 - addi r21, r21, 0x1 - mr r3, r0 - bl 0x16C14 - cmpw r3, r22 - beq- .loc_0x224 - li r0, 0 - b .loc_0x250 - - .loc_0x224: - lbz r0, 0x0(r20) - cmplwi r0, 0 - bne+ .loc_0x1F4 - lbz r0, 0x0(r21) - cmplwi r0, 0x2F - beq- .loc_0x244 - cmplwi r0, 0 - bne- .loc_0x24C - - .loc_0x244: - li r0, 0x1 - b .loc_0x250 - - .loc_0x24C: - li r0, 0 - - .loc_0x250: - cmpwi r0, 0x1 - beq- .loc_0x2AC - - .loc_0x258: - lwz r0, 0x329C(r13) - add r3, r0, r28 - lwz r0, 0x0(r3) - rlwinm. r0,r0,0,0,7 - bne- .loc_0x274 - li r0, 0 - b .loc_0x278 - - .loc_0x274: - li r0, 0x1 - - .loc_0x278: - cmpwi r0, 0 - beq- .loc_0x288 - lwz r0, 0x8(r3) - b .loc_0x28C - - .loc_0x288: - addi r0, r26, 0x1 - - .loc_0x28C: - mr r26, r0 - - .loc_0x290: - lwz r3, 0x329C(r13) - addi r0, r3, 0x8 - lwzx r0, r29, r0 - cmplw r26, r0 - blt+ .loc_0x1B4 - li r3, -0x1 - b .loc_0x2CC - - .loc_0x2AC: - cmpwi r30, 0 - bne- .loc_0x2BC - mr r3, r26 - b .loc_0x2CC - - .loc_0x2BC: - add r23, r27, r23 - addi r28, r26, 0 - addi r23, r23, 0x1 - b .loc_0x24 - - .loc_0x2CC: - lmw r20, 0x18(r1) - lwz r0, 0x4C(r1) - addi r1, r1, 0x48 - mtlr r0 - blr - */ + const char* ptr; + char* stringPtr; + BOOL isDir; + u32 length; + u32 dirLookAt; + u32 i; + const char* origPathPtr = pathPtr; + const char* extentionStart; + BOOL illegal; + BOOL extention; + + dirLookAt = currentDirectory; + + while (1) { + + if ((u8)*pathPtr == '\0') { + return (s32)dirLookAt; + } else if ((u8)*pathPtr == '/') { + dirLookAt = 0; + pathPtr++; + continue; + } else if ((u8)*pathPtr == '.') { + if ((u8) * (pathPtr + 1) == '.') { + if ((u8) * (pathPtr + 2) == '/') { + dirLookAt = parentDir(dirLookAt); + pathPtr += 3; + continue; + } else if ((u8) * (pathPtr + 2) == '\0') { + return (s32)parentDir(dirLookAt); + } + } else if ((u8) * (pathPtr + 1) == '/') { + pathPtr += 2; + continue; + } else if ((u8) * (pathPtr + 1) == '\0') { + return (s32)dirLookAt; + } + } + + if (__DVDLongFileNameFlag == 0) { + extention = FALSE; + illegal = FALSE; + + for (ptr = pathPtr; ((u8)*ptr != '\0') && ((u8)*ptr != '/'); ptr++) { + if ((u8)*ptr == '.') { + if ((ptr - pathPtr > 8) || (extention == TRUE)) { + illegal = TRUE; + break; + } + extention = TRUE; + extentionStart = ptr + 1; + + } else if ((u8)*ptr == ' ') + illegal = TRUE; + } + + if ((extention == TRUE) && (ptr - extentionStart > 3)) + illegal = TRUE; + + if (illegal) + OSErrorLine(373, + "DVDConvertEntrynumToPath(possibly DVDOpen or DVDChangeDir or DVDOpenDir): " + "specified directory or file (%s) doesn't match standard 8.3 format. This is a " + "temporary restriction and will be removed soon\n", + origPathPtr); + } else { + for (ptr = pathPtr; ((u8)*ptr != '\0') && ((u8)*ptr != '/'); ptr++) + ; + } + + isDir = ((u8)*ptr == '\0') ? FALSE : TRUE; + length = (u32)(ptr - pathPtr); + + ptr = pathPtr; + + for (i = dirLookAt + 1; i < nextDir(dirLookAt); i = entryIsDir(i) ? nextDir(i) : (i + 1)) { + if ((entryIsDir(i) == FALSE) && (isDir == TRUE)) { + continue; + } + + stringPtr = FstStringStart + stringOff(i); + + if (isSame(ptr, stringPtr) == TRUE) { + goto next_hier; + } + } + + return -1; + + next_hier: + if (!isDir) { + return (s32)i; + } + + dirLookAt = i; + pathPtr += length + 1; + } } /* @@ -315,48 +183,18 @@ void DVDConvertPathToEntrynum(void) * Address: 801FF298 * Size: 000074 */ -void DVDFastOpen(void) +BOOL DVDFastOpen(s32 entrynum, DVDFileInfo* fileInfo) { - /* - .loc_0x0: - cmpwi r3, 0 - blt- .loc_0x3C - lwz r0, 0x32A4(r13) - cmplw r3, r0 - bge- .loc_0x3C - mulli r6, r3, 0xC - lwz r3, 0x329C(r13) - lwzx r0, r3, r6 - rlwinm. r0,r0,0,0,7 - bne- .loc_0x30 - li r0, 0 - b .loc_0x34 - - .loc_0x30: - li r0, 0x1 - - .loc_0x34: - cmpwi r0, 0 - beq- .loc_0x44 - - .loc_0x3C: - li r3, 0 - blr - - .loc_0x44: - add r3, r3, r6 - lwz r5, 0x4(r3) - li r0, 0 - li r3, 0x1 - stw r5, 0x30(r4) - lwz r5, 0x329C(r13) - add r5, r5, r6 - lwz r5, 0x8(r5) - stw r5, 0x34(r4) - stw r0, 0x38(r4) - stw r0, 0xC(r4) - blr - */ + if ((entrynum < 0) || (entrynum >= MaxEntryNum) || entryIsDir(entrynum)) { + return FALSE; + } + + fileInfo->startAddr = filePosition(entrynum); + fileInfo->length = fileLength(entrynum); + fileInfo->callback = (DVDCallback)NULL; + fileInfo->cBlock.state = DVD_STATE_END; + + return TRUE; } /* @@ -364,71 +202,29 @@ void DVDFastOpen(void) * Address: 801FF30C * Size: 0000C8 */ -void DVDOpen(void) +BOOL DVDOpen(char* fileName, DVDFileInfo* fileInfo) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x98(r1) - stw r31, 0x94(r1) - addi r31, r4, 0 - stw r30, 0x90(r1) - addi r30, r3, 0 - bl -0x370 - cmpwi r3, 0 - bge- .loc_0x54 - addi r3, r1, 0x10 - li r4, 0x80 - bl 0x21C - lis r3, 0x802F - crclr 6, 0x6 - subi r3, r3, 0x7AD8 - addi r4, r30, 0 - addi r5, r1, 0x10 - bl -0x7AC4 - li r3, 0 - b .loc_0xB0 - - .loc_0x54: - mulli r5, r3, 0xC - lwz r3, 0x329C(r13) - lwzx r0, r3, r5 - rlwinm. r0,r0,0,0,7 - bne- .loc_0x70 - li r0, 0 - b .loc_0x74 - - .loc_0x70: - li r0, 0x1 - - .loc_0x74: - cmpwi r0, 0 - beq- .loc_0x84 - li r3, 0 - b .loc_0xB0 - - .loc_0x84: - add r3, r3, r5 - lwz r4, 0x4(r3) - li r0, 0 - li r3, 0x1 - stw r4, 0x30(r31) - lwz r4, 0x329C(r13) - add r4, r4, r5 - lwz r4, 0x8(r4) - stw r4, 0x34(r31) - stw r0, 0x38(r31) - stw r0, 0xC(r31) - - .loc_0xB0: - lwz r0, 0x9C(r1) - lwz r31, 0x94(r1) - lwz r30, 0x90(r1) - mtlr r0 - addi r1, r1, 0x98 - blr - */ + s32 entry; + char currentDir[128]; + + entry = DVDConvertPathToEntrynum(fileName); + + if (0 > entry) { + DVDGetCurrentDir(currentDir, 128); + OSReport("Warning: DVDOpen(): file '%s' was not found under %s.\n", fileName, currentDir); + return FALSE; + } + + if (entryIsDir(entry)) { + return FALSE; + } + + fileInfo->startAddr = filePosition(entry); + fileInfo->length = fileLength(entry); + fileInfo->callback = (DVDCallback)NULL; + fileInfo->cBlock.state = DVD_STATE_END; + + return TRUE; } /* @@ -436,20 +232,10 @@ void DVDOpen(void) * Address: 801FF3D4 * Size: 000024 */ -void DVDClose(void) +BOOL DVDClose(DVDFileInfo* fileInfo) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x8(r1) - bl 0x284C - lwz r0, 0xC(r1) - li r3, 0x1 - addi r1, r1, 0x8 - mtlr r0 - blr - */ + DVDCancel(&(fileInfo->cBlock)); + return TRUE; } /* @@ -457,9 +243,16 @@ void DVDClose(void) * Address: ........ * Size: 000038 */ -void myStrncpy(void) +static u32 myStrncpy(char* dest, char* src, u32 maxlen) { - // UNUSED FUNCTION + u32 i = maxlen; + + while ((i > 0) && ((u8)*src != 0)) { + *dest++ = *src++; + i--; + } + + return (maxlen - i); } /* @@ -467,123 +260,28 @@ void myStrncpy(void) * Address: 801FF3F8 * Size: 000160 */ -void entryToPath(void) +static u32 entryToPath(u32 entry, char* path, u32 maxlen) { - /* - .loc_0x0: - mflr r0 - cmplwi r3, 0 - stw r0, 0x4(r1) - stwu r1, -0x30(r1) - stw r31, 0x2C(r1) - stw r30, 0x28(r1) - addi r30, r5, 0 - stw r29, 0x24(r1) - addi r29, r4, 0 - stw r28, 0x20(r1) - bne- .loc_0x34 - li r3, 0 - b .loc_0x140 - - .loc_0x34: - lwz r4, 0x329C(r13) - mulli r3, r3, 0xC - lwz r6, 0x32A0(r13) - addi r5, r4, 0x4 - lwzx r0, r4, r3 - lwzx r3, r5, r3 - rlwinm r0,r0,0,8,31 - cmplwi r3, 0 - add r31, r6, r0 - bne- .loc_0x64 - li r3, 0 - b .loc_0xE0 - - .loc_0x64: - mulli r3, r3, 0xC - lwzx r0, r4, r3 - mr r4, r29 - lwzx r3, r5, r3 - addi r5, r30, 0 - rlwinm r0,r0,0,8,31 - add r28, r6, r0 - bl .loc_0x0 - cmplw r3, r30 - bne- .loc_0x90 - b .loc_0xE0 - - .loc_0x90: - addi r0, r3, 0 - addi r3, r3, 0x1 - li r4, 0x2F - sub r6, r30, r3 - stbx r4, r29, r0 - addi r4, r6, 0 - add r5, r29, r3 - b .loc_0xC4 - - .loc_0xB0: - lbz r0, 0x0(r28) - addi r28, r28, 0x1 - subi r4, r4, 0x1 - stb r0, 0x0(r5) - addi r5, r5, 0x1 - - .loc_0xC4: - cmplwi r4, 0 - beq- .loc_0xD8 - lbz r0, 0x0(r28) - cmplwi r0, 0 - bne+ .loc_0xB0 - - .loc_0xD8: - sub r0, r6, r4 - add r3, r3, r0 - - .loc_0xE0: - cmplw r3, r30 - bne- .loc_0xEC - b .loc_0x140 - - .loc_0xEC: - addi r0, r3, 0 - addi r3, r3, 0x1 - li r4, 0x2F - sub r7, r30, r3 - stbx r4, r29, r0 - addi r6, r31, 0 - addi r4, r7, 0 - add r5, r29, r3 - b .loc_0x124 - - .loc_0x110: - lbz r0, 0x0(r6) - addi r6, r6, 0x1 - subi r4, r4, 0x1 - stb r0, 0x0(r5) - addi r5, r5, 0x1 - - .loc_0x124: - cmplwi r4, 0 - beq- .loc_0x138 - lbz r0, 0x0(r6) - cmplwi r0, 0 - bne+ .loc_0x110 - - .loc_0x138: - sub r0, r7, r4 - add r3, r3, r0 - - .loc_0x140: - lwz r0, 0x34(r1) - lwz r31, 0x2C(r1) - lwz r30, 0x28(r1) - mtlr r0 - lwz r29, 0x24(r1) - lwz r28, 0x20(r1) - addi r1, r1, 0x30 - blr - */ + char* name; + u32 loc; + + if (entry == 0) { + return 0; + } + + name = FstStringStart + stringOff(entry); + + loc = entryToPath(parentDir(entry), path, maxlen); + + if (loc == maxlen) { + return loc; + } + + *(path + loc++) = '/'; + + loc += myStrncpy(path + loc, name, maxlen - loc); + + return loc; } /* @@ -591,9 +289,28 @@ void entryToPath(void) * Address: ........ * Size: 000154 */ -void DVDConvertEntrynumToPath(void) +static BOOL DVDConvertEntrynumToPath(s32 entrynum, char* path, u32 maxlen) { - // UNUSED FUNCTION + u32 loc; + + loc = entryToPath((u32)entrynum, path, maxlen); + + if (loc == maxlen) { + path[maxlen - 1] = '\0'; + return FALSE; + } + + if (entryIsDir(entrynum)) { + if (loc == maxlen - 1) { + path[loc] = '\0'; + return FALSE; + } + + path[loc++] = '/'; + } + + path[loc] = '\0'; + return TRUE; } /* @@ -601,82 +318,24 @@ void DVDConvertEntrynumToPath(void) * Address: 801FF558 * Size: 0000C4 */ -void DVDGetCurrentDir(void) -{ - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x20(r1) - stw r31, 0x1C(r1) - stw r30, 0x18(r1) - addi r30, r4, 0 - addi r5, r30, 0 - stw r29, 0x14(r1) - addi r29, r3, 0 - addi r4, r29, 0 - lwz r31, 0x32A8(r13) - addi r3, r31, 0 - bl -0x190 - cmplw r3, r30 - bne- .loc_0x4C - li r0, 0 - add r3, r29, r30 - stb r0, -0x1(r3) - b .loc_0xA4 - - .loc_0x4C: - mulli r0, r31, 0xC - lwz r4, 0x329C(r13) - lwzx r0, r4, r0 - rlwinm. r0,r0,0,0,7 - bne- .loc_0x68 - li r0, 0 - b .loc_0x6C - - .loc_0x68: - li r0, 0x1 - - .loc_0x6C: - cmpwi r0, 0 - beq- .loc_0x98 - subi r0, r30, 0x1 - cmplw r3, r0 - bne- .loc_0x8C - li r0, 0 - stbx r0, r29, r3 - b .loc_0xA4 - - .loc_0x8C: - li r0, 0x2F - stbx r0, r29, r3 - addi r3, r3, 0x1 - - .loc_0x98: - li r0, 0 - stbx r0, r29, r3 - li r0, 0x1 - - .loc_0xA4: - mr r3, r0 - lwz r0, 0x24(r1) - lwz r31, 0x1C(r1) - lwz r30, 0x18(r1) - mtlr r0 - lwz r29, 0x14(r1) - addi r1, r1, 0x20 - blr - */ -} +BOOL DVDGetCurrentDir(char* path, u32 maxlen) { return DVDConvertEntrynumToPath((s32)currentDirectory, path, maxlen); } /* * --INFO-- * Address: ........ * Size: 000060 */ -void DVDChangeDir(void) +BOOL DVDChangeDir(char* dirName) { - // UNUSED FUNCTION + s32 entry; + entry = DVDConvertPathToEntrynum(dirName); + if ((entry < 0) || (entryIsDir(entry) == FALSE)) { + return FALSE; + } + + currentDirectory = (u32)entry; + + return TRUE; } /* @@ -684,67 +343,20 @@ void DVDChangeDir(void) * Address: 801FF61C * Size: 0000C0 */ -void DVDReadAsyncPrio(void) +BOOL DVDReadAsyncPrio(DVDFileInfo* fileInfo, void* addr, s32 length, s32 offset, DVDCallback callback, s32 prio) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x38(r1) - stmw r26, 0x20(r1) - mr. r29, r6 - addi r26, r3, 0 - addi r27, r4, 0 - addi r28, r5, 0 - addi r30, r7, 0 - addi r31, r8, 0 - blt- .loc_0x38 - lwz r0, 0x34(r26) - cmplw r29, r0 - blt- .loc_0x50 - - .loc_0x38: - lis r3, 0x802F - crclr 6, 0x6 - subi r5, r3, 0x7AA0 - addi r3, r13, 0x2A10 - li r4, 0x2DF - bl -0x7D58 - - .loc_0x50: - add. r4, r29, r28 - blt- .loc_0x68 - lwz r3, 0x34(r26) - addi r0, r3, 0x20 - cmplw r4, r0 - blt- .loc_0x80 - - .loc_0x68: - lis r3, 0x802F - crclr 6, 0x6 - subi r5, r3, 0x7AA0 - addi r3, r13, 0x2A10 - li r4, 0x2E5 - bl -0x7D88 - - .loc_0x80: - stw r30, 0x38(r26) - lis r3, 0x8020 - subi r7, r3, 0x924 - lwz r0, 0x30(r26) - addi r3, r26, 0 - addi r4, r27, 0 - addi r5, r28, 0 - addi r8, r31, 0 - add r6, r0, r29 - bl 0x1E00 - lmw r26, 0x20(r1) - li r3, 0x1 - lwz r0, 0x3C(r1) - addi r1, r1, 0x38 - mtlr r0 - blr - */ + if (!((0 <= offset) && (offset < fileInfo->length))) { + OSErrorLine(735, "DVDReadAsync(): specified area is out of the file "); + } + + if (!((0 <= offset + length) && (offset + length < fileInfo->length + DVD_MIN_TRANSFER_SIZE))) { + OSErrorLine(741, "DVDReadAsync(): specified area is out of the file "); + } + + fileInfo->callback = callback; + DVDReadAbsAsyncPrio(&(fileInfo->cBlock), addr, length, (s32)(fileInfo->startAddr + offset), cbForReadAsync, prio); + + return TRUE; } /* @@ -752,25 +364,14 @@ void DVDReadAsyncPrio(void) * Address: 801FF6DC * Size: 000030 */ -void cbForReadAsync(void) +static void cbForReadAsync(s32 result, DVDCommandBlock* block) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x8(r1) - lwz r12, 0x38(r4) - cmplwi r12, 0 - beq- .loc_0x20 - mtlr r12 - blrl - - .loc_0x20: - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ + DVDFileInfo* fileInfo; + + fileInfo = (DVDFileInfo*)((char*)block - offsetof(DVDFileInfo, cBlock)); + if (fileInfo->callback) { + (fileInfo->callback)(result, fileInfo); + } } /* @@ -778,103 +379,53 @@ void cbForReadAsync(void) * Address: 801FF70C * Size: 000118 */ -void DVDReadPrio(void) +s32 DVDReadPrio(DVDFileInfo* fileInfo, void* addr, s32 length, s32 offset, s32 prio) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x38(r1) - stmw r27, 0x24(r1) - mr. r29, r6 - addi r31, r3, 0 - addi r27, r4, 0 - addi r28, r5, 0 - addi r30, r7, 0 - blt- .loc_0x34 - lwz r0, 0x34(r31) - cmplw r29, r0 - blt- .loc_0x4C - - .loc_0x34: - lis r3, 0x802F - crclr 6, 0x6 - subi r5, r3, 0x7A6C - addi r3, r13, 0x2A10 - li r4, 0x325 - bl -0x7E44 - - .loc_0x4C: - add. r4, r29, r28 - blt- .loc_0x64 - lwz r3, 0x34(r31) - addi r0, r3, 0x20 - cmplw r4, r0 - blt- .loc_0x7C - - .loc_0x64: - lis r3, 0x802F - crclr 6, 0x6 - subi r5, r3, 0x7A6C - addi r3, r13, 0x2A10 - li r4, 0x32B - bl -0x7E74 - - .loc_0x7C: - lwz r0, 0x30(r31) - lis r4, 0x8020 - subi r7, r4, 0x7DC - addi r3, r31, 0 - addi r4, r27, 0 - addi r5, r28, 0 - addi r8, r30, 0 - add r6, r0, r29 - bl 0x1D18 - cmpwi r3, 0 - bne- .loc_0xB0 - li r3, -0x1 - b .loc_0x104 - - .loc_0xB0: - bl -0x6840 - mr r30, r3 - - .loc_0xB8: - lwz r0, 0xC(r31) - cmpwi r0, 0 - bne- .loc_0xCC - lwz r31, 0x20(r31) - b .loc_0xF8 - - .loc_0xCC: - cmpwi r0, -0x1 - bne- .loc_0xDC - li r31, -0x1 - b .loc_0xF8 - - .loc_0xDC: - cmpwi r0, 0xA - bne- .loc_0xEC - li r31, -0x3 - b .loc_0xF8 - - .loc_0xEC: - addi r3, r13, 0x32B0 - bl -0x2E40 - b .loc_0xB8 - - .loc_0xF8: - mr r3, r30 - bl -0x6864 - mr r3, r31 - - .loc_0x104: - lmw r27, 0x24(r1) - lwz r0, 0x3C(r1) - addi r1, r1, 0x38 - mtlr r0 - blr - */ + BOOL result; + DVDCommandBlock* block; + s32 state; + BOOL enabled; + s32 retVal; + + if (!((0 <= offset) && (offset < fileInfo->length))) { + OSErrorLine(805, "DVDRead(): specified area is out of the file "); + } + + if (!((0 <= offset + length) && (offset + length < fileInfo->length + DVD_MIN_TRANSFER_SIZE))) { + OSErrorLine(811, "DVDRead(): specified area is out of the file "); + } + + block = &(fileInfo->cBlock); + + result = DVDReadAbsAsyncPrio(block, addr, length, (s32)(fileInfo->startAddr + offset), cbForReadSync, prio); + + if (result == FALSE) { + return -1; + } + + enabled = OSDisableInterrupts(); + + while (TRUE) { + state = ((volatile DVDCommandBlock*)block)->state; + + if (state == DVD_STATE_END) { + retVal = (s32)block->transferredSize; + break; + } + if (state == DVD_STATE_FATAL_ERROR) { + retVal = DVD_RESULT_FATAL_ERROR; + break; + } + if (state == DVD_STATE_CANCELED) { + retVal = DVD_RESULT_CANCELED; + break; + } + + OSSleepThread(&__DVDThreadQueue); + } + + OSRestoreInterrupts(enabled); + return retVal; } /* @@ -882,30 +433,22 @@ void DVDReadPrio(void) * Address: 801FF824 * Size: 000024 */ -void cbForReadSync(void) -{ - /* - .loc_0x0: - mflr r0 - addi r3, r13, 0x32B0 - stw r0, 0x4(r1) - stwu r1, -0x8(r1) - bl -0x2D8C - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ -} +void cbForReadSync(s32 result, DVDCommandBlock* block) { OSWakeupThread(&__DVDThreadQueue); } /* * --INFO-- * Address: ........ * Size: 000098 */ -void DVDSeekAsyncPrio(void) +BOOL DVDSeekAsyncPrio(DVDFileInfo* fileInfo, s32 offset, DVDCallback callback, s32 prio) { - // UNUSED FUNCTION + if (!((0 <= offset) && (offset < fileInfo->length))) { + OSErrorLine(881, "DVDSeek(): offset is out of the file "); + } + + fileInfo->callback = callback; + DVDSeekAbsAsyncPrio(&fileInfo->cBlock, (u32)(char*)fileInfo->startAddr + offset, cbForSeekAsync, prio); + return TRUE; } /* @@ -913,7 +456,7 @@ void DVDSeekAsyncPrio(void) * Address: ........ * Size: 000030 */ -void cbForSeekAsync(void) +void cbForSeekAsync(s32 result, DVDCommandBlock* block) { // UNUSED FUNCTION } @@ -923,7 +466,7 @@ void cbForSeekAsync(void) * Address: ........ * Size: 0000AC */ -void DVDSeekPrio(void) +s32 DVDSeekPrio(DVDFileInfo* fileInfo, s32 offset, s32 prio) { // UNUSED FUNCTION } @@ -933,7 +476,7 @@ void DVDSeekPrio(void) * Address: ........ * Size: 000024 */ -void cbForSeekSync(void) +void cbForSeekSync(s32 result, DVDCommandBlock* block) { // UNUSED FUNCTION } @@ -943,7 +486,7 @@ void cbForSeekSync(void) * Address: ........ * Size: 000020 */ -void DVDGetFileInfoStatus(void) +s32 DVDGetFileInfoStatus(DVDFileInfo* fileInfo) { // UNUSED FUNCTION } @@ -953,7 +496,7 @@ void DVDGetFileInfoStatus(void) * Address: ........ * Size: 000084 */ -void DVDOpenDir(void) +BOOL DVDOpenDir(char* dirName, DVDDir* dir) { // UNUSED FUNCTION } @@ -963,7 +506,7 @@ void DVDOpenDir(void) * Address: ........ * Size: 0000A4 */ -void DVDReadDir(void) +BOOL DVDReadDir(DVDDir* dir, DVDDirEntry* dirent) { // UNUSED FUNCTION } @@ -973,7 +516,7 @@ void DVDReadDir(void) * Address: ........ * Size: 000008 */ -void DVDCloseDir(void) +BOOL DVDCloseDir(DVDDir* dir) { // UNUSED FUNCTION } @@ -983,90 +526,48 @@ void DVDCloseDir(void) * Address: ........ * Size: 00000C */ -void DVDGetFSTLocation(void) +void* DVDGetFSTLocation() { // UNUSED FUNCTION } +#define RoundUp32KB(x) (((u32)(x) + 32 * 1024 - 1) & ~(32 * 1024 - 1)) +#define Is32KBAligned(x) (((u32)(x) & (32 * 1024 - 1)) == 0) + /* * --INFO-- * Address: 801FF848 * Size: 0000EC */ -void DVDPrepareStreamAsync(void) +BOOL DVDPrepareStreamAsync(DVDFileInfo* fileInfo, u32 length, u32 offset, DVDCallback callback) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x38(r1) - stmw r27, 0x24(r1) - addi r29, r5, 0 - mr r27, r3 - addi r28, r4, 0 - addi r30, r6, 0 - lwz r7, 0x30(r3) - lis r3, 0x802F - subi r31, r3, 0x7BA0 - add r0, r7, r29 - rlwinm. r0,r0,0,17,31 - beq- .loc_0x54 - addi r6, r7, 0 - crclr 6, 0x6 - addi r7, r29, 0 - addi r5, r31, 0x18C - addi r3, r13, 0x2A10 - li r4, 0x47E - bl -0x7F88 - - .loc_0x54: - cmplwi r28, 0 - bne- .loc_0x64 - lwz r0, 0x34(r27) - sub r28, r0, r29 - - .loc_0x64: - rlwinm. r0,r28,0,17,31 - beq- .loc_0x84 - addi r6, r28, 0 - crclr 6, 0x6 - addi r5, r31, 0x1F4 - addi r3, r13, 0x2A10 - li r4, 0x488 - bl -0x7FB8 - - .loc_0x84: - lwz r3, 0x34(r27) - cmplw r29, r3 - bge- .loc_0x9C - add r0, r29, r28 - cmplw r0, r3 - ble- .loc_0xB8 - - .loc_0x9C: - addi r6, r29, 0 - crclr 6, 0x6 - addi r7, r28, 0 - addi r5, r31, 0x24C - addi r3, r13, 0x2A10 - li r4, 0x490 - bl -0x7FEC - - .loc_0xB8: - stw r30, 0x38(r27) - lis r3, 0x8020 - subi r6, r3, 0x6CC - lwz r0, 0x30(r27) - addi r3, r27, 0 - addi r4, r28, 0 - add r5, r0, r29 - bl 0x1E24 - lmw r27, 0x24(r1) - lwz r0, 0x3C(r1) - addi r1, r1, 0x38 - mtlr r0 - blr - */ + u32 start; + + start = fileInfo->startAddr + offset; + + if (!Is32KBAligned(start)) { + OSErrorLine(1150, + "DVDPrepareStreamAsync(): Specified start address (filestart(0x%x) + offset(0x%x)) is " + "not 32KB aligned", + fileInfo->startAddr, offset); + } + + if (length == 0) + length = fileInfo->length - offset; + + if (!Is32KBAligned(length)) { + OSErrorLine(1160, "DVDPrepareStreamAsync(): Specified length (0x%x) is not a multiple of 32768(32*1024)", length); + } + + if (!((offset < fileInfo->length) && (offset + length <= fileInfo->length))) { + OSErrorLine(1168, + "DVDPrepareStreamAsync(): The area specified (offset(0x%x), length(0x%x)) is out of " + "the file", + offset, length); + } + + fileInfo->callback = callback; + return DVDPrepareStreamAbsAsync(&(fileInfo->cBlock), length, fileInfo->startAddr + offset, cbForPrepareStreamAsync); } /* @@ -1074,25 +575,14 @@ void DVDPrepareStreamAsync(void) * Address: 801FF934 * Size: 000030 */ -void cbForPrepareStreamAsync(void) +void cbForPrepareStreamAsync(s32 result, DVDCommandBlock* block) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x8(r1) - lwz r12, 0x38(r4) - cmplwi r12, 0 - beq- .loc_0x20 - mtlr r12 - blrl - - .loc_0x20: - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ + struct DVDFileInfo* fileInfo; + + fileInfo = (struct DVDFileInfo*)&block->next; + if (fileInfo->callback) { + (fileInfo->callback)(result, fileInfo); + } } /* @@ -1100,8 +590,65 @@ void cbForPrepareStreamAsync(void) * Address: ........ * Size: 000144 */ -void DVDPrepareStream(void) +s32 DVDPrepareStream(DVDFileInfo* fileInfo, u32 length, u32 offset) { + BOOL result; + DVDCommandBlock* block; + s32 state; + BOOL enabled; + s32 retVal; + u32 start; + + start = fileInfo->startAddr + offset; + + if (!Is32KBAligned(start)) { + OSErrorLine(0x4BF, + "DVDPrepareStream(): Specified start address (filestart(0x%x) + offset(0x%x)) is not " + "32KB aligned", + fileInfo->startAddr, offset); + } + + if (length == 0) + length = fileInfo->length - offset; + + if (!Is32KBAligned(length)) { + OSErrorLine(0x4C9, "DVDPrepareStream(): Specified length (0x%x) is not a multiple of 32768(32*1024)", length); + } + + if (!((offset < fileInfo->length) && (offset + length <= fileInfo->length))) { + OSErrorLine(0x4D1, "DVDPrepareStream(): The area specified (offset(0x%x), length(0x%x)) is out of the file", offset, length); + } + + block = &(fileInfo->cBlock); + result = DVDPrepareStreamAbsAsync(block, length, start, cbForPrepareStreamSync); + + if (result == FALSE) { + return -1; + } + + enabled = OSDisableInterrupts(); + + while (1) { + state = ((volatile DVDCommandBlock*)block)->state; + + if (state == DVD_STATE_END) { + retVal = 0; + break; + } + if (state == DVD_STATE_FATAL_ERROR) { + retVal = DVD_RESULT_FATAL_ERROR; + break; + } + if (state == DVD_STATE_CANCELED) { + retVal = DVD_RESULT_CANCELED; + break; + } + + OSSleepThread(&__DVDThreadQueue); + } + + OSRestoreInterrupts(enabled); + return retVal; // UNUSED FUNCTION } @@ -1110,7 +657,7 @@ void DVDPrepareStream(void) * Address: ........ * Size: 000024 */ -void cbForPrepareStreamSync(void) +void cbForPrepareStreamSync(s32 result, DVDCommandBlock* block) { // UNUSED FUNCTION } @@ -1120,7 +667,7 @@ void cbForPrepareStreamSync(void) * Address: ........ * Size: 00006C */ -void DVDGetTransferredSize(void) +s32 DVDGetTransferredSize(DVDFileInfo* fileinfo) { // UNUSED FUNCTION } diff --git a/src/dvd/dvdlow.c b/src/dvd/dvdlow.c index 82cec3b2..44fe1bdc 100644 --- a/src/dvd/dvdlow.c +++ b/src/dvd/dvdlow.c @@ -1,4 +1,45 @@ -#include "types.h" +#include "Dolphin/dvd.h" +#include "Dolphin/os.h" + +static BOOL FirstRead = TRUE; +static volatile BOOL StopAtNextInt = FALSE; +static u32 LastLength = 0; +static DVDLowCallback Callback = NULL; +static DVDLowCallback ResetCoverCallback = NULL; +static volatile OSTime LastResetEnd = 0; +static vu32 ResetOccurred = FALSE; +static volatile BOOL WaitingCoverClose = FALSE; +static BOOL Breaking = FALSE; +static vu32 WorkAroundType = 0; +static u32 WorkAroundSeekLocation = 0; +static volatile OSTime LastReadFinished = 0; +static OSTime LastReadIssued = 0; +static volatile BOOL LastCommandWasRead = FALSE; +static vu32 NextCommandNumber = 0; + +typedef struct DVDBuffer { + void* addr; + u32 length; + u32 offset; +} DVDBuffer; + +typedef struct DVDCommand { + s32 cmd; + void* addr; + u32 length; + u32 offset; + DVDLowCallback callback; +} DVDCommand; + +static DVDCommand CommandList[3]; +static OSAlarm AlarmForWA; +static OSAlarm AlarmForTimeout; +static OSAlarm AlarmForBreak; +static DVDBuffer Prev; +static DVDBuffer Curr; + +// forward declare some statics: +static void Read(void* addr, u32 length, u32 offset, DVDLowCallback callback); /* * --INFO-- @@ -7,25 +48,10 @@ */ void __DVDInitWA(void) { - /* - .loc_0x0: - mflr r0 - lis r3, 0x803D - stw r0, 0x4(r1) - li r0, 0 - li r4, 0 - stwu r1, -0x8(r1) - stw r0, 0x3294(r13) - li r0, -0x1 - stw r0, 0x3020(r3) - li r3, 0 - bl 0xDE8 - bl -0x8098 - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ + NextCommandNumber = 0; + CommandList[0].cmd = -1; + __DVDLowSetWAType(0, 0); + OSInitAlarm(); } /* @@ -33,8 +59,21 @@ void __DVDInitWA(void) * Address: ........ * Size: 000094 */ -void ProcessNextCommand(void) +static BOOL ProcessNextCommand(void) { + s32 n = NextCommandNumber; + + if (CommandList[n].cmd == 1) { + ++NextCommandNumber; + Read(CommandList[n].addr, CommandList[n].length, CommandList[n].offset, CommandList[n].callback); + return TRUE; + } else if (CommandList[n].cmd == 2) { + ++NextCommandNumber; + DVDLowSeek(CommandList[n].offset, CommandList[n].callback); + return TRUE; + } + + return FALSE; // UNUSED FUNCTION } @@ -43,240 +82,107 @@ void ProcessNextCommand(void) * Address: 801FE16C * Size: 0002F4 */ -void __DVDInterruptHandler(void) +void __DVDInterruptHandler(__OSInterrupt interrupt, OSContext* context) { - /* - .loc_0x0: - mflr r0 - lis r3, 0x803D - stw r0, 0x4(r1) - stwu r1, -0x2E8(r1) - stw r31, 0x2E4(r1) - stw r30, 0x2E0(r1) - addi r30, r3, 0x3020 - addi r3, r30, 0x68 - stw r29, 0x2DC(r1) - li r29, 0 - stw r28, 0x2D8(r1) - addi r28, r4, 0 - bl -0x7D68 - lwz r0, 0x3290(r13) - cmpwi r0, 0 - beq- .loc_0x7C - bl -0xDEC - stw r4, 0x3284(r13) - li r0, 0 - stw r3, 0x3280(r13) - stw r0, 0x2A08(r13) - lwz r0, 0xC4(r30) - stw r0, 0xB8(r30) - lwz r0, 0xC8(r30) - stw r0, 0xBC(r30) - lwz r0, 0xCC(r30) - stw r0, 0xC0(r30) - lwz r0, 0x3250(r13) - cmpwi r0, 0x1 - bne- .loc_0x7C - ori r29, r29, 0x8 - - .loc_0x7C: - li r0, 0 - stw r0, 0x3290(r13) - lis r3, 0xCC00 - stw r0, 0x3250(r13) - lwz r0, 0x6000(r3) - andi. r4, r0, 0x2A - andi. r3, r0, 0x54 - rlwinm r0,r4,1,0,30 - and r3, r3, r0 - rlwinm. r0,r3,0,25,25 - beq- .loc_0xAC - ori r29, r29, 0x8 - - .loc_0xAC: - rlwinm. r0,r3,0,27,27 - beq- .loc_0xB8 - ori r29, r29, 0x1 - - .loc_0xB8: - rlwinm. r0,r3,0,29,29 - beq- .loc_0xC4 - ori r29, r29, 0x2 - - .loc_0xC4: - cmplwi r29, 0 - beq- .loc_0xD4 - li r0, 0 - stw r0, 0x3268(r13) - - .loc_0xD4: - or r0, r3, r4 - lis r31, 0xCC00 - stw r0, 0x6000(r31) - lwz r0, 0x3268(r13) - cmplwi r0, 0 - beq- .loc_0x184 - bl -0xE98 - lis r5, 0x8000 - lwz r7, 0x3260(r13) - lwz r6, 0xF8(r5) - lis r5, 0x1062 - addi r5, r5, 0x4DD3 - lwz r8, 0x3264(r13) - rlwinm r6,r6,30,2,31 - mulhwu r5, r5, r6 - rlwinm r5,r5,26,6,31 - mulli r5, r5, 0xC8 - subc r6, r4, r8 - subfe r3, r7, r3 - li r0, 0 - xoris r4, r3, 0x8000 - xoris r3, r0, 0x8000 - subc r0, r6, r5 - subfe r3, r3, r4 - subfe r3, r4, r4 - neg. r3, r3 - beq- .loc_0x184 - lwzu r0, 0x6004(r31) - rlwinm r3,r0,0,29,29 - rlwinm r0,r0,1,29,29 - and r0, r3, r0 - rlwinm. r0,r0,0,29,29 - beq- .loc_0x178 - lwz r12, 0x325C(r13) - cmplwi r12, 0 - beq- .loc_0x170 - mtlr r12 - li r3, 0x4 - blrl - - .loc_0x170: - li r0, 0 - stw r0, 0x325C(r13) - - .loc_0x178: - lwz r0, 0x0(r31) - stw r0, 0x0(r31) - b .loc_0x1D8 - - .loc_0x184: - lwz r0, 0x326C(r13) - cmpwi r0, 0 - beq- .loc_0x1CC - lis r3, 0xCC00 - addi r5, r3, 0x6000 - lwzu r0, 0x4(r5) - rlwinm r4,r0,0,30,30 - rlwinm r3,r0,0,29,29 - rlwinm r0,r0,1,29,29 - and r3, r3, r0 - rlwinm. r0,r3,0,29,29 - beq- .loc_0x1B8 - ori r29, r29, 0x4 - - .loc_0x1B8: - or r0, r3, r4 - stw r0, 0x0(r5) - li r0, 0 - stw r0, 0x326C(r13) - b .loc_0x1D8 - - .loc_0x1CC: - lis r3, 0xCC00 - li r0, 0 - stw r0, 0x6004(r3) - - .loc_0x1D8: - rlwinm. r0,r29,0,28,28 - beq- .loc_0x1F0 - lwz r0, 0x3270(r13) - cmpwi r0, 0 - bne- .loc_0x1F0 - rlwinm r29,r29,0,29,27 - - .loc_0x1F0: - rlwinm. r0,r29,0,31,31 - beq- .loc_0x274 - lwz r0, 0x3294(r13) - mulli r0, r0, 0x14 - lwzx r3, r30, r0 - cmpwi r3, 0x1 - bne- .loc_0x238 - lwz r3, 0x3294(r13) - add r6, r30, r0 - addi r0, r3, 0x1 - stw r0, 0x3294(r13) - lwz r3, 0x4(r6) - lwz r4, 0x8(r6) - lwz r5, 0xC(r6) - lwz r6, 0x10(r6) - bl 0x1BC - li r0, 0x1 - b .loc_0x268 - - .loc_0x238: - cmpwi r3, 0x2 - bne- .loc_0x264 - lwz r3, 0x3294(r13) - add r4, r30, r0 - addi r0, r3, 0x1 - stw r0, 0x3294(r13) - lwz r3, 0xC(r4) - lwz r4, 0x10(r4) - bl 0x5B8 - li r0, 0x1 - b .loc_0x268 - - .loc_0x264: - li r0, 0 - - .loc_0x268: - cmpwi r0, 0 - beq- .loc_0x284 - b .loc_0x2D4 - - .loc_0x274: - li r0, -0x1 - stw r0, 0x0(r30) - li r0, 0 - stw r0, 0x3294(r13) - - .loc_0x284: - addi r3, r1, 0x10 - bl -0x6FB8 - addi r3, r1, 0x10 - bl -0x7188 - cmplwi r29, 0 - beq- .loc_0x2C4 - lwz r12, 0x3258(r13) - li r0, 0 - cmplwi r12, 0 - stw r0, 0x3258(r13) - beq- .loc_0x2BC - mtlr r12 - addi r3, r29, 0 - blrl - - .loc_0x2BC: - li r0, 0 - stw r0, 0x3270(r13) - - .loc_0x2C4: - addi r3, r1, 0x10 - bl -0x6FF8 - mr r3, r28 - bl -0x71C8 - - .loc_0x2D4: - lwz r0, 0x2EC(r1) - lwz r31, 0x2E4(r1) - lwz r30, 0x2E0(r1) - mtlr r0 - lwz r29, 0x2DC(r1) - lwz r28, 0x2D8(r1) - addi r1, r1, 0x2E8 - blr - */ + DVDLowCallback cb; + OSContext exceptionContext; + u32 cause = 0; + u32 reg; + u32 intr; + u32 mask; + + OSCancelAlarm(&AlarmForTimeout); + + if (LastCommandWasRead) { + LastReadFinished = __OSGetSystemTime(); + FirstRead = FALSE; + Prev.addr = Curr.addr; + Prev.length = Curr.length; + Prev.offset = Curr.offset; + if (StopAtNextInt == TRUE) { + cause |= 8; + } + } + + LastCommandWasRead = FALSE; + StopAtNextInt = FALSE; + reg = __DIRegs[0]; + mask = reg & 0x2a; + intr = (reg & 0x54) & (mask << 1); + + if (intr & 0x40) { + cause |= 8; + } + + if (intr & 0x10) { + cause |= 1; + } + + if (intr & 4) { + cause |= 2; + } + + if (cause) { + ResetOccurred = FALSE; + } + + __DIRegs[0] = intr | mask; + + if (ResetOccurred && (__OSGetSystemTime() - LastResetEnd) < OSMillisecondsToTicks(200)) { + reg = __DIRegs[1]; + mask = reg & 0x2; + intr = (reg & 4) & (mask << 1); + if (intr & 4) { + if (ResetCoverCallback) { + ResetCoverCallback(4); + } + ResetCoverCallback = NULL; + } + + __DIRegs[1] = __DIRegs[1]; + } else if (WaitingCoverClose) { + reg = __DIRegs[1]; + mask = reg & 2; + intr = (reg & 4) & (mask << 1); + + if (intr & 4) { + cause |= 4; + } + + __DIRegs[1] = intr | mask; + WaitingCoverClose = FALSE; + } else { + __DIRegs[1] = 0; + } + + if ((cause & 8) && !Breaking) { + cause &= ~8; + } + + if ((cause & 1)) { + if (ProcessNextCommand()) { + return; + } + } else { + CommandList[0].cmd = -1; + NextCommandNumber = 0; + } + + OSClearContext(&exceptionContext); + OSSetCurrentContext(&exceptionContext); + + if (cause) { + cb = Callback; + Callback = NULL; + if (cb) { + cb(cause); + } + + Breaking = FALSE; + } + + OSClearContext(&exceptionContext); + OSSetCurrentContext(context); } /* @@ -284,90 +190,27 @@ void __DVDInterruptHandler(void) * Address: 801FE460 * Size: 000084 */ -void AlarmHandler(void) -{ - /* - .loc_0x0: - mflr r0 - lis r3, 0x803D - stw r0, 0x4(r1) - addi r4, r3, 0x3020 - stwu r1, -0x8(r1) - lwz r0, 0x3294(r13) - mulli r0, r0, 0x14 - lwzx r3, r4, r0 - cmpwi r3, 0x1 - bne- .loc_0x50 - lwz r3, 0x3294(r13) - add r6, r4, r0 - addi r0, r3, 0x1 - stw r0, 0x3294(r13) - lwz r3, 0x4(r6) - lwz r4, 0x8(r6) - lwz r5, 0xC(r6) - lwz r6, 0x10(r6) - bl 0xAC - b .loc_0x74 - - .loc_0x50: - cmpwi r3, 0x2 - bne- .loc_0x74 - lwz r3, 0x3294(r13) - add r4, r4, r0 - addi r0, r3, 0x1 - stw r0, 0x3294(r13) - lwz r3, 0xC(r4) - lwz r4, 0x10(r4) - bl 0x4AC - - .loc_0x74: - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ -} +static void AlarmHandler(OSAlarm* alarm, OSContext* context) { ProcessNextCommand(); } /* * --INFO-- * Address: 801FE4E4 * Size: 000070 */ -void AlarmHandlerForTimeout(void) +static void AlarmHandlerForTimeout(OSAlarm* alarm, OSContext* context) { - /* - .loc_0x0: - mflr r0 - li r3, 0x400 - stw r0, 0x4(r1) - stwu r1, -0x2E0(r1) - stw r31, 0x2DC(r1) - addi r31, r4, 0 - bl -0x51E0 - addi r3, r1, 0x10 - bl -0x70C8 - addi r3, r1, 0x10 - bl -0x7298 - lwz r12, 0x3258(r13) - li r0, 0 - cmplwi r12, 0 - stw r0, 0x3258(r13) - beq- .loc_0x4C - mtlr r12 - li r3, 0x10 - blrl - - .loc_0x4C: - addi r3, r1, 0x10 - bl -0x70F8 - mr r3, r31 - bl -0x72C8 - lwz r0, 0x2E4(r1) - lwz r31, 0x2DC(r1) - addi r1, r1, 0x2E0 - mtlr r0 - blr - */ + OSContext tmpContext; + DVDLowCallback callback; + __OSMaskInterrupts(0x400); + OSClearContext(&tmpContext); + OSSetCurrentContext(&tmpContext); + callback = Callback; + Callback = NULL; + if (callback != NULL) { + callback(0x10); + } + OSClearContext(&tmpContext); + OSSetCurrentContext(context); } /* @@ -375,8 +218,10 @@ void AlarmHandlerForTimeout(void) * Address: ........ * Size: 000064 */ -void SetTimeoutAlarm(void) +static void SetTimeoutAlarm(OSTime timeout) { + OSCreateAlarm(&AlarmForTimeout); + OSSetAlarm(&AlarmForTimeout, timeout, AlarmHandlerForTimeout); // UNUSED FUNCTION } @@ -385,83 +230,26 @@ void SetTimeoutAlarm(void) * Address: 801FE554 * Size: 000110 */ -void Read(void) +static void Read(void* addr, u32 length, u32 offset, DVDLowCallback callback) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - li r0, 0 - stwu r1, -0x28(r1) - stw r31, 0x24(r1) - stw r30, 0x20(r1) - addi r30, r5, 0 - stw r29, 0x1C(r1) - addi r29, r4, 0 - stw r28, 0x18(r1) - addi r28, r3, 0 - stw r0, 0x3250(r13) - li r0, 0x1 - stw r6, 0x3258(r13) - lis r6, 0x803D - addi r31, r6, 0x3020 - stw r0, 0x3290(r13) - bl -0x11D8 - stw r4, 0x328C(r13) - lis r4, 0xCC00 - lis r0, 0xA0 - stw r3, 0x3288(r13) - addi r4, r4, 0x6000 - lis r3, 0xA800 - stw r3, 0x8(r4) - rlwinm r3,r30,30,2,31 - cmplw r29, r0 - stw r3, 0xC(r4) - li r0, 0x3 - stw r29, 0x10(r4) - stw r28, 0x14(r4) - stw r29, 0x18(r4) - stw r29, 0x3254(r13) - stw r0, 0x1C(r4) - ble- .loc_0xC0 - lis r3, 0x8000 - lwz r0, 0xF8(r3) - addi r3, r31, 0x68 - rlwinm r0,r0,30,2,31 - mulli r30, r0, 0x14 - bl -0x84E8 - lis r3, 0x8020 - subi r7, r3, 0x1B1C - addi r6, r30, 0 - addi r3, r31, 0x68 - li r5, 0 - bl -0x82A4 - b .loc_0xF0 - - .loc_0xC0: - lis r3, 0x8000 - lwz r0, 0xF8(r3) - addi r3, r31, 0x68 - rlwinm r0,r0,30,2,31 - mulli r30, r0, 0xA - bl -0x851C - lis r3, 0x8020 - subi r7, r3, 0x1B1C - addi r6, r30, 0 - addi r3, r31, 0x68 - li r5, 0 - bl -0x82D8 - - .loc_0xF0: - lwz r0, 0x2C(r1) - lwz r31, 0x24(r1) - lwz r30, 0x20(r1) - mtlr r0 - lwz r29, 0x1C(r1) - lwz r28, 0x18(r1) - addi r1, r1, 0x28 - blr - */ + StopAtNextInt = FALSE; + LastCommandWasRead = TRUE; + Callback = callback; + LastReadIssued = __OSGetSystemTime(); + + __DIRegs[2] = 0xa8000000; + __DIRegs[3] = offset / 4; + __DIRegs[4] = length; + __DIRegs[5] = (u32)addr; + __DIRegs[6] = length; + LastLength = length; + __DIRegs[7] = 3; + + if (length > 0xa00000) { + SetTimeoutAlarm(OSSecondsToTicks(20)); + } else { + SetTimeoutAlarm(OSSecondsToTicks(10)); + } } /* @@ -479,8 +267,16 @@ void AudioBufferOn(void) * Address: ........ * Size: 0000A0 */ -void HitCache(void) +BOOL HitCache(DVDBuffer* cur, DVDBuffer* prev) { + u32 uVar1 = (prev->offset + prev->length - 1) >> 15; + u32 uVar2 = (cur->offset >> 15); + u32 iVar3 = (DVDGetCurrentDiskID()->streaming ? TRUE : FALSE) ? 5 : 15; + + if ((uVar2 > uVar1 - 2) || (uVar2 < uVar1 + iVar3 + 3)) { + return TRUE; + } + return FALSE; // UNUSED FUNCTION } @@ -489,8 +285,11 @@ void HitCache(void) * Address: ........ * Size: 000034 */ -void DoJustRead(void) +static void DoJustRead(void* addr, u32 length, u32 offset, DVDLowCallback callback) { + CommandList[0].cmd = -1; + NextCommandNumber = 0; + Read(addr, length, offset, callback); // UNUSED FUNCTION } @@ -499,47 +298,25 @@ void DoJustRead(void) * Address: 801FE664 * Size: 000080 */ -void SeekTwiceBeforeRead(void) +static void SeekTwiceBeforeRead(void* addr, u32 length, u32 offset, DVDLowCallback callback) { - /* - .loc_0x0: - mflr r0 - lis r7, 0x803D - stw r0, 0x4(r1) - rlwinm. r8,r5,0,0,16 - addi r9, r7, 0x3020 - stwu r1, -0x8(r1) - bne- .loc_0x24 - li r10, 0 - b .loc_0x2C - - .loc_0x24: - lwz r0, 0x3278(r13) - add r10, r8, r0 - - .loc_0x2C: - li r0, 0x2 - stw r0, 0x0(r9) - li r8, 0x1 - li r7, -0x1 - stw r10, 0xC(r9) - li r0, 0 - stw r6, 0x10(r9) - stw r8, 0x14(r9) - stw r3, 0x18(r9) - mr r3, r10 - stw r4, 0x1C(r9) - mr r4, r6 - stw r5, 0x20(r9) - stw r6, 0x24(r9) - stw r7, 0x28(r9) - stw r0, 0x3294(r13) - bl 0x2AC - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ + u32 newOffset = offset & ~0x7FFF; + if (!newOffset) { + newOffset = 0; + } else { + newOffset += WorkAroundSeekLocation; + } + CommandList[0].cmd = 2; + CommandList[0].offset = newOffset; + CommandList[0].callback = callback; + CommandList[1].cmd = 1; + CommandList[1].addr = addr; + CommandList[1].length = length; + CommandList[1].offset = offset; + CommandList[1].callback = callback; + CommandList[2].cmd = -1; + NextCommandNumber = 0; + DVDLowSeek(newOffset, callback); } /* @@ -547,8 +324,17 @@ void SeekTwiceBeforeRead(void) * Address: ........ * Size: 00008C */ -void WaitBeforeRead(void) +static void WaitBeforeRead(void* addr, u32 length, u32 offset, DVDLowCallback callback, OSTime timeout) { + CommandList[0].cmd = 1; + CommandList[0].addr = addr; + CommandList[0].length = length; + CommandList[0].offset = offset; + CommandList[0].callback = callback; + CommandList[1].cmd = -1; + NextCommandNumber = 0; + OSCreateAlarm(&AlarmForWA); + OSSetAlarm(&AlarmForWA, timeout, AlarmHandler); // UNUSED FUNCTION } @@ -557,205 +343,40 @@ void WaitBeforeRead(void) * Address: 801FE6E4 * Size: 000298 */ -void DVDLowRead(void) +BOOL DVDLowRead(void* addr, u32 length, u32 offset, DVDLowCallback callback) { - /* - .loc_0x0: - mflr r0 - lis r7, 0xCC00 - stw r0, 0x4(r1) - stwu r1, -0x40(r1) - stmw r22, 0x18(r1) - addi r25, r4, 0 - addi r4, r7, 0x6000 - lis r7, 0x803D - addi r31, r7, 0x3020 - addi r24, r3, 0 - addi r26, r5, 0 - addi r30, r31, 0xCC - mr r27, r6 - stw r25, 0x18(r4) - stw r24, 0xC4(r31) - stw r25, 0xC8(r31) - stw r26, 0xCC(r31) - lwz r0, 0x3274(r13) - cmplwi r0, 0 - bne- .loc_0x78 - li r0, -0x1 - stw r0, 0x0(r31) - li r0, 0 - addi r3, r24, 0 - stw r0, 0x3294(r13) - addi r4, r25, 0 - addi r5, r26, 0 - addi r6, r27, 0 - bl -0x200 - b .loc_0x280 - - .loc_0x78: - lwz r0, 0x3274(r13) - cmplwi r0, 0x1 - bne- .loc_0x280 - lwz r0, 0x2A08(r13) - cmpwi r0, 0 - beq- .loc_0xA8 - addi r3, r24, 0 - addi r4, r25, 0 - addi r5, r26, 0 - addi r6, r27, 0 - bl -0x120 - b .loc_0x280 - - .loc_0xA8: - addi r29, r31, 0xBC - lwz r0, 0x0(r30) - addi r28, r31, 0xC0 - lwz r3, 0xBC(r31) - lwz r4, 0xC0(r31) - rlwinm r23,r0,17,15,31 - subi r0, r3, 0x1 - add r0, r4, r0 - rlwinm r22,r0,17,15,31 - bl 0x354C - lbz r0, 0x8(r3) - cmplwi r0, 0 - beq- .loc_0xE4 - li r0, 0x1 - b .loc_0xE8 - - .loc_0xE4: - li r0, 0 - - .loc_0xE8: - cmpwi r0, 0 - beq- .loc_0xF8 - li r3, 0x5 - b .loc_0xFC - - .loc_0xF8: - li r3, 0xF - - .loc_0xFC: - subi r0, r22, 0x2 - cmplw r23, r0 - bgt- .loc_0x118 - addi r0, r3, 0x3 - add r0, r22, r0 - cmplw r23, r0 - bge- .loc_0x120 - - .loc_0x118: - li r0, 0x1 - b .loc_0x124 - - .loc_0x120: - li r0, 0 - - .loc_0x124: - cmpwi r0, 0 - bne- .loc_0x154 - li r0, -0x1 - stw r0, 0x0(r31) - li r0, 0 - addi r3, r24, 0 - stw r0, 0x3294(r13) - addi r4, r25, 0 - addi r5, r26, 0 - addi r6, r27, 0 - bl -0x2DC - b .loc_0x280 - - .loc_0x154: - lwz r3, 0x0(r29) - lwz r4, 0x0(r28) - subi r3, r3, 0x1 - lwz r0, 0x0(r30) - add r3, r4, r3 - rlwinm r3,r3,17,15,31 - rlwinm r4,r0,17,15,31 - cmplw r3, r4 - beq- .loc_0x184 - addi r0, r3, 0x1 - cmplw r0, r4 - bne- .loc_0x26C - - .loc_0x184: - bl -0x14A8 - lis r5, 0x8000 - lwz r8, 0x3280(r13) - lwz r0, 0xF8(r5) - lis r5, 0x1062 - lwz r9, 0x3284(r13) - li r6, 0 - rlwinm r7,r0,30,2,31 - addi r0, r5, 0x4DD3 - mulhwu r0, r0, r7 - rlwinm r0,r0,26,6,31 - subc r9, r4, r9 - subfe r8, r8, r3 - mulli r5, r0, 0x5 - xoris r4, r6, 0x8000 - xoris r3, r8, 0x8000 - subc r0, r5, r9 - subfe r3, r3, r4 - subfe r3, r4, r4 - neg. r3, r3 - beq- .loc_0x1FC - li r0, -0x1 - stw r0, 0x0(r31) - addi r3, r24, 0 - addi r4, r25, 0 - stw r6, 0x3294(r13) - addi r5, r26, 0 - addi r6, r27, 0 - bl -0x384 - b .loc_0x280 - - .loc_0x1FC: - li r0, 0x1 - stw r0, 0x0(r31) - lis r3, 0x431C - subi r0, r3, 0x217D - stw r24, 0x4(r31) - mulhwu r0, r0, r7 - stw r25, 0x8(r31) - stw r26, 0xC(r31) - rlwinm r0,r0,17,15,31 - mulli r3, r0, 0x1F4 - stw r27, 0x10(r31) - li r0, -0x1 - stw r0, 0x14(r31) - subc r5, r5, r9 - subfe r4, r8, r6 - rlwinm r0,r3,29,3,31 - stw r6, 0x3294(r13) - addc r23, r5, r0 - adde r22, r4, r6 - addi r3, r31, 0x40 - bl -0x8824 - lis r3, 0x8020 - subi r7, r3, 0x1BA0 - addi r6, r23, 0 - addi r5, r22, 0 - addi r3, r31, 0x40 - bl -0x85E0 - b .loc_0x280 - - .loc_0x26C: - addi r3, r24, 0 - addi r4, r25, 0 - addi r5, r26, 0 - addi r6, r27, 0 - bl -0x2FC - - .loc_0x280: - lmw r22, 0x18(r1) - li r3, 0x1 - lwz r0, 0x44(r1) - addi r1, r1, 0x40 - mtlr r0 - blr - */ + OSTime diff; + u32 prev; + + __DIRegs[6] = length; + Curr.addr = addr; + Curr.length = length; + Curr.offset = offset; + + if (WorkAroundType == 0) { + DoJustRead(addr, length, offset, callback); + } else if (WorkAroundType == 1) { + if (FirstRead) { + SeekTwiceBeforeRead(addr, length, offset, callback); + } else { + if (!HitCache(&Curr, &Prev)) { + DoJustRead(addr, length, offset, callback); + } else { + prev = (Prev.offset + Prev.length - 1) >> 15; + if (prev == Curr.offset >> 15 || prev + 1 == Curr.offset >> 15) { + diff = __OSGetSystemTime() - LastReadFinished; + if (OSMillisecondsToTicks(5) < diff) { + DoJustRead(addr, length, offset, callback); + } else { + WaitBeforeRead(addr, length, offset, callback, OSMillisecondsToTicks(5) - diff + OSMicrosecondsToTicks(500)); + } + } else { + SeekTwiceBeforeRead(addr, length, offset, callback); + } + } + } + } + return TRUE; } /* @@ -763,48 +384,15 @@ void DVDLowRead(void) * Address: 801FE97C * Size: 000094 */ -void DVDLowSeek(void) +BOOL DVDLowSeek(u32 offset, DVDLowCallback callback) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - li r0, 0 - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - stw r30, 0x10(r1) - stw r4, 0x3258(r13) - lis r4, 0xCC00 - addi r4, r4, 0x6000 - stw r0, 0x3250(r13) - lis r0, 0xAB00 - stw r0, 0x8(r4) - rlwinm r0,r3,30,2,31 - lis r3, 0x803D - stw r0, 0xC(r4) - li r0, 0x1 - addi r31, r3, 0x3088 - stw r0, 0x1C(r4) - lis r4, 0x8000 - addi r3, r31, 0 - lwz r0, 0xF8(r4) - rlwinm r0,r0,30,2,31 - mulli r30, r0, 0xA - bl -0x88CC - lis r3, 0x8020 - subi r7, r3, 0x1B1C - addi r3, r31, 0 - addi r6, r30, 0 - li r5, 0 - bl -0x8688 - lwz r0, 0x1C(r1) - li r3, 0x1 - lwz r31, 0x14(r1) - lwz r30, 0x10(r1) - mtlr r0 - addi r1, r1, 0x18 - blr - */ + StopAtNextInt = FALSE; + Callback = callback; + __DIRegs[2] = 0xab000000; + __DIRegs[3] = offset / 4; + __DIRegs[7] = 1; + SetTimeoutAlarm(OSSecondsToTicks(10)); + return TRUE; } /* @@ -812,22 +400,13 @@ void DVDLowSeek(void) * Address: 801FEA10 * Size: 00002C */ -void DVDLowWaitCoverClose(void) +BOOL DVDLowWaitCoverClose(DVDLowCallback callback) { - /* - .loc_0x0: - li r0, 0x1 - stw r3, 0x3258(r13) - lis r3, 0xCC00 - stw r0, 0x326C(r13) - li r0, 0 - addi r4, r3, 0x6000 - stw r0, 0x3250(r13) - li r0, 0x2 - li r3, 0x1 - stw r0, 0x4(r4) - blr - */ + Callback = callback; + WaitingCoverClose = TRUE; + StopAtNextInt = FALSE; + __DIRegs[1] = 2; + return TRUE; } /* @@ -835,52 +414,18 @@ void DVDLowWaitCoverClose(void) * Address: 801FEA3C * Size: 0000A4 */ -void DVDLowReadDiskID(void) +BOOL DVDLowReadDiskID(DVDDiskID* diskID, DVDLowCallback callback) { - /* - .loc_0x0: - mflr r0 - li r8, 0 - stw r0, 0x4(r1) - lis r5, 0xA800 - addi r0, r5, 0x40 - stwu r1, -0x18(r1) - li r6, 0x20 - lis r5, 0x8000 - stw r31, 0x14(r1) - stw r30, 0x10(r1) - stw r4, 0x3258(r13) - lis r4, 0xCC00 - addi r7, r4, 0x6000 - stw r8, 0x3250(r13) - stw r0, 0x6008(r4) - lis r4, 0x803D - li r0, 0x3 - stw r8, 0xC(r7) - addi r31, r4, 0x3088 - stw r6, 0x10(r7) - stw r3, 0x14(r7) - mr r3, r31 - stw r6, 0x18(r7) - stw r0, 0x1C(r7) - lwz r0, 0xF8(r5) - rlwinm r0,r0,30,2,31 - mulli r30, r0, 0xA - bl -0x899C - lis r3, 0x8020 - subi r7, r3, 0x1B1C - addi r3, r31, 0 - addi r6, r30, 0 - li r5, 0 - bl -0x8758 - lwz r0, 0x1C(r1) - li r3, 0x1 - lwz r31, 0x14(r1) - lwz r30, 0x10(r1) - mtlr r0 - addi r1, r1, 0x18 - blr - */ + StopAtNextInt = FALSE; + Callback = callback; + __DIRegs[2] = 0xa8000040; + __DIRegs[3] = 0; + __DIRegs[4] = sizeof(DVDDiskID); + __DIRegs[5] = (u32)diskID; + __DIRegs[6] = sizeof(DVDDiskID); + __DIRegs[7] = 3; + SetTimeoutAlarm(OSSecondsToTicks(10)); + return TRUE; } /* @@ -888,46 +433,14 @@ void DVDLowReadDiskID(void) * Address: 801FEAE0 * Size: 00008C */ -void DVDLowStopMotor(void) +BOOL DVDLowStopMotor(DVDLowCallback callback) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - li r0, 0 - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - stw r30, 0x10(r1) - stw r3, 0x3258(r13) - lis r3, 0xCC00 - addi r4, r3, 0x6000 - stw r0, 0x3250(r13) - lis r0, 0xE300 - stw r0, 0x6008(r3) - li r0, 0x1 - lis r3, 0x803D - stw r0, 0x1C(r4) - lis r4, 0x8000 - addi r31, r3, 0x3088 - lwz r0, 0xF8(r4) - addi r3, r31, 0 - rlwinm r0,r0,30,2,31 - mulli r30, r0, 0xA - bl -0x8A28 - lis r3, 0x8020 - subi r7, r3, 0x1B1C - addi r3, r31, 0 - addi r6, r30, 0 - li r5, 0 - bl -0x87E4 - lwz r0, 0x1C(r1) - li r3, 0x1 - lwz r31, 0x14(r1) - lwz r30, 0x10(r1) - mtlr r0 - addi r1, r1, 0x18 - blr - */ + StopAtNextInt = FALSE; + Callback = callback; + __DIRegs[2] = 0xe3000000; + __DIRegs[7] = 1; + SetTimeoutAlarm(OSSecondsToTicks(10)); + return TRUE; } /* @@ -935,46 +448,14 @@ void DVDLowStopMotor(void) * Address: 801FEB6C * Size: 00008C */ -void DVDLowRequestError(void) +BOOL DVDLowRequestError(DVDLowCallback callback) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - li r0, 0 - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - stw r30, 0x10(r1) - stw r3, 0x3258(r13) - lis r3, 0xCC00 - addi r4, r3, 0x6000 - stw r0, 0x3250(r13) - lis r0, 0xE000 - stw r0, 0x6008(r3) - li r0, 0x1 - lis r3, 0x803D - stw r0, 0x1C(r4) - lis r4, 0x8000 - addi r31, r3, 0x3088 - lwz r0, 0xF8(r4) - addi r3, r31, 0 - rlwinm r0,r0,30,2,31 - mulli r30, r0, 0xA - bl -0x8AB4 - lis r3, 0x8020 - subi r7, r3, 0x1B1C - addi r3, r31, 0 - addi r6, r30, 0 - li r5, 0 - bl -0x8870 - lwz r0, 0x1C(r1) - li r3, 0x1 - lwz r31, 0x14(r1) - lwz r30, 0x10(r1) - mtlr r0 - addi r1, r1, 0x18 - blr - */ + StopAtNextInt = FALSE; + Callback = callback; + __DIRegs[2] = 0xe0000000; + __DIRegs[7] = 1; + SetTimeoutAlarm(OSSecondsToTicks(10)); + return TRUE; } /* @@ -982,50 +463,17 @@ void DVDLowRequestError(void) * Address: 801FEBF8 * Size: 00009C */ -void DVDLowInquiry(void) +BOOL DVDLowInquiry(DVDDriveInfo* info, DVDLowCallback callback) { - /* - .loc_0x0: - mflr r0 - li r6, 0x20 - stw r0, 0x4(r1) - li r0, 0 - lis r5, 0x8000 - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - stw r30, 0x10(r1) - stw r4, 0x3258(r13) - lis r4, 0xCC00 - addi r7, r4, 0x6000 - stw r0, 0x3250(r13) - lis r0, 0x1200 - stw r0, 0x6008(r4) - lis r4, 0x803D - li r0, 0x3 - stw r6, 0x10(r7) - addi r31, r4, 0x3088 - stw r3, 0x14(r7) - addi r3, r31, 0 - stw r6, 0x18(r7) - stw r0, 0x1C(r7) - lwz r0, 0xF8(r5) - rlwinm r0,r0,30,2,31 - mulli r30, r0, 0xA - bl -0x8B50 - lis r3, 0x8020 - subi r7, r3, 0x1B1C - addi r3, r31, 0 - addi r6, r30, 0 - li r5, 0 - bl -0x890C - lwz r0, 0x1C(r1) - li r3, 0x1 - lwz r31, 0x14(r1) - lwz r30, 0x10(r1) - mtlr r0 - addi r1, r1, 0x18 - blr - */ + StopAtNextInt = FALSE; + Callback = callback; + __DIRegs[2] = 0x12000000; + __DIRegs[4] = sizeof(DVDDriveInfo); + __DIRegs[5] = (u32)info; + __DIRegs[6] = sizeof(DVDDriveInfo); + __DIRegs[7] = 3; + SetTimeoutAlarm(OSSecondsToTicks(10)); + return TRUE; } /* @@ -1033,49 +481,16 @@ void DVDLowInquiry(void) * Address: 801FEC94 * Size: 000098 */ -void DVDLowAudioStream(void) +BOOL DVDLowAudioStream(u32 subcmd, u32 length, u32 offset, DVDLowCallback callback) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - li r0, 0 - stwu r1, -0x20(r1) - stw r31, 0x1C(r1) - stw r30, 0x18(r1) - stw r6, 0x3258(r13) - lis r6, 0xCC00 - addi r6, r6, 0x6000 - stw r0, 0x3250(r13) - oris r0, r3, 0xE100 - lis r3, 0x803D - stw r0, 0x8(r6) - rlwinm r0,r5,30,2,31 - addi r31, r3, 0x3088 - stw r0, 0xC(r6) - li r0, 0x1 - addi r3, r31, 0 - stw r4, 0x10(r6) - lis r4, 0x8000 - stw r0, 0x1C(r6) - lwz r0, 0xF8(r4) - rlwinm r0,r0,30,2,31 - mulli r30, r0, 0xA - bl -0x8BE8 - lis r3, 0x8020 - subi r7, r3, 0x1B1C - addi r3, r31, 0 - addi r6, r30, 0 - li r5, 0 - bl -0x89A4 - lwz r0, 0x24(r1) - li r3, 0x1 - lwz r31, 0x1C(r1) - lwz r30, 0x18(r1) - mtlr r0 - addi r1, r1, 0x20 - blr - */ + StopAtNextInt = FALSE; + Callback = callback; + __DIRegs[2] = subcmd | 0xe1000000; + __DIRegs[3] = offset >> 2; + __DIRegs[4] = length; + __DIRegs[7] = 1; + SetTimeoutAlarm(OSSecondsToTicks(10)); + return TRUE; } /* @@ -1083,46 +498,14 @@ void DVDLowAudioStream(void) * Address: 801FED2C * Size: 00008C */ -void DVDLowRequestAudioStatus(void) +BOOL DVDLowRequestAudioStatus(u32 subcmd, DVDLowCallback callback) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - li r0, 0 - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - stw r30, 0x10(r1) - stw r4, 0x3258(r13) - lis r4, 0xCC00 - addi r4, r4, 0x6000 - stw r0, 0x3250(r13) - oris r0, r3, 0xE200 - lis r3, 0x803D - stw r0, 0x8(r4) - li r0, 0x1 - addi r31, r3, 0x3088 - stw r0, 0x1C(r4) - lis r4, 0x8000 - addi r3, r31, 0 - lwz r0, 0xF8(r4) - rlwinm r0,r0,30,2,31 - mulli r30, r0, 0xA - bl -0x8C74 - lis r3, 0x8020 - subi r7, r3, 0x1B1C - addi r3, r31, 0 - addi r6, r30, 0 - li r5, 0 - bl -0x8A30 - lwz r0, 0x1C(r1) - li r3, 0x1 - lwz r31, 0x14(r1) - lwz r30, 0x10(r1) - mtlr r0 - addi r1, r1, 0x18 - blr - */ + StopAtNextInt = FALSE; + Callback = callback; + __DIRegs[2] = subcmd | 0xe2000000; + __DIRegs[7] = 1; + SetTimeoutAlarm(OSSecondsToTicks(10)); + return TRUE; } /* @@ -1130,52 +513,14 @@ void DVDLowRequestAudioStatus(void) * Address: 801FEDB8 * Size: 00009C */ -void DVDLowAudioBufferConfig(void) +BOOL DVDLowAudioBufferConfig(BOOL enable, u32 size, DVDLowCallback callback) { - /* - .loc_0x0: - mflr r0 - cmpwi r3, 0 - stw r0, 0x4(r1) - li r0, 0 - stwu r1, -0x20(r1) - stw r31, 0x1C(r1) - stw r30, 0x18(r1) - stw r5, 0x3258(r13) - stw r0, 0x3250(r13) - beq- .loc_0x2C - lis r0, 0x1 - - .loc_0x2C: - oris r0, r0, 0xE400 - lis r3, 0xCC00 - or r0, r4, r0 - addi r3, r3, 0x6000 - stw r0, 0x8(r3) - li r0, 0x1 - lis r4, 0x8000 - stw r0, 0x1C(r3) - lis r3, 0x803D - addi r31, r3, 0x3088 - lwz r0, 0xF8(r4) - addi r3, r31, 0 - rlwinm r0,r0,30,2,31 - mulli r30, r0, 0xA - bl -0x8D10 - lis r3, 0x8020 - subi r7, r3, 0x1B1C - addi r3, r31, 0 - addi r6, r30, 0 - li r5, 0 - bl -0x8ACC - lwz r0, 0x24(r1) - li r3, 0x1 - lwz r31, 0x1C(r1) - lwz r30, 0x18(r1) - mtlr r0 - addi r1, r1, 0x20 - blr - */ + StopAtNextInt = FALSE; + Callback = callback; + __DIRegs[2] = 0xe4000000 | (enable != 0 ? 0x10000 : 0) | size; + __DIRegs[7] = 1; + SetTimeoutAlarm(OSSecondsToTicks(10)); + return TRUE; } /* @@ -1183,60 +528,22 @@ void DVDLowAudioBufferConfig(void) * Address: 801FEE54 * Size: 0000BC */ -void DVDLowReset(void) +void DVDLowReset() { - /* - .loc_0x0: - mflr r0 - lis r4, 0xCC00 - stw r0, 0x4(r1) - li r0, 0x2 - stwu r1, -0x20(r1) - stmw r26, 0x8(r1) - addi r31, r4, 0x3000 - stw r0, 0x6004(r4) - lwz r30, 0x3024(r4) - rlwinm r0,r30,0,30,28 - ori r0, r0, 0x1 - stwu r0, 0x24(r31) - bl -0x1AC4 - lis r5, 0x8000 - lwz r0, 0xF8(r5) - lis r5, 0x431C - subi r5, r5, 0x217D - rlwinm r0,r0,30,2,31 - mulhwu r0, r5, r0 - rlwinm r0,r0,17,15,31 - mulli r0, r0, 0xC - addi r26, r4, 0 - addi r27, r3, 0 - rlwinm r28,r0,29,3,31 - li r29, 0 - - .loc_0x64: - bl -0x1AF8 - subc r5, r4, r26 - subfe r0, r27, r3 - xoris r4, r0, 0x8000 - xoris r3, r29, 0x8000 - subc r0, r5, r28 - subfe r3, r3, r4 - subfe r3, r4, r4 - neg. r3, r3 - bne+ .loc_0x64 - ori r0, r30, 0x5 - stw r0, 0x0(r31) - li r0, 0x1 - stw r0, 0x3268(r13) - bl -0x1B30 - stw r4, 0x3264(r13) - stw r3, 0x3260(r13) - lmw r26, 0x8(r1) - lwz r0, 0x24(r1) - addi r1, r1, 0x20 - mtlr r0 - blr - */ + u32 reg; + OSTime resetStart; + + __DIRegs[1] = 2; + reg = __PIRegs[9]; + __PIRegs[9] = (reg & ~4) | 1; + + resetStart = __OSGetSystemTime(); + while ((__OSGetSystemTime() - resetStart) < OSMicrosecondsToTicks(12)) + ; + + __PIRegs[9] = reg | 5; + ResetOccurred = TRUE; + LastResetEnd = __OSGetSystemTime(); } /* @@ -1284,16 +591,11 @@ void SetBreakAlarm(void) * Address: 801FEF10 * Size: 000014 */ -void DVDLowBreak(void) +BOOL DVDLowBreak() { - /* - .loc_0x0: - li r0, 0x1 - stw r0, 0x3250(r13) - li r3, 0x1 - stw r0, 0x3270(r13) - blr - */ + StopAtNextInt = TRUE; + Breaking = TRUE; + return TRUE; } /* @@ -1301,17 +603,13 @@ void DVDLowBreak(void) * Address: 801FEF24 * Size: 000018 */ -void DVDLowClearCallback(void) +DVDLowCallback DVDLowClearCallback() { - /* - .loc_0x0: - lis r3, 0xCC00 - li r0, 0 - stw r0, 0x6004(r3) - lwz r3, 0x3258(r13) - stw r0, 0x3258(r13) - blr - */ + DVDLowCallback old; + __DIRegs[1] = 0; + old = Callback; + Callback = NULL; + return old; } /* @@ -1329,26 +627,11 @@ void DVDLowGetCoverStatus(void) * Address: 801FEF3C * Size: 000044 */ -void __DVDLowSetWAType(void) +void __DVDLowSetWAType(u32 type, u32 location) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - addi r31, r4, 0 - stw r30, 0x10(r1) - addi r30, r3, 0 - bl -0x5FDC - stw r30, 0x3274(r13) - stw r31, 0x3278(r13) - bl -0x5FC0 - lwz r0, 0x1C(r1) - lwz r31, 0x14(r1) - lwz r30, 0x10(r1) - mtlr r0 - addi r1, r1, 0x18 - blr - */ + BOOL enabled; + enabled = OSDisableInterrupts(); + WorkAroundType = type; + WorkAroundSeekLocation = location; + OSRestoreInterrupts(enabled); } diff --git a/src/dvd/dvdqueue.c b/src/dvd/dvdqueue.c index 7bcb5068..f2c060d7 100644 --- a/src/dvd/dvdqueue.c +++ b/src/dvd/dvdqueue.c @@ -1,29 +1,23 @@ #include "types.h" +#include "Dolphin/dvd.h" + +struct DVDQueue WaitingQueue[4]; /* * --INFO-- * Address: 80201F04 * Size: 000038 */ -void __DVDClearWaitingQueue(void) +void __DVDClearWaitingQueue() { - /* - .loc_0x0: - lis r3, 0x803D - addi r3, r3, 0x31D8 - stw r3, 0x0(r3) - addi r5, r3, 0x8 - addi r4, r3, 0x10 - stw r3, 0x4(r3) - addi r3, r3, 0x18 - stw r5, 0x0(r5) - stw r5, 0x4(r5) - stw r4, 0x0(r4) - stw r4, 0x4(r4) - stw r3, 0x0(r3) - stw r3, 0x4(r3) - blr - */ + int i; + + for (i = 0; i < 4; i++) { + struct DVDQueue* ptr = &WaitingQueue[i]; + + ptr->mHead = ptr; + ptr->mTail = ptr; + } } /* @@ -31,37 +25,19 @@ void __DVDClearWaitingQueue(void) * Address: 80201F3C * Size: 000068 */ -void __DVDPushWaitingQueue(void) +BOOL __DVDPushWaitingQueue(int idx, struct DVDQueue* newTail) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - addi r31, r4, 0 - stw r30, 0x10(r1) - addi r30, r3, 0 - bl -0x8FDC - lis r4, 0x803D - rlwinm r5,r30,3,0,28 - addi r0, r4, 0x31D8 - add r5, r0, r5 - lwz r4, 0x4(r5) - stw r31, 0x0(r4) - lwz r0, 0x4(r5) - stw r0, 0x4(r31) - stw r5, 0x0(r31) - stw r31, 0x4(r5) - bl -0x8FE0 - lwz r0, 0x1C(r1) - li r3, 0x1 - lwz r31, 0x14(r1) - lwz r30, 0x10(r1) - mtlr r0 - addi r1, r1, 0x18 - blr - */ + BOOL intrEnabled = OSDisableInterrupts(); + + struct DVDQueue* waitingQueue = &WaitingQueue[idx]; + + waitingQueue->mTail->mHead = newTail; + newTail->mTail = waitingQueue->mTail; + newTail->mHead = waitingQueue; + waitingQueue->mTail = newTail; + + OSRestoreInterrupts(intrEnabled); + return TRUE; } /* @@ -79,57 +55,32 @@ void PopWaitingQueuePrio(void) * Address: 80201FA4 * Size: 0000A0 */ -void __DVDPopWaitingQueue(void) +DVDQueue* __DVDPopWaitingQueue() { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x10(r1) - stw r31, 0xC(r1) - bl -0x9038 - li r0, 0x4 - lis r4, 0x803D - mtctr r0 - addi r4, r4, 0x31D8 - li r31, 0 - - .loc_0x28: - lwz r0, 0x0(r4) - cmplw r0, r4 - beq- .loc_0x78 - bl -0x9034 - bl -0x9060 - lis r4, 0x803D - rlwinm r5,r31,3,0,28 - addi r0, r4, 0x31D8 - add r5, r0, r5 - lwz r31, 0x0(r5) - lwz r0, 0x0(r31) - stw r0, 0x0(r5) - lwz r4, 0x0(r31) - stw r5, 0x4(r4) - bl -0x9060 - li r0, 0 - stw r0, 0x0(r31) - mr r3, r31 - stw r0, 0x4(r31) - b .loc_0x8C - - .loc_0x78: - addi r4, r4, 0x8 - addi r31, r31, 0x1 - bdnz+ .loc_0x28 - bl -0x9084 - li r3, 0 - - .loc_0x8C: - lwz r0, 0x14(r1) - lwz r31, 0xC(r1) - addi r1, r1, 0x10 - mtlr r0 - blr - */ + BOOL intrEnabled = OSDisableInterrupts(); + int i; + + for (i = 0; i < 4; i++) { + if (WaitingQueue[i].mHead != &WaitingQueue[i]) { + DVDQueue* tempQueue; + DVDQueue* outQueue; + + OSRestoreInterrupts(intrEnabled); + + intrEnabled = OSDisableInterrupts(); + tempQueue = &WaitingQueue[i]; + outQueue = tempQueue->mHead; + tempQueue->mHead = outQueue->mHead; + outQueue->mHead->mTail = tempQueue; + OSRestoreInterrupts(intrEnabled); + + outQueue->mHead = nullptr; + outQueue->mTail = nullptr; + return outQueue; + } + } + OSRestoreInterrupts(intrEnabled); + return nullptr; } /* @@ -137,39 +88,19 @@ void __DVDPopWaitingQueue(void) * Address: 80202044 * Size: 000058 */ -void __DVDCheckWaitingQueue(void) +BOOL __DVDCheckWaitingQueue() { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x8(r1) - bl -0x90D4 - li r0, 0x4 - lis r4, 0x803D - mtctr r0 - addi r4, r4, 0x31D8 - - .loc_0x20: - lwz r0, 0x0(r4) - cmplw r0, r4 - beq- .loc_0x38 - bl -0x90CC - li r3, 0x1 - b .loc_0x48 - - .loc_0x38: - addi r4, r4, 0x8 - bdnz+ .loc_0x20 - bl -0x90E0 - li r3, 0 - - .loc_0x48: - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ + BOOL intrEnabled = OSDisableInterrupts(); + int i; + + for (i = 0; i < 4; i++) { + if (WaitingQueue[i].mHead != &WaitingQueue[i]) { + OSRestoreInterrupts(intrEnabled); + return TRUE; + } + } + OSRestoreInterrupts(intrEnabled); + return FALSE; } /* @@ -177,41 +108,20 @@ void __DVDCheckWaitingQueue(void) * Address: 8020209C * Size: 000060 */ -void __DVDDequeueWaitingQueue(void) +BOOL __DVDDequeueWaitingQueue(DVDQueue* queue) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - mr r31, r3 - bl -0x9134 - lwz r4, 0x4(r31) - lwz r5, 0x0(r31) - cmplwi r4, 0 - beq- .loc_0x30 - cmplwi r5, 0 - bne- .loc_0x3C - - .loc_0x30: - bl -0x9128 - li r3, 0 - b .loc_0x4C - - .loc_0x3C: - stw r5, 0x0(r4) - stw r4, 0x4(r5) - bl -0x913C - li r3, 0x1 - - .loc_0x4C: - lwz r0, 0x1C(r1) - lwz r31, 0x14(r1) - addi r1, r1, 0x18 - mtlr r0 - blr - */ + BOOL intrEnabled = OSDisableInterrupts(); + struct DVDQueue* tail = queue->mTail; + struct DVDQueue* head = queue->mHead; + + if (tail == nullptr || head == nullptr) { + OSRestoreInterrupts(intrEnabled); + return FALSE; + } + tail->mHead = head; + head->mTail = tail; + OSRestoreInterrupts(intrEnabled); + return TRUE; } /* diff --git a/src/dvd/fstload.c b/src/dvd/fstload.c index c936477b..cf1ad2bf 100644 --- a/src/dvd/fstload.c +++ b/src/dvd/fstload.c @@ -1,77 +1,55 @@ -#include "types.h" +#include "Dolphin/os.h" +#include "Dolphin/dvd.h" + +struct bb2struct { + u32 _00; + u32 offset; + s32 length; + u32 maxLength; + void* addr; +}; + +static u32 status; +static struct bb2struct* bb2; // pointer? +static u8* idTmp; // also pointer +static u8 bb2Buf[0x3F]; +static u8 block[0x30]; + +struct blah { + s8 Gamecode[4]; + s8 Company[2]; + u8 DiskID; + u8 Version; + u8 Streaming; + u8 filler[0x38 - 0x9]; + void* FSTLocationInRam; + u32 FSTMaxLength; +}; // diskinfo : 0x80000000; /* * --INFO-- * Address: 80202254 * Size: 0000D8 */ -void cb(void) +static void cb(s32 type, DVDCommandBlock* cmdBlock) { - /* - .loc_0x0: - mflr r0 - cmpwi r3, 0 - stw r0, 0x4(r1) - stwu r1, -0x18(r1) - stw r31, 0x14(r1) - addi r31, r4, 0 - ble- .loc_0x94 - lwz r0, 0x3300(r13) - cmpwi r0, 0x1 - beq- .loc_0x60 - bge- .loc_0xC4 - cmpwi r0, 0 - bge- .loc_0x38 - b .loc_0xC4 - - .loc_0x38: - li r0, 0x1 - lwz r4, 0x3304(r13) - lis r3, 0x8020 - stw r0, 0x3300(r13) - addi r7, r3, 0x2254 - addi r3, r31, 0 - li r5, 0x20 - li r6, 0x420 - bl -0xD10 - b .loc_0xC4 - - .loc_0x60: - li r0, 0x2 - lwz r6, 0x3304(r13) - stw r0, 0x3300(r13) - lis r3, 0x8020 - addi r7, r3, 0x2254 - lwz r5, 0x8(r6) - mr r3, r31 - lwz r4, 0x10(r6) - addi r0, r5, 0x1F - lwz r6, 0x4(r6) - rlwinm r5,r0,0,0,26 - bl -0xD44 - b .loc_0xC4 - - .loc_0x94: - cmpwi r3, -0x1 - beq- .loc_0xC4 - cmpwi r3, -0x4 - bne- .loc_0xC4 - li r0, 0 - stw r0, 0x3300(r13) - bl -0xA34 - lis r3, 0x8020 - lwz r4, 0x3308(r13) - addi r5, r3, 0x2254 - addi r3, r31, 0 - bl -0xCA8 - - .loc_0xC4: - lwz r0, 0x1C(r1) - lwz r31, 0x14(r1) - addi r1, r1, 0x18 - mtlr r0 - blr - */ + if (type > 0) { + switch (status) { + case 0: + status = 1; + DVDReadAbsAsyncForBS(cmdBlock, bb2, 0x20, 0x420, cb); + break; + case 1: + status = 2; + DVDReadAbsAsyncForBS(cmdBlock, bb2->addr, OSRoundUp32B(bb2->length), bb2->offset, cb); + break; + } + } else if (type == -1) { + } else if (type == -4) { + status = 0; + DVDReset(); + DVDReadDiskID(cmdBlock, idTmp, cb); + } } /* @@ -81,97 +59,36 @@ void cb(void) */ void __fstLoad(void) { - /* - .loc_0x0: - mflr r0 - lis r3, 0x802F - stw r0, 0x4(r1) - stwu r1, -0x60(r1) - stw r31, 0x5C(r1) - subi r31, r3, 0x7690 - stw r30, 0x58(r1) - stw r29, 0x54(r1) - bl -0xBA34 - lis r3, 0x803D - addi r3, r3, 0x31F8 - addi r4, r1, 0x2B - addi r0, r3, 0x1F - rlwinm r3,r4,0,0,26 - rlwinm r0,r0,0,0,26 - stw r3, 0x3308(r13) - stw r0, 0x3304(r13) - bl -0xAA4 - lis r3, 0x803D - lwz r4, 0x3308(r13) - lis r5, 0x8020 - addi r3, r3, 0x3238 - addi r5, r5, 0x2254 - bl -0xD1C - - .loc_0x60: - bl -0xA7C - cmpwi r3, 0 - bne+ .loc_0x60 - lwz r3, 0x3304(r13) - lis r29, 0x8000 - lis r30, 0x8000 - lwz r0, 0x10(r3) - addi r3, r29, 0 - li r5, 0x20 - stw r0, 0x38(r30) - lwz r4, 0x3304(r13) - lwz r0, 0xC(r4) - stw r0, 0x3C(r30) - lwz r4, 0x3308(r13) - bl -0x1FEFB0 - addi r3, r13, 0x2A20 - crclr 6, 0x6 - bl -0xAB40 - lbz r4, 0x0(r29) - mr r3, r31 - lbz r5, 0x1(r29) - crclr 6, 0x6 - lbz r6, 0x2(r29) - lbz r7, 0x3(r29) - bl -0xAB5C - lbz r4, 0x4(r29) - addi r3, r31, 0x1C - lbz r5, 0x5(r29) - crclr 6, 0x6 - bl -0xAB70 - lbz r4, 0x6(r29) - addi r3, r31, 0x34 - crclr 6, 0x6 - bl -0xAB80 - lbz r4, 0x7(r29) - addi r3, r31, 0x48 - crclr 6, 0x6 - bl -0xAB90 - lbz r0, 0x8(r30) - cmplwi r0, 0 - bne- .loc_0x10C - addi r4, r13, 0x2A24 - b .loc_0x110 - - .loc_0x10C: - addi r4, r13, 0x2A28 + int status; + char* onStr; + u8 idBuffer[64]; + void* arenaHi; + struct blah* di; - .loc_0x110: - crclr 6, 0x6 - addi r3, r31, 0x5C - bl -0xABB4 - addi r3, r13, 0x2A20 - crclr 6, 0x6 - bl -0xABC0 - lwz r3, 0x3304(r13) - lwz r3, 0x10(r3) - bl -0xBB34 - lwz r0, 0x64(r1) - lwz r31, 0x5C(r1) - lwz r30, 0x58(r1) - mtlr r0 - lwz r29, 0x54(r1) - addi r1, r1, 0x60 - blr - */ + arenaHi = OSGetArenaHi(); + idTmp = (void*)OSRoundUp32B(idBuffer); + bb2 = (void*)OSRoundUp32B(bb2Buf); + DVDReset(); + DVDReadDiskID(&block, idTmp, cb); + do { + status = DVDGetDriveStatus(); + } while (status != DVD_STATE_END); + di = (void*)OS_BASE_CACHED; + di->FSTLocationInRam = bb2->addr; + di->FSTMaxLength = bb2->maxLength; + memcpy(di, idTmp, 32); + OSReport("\n"); + OSReport(" Game Name ... %c%c%c%c\n", (u8)di->Gamecode[0], (u8)di->Gamecode[1], (u8)di->Gamecode[2], (u8)di->Gamecode[3]); + OSReport(" Company ..... %c%c\n", (u8)di->Company[0], (u8)di->Company[1]); + OSReport(" Disk # ...... %d\n", di->DiskID); + OSReport(" Game ver .... %d\n", di->Version); + if (di->Streaming == 0) { + onStr = "OFF"; + } else { + onStr = "ON"; + } + OSReport(" Streaming ... %s\n", onStr); + OSReport("\n"); + OSSetArenaHi(bb2->addr); + return; } diff --git a/src/mtx/mtx.c b/src/mtx/mtx.c index 9faa03ab..1bd58cb6 100644 --- a/src/mtx/mtx.c +++ b/src/mtx/mtx.c @@ -16,9 +16,11 @@ void C_MTXIdentity(Mtx mtx) mtx[0][0] = 1.0f; mtx[0][1] = 0.0f; mtx[0][2] = 0.0f; + mtx[1][0] = 0.0f; mtx[1][1] = 1.0f; mtx[1][2] = 0.0f; + mtx[2][0] = 0.0f; mtx[2][1] = 0.0f; mtx[2][2] = 1.0f; @@ -392,39 +394,22 @@ void MTXRotAxisRad(void) * Address: 801FDDE0 * Size: 00003C */ -void MTXTrans(Mtx m) +void MTXTrans(Mtx m, f32 xT, f32 yT, f32 zT) { m[0][0] = 1.0f; m[0][1] = 0.0f; m[0][2] = 0.0f; - m[0][3] = 0.0f; + m[0][3] = xT; + m[1][0] = 0.0f; m[1][1] = 1.0f; m[1][2] = 0.0f; - m[1][3] = 0.0f; + m[1][3] = yT; + m[2][0] = 0.0f; m[2][1] = 0.0f; m[2][2] = 1.0f; - m[2][3] = 0.0f; - - /* - .loc_0x0: - lfs f4, -0x3D28(r2) - stfs f4, 0x0(r3) - lfs f0, -0x3D24(r2) - stfs f0, 0x4(r3) - stfs f0, 0x8(r3) - stfs f1, 0xC(r3) - stfs f0, 0x10(r3) - stfs f4, 0x14(r3) - stfs f0, 0x18(r3) - stfs f2, 0x1C(r3) - stfs f0, 0x20(r3) - stfs f0, 0x24(r3) - stfs f4, 0x28(r3) - stfs f3, 0x2C(r3) - blr - */ + m[2][3] = zT; } /* @@ -442,25 +427,22 @@ void MTXTransApply(void) * Address: 801FDE1C * Size: 000038 */ -void MTXScale(void) +void MTXScale(Mtx m, f32 xS, f32 yS, f32 zS) { - /* - .loc_0x0: - stfs f1, 0x0(r3) - lfs f0, -0x3D24(r2) - stfs f0, 0x4(r3) - stfs f0, 0x8(r3) - stfs f0, 0xC(r3) - stfs f0, 0x10(r3) - stfs f2, 0x14(r3) - stfs f0, 0x18(r3) - stfs f0, 0x1C(r3) - stfs f0, 0x20(r3) - stfs f0, 0x24(r3) - stfs f3, 0x28(r3) - stfs f0, 0x2C(r3) - blr - */ + m[0][0] = xS; + m[0][1] = 0.0f; + m[0][2] = 0.0f; + m[0][3] = 0.0f; + + m[1][0] = 0.0f; + m[1][1] = yS; + m[1][2] = 0.0f; + m[1][3] = 0.0f; + + m[2][0] = 0.0f; + m[2][1] = 0.0f; + m[2][2] = zS; + m[2][3] = 0.0f; } /* @@ -508,8 +490,25 @@ void MTXLookAt(void) * Address: ........ * Size: 000094 */ -void MTXLightFrustum(void) +void MTXLightFrustum(Mtx m, f32 t, f32 b, f32 l, f32 r, f32 n, f32 scaleS, f32 scaleT, f32 transS, f32 transT) { + // this is just here for the float ordering + f32 tmp; + + tmp = 1 / (r - l); + m[0][0] = (scaleS * (n * tmp)); + m[0][1] = 0; + m[0][2] = (scaleS * (tmp * (r + l))) - transS; + m[0][3] = 0; + tmp = 1 / (t - b); + m[1][0] = 0; + m[1][1] = (scaleT * (n * tmp)); + m[1][2] = (scaleT * (tmp * (t + b))) - transT; + m[1][3] = 0; + m[2][0] = 0; + m[2][1] = 0; + m[2][2] = -1; + m[2][3] = 0; // UNUSED FUNCTION } @@ -518,8 +517,30 @@ void MTXLightFrustum(void) * Address: 801FDE54 * Size: 0000CC */ -void MTXLightPerspective(void) +void MTXLightPerspective(Mtx m, f32 fovY, f32 aspect, f32 scaleS, f32 scaleT, f32 transS, f32 transT) { + f32 angle; + f32 cot; + + angle = fovY * 0.5f; + angle = MTXDegToRad(angle); + + cot = 1.0f / tanf(angle); + + m[0][0] = (cot / aspect) * scaleS; + m[0][1] = 0.0f; + m[0][2] = -transS; + m[0][3] = 0.0f; + + m[1][0] = 0.0f; + m[1][1] = cot * scaleT; + m[1][2] = -transT; + m[1][3] = 0.0f; + + m[2][0] = 0.0f; + m[2][1] = 0.0f; + m[2][2] = -1.0f; + m[2][3] = 0.0f; /* .loc_0x0: mflr r0 diff --git a/src/mtx/mtx44.c b/src/mtx/mtx44.c index cefd92c5..50d9c3c7 100644 --- a/src/mtx/mtx44.c +++ b/src/mtx/mtx44.c @@ -1,12 +1,35 @@ -#include "types.h" +#include "Dolphin/mtx.h" +#include "math.h" /* * --INFO-- * Address: ........ * Size: 00009C */ -void MTXFrustum(void) +void MTXFrustum(Mtx m, f32 t, f32 b, f32 l, f32 r, f32 n, f32 f) { + // just for float ordering + f32 tmp; + + tmp = 1 / (r - l); + m[0][0] = (2 * n * tmp); + m[0][1] = 0; + m[0][2] = (tmp * (r + l)); + m[0][3] = 0; + tmp = 1 / (t - b); + m[1][0] = 0; + m[1][1] = (2 * n * tmp); + m[1][2] = (tmp * (t + b)); + m[1][3] = 0; + m[2][0] = 0; + m[2][1] = 0; + tmp = 1 / (f - n); + m[2][2] = (-n * tmp); + m[2][3] = (tmp * -(f * n)); + m[3][0] = 0; + m[3][1] = 0; + m[3][2] = -1; + m[3][3] = 0; // UNUSED FUNCTION } @@ -15,63 +38,32 @@ void MTXFrustum(void) * Address: 801FDF20 * Size: 0000D0 */ -void MTXPerspective(void) +void MTXPerspective(Mtx m, f32 fovY, f32 aspect, f32 n, f32 f) { - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x40(r1) - stfd f31, 0x38(r1) - fmr f31, f4 - stfd f30, 0x30(r1) - fmr f30, f3 - stfd f29, 0x28(r1) - fmr f29, f2 - stw r31, 0x24(r1) - mr r31, r3 - lfs f5, -0x3D00(r2) - lfs f0, -0x3CFC(r2) - fmuls f1, f5, f1 - fmuls f1, f0, f1 - bl 0x1DB74 - lfs f3, -0x3D10(r2) - fsubs f2, f31, f30 - fmuls f0, f31, f30 - fdivs f4, f3, f1 - fdivs f1, f4, f29 - stfs f1, 0x0(r31) - fdivs f3, f3, f2 - lfs f2, -0x3D08(r2) - stfs f2, 0x4(r31) - stfs f2, 0x8(r31) - stfs f2, 0xC(r31) - stfs f2, 0x10(r31) - fneg f1, f30 - fneg f0, f0 - stfs f4, 0x14(r31) - fmuls f1, f1, f3 - stfs f2, 0x18(r31) - fmuls f0, f3, f0 - stfs f2, 0x1C(r31) - stfs f2, 0x20(r31) - stfs f2, 0x24(r31) - stfs f1, 0x28(r31) - stfs f0, 0x2C(r31) - stfs f2, 0x30(r31) - stfs f2, 0x34(r31) - lfs f0, -0x3D04(r2) - stfs f0, 0x38(r31) - stfs f2, 0x3C(r31) - lwz r0, 0x44(r1) - lfd f31, 0x38(r1) - lfd f30, 0x30(r1) - lfd f29, 0x28(r1) - lwz r31, 0x24(r1) - addi r1, r1, 0x40 - mtlr r0 - blr - */ + f32 angle; + f32 cot; + f32 tmp; + + angle = (0.5f * fovY); + angle = angle * 0.017453293f; + cot = 1.0f / tanf(angle); + m[0][0] = (cot / aspect); + m[0][1] = 0.0f; + m[0][2] = 0.0f; + m[0][3] = 0.0f; + m[1][0] = 0.0f; + m[1][1] = (cot); + m[1][2] = 0.0f; + m[1][3] = 0.0f; + m[2][0] = 0.0f; + m[2][1] = 0.0f; + tmp = 1.0f / (f - n); + m[2][2] = (-n * tmp); + m[2][3] = (tmp * -(f * n)); + m[3][0] = 0.0f; + m[3][1] = 0.0f; + m[3][2] = -1.0f; + m[3][3] = 0.0f; } /* @@ -79,47 +71,27 @@ void MTXPerspective(void) * Address: 801FDFF0 * Size: 000098 */ -void MTXOrtho(void) +void MTXOrtho(Mtx m, f32 t, f32 b, f32 l, f32 r, f32 n, f32 f) { - /* - .loc_0x0: - fsubs f8, f4, f3 - lfs f9, -0x3D10(r2) - fsubs f0, f1, f2 - lfs f7, -0x3D0C(r2) - fadds f3, f4, f3 - fdivs f10, f9, f8 - fdivs f8, f9, f0 - fmuls f4, f7, f10 - fneg f3, f3 - fsubs f0, f6, f5 - stfs f4, 0x0(r3) - fadds f1, f1, f2 - fmuls f2, f10, f3 - lfs f3, -0x3D08(r2) - fneg f1, f1 - stfs f3, 0x4(r3) - fdivs f4, f9, f0 - stfs f3, 0x8(r3) - stfs f2, 0xC(r3) - stfs f3, 0x10(r3) - fmuls f2, f7, f8 - fneg f0, f6 - fmuls f1, f8, f1 - stfs f2, 0x14(r3) - fmuls f0, f0, f4 - stfs f3, 0x18(r3) - stfs f1, 0x1C(r3) - stfs f3, 0x20(r3) - stfs f3, 0x24(r3) - lfs f1, -0x3D04(r2) - fmuls f1, f1, f4 - stfs f1, 0x28(r3) - stfs f0, 0x2C(r3) - stfs f3, 0x30(r3) - stfs f3, 0x34(r3) - stfs f3, 0x38(r3) - stfs f9, 0x3C(r3) - blr - */ + f32 tmp; + + tmp = 1.0f / (r - l); + m[0][0] = 2.0f * tmp; + m[0][1] = 0.0f; + m[0][2] = 0.0f; + m[0][3] = (tmp * -(r + l)); + tmp = 1.0f / (t - b); + m[1][0] = 0.0f; + m[1][1] = 2.0f * tmp; + m[1][2] = 0.0f; + m[1][3] = (tmp * -(t + b)); + m[2][0] = 0.0f; + m[2][1] = 0.0f; + tmp = 1.0f / (f - n); + m[2][2] = (-1.0f * tmp); + m[2][3] = (-f * tmp); + m[3][0] = 0.0f; + m[3][1] = 0.0f; + m[3][2] = 0.0f; + m[3][3] = 1.0f; } diff --git a/src/mtx/vec.c b/src/mtx/vec.c index dbc4cf2a..0d09b1bc 100644 --- a/src/mtx/vec.c +++ b/src/mtx/vec.c @@ -1,4 +1,7 @@ -#include "types.h" +#include "Dolphin/vec.h" +#include "math.h" + +#pragma - fp_contract off /* * --INFO-- @@ -95,18 +98,15 @@ void C_VECSquareMag(void) * Address: 801FE088 * Size: 00001C */ -void PSVECSquareMag(void) -{ - /* - .loc_0x0: - psq_l f2,0x0(r3),0,0 - ps_mul f3, f2, f2 - lfs f4, 0x8(r3) - ps_madd f5, f4, f4, f3 - ps_sum0 f1, f5, f3, f3 - blr - blr - */ +ASM f32 PSVECSquareMag(register Vec* v) { +#ifdef __MWERKS__ // clang-format off + psq_l f2, Vec.x(v), 0, 0 + ps_mul f3, f2, f2 + lfs f4, Vec.z(v) + ps_madd f5, f4, f4, f3 + ps_sum0 f1, f5, f3, f3 // return square mag in f1 + blr //! whoops! an extra blr is added by the compiler since 1 is added automatically. +#endif // clang-format on } /* @@ -114,47 +114,9 @@ void PSVECSquareMag(void) * Address: 801FE0A4 * Size: 000088 */ -void VECMag(void) -{ - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x10(r1) - bl -0x28 - lfs f0, -0x3CF8(r2) - fcmpo cr0, f1, f0 - ble- .loc_0x78 - fsqrte f2, f1 - lfd f4, -0x3CF0(r2) - lfd f3, -0x3CE8(r2) - fmul f0, f2, f2 - fmul f2, f4, f2 - fmul f0, f1, f0 - fsub f0, f3, f0 - fmul f2, f2, f0 - fmul f0, f2, f2 - fmul f2, f4, f2 - fmul f0, f1, f0 - fsub f0, f3, f0 - fmul f2, f2, f0 - fmul f0, f2, f2 - fmul f2, f4, f2 - fmul f0, f1, f0 - fsub f0, f3, f0 - fmul f0, f2, f0 - fmul f0, f1, f0 - frsp f0, f0 - stfs f0, 0xC(r1) - lfs f1, 0xC(r1) - b .loc_0x78 - - .loc_0x78: - lwz r0, 0x14(r1) - addi r1, r1, 0x10 - mtlr r0 - blr - */ +f32 VECMag(Vec* v) +{ + return sqrtf(PSVECSquareMag(v)); } /* @@ -246,3 +208,5 @@ void VECDistance(void) { // UNUSED FUNCTION } + +#pragma - fp_contract reset