diff --git a/configure.py b/configure.py
index 19ca6332a..4b843440b 100755
--- a/configure.py
+++ b/configure.py
@@ -1615,8 +1615,8 @@
["sysGCU/THPAudioDecode", True],
["sysGCU/THPDraw", True],
"sysGCU/THPPlayer",
- "sysGCU/THPRead",
- "sysGCU/THPVideoDecode",
+ ["sysGCU/THPRead", True],
+ ["sysGCU/THPVideoDecode", True],
"sysGCU/pikmin2THPPlayer",
["sysGCU/captionMgr", True],
"sysGCU/captionMessage",
diff --git a/docs/recommended_todo.md b/docs/recommended_todo.md
index 25a9282a0..97200ef3e 100644
--- a/docs/recommended_todo.md
+++ b/docs/recommended_todo.md
@@ -183,10 +183,10 @@
| File | Size (bytes) | File | Size (bytes) |
| ---- | ---- | ---- | ---- |
| JSTObjectSpecialActor.cpp | 2289 | illustratedBookMessage.cpp | 2964 |
-| captionMessage.cpp | 3528 | messageObj.cpp | 4504 |
-| aramMgr.cpp | 4920 | gameflow.cpp | 5333 |
-| messageMgr.cpp | 6825 | THPVideoDecode.c | 6852 |
-| THPRead.c | 7129 | section.cpp | 7138 |
+| THPRead.c | 3382 | captionMessage.cpp | 3528 |
+| messageObj.cpp | 4504 | aramMgr.cpp | 4920 |
+| gameflow.cpp | 5333 | THPVideoDecode.c | 6105 |
+| messageMgr.cpp | 6825 | section.cpp | 7138 |
| moviePlayerPauseAndDraw.cpp | 8587 | screenScene.cpp | 9274 |
| JSTObjectParticleActor.cpp | 11032 | loadResource.cpp | 11781 |
| heapStatus.cpp | 12405 | dvdThread.cpp | 12535 |
diff --git a/include/THP/THPRead.h b/include/THP/THPRead.h
index dbab0c0a0..35aecc60d 100644
--- a/include/THP/THPRead.h
+++ b/include/THP/THPRead.h
@@ -9,25 +9,17 @@
extern "C" {
#endif // ifdef __cplusplus
-BOOL CreateReadThread(int);
+BOOL CreateReadThread(OSPriority prio);
void ReadThreadStart();
void ReadThreadCancel();
-void Reader(void*);
-OSMessage PopReadedBuffer();
-BOOL PushReadedBuffer(OSMessage*);
-OSMessage PopFreeReadBuffer();
-BOOL PushFreeReadBuffer(OSMessage*);
-OSMessage PopReadedBuffer2();
-BOOL PushReadedBuffer2(OSMessage*);
-extern BOOL ReadThreadCreated;
-extern OSMessageQueue FreeReadBufferQueue;
-extern OSMessageQueue ReadedBufferQueue;
-extern OSMessageQueue ReadedBufferQueue2;
-extern THPReadBuffer* FreeReadBufferMessage[10];
-extern THPReadBuffer* ReadedBufferMessage[10];
-extern THPReadBuffer* ReadedBufferMessage2[10];
-extern OSThread ReadThread;
+// TODO: figure out if these returns THPBuffer * instead(DWARF info pls)
+void* PopReadedBuffer();
+void* PopFreeReadBuffer();
+void* PopReadedBuffer2();
+void* PushReadedBuffer(void* buffer);
+void PushFreeReadBuffer(void* buffer);
+void PushReadedBuffer2(void* buffer);
#ifdef __cplusplus
};
diff --git a/src/sysGCU/Makefile b/src/sysGCU/Makefile
index d66506838..43f0f2111 100644
--- a/src/sysGCU/Makefile
+++ b/src/sysGCU/Makefile
@@ -55,8 +55,8 @@ GC_FILES:=\
$(BUILD_DIR)/src/sysGCU/THPAudioDecode.o\
$(BUILD_DIR)/src/sysGCU/THPDraw.o\
$(BUILD_DIR)/asm/sysGCU/THPPlayer.o\
- $(BUILD_DIR)/asm/sysGCU/THPRead.o\
- $(BUILD_DIR)/asm/sysGCU/THPVideoDecode.o\
+ $(BUILD_DIR)/src/sysGCU/THPRead.o\
+ $(BUILD_DIR)/src/sysGCU/THPVideoDecode.o\
$(BUILD_DIR)/asm/sysGCU/pikmin2THPPlayer.o\
$(BUILD_DIR)/src/sysGCU/captionMgr.o\
$(BUILD_DIR)/asm/sysGCU/captionMessage.o\
diff --git a/src/sysGCU/THPRead.c b/src/sysGCU/THPRead.c
index 77b5c6040..4585c08b8 100644
--- a/src/sysGCU/THPRead.c
+++ b/src/sysGCU/THPRead.c
@@ -1,369 +1,123 @@
+#include "THP/THPPlayer.h"
#include "THP/THPRead.h"
#include "THP/THPVideoDecode.h"
+
#include "Dolphin/os.h"
#include "types.h"
+#define STACK_SIZE 4096
+#define BUFFER_COUNT 10
+
+static OSMessageQueue FreeReadBufferQueue;
+static OSMessageQueue ReadedBufferQueue;
+static OSMessageQueue ReadedBufferQueue2;
+
+static OSMessage FreeReadBufferMessage[BUFFER_COUNT];
+static OSMessage ReadedBufferMessage[BUFFER_COUNT];
+static OSMessage ReadedBufferMessage2[BUFFER_COUNT];
+
+static OSThread ReadThread;
+static u8 ReadThreadStack[STACK_SIZE];
+
+static void* Reader(void* arg);
+bool gTHPReaderDvdAccess;
+static BOOL ReadThreadCreated;
+
/*
* --INFO--
* Address: 8044F58C
* Size: 0000A0
*/
-BOOL CreateReadThread(int priority)
+BOOL CreateReadThread(OSPriority prio)
{
- BOOL created = OSCreateThread(&ReadThread, Reader, nullptr, &VideoDecodeThread, 0x1000, priority, 1);
- if (created) {
- OSInitMessageQueue(&FreeReadBufferQueue, &FreeReadBufferMessage, 10);
- OSInitMessageQueue(&ReadedBufferQueue, &ReadedBufferMessage, 10);
- OSInitMessageQueue(&ReadedBufferQueue2, &ReadedBufferMessage2, 10);
- ReadThreadCreated = TRUE;
- }
- return created != FALSE;
+ if (OSCreateThread(&ReadThread, Reader, NULL, ReadThreadStack + 4096, 4096, prio, 1) == FALSE)
+ return FALSE;
- /*
- .loc_0x0:
- stwu r1, -0x10(r1)
- mflr r0
- lis r4, 0x8045
- lis r5, 0x8050
- stw r0, 0x14(r1)
- subi r4, r4, 0x964
- mr r8, r3
- li r7, 0x1000
- stw r31, 0xC(r1)
- addi r31, r5, 0x4320
- addi r6, r31, 0x3F0
- li r5, 0
- addi r3, r31, 0xD8
- li r9, 0x1
- addi r6, r6, 0x1000
- bl -0x35D5FC
- cmpwi r3, 0
- bne- .loc_0x50
- li r3, 0
- b .loc_0x8C
+ OSInitMessageQueue(&FreeReadBufferQueue, FreeReadBufferMessage, BUFFER_COUNT);
+ OSInitMessageQueue(&ReadedBufferQueue, ReadedBufferMessage, BUFFER_COUNT);
+ OSInitMessageQueue(&ReadedBufferQueue2, ReadedBufferMessage2, BUFFER_COUNT);
+ ReadThreadCreated = TRUE;
- .loc_0x50:
- addi r3, r31, 0
- addi r4, r31, 0x60
- li r5, 0xA
- bl -0x36012C
- addi r3, r31, 0x20
- addi r4, r31, 0x88
- li r5, 0xA
- bl -0x36013C
- addi r3, r31, 0x40
- addi r4, r31, 0xB0
- li r5, 0xA
- bl -0x36014C
- li r0, 0x1
- li r3, 0x1
- stw r0, -0x63AC(r13)
-
- .loc_0x8C:
- lwz r0, 0x14(r1)
- lwz r31, 0xC(r1)
- mtlr r0
- addi r1, r1, 0x10
- blr
- */
+ return TRUE;
}
-/*
- * --INFO--
- * Address: 8044F62C
- * Size: 000034
- */
void ReadThreadStart()
{
- if (ReadThreadCreated != FALSE) {
+ if (ReadThreadCreated)
OSResumeThread(&ReadThread);
- }
- /*
- .loc_0x0:
- stwu r1, -0x10(r1)
- mflr r0
- stw r0, 0x14(r1)
- lwz r0, -0x63AC(r13)
- cmpwi r0, 0
- beq- .loc_0x24
- lis r3, 0x8050
- addi r3, r3, 0x43F8
- bl -0x35D158
-
- .loc_0x24:
- lwz r0, 0x14(r1)
- mtlr r0
- addi r1, r1, 0x10
- blr
- */
}
-/*
- * --INFO--
- * Address: 8044F660
- * Size: 00003C
- */
void ReadThreadCancel()
{
- if (ReadThreadCreated != FALSE) {
+ if (ReadThreadCreated) {
OSCancelThread(&ReadThread);
ReadThreadCreated = FALSE;
}
- /*
- .loc_0x0:
- stwu r1, -0x10(r1)
- mflr r0
- stw r0, 0x14(r1)
- lwz r0, -0x63AC(r13)
- cmpwi r0, 0
- beq- .loc_0x2C
- lis r3, 0x8050
- addi r3, r3, 0x43F8
- bl -0x35D3E8
- li r0, 0
- stw r0, -0x63AC(r13)
-
- .loc_0x2C:
- lwz r0, 0x14(r1)
- mtlr r0
- addi r1, r1, 0x10
- blr
- */
}
-/*
- * --INFO--
- * Address: 8044F69C
- * Size: 0000EC
- */
-void Reader(void*)
+static void* Reader(void* arg)
{
- /*
- .loc_0x0:
- stwu r1, -0x20(r1)
- mflr r0
- lis r3, 0x8051
- stw r0, 0x24(r1)
- stmw r27, 0xC(r1)
- addi r31, r3, 0x4490
- li r28, 0
- lwz r30, 0xB8(r31)
- lwz r29, 0xBC(r31)
-
- .loc_0x24:
- bl 0x12C
- li r0, 0x1
- mr r27, r3
- stb r0, -0x63B0(r13)
- mr r3, r31
- mr r5, r29
- mr r6, r30
- lwz r4, 0x0(r27)
- li r7, 0x2
- bl -0x372C64
- li r0, 0
- cmpw r3, r29
- stb r0, -0x63B0(r13)
- beq- .loc_0x88
- cmpwi r3, -0x1
- bne- .loc_0x6C
- li r0, -0x1
- stw r0, 0xA8(r31)
-
- .loc_0x6C:
- cmpwi r28, 0
- bne- .loc_0x7C
- li r3, 0
- bl -0x10E0
-
- .loc_0x7C:
- lis r3, 0x8050
- addi r3, r3, 0x43F8
- bl -0x35CFA4
-
- .loc_0x88:
- stw r28, 0x4(r27)
- mr r3, r27
- bl 0x90
- lwz r0, 0xC0(r31)
- add r30, r30, r29
- lwz r6, 0x50(r31)
- add r4, r28, r0
- lwz r5, 0x0(r27)
- divwu r3, r4, r6
- subi r0, r6, 0x1
- lwz r29, 0x0(r5)
- mullw r3, r3, r6
- sub r3, r4, r3
- cmplw r3, r0
- bne- .loc_0xE4
- lbz r0, 0xA6(r31)
- rlwinm. r0,r0,0,31,31
- beq- .loc_0xD8
- lwz r30, 0x64(r31)
- b .loc_0xE4
-
- .loc_0xD8:
- lis r3, 0x8050
- addi r3, r3, 0x43F8
- bl -0x35D000
-
- .loc_0xE4:
- addi r28, r28, 0x1
- b .loc_0x24
- */
+ THPReadBuffer* buf;
+ s32 curFrame;
+ s32 status;
+ s32 offset = ActivePlayer.mInitOffset;
+ s32 initReadSize = ActivePlayer.mInitReadSize;
+ s32 frame = 0;
+
+ while (TRUE) {
+ buf = (THPReadBuffer*)PopFreeReadBuffer();
+ gTHPReaderDvdAccess = true;
+ status = DVDReadPrio(&ActivePlayer.mFileInfo, buf->mPtr, initReadSize, offset, 2);
+ gTHPReaderDvdAccess = false;
+ if (status != initReadSize) {
+ if (status == -1)
+ ActivePlayer.mDvdError = -1;
+ if (frame == 0)
+ PrepareReady(FALSE);
+
+ OSSuspendThread(&ReadThread);
+ }
+
+ buf->mFrameNumber = frame;
+ PushReadedBuffer(buf);
+ offset += initReadSize;
+ initReadSize = *(s32*)buf->mPtr;
+
+ curFrame = (frame + ActivePlayer.mInitReadFrame) % ActivePlayer.mHeader.mNumFrames;
+ if (curFrame == ActivePlayer.mHeader.mNumFrames - 1) {
+ if ((ActivePlayer.mPlayFlag & 1))
+ offset = ActivePlayer.mHeader.mMovieDataOffsets;
+ else
+ OSSuspendThread(&ReadThread);
+ }
+
+ frame++;
+ }
}
-/*
- * --INFO--
- * Address: 8044F788
- * Size: 000034
- */
-OSMessage PopReadedBuffer()
+void* PopReadedBuffer()
{
- OSMessage msg;
- OSReceiveMessage(&ReadedBufferQueue, msg, OS_MESSAGE_BLOCK);
- return msg;
- /*
- .loc_0x0:
- stwu r1, -0x10(r1)
- mflr r0
- lis r3, 0x8050
- li r5, 0x1
- stw r0, 0x14(r1)
- addi r4, r1, 0x8
- addi r3, r3, 0x4340
- bl -0x3601C0
- lwz r0, 0x14(r1)
- lwz r3, 0x8(r1)
- mtlr r0
- addi r1, r1, 0x10
- blr
- */
+ void* buffer;
+ OSReceiveMessage(&ReadedBufferQueue, &buffer, OS_MESSAGE_BLOCK);
+ return buffer;
}
-/*
- * --INFO--
- * Address: 8044F7BC
- * Size: 000030
- */
-BOOL PushReadedBuffer(OSMessage* msg)
-{
- return OSSendMessage(&ReadedBufferQueue, msg, OS_MESSAGE_BLOCK);
- /*
- .loc_0x0:
- stwu r1, -0x10(r1)
- mflr r0
- lis r5, 0x8050
- mr r4, r3
- stw r0, 0x14(r1)
- addi r3, r5, 0x4340
- li r5, 0x1
- bl -0x3602BC
- lwz r0, 0x14(r1)
- mtlr r0
- addi r1, r1, 0x10
- blr
- */
-}
+void* PushReadedBuffer(void* buffer) { OSSendMessage(&ReadedBufferQueue, buffer, 1); }
-/*
- * --INFO--
- * Address: 8044F7EC
- * Size: 000034
- */
-OSMessage PopFreeReadBuffer()
+void* PopFreeReadBuffer()
{
- OSMessage msg;
- OSReceiveMessage(&FreeReadBufferQueue, msg, OS_MESSAGE_BLOCK);
- return msg;
- /*
- .loc_0x0:
- stwu r1, -0x10(r1)
- mflr r0
- lis r3, 0x8050
- li r5, 0x1
- stw r0, 0x14(r1)
- addi r4, r1, 0x8
- addi r3, r3, 0x4320
- bl -0x360224
- lwz r0, 0x14(r1)
- lwz r3, 0x8(r1)
- mtlr r0
- addi r1, r1, 0x10
- blr
- */
+ void* buffer;
+ OSReceiveMessage(&FreeReadBufferQueue, &buffer, OS_MESSAGE_BLOCK);
+ return buffer;
}
-/*
- * --INFO--
- * Address: 8044F820
- * Size: 000030
- */
-BOOL PushFreeReadBuffer(OSMessage*)
-{
- /*
- .loc_0x0:
- stwu r1, -0x10(r1)
- mflr r0
- lis r5, 0x8050
- mr r4, r3
- stw r0, 0x14(r1)
- addi r3, r5, 0x4320
- li r5, 0x1
- bl -0x360320
- lwz r0, 0x14(r1)
- mtlr r0
- addi r1, r1, 0x10
- blr
- */
-}
+void PushFreeReadBuffer(void* buffer) { OSSendMessage(&FreeReadBufferQueue, buffer, 1); }
-/*
- * --INFO--
- * Address: 8044F850
- * Size: 000034
- */
-OSMessage PopReadedBuffer2()
+void* PopReadedBuffer2()
{
- /*
- .loc_0x0:
- stwu r1, -0x10(r1)
- mflr r0
- lis r3, 0x8050
- li r5, 0x1
- stw r0, 0x14(r1)
- addi r4, r1, 0x8
- addi r3, r3, 0x4360
- bl -0x360288
- lwz r0, 0x14(r1)
- lwz r3, 0x8(r1)
- mtlr r0
- addi r1, r1, 0x10
- blr
- */
+ void* buffer;
+ OSReceiveMessage(&ReadedBufferQueue2, &buffer, OS_MESSAGE_BLOCK);
+ return buffer;
}
-/*
- * --INFO--
- * Address: 8044F884
- * Size: 000030
- */
-BOOL PushReadedBuffer2(OSMessage*)
-{
- /*
- .loc_0x0:
- stwu r1, -0x10(r1)
- mflr r0
- lis r5, 0x8050
- mr r4, r3
- stw r0, 0x14(r1)
- addi r3, r5, 0x4360
- li r5, 0x1
- bl -0x360384
- lwz r0, 0x14(r1)
- mtlr r0
- addi r1, r1, 0x10
- blr
- */
-}
+void PushReadedBuffer2(void* buffer) { OSSendMessage(&ReadedBufferQueue2, buffer, 1); }
diff --git a/src/sysGCU/THPVideoDecode.c b/src/sysGCU/THPVideoDecode.c
index 17e0d504e..b3b891edf 100644
--- a/src/sysGCU/THPVideoDecode.c
+++ b/src/sysGCU/THPVideoDecode.c
@@ -72,6 +72,7 @@ void VideoDecodeThreadCancel()
*/
static void* VideoDecoder(void* arg)
{
+ BOOL old;
THPReadBuffer* thpBuffer;
int decodedFrame = ActivePlayer.mVideoDecodeCount;
@@ -85,8 +86,9 @@ static void* VideoDecoder(void* arg)
VideoDecode(thpBuffer);
PushFreeReadBuffer((OSMessage*)thpBuffer);
- OSDisableInterrupts();
- OSRestoreInterrupts(ActivePlayer.mVideoDecodeCount++); // regswap r3/r4 here
+ old = OSDisableInterrupts();
+ ActivePlayer.mVideoDecodeCount++;
+ OSRestoreInterrupts(old);
}
}
@@ -119,10 +121,10 @@ static void* VideoDecoderForOnMemory(void* arg)
readBuffer.mPtr = (u8*)arg;
while (TRUE) {
if (ActivePlayer.mAudioExist) {
- OSDisableInterrupts();
- OSRestoreInterrupts(ActivePlayer.mVideoDecodeCount++); // regswap r3/r4 here
- while (i--) {
+ while (ActivePlayer.mVideoDecodeCount < 0) {
+ BOOL old = OSDisableInterrupts();
ActivePlayer.mVideoDecodeCount++;
+ OSRestoreInterrupts(old);
s32 remaining = (frame + ActivePlayer.mInitReadFrame) % ActivePlayer.mHeader.mNumFrames;
if (remaining == ActivePlayer.mHeader.mNumFrames - 1) {
if ((ActivePlayer.mPlayFlag & 1) == 0)
@@ -172,6 +174,7 @@ static void VideoDecode(THPReadBuffer* readBuffer)
s32 i;
u32* tileOffsets;
u8* tile;
+ BOOL old;
tileOffsets = (u32*)(readBuffer->mPtr + 8);
tile = &readBuffer->mPtr[ActivePlayer.mCompInfo.mNumComponents * 4] + 8;
@@ -191,8 +194,9 @@ static void VideoDecode(THPReadBuffer* readBuffer)
}
textureSet->mFrameNumber = readBuffer->mFrameNumber;
PushDecodedTextureSet((OSMessage*)textureSet);
- OSDisableInterrupts();
- OSRestoreInterrupts(ActivePlayer.mVideoDecodeCount++); // regswap r3/r4 here
+ old = OSDisableInterrupts();
+ ActivePlayer.mVideoDecodeCount++;
+ OSRestoreInterrupts(old);
}
}