diff --git a/README.md b/README.md index d884ad2d..85dffd30 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,9 @@ Progress | Decompiled | Matched | Linked | Data | |:---:|:---:|:---:|:---:| -| 82.68% | 73.43% | 56.76% | 86.31% | +| 82.99% | 73.54% | 56.76% | 86.39% | -units 748/1027 complete, functions 10710/12563 matched. +units 748/1027 complete, functions 10733/12563 matched. decompiled = code with a C/C++ implementation (objdiff fuzzy), matched = byte-exact code, linked = code actually linked into the DOL, data = byte-exact data diff --git a/include/iplMemoryCardLib.h b/include/iplMemoryCardLib.h index 5699a533..8891cbcb 100644 --- a/include/iplMemoryCardLib.h +++ b/include/iplMemoryCardLib.h @@ -61,7 +61,7 @@ namespace ipl { const char* getIconComment(u8 slot, s16 fileNo); void initCardThread(); -#ifdef IPL_MEMORY_CARD_CPP +#if defined(IPL_MEMORY_CARD_CPP) || defined(IPL_CARD_SEQUENCE_CPP) BOOL shutdownCardThread(); #else bool shutdownCardThread(); diff --git a/src/scene/cardSequence/iplCardSequence.cpp b/src/scene/cardSequence/iplCardSequence.cpp new file mode 100644 index 00000000..d2defd6b --- /dev/null +++ b/src/scene/cardSequence/iplCardSequence.cpp @@ -0,0 +1,1307 @@ +#define IPL_CARD_SEQUENCE_CPP +#include "iplSystem.h" +#include "iplMemoryCardLib.h" + +#include +#include +#include +#include +#include + +namespace ipl { +namespace memorycard { + +struct CardThreadState { + CardState slots[2]; + FileInfo files[2][127]; + void* mountBuffers[2]; + s32 mounted[2]; + OSMessageQueue responses; + OSMessageQueue requests; + OSMessage responseMessages[16]; + OSMessage requestMessages[16]; + OSThread thread; + u8 stack[0x8000]; + char temporaryFileName[32]; + CARDFileInfo sourceFile; + CARDFileInfo destinationFile; + void* transferBuffer; + IconState icons[2][127]; + u8* images[2][127]; + char* comments[2][127]; + u8 commentBuffer[0x400] ATTRIBUTE_ALIGN(32); + u8* imageReadBuffer; + s32 lastCommand; + s32 lastResult; + s32 completionResult; + s32 scanComplete; + s32 scanPending; + s32 writePending; + u8 sourceSlot; + u8 valid; + u8 destinationFileNo; + u8 stopRequested; +}; + +MEMAllocator sAllocator_; +static CardThreadState* sThread; +extern "C" void* CardSequence_813D2C8C(); +static char sCardSlotName[3] = "AB"; + +FileInfo* getCardDirState() { + return sThread->files[0]; +} + +CardState* getCardSlotState() { + return sThread->slots; +} + +long getCardLastSrcSlot() { + return sThread->sourceSlot; +} + +long getCardLastSendCmd() { + return sThread->lastCommand; +} + +long getCardLastCMDFCmdResult() { + return sThread->completionResult; +} + +extern "C" s32 CardSequence_813D205C(u8 slot) { + sThread->sourceSlot = slot; + sThread->lastCommand = 9; + sThread->lastResult = -21; + return OSSendMessage(&sThread->requests, (OSMessage)(slot << 16 | 9), 0); +} + +void sendCardFormatCmd(u8 slot) { + sThread->sourceSlot = slot; + sThread->lastCommand = 1; + sThread->lastResult = -21; + sThread->completionResult = -21; + OSSendMessage(&sThread->requests, (OSMessage)(slot << 16 | 1), 0); +} + +void sendCardCopyCmd(u8 slot, s16 fileNo) { + u32 message = ((u8)fileNo << 24) | ((slot & 1) << 16); + sThread->sourceSlot = slot; + sThread->lastCommand = 2; + sThread->lastResult = -21; + sThread->completionResult = -21; + message |= 2; + OSSendMessage(&sThread->requests, (OSMessage)message, 0); +} + +void sendCardMoveCmd(u8 slot, s16 fileNo) { + u32 message = ((u8)fileNo << 24) | ((slot & 1) << 16); + sThread->sourceSlot = slot; + sThread->lastCommand = 3; + sThread->lastResult = -21; + sThread->completionResult = -21; + message |= 3; + OSSendMessage(&sThread->requests, (OSMessage)message, 0); +} + +void sendCardDeleteCmd(u8 slot, s16 fileNo) { + u32 message = ((u8)fileNo << 24) | ((slot & 1) << 16); + sThread->sourceSlot = slot; + sThread->lastCommand = 4; + sThread->lastResult = -21; + sThread->completionResult = -21; + message |= 4; + OSSendMessage(&sThread->requests, (OSMessage)message, 0); +} + +void sendCardThreadStopCmd() NO_INLINE { + CardSequence_813D205C(0); + CardSequence_813D205C(1); + OSSendMessage(&sThread->requests, (OSMessage)0xB, 0); +} + +extern "C" void CardSequence_813D222C(u8 slot) { + s32 result; + + if (sThread->valid == 1) { + result = CARDProbe(slot); + if (result != 0) { + + if (sThread->slots[slot].state < 2) { + sThread->sourceSlot = slot; + sThread->lastCommand = 0; + sThread->lastResult = -21; + if (OSSendMessage(&sThread->requests, + (OSMessage)(slot << 16), 0)) { + sThread->slots[slot].state = 3; + sThread->slots[slot].changed = 0; + } + goto done; + } + } + if (result == 0) { + if (sThread->slots[slot].state > 2 && CardSequence_813D205C(slot)) { + sThread->slots[slot].changed = 0; + } + } + } +done: + return; +} + +extern "C" s32 CardSequence_813D233C(s32 result) { + switch (result) { + case 0: + return 0; + case -2: + return -28; + case -3: + return -28; + case -4: + return -28; + case -5: + return -28; + case -6: + return -28; + case -7: + return -24; + case -8: + return -25; + case -9: + return -25; + case -10: + return -28; + case -11: + return -28; + case -12: + return -28; + case -13: + return -28; + case -27: + return -27; + default: + return -28; + } +} + +void probeCard() { + OSMessage message; + + CardSequence_813D222C(0); + CardSequence_813D222C(1); + while (OSReceiveMessage(&sThread->responses, &message, 0)) { + s16 slot = (s16)(((u32)message >> 16) & 1); + s8 command = (u8)(u32)message; + + if (command == 10) { + goto updateSlotState; + } + if (command != 11) { + goto checkCommand; + } +updateSlotState: + { + sThread->valid = (u32)message >> 8; + continue; + } +checkCommand: + if (command == 12) { + sThread->scanComplete = 1; + sThread->scanPending = 1; + continue; + } else { + + switch ((s8)(((u32)message << 16) >> 24)) { + case -27: + case -7: + case 0: + OSReport("ready or exit\n"); + + sThread->slots[slot].state = 4; + break; + case -9: + case -8: + OSReport("Card hasn't enough space\n"); + + sThread->slots[slot].state = 4; + break; + case -2: + OSReport("Wrong Device\n"); + + sThread->slots[slot].state = 5; + break; + case -6: + OSReport("Broken Card\n"); + + sThread->slots[slot].state = 6; + break; + case -13: + OSReport("Other Language\n"); + + sThread->slots[slot].state = 8; + break; + case -3: + OSReport("Detach Card\n"); + + sThread->slots[slot].state = 0; + break; + default: + OSReport("Other Error\n"); + + sThread->slots[slot].state = 10; + break; + } + + sThread->lastResult = CardSequence_813D233C((s8)(((u32)message << 16) >> 24)); + if ((s8)(u8)(u32)message != 0 && (s8)(u8)(u32)message != 9) { + sThread->completionResult = CardSequence_813D233C((s8)(((u32)message << 16) >> 24)); + if (sThread->sourceSlot != (s16)(((u32)message >> 16) & 1)) { + sThread->destinationFileNo = (u32)message >> 24; + } + } + OSReport("set slot state : ErrorCode: %d\n", sThread->lastResult); + sThread->slots[slot].changed = 1; + sThread->scanPending = 0; + } + } +} + +void initCardThread() { + EGG::Heap* heap; + u32 size; + void* memory; + MEMHeapHandle handle; + u32 offset; + u32 commentOffset; + u32 index; + + if (sThread == 0) { + heap = System::createMem1AppHeap(); + size = heap->getAllocatableSize(4); + heap = System::createMem1AppHeap(); + memory = heap->alloc(size, 4); + handle = MEMCreateExpHeapEx(memory, size, 0); + MEMInitAllocatorForExpHeap(&sAllocator_, handle, 32); + sAllocator_.heapParam2 = (u32)memory; + OSReport("HEAP CREATE: %p %d\n", memory, size); + sThread = (CardThreadState*)MEMAllocFromAllocator(&sAllocator_, sizeof(CardThreadState)); + memset(sThread, 0, sizeof(CardThreadState)); + sThread->images[0][0] = (u8*)MEMAllocFromAllocator(&sAllocator_, 0x594C00); + sThread->comments[0][0] = (char*)MEMAllocFromAllocator(&sAllocator_, 0x3F80); + sThread->imageReadBuffer = (u8*)MEMAllocFromAllocator(&sAllocator_, 0x5A00); + sThread->transferBuffer = MEMAllocFromAllocator(&sAllocator_, 0x40000); + sThread->mountBuffers[0] = MEMAllocFromAllocator(&sAllocator_, 0xA000); + sThread->mountBuffers[1] = MEMAllocFromAllocator(&sAllocator_, 0xA000); + } + + offset = 0; + commentOffset = 0; + for (index = 0; index < 127; index++) { + u8* image = sThread->images[0][0] + offset; + offset += 0x5A00; + sThread->images[0][index] = image; + char* comment = sThread->comments[0][0] + commentOffset; + commentOffset += 0x40; + sThread->comments[0][index] = comment; + } + for (index = 0; index < 127; index++) { + sThread->images[1][index] = sThread->images[0][0] + (index + 127) * 0x5A00; + sThread->comments[1][index] = sThread->comments[0][0] + (index + 127) * 0x40; + } + + OSInitMessageQueue(&sThread->responses, sThread->responseMessages, 0x10); + OSInitMessageQueue(&sThread->requests, sThread->requestMessages, 0x10); + OSCreateThread(&sThread->thread, (void* (*)(void*))CardSequence_813D2C8C, 0, + sThread->stack + sizeof(sThread->stack), 0x8000, 0x11, 0); + OSResumeThread(&sThread->thread); + OSSendMessage(&sThread->requests, (OSMessage)10, 0); + OSReport("initCardThread successfully\n"); +} + +BOOL shutdownCardThread() { + s32 result = 1; + + if (sThread != 0) { + result = 0; + if (sThread->stopRequested == 0) { + sendCardThreadStopCmd(); + sThread->stopRequested = 1; + } + if (OSIsThreadTerminated(&sThread->thread)) { + void* threadResult; + OSJoinThread(&sThread->thread, &threadResult); + MEMFreeToAllocator(&sAllocator_, sThread->mountBuffers[1]); + MEMFreeToAllocator(&sAllocator_, sThread->mountBuffers[0]); + MEMFreeToAllocator(&sAllocator_, sThread->transferBuffer); + MEMFreeToAllocator(&sAllocator_, sThread->imageReadBuffer); + MEMFreeToAllocator(&sAllocator_, sThread->images[0][0]); + MEMFreeToAllocator(&sAllocator_, sThread->comments[0][0]); + MEMFreeToAllocator(&sAllocator_, sThread); + sThread = 0; + MEMDestroyExpHeap((MEMHeapHandle)sAllocator_.heap); + System::createMem1AppHeap()->free((void*)sAllocator_.heapParam2); + System::destroyMem1AppHeap(); + OSReport("HEAP DESTROY\n"); + result = 1; + } + } + + return result; +} + +IconState* getIconStateArray() { + return sThread->icons[0]; +} + +const char* getIconComment(u8 slot, s16 index) { + return sThread->comments[slot][index]; +} + +extern "C" void CardSequence_813D2A74(s32 slot, s32 index) { + BOOL interrupts = OSDisableInterrupts(); + sThread->files[slot][index].fileNo = 0; + sThread->files[slot][index].size = 0; + sThread->files[slot][index].canCopy = 1; + sThread->files[slot][index].canMove = 1; + sThread->files[slot][index].unk_0x06 = 0; + sThread->icons[slot][index].unk_0x01 = 0; + sThread->icons[slot][index].bannerEnable = 0; + OSRestoreInterrupts(interrupts); +} + +extern "C" void CardSequence_813D2B40(s32 slot) { + u16 memSize; + u32 sectorSize; + s32 freeBytes; + s32 freeFiles; + s32 result = CARDGetMemSize(slot, &memSize); + if (result < 0) { + OSReport("Get Memsize Error-%d: %d: %d\n", slot, result, memSize); + } + result = CARDGetSectorSize(slot, §orSize); + if (result < 0) { + OSReport("Get Sector Error- %d: %d\n", result, sectorSize); + } + result = CARDFreeBlocks(slot, &freeBytes, &freeFiles); + if (result < 0) { + OSReport("Get FreeBlocks Error- %d %d\n", result, freeBytes); + } + if (CARDGetResultCode(slot) >= 0) { + BOOL interrupts = OSDisableInterrupts(); + + sThread->slots[slot].unk_0x0C = ((u32)memSize << 17) / sectorSize - 5; + sThread->slots[slot].key = sectorSize; + sThread->slots[slot].unk_0x0E = freeFiles; + sThread->slots[slot].freeBlocks = freeBytes / sectorSize; + OSRestoreInterrupts(interrupts); + OSReport("%d:%dbytes sectorsize , %d blocks , %d freeEntry\n", slot, + sThread->slots[slot].key, sThread->slots[slot].freeBlocks, + sThread->slots[slot].unk_0x0E); + } +} + +extern "C" s32 CardSequence_813D320C(s32 slot, s32 result); +extern "C" s32 CardSequence_813D32A8(s32 slot, s32 state, s32 result); +extern "C" void CardSequence_813D3380(s32 slot, s32 state, s32 fileNo); +extern "C" void CardSequence_813D33D8(s32 slot); +extern "C" s32 CardSequence_813D3424(s32 slot, s32 fileNo, CARDDir* dir); +extern "C" void CardSequence_813D3D14(s32 slot, s16 fileNo, s32 command); + +static inline void sendValidityResponse(u32 command, u32 valid) { + union { + u32 value; + struct { u32 upper : 16; u32 valid : 8; u32 command : 8; } fields; + } reply; + reply.value = command; + reply.fields.valid = valid; + OSSendMessage(&sThread->responses, (OSMessage)reply.value, 1); +} + +extern "C" void* CardSequence_813D2C8C() { + struct CardThreadLocals { + u8 compareName[6]; + u32 message; + CARDDir freeDir; + CARDDir mountDir; + CARDDir listingDir; + char fileName[33]; + } local; + BOOL brokenFile; + BOOL exitThread = FALSE; + u32 validState = TRUE; + u32 command; + s16 fileNo; + u8 slot; + s32 result; + s32 file; + u32 listingFile; + s32 outerSlot; + s32 freeFile; + s32 freeBlocks; + + sThread->mounted[0] = 0; + sThread->mounted[1] = 0; + goto loopCheck; +loopStart: + OSReceiveMessage(&sThread->requests, (OSMessage*)&local.message, 1); + OSReport("CARD THREAD: Message received %d\n", (s8)(u8)local.message); + if (validState != TRUE) { + goto loopCheck; + } + command = local.message & 0xFF; + + switch ((s8)command) { + case 10: + validState = TRUE; + OSReport("card thread valid state changed:%d\n", 1); + sendValidityResponse(command, validState); + break; + case 11: + exitThread = TRUE; + break; + case 0: { + slot = (local.message >> 16) & 1; + command = (local.message >> 14) & 4; + result = CARDMount(slot, sThread->mountBuffers[command / 4], 0); + if (result < 0) { + result = CardSequence_813D320C(slot, result); + if (result != 0) { + goto mountContinue; + } + break; + } + result = CARDCheck(slot); + if (result < 0) { + goto mountError; + } + + mountContinue: + CardSequence_813D33D8(slot); + for (file = 0; file < 0x7F; ++file) { + result = __CARDGetStatusEx(slot, (s16)file, &local.mountDir); + if (result < 0) { + result = CardSequence_813D320C(slot, result); + if (result != 0) { + continue; + } + goto loopCheck; + } else { + local.compareName[0] = 0; + local.compareName[1] = 0; + local.compareName[2] = 0; + local.compareName[3] = 0; + local.compareName[4] = 0; + local.compareName[5] = 0; + if (strncmp((const char*)local.mountDir.fileName, "Broken File", 0xB) == 0 && + memcmp(local.mountDir.gameName, local.compareName + 2, 4) == 0 && + memcmp(local.mountDir.company, local.compareName, 2) == 0) { + brokenFile = TRUE; + } else { + brokenFile = FALSE; + } + if (brokenFile) { + result = CARDFastDelete(slot, (s16)file); + if (result < 0) { + goto mountError; + } + } else { + result = CardSequence_813D3424(slot, (s16)file, &local.mountDir); + if (result < 0) { + goto mountError; + } + } + } + } + CardSequence_813D2B40(slot); + sThread->mounted[command / 4] = 1; + CardSequence_813D3380(slot, 0, 0); + OSReport("Slot %c\n", sCardSlotName[slot]); + for (listingFile = 0; listingFile < 0x7F; ++listingFile) { + if (__CARDGetStatusEx(slot, (u16)listingFile, &local.listingDir) == 0) { + memcpy(local.fileName, local.listingDir.fileName, 0x20); + local.fileName[0x20] = 0; + OSReport("%d - %s: %d blocks\n", (u16)listingFile, local.fileName, + local.listingDir.length); + } + } + break; + + mountError: + OSReport("MountError\n"); + CardSequence_813D32A8(slot, 0, result); + break; + } + case 9: + CardSequence_813D32A8((local.message >> 16) & 1, 9, -3); + break; + case 1: { + slot = (local.message >> 16) & 1; + result = CARDFormat(slot); + if (result < 0) { + CardSequence_813D32A8(slot, 1, result); + } else { + sThread->mounted[slot] = 1; + CardSequence_813D2B40(slot); + CardSequence_813D3380(slot, 1, 0); + } + break; + } + case 2: + CardSequence_813D3D14((local.message >> 16) & 1, + (s16)(u8)(local.message >> 24), (s8)command); + break; + case 3: + CardSequence_813D3D14((local.message >> 16) & 1, + (s16)(u8)(local.message >> 24), (s8)command); + break; + case 4: { + slot = (local.message >> 16) & 1; + fileNo = (s16)(local.message >> 24); + result = CARDFastDelete(slot, fileNo); + if (result < 0) { + CardSequence_813D32A8(slot, 4, result); + } else { + CardSequence_813D2A74(slot, fileNo); + CardSequence_813D2B40(slot); + CardSequence_813D3380(slot, 4, (u8)fileNo); + } + break; + } + case 12: + outerSlot = 0; + + do { + if (sThread->mounted[outerSlot] != 0) { + freeBlocks = 0; + for (freeFile = 0; freeFile < 0x7F; ++freeFile) { + result = __CARDGetStatusEx(outerSlot, freeFile, &local.freeDir); + if (result < 0 && result != -4) { + freeBlocks = -1; + break; + } + if (result == 0 && + memcmp(local.freeDir.gameName, (void*)0x80000000, 4) == 0 && + memcmp(local.freeDir.company, (void*)0x80000004, 2) == 0) { + freeBlocks += local.freeDir.length; + } + } + sThread->slots[outerSlot].unk_0x12 = freeBlocks; + } + ++outerSlot; + + } while (outerSlot < 2); + CardSequence_813D3380(0, 0xC, 0); + break; + default: + break; + } +loopCheck: + if (!exitThread) { + goto loopStart; + } + return 0; +} + +extern "C" s32 CardSequence_813D3C38(s32 slot, s16 fileNo); + +extern "C" void CardSequence_813D3140() { + u32 slot = 0; + s32 fileNo; + + s32 flagValue = 1; + do { + fileNo = 0; + + do { + sThread->files[slot][fileNo].unk_0x06 = 0; + if (sThread->mounted[slot] != 0 && + sThread->mounted[slot ^ 1] != 0 && + CardSequence_813D3C38((u8)slot, (s16)fileNo) < 0) { + sThread->files[slot][fileNo].unk_0x06 = flagValue; + } + ++fileNo; + + } while (fileNo < 0x7F); + ++slot; + + } while ((s32)slot < 2); +} + +extern "C" s32 CardSequence_813D320C(s32 slot, s32 result) { + if (result == -5) { + goto reportError; + } + if (result >= -5) { + goto positiveResult; + } + if (result >= -6) { + goto repairedCard; + } + goto reportError; + +positiveResult: + if (result < -3) { + return 1; + } + goto reportError; + +repairedCard: + { + result = CARDCheck(slot); + if (result < 0) { + return CardSequence_813D32A8(slot, 0, result); + } + OSReport("CardThread:Broken Card Repaired\n"); + return 1; + } + +reportError: + return CardSequence_813D32A8(slot, 0, result); +} + +extern "C" s32 CardSequence_813D32A8(s32 slot, s32 state, s32 result) { + if (result != -7 && result != -27 && result != -8 && result != -9 && + (CardSequence_813D33D8(slot), result != -6) && result != -13) { + OSReport("DEBUG: UnmountCard %d \n", slot); + CARDUnmount(slot); + sThread->mounted[slot & 0xFF] = 0; + } + OSReport("DEBUG: CardThread error %d %d\n", state, result); + CardSequence_813D3140(); + OSSendMessage(&sThread->responses, + (OSMessage)(((slot & 1) << 16) | ((result & 0xFF) << 8) | (state & 0xFF)), 1); + return 0; +} + +extern "C" void CardSequence_813D3380(s32 slot, s32 state, s32 fileNo) { + CardSequence_813D3140(); + OSMessageQueue* queue = &sThread->responses; + union CardThreadMessage { + u32 value; + struct { + u32 fileNo : 8; + u32 padding0 : 7; + u32 slot : 1; + u32 padding1 : 8; + u32 state : 8; + } fields; + } message; + message.value = (u32)fileNo << 24; + message.fields.slot = slot; + message.fields.state = state; + OSSendMessage(queue, (OSMessage)message.value, 1); +} + +extern "C" void CardSequence_813D33D8(s32 slot) { + s32 file = 0; + do { + CardSequence_813D2A74(slot, (s16)file); + file = file + 1; + } while (file < 0x7F); +} + +extern "C" s32 CardSequence_813D3424(s32 slot, s32 fileNo, CARDDir* dir) { + struct CardSequenceFileData { + s32 commentSectorSize; + s32 sectorSize; + CARDFileInfo fileInfo; + } local; + s32 result = CARDFastOpen(slot, fileNo, &local.fileInfo); + if (result < 0) { + return result; + } + + u32 bannerImageSize = 0; + u8 format = dir->bannerFormat & 3; + u32 iconAddress = dir->iconAddr; + u32 iconAddressBase = iconAddress & 0xFFFFFE00; + s32 iconAddressOffset = iconAddress - iconAddressBase; + + switch (format) { + case 1: { + bannerImageSize = 0xE00; + sThread->icons[slot][fileNo].bannerEnable = 1; + sThread->icons[slot][fileNo].bannerType = 9; + sThread->icons[slot][fileNo].bannerOffset = + (s32)sThread->images[slot][fileNo] - + (s32)&sThread->icons[slot][fileNo]; + sThread->icons[slot][fileNo].bannerTlutOffset = + sThread->icons[slot][fileNo].bannerOffset + 0xC00; + sThread->icons[slot][fileNo].iconOffset[0] = + sThread->icons[slot][fileNo].bannerTlutOffset + 0x200; + break; + } + case 2: { + bannerImageSize = 0x1800; + sThread->icons[slot][fileNo].bannerEnable = 1; + sThread->icons[slot][fileNo].bannerType = 5; + sThread->icons[slot][fileNo].bannerOffset = + (s32)sThread->images[slot][fileNo] - + (s32)&sThread->icons[slot][fileNo]; + sThread->icons[slot][fileNo].iconOffset[0] = + sThread->icons[slot][fileNo].bannerOffset + 0x1800; + break; + } + default: { + sThread->icons[slot][fileNo].bannerEnable = 0; + sThread->icons[slot][fileNo].bannerType = 0; + sThread->icons[slot][fileNo].iconOffset[0] = + (s32)sThread->images[slot][fileNo] - + (s32)&sThread->icons[slot][fileNo]; + break; + } + } + + s32 shift = 0; + s32 iconImageSize = 0; + s32 iconCount = 0; + s32 icon; + sThread->icons[slot][fileNo].unk_0x02 = 0; + sThread->icons[slot][fileNo].anmMax = 0; + sThread->icons[slot][fileNo].anmFrameBits = dir->iconSpeed; + sThread->icons[slot][fileNo].unk_0x06 = + (dir->iconSpeed & 3) << 2; + for (icon = 0; icon < 8; ++icon) { + s32 iconSpeed = (dir->iconSpeed >> shift) & 3; + if (iconSpeed != 0) { + sThread->icons[slot][fileNo].anmMax = + sThread->icons[slot][fileNo].anmMax + (iconSpeed << 2); + } else { + sThread->icons[slot][fileNo].unk_0x07 = + (u8)(((dir->iconSpeed >> ((iconCount - 1) * 2)) << 2) & 0xC); + goto iconSpeedDone; + } + iconCount = iconCount + 1; + shift = shift + 2; + } + sThread->icons[slot][fileNo].unk_0x07 = + ((u16)dir->iconSpeed >> 0xC) & 0xC; + +iconSpeedDone: + if ((dir->iconSpeed & 3) == 0 || (dir->iconFormat & 3) == 0) { + iconImageSize = 0; + sThread->icons[slot][fileNo].unk_0x01 = 0; + } else { + BOOL hasTlut = FALSE; + iconCount = 0; + shift = 0; + for (icon = 0; icon < 8; ++icon) { + if (((dir->iconSpeed >> shift) & 3) != 0) { + s32 iconFormat = (dir->iconFormat >> shift) & 3; + s32 paletteSize; + switch (iconFormat) { + case 1: + paletteSize = 0x400; + hasTlut = TRUE; + sThread->icons[slot][fileNo].iconFmt[iconCount] = 9; + break; + case 2: + paletteSize = 0x800; + sThread->icons[slot][fileNo].iconFmt[iconCount] = 5; + break; + case 0: { + paletteSize = 0; + u8* iconPalette = + &sThread->icons[slot][fileNo].iconFmt[iconCount]; + *iconPalette = iconPalette[-1]; + break; + } + default: + paletteSize = 0; + break; + } + if (iconCount < 7) { + sThread->icons[slot][fileNo].iconOffset[iconCount + 1] = + paletteSize + sThread->icons[slot][fileNo].iconOffset[iconCount]; + } else { + sThread->icons[0][0].iconTlutOffset = + paletteSize + sThread->icons[slot][fileNo].iconOffset[iconCount]; + } + iconImageSize += paletteSize; + sThread->icons[slot][fileNo].unk_0x02 = + sThread->icons[slot][fileNo].unk_0x02 + 1; + iconCount = iconCount + 1; + shift += 2; + } else { + sThread->icons[slot][fileNo].iconTlutOffset = + sThread->icons[slot][fileNo].iconOffset[iconCount]; + break; + } + } + if (hasTlut) { + iconImageSize += 0x200; + } + sThread->icons[slot][fileNo].unk_0x01 = 1; + sThread->icons[slot][fileNo].anmType = dir->bannerFormat & 4; + sThread->icons[slot][fileNo].anmDelta = 1; + } + + s32 totalImageSize = bannerImageSize + iconImageSize; + u32 transferSize = (totalImageSize + iconAddressOffset + 0x1FF) & 0xFFFFFE00; + result = CARDGetSectorSize(slot, (u32*)&local.sectorSize); + if (result < 0) { + result = 0; + sThread->icons[slot][fileNo].bannerEnable = 0; + sThread->icons[slot][fileNo].unk_0x01 = 0; + } else { + u32 fileSize = (u32)dir->length * local.sectorSize; + if (iconAddressBase > fileSize) { + result = 0; + sThread->icons[slot][fileNo].bannerEnable = 0; + sThread->icons[slot][fileNo].unk_0x01 = 0; + } else if (iconAddressBase + transferSize > fileSize) { + result = 0; + sThread->icons[slot][fileNo].bannerEnable = 0; + sThread->icons[slot][fileNo].unk_0x01 = 0; + } else if (totalImageSize > 0) { + result = CARDRead(&local.fileInfo, sThread->imageReadBuffer, + transferSize, iconAddressBase); + if (result < 0) { + goto closeFile; + } + memcpy(sThread->images[slot][fileNo], + (u8*)sThread->imageReadBuffer + iconAddressOffset, + totalImageSize); + DCStoreRange(sThread->images[slot][fileNo], transferSize); + } + } + + iconAddress = dir->commentAddr; + iconAddressBase = iconAddress & 0xFFFFFE00; + s32 commentDataOffset = iconAddress - iconAddressBase; + s32 imageSize = ((iconAddress + 0x23F) & 0xFFFFFE00) - iconAddressBase; + result = CARDGetSectorSize(slot, (u32*)&local.commentSectorSize); + if (result < 0) { + goto clearComment; + } + if ((s32)iconAddressBase >= 0) { + u32 fileSize = (u32)dir->length * local.commentSectorSize; + if (iconAddressBase <= fileSize) { + u32 endOffset = iconAddressBase + imageSize; + if ((s32)endOffset >= 0 && endOffset <= fileSize) { + result = CARDRead(&local.fileInfo, sThread->commentBuffer, imageSize, + iconAddressBase); + if (result < 0) { + goto clearComment; + } + memset(sThread->comments[slot][fileNo], 0, 0x40); + memcpy(sThread->comments[slot][fileNo], + sThread->commentBuffer + commentDataOffset, 0x40); + result = 0; + goto commentDone; + } + } + } + result = 0; + goto clearComment; + +clearComment: + memset(sThread->comments[slot][fileNo], 0, 0x40); + +commentDone: + if (result >= 0) { + goto closeFileSuccess; + } +closeFile: + return result; + +closeFileSuccess: + result = CARDClose(&local.fileInfo); + if (result < 0) { + return result; + } + + sThread->files[slot][fileNo].fileNo = 1; + sThread->files[slot][fileNo].size = dir->length; + sThread->files[slot][fileNo].key = dir->time; + sThread->files[slot][fileNo].canCopy = ((dir->permission >> 3) & 1) ^ 1; + sThread->files[slot][fileNo].canMove = ((dir->permission >> 4) & 1) ^ 1; + return 0; +} + +extern "C" void CardSequence_813D3C24(s32, s32) { + sThread->writePending = 0; +} + +extern "C" s32 CardSequence_813D3C38(s32 slot, s16 fileNo) { + CARDDir sourceDir; + CARDDir otherDir; + s32 result = __CARDGetStatusEx(slot, fileNo, &sourceDir); + if (result < 0) { + return result; + } + + s32 file = 0; + do { + s32 otherResult = __CARDGetStatusEx(slot ^ 1, file, &otherDir); + if (otherResult < 0 && otherResult != -4) { + return otherResult; + } + if (otherResult == 0 && memcmp(sourceDir.gameName, otherDir.gameName, 4) == 0 && + memcmp(sourceDir.company, otherDir.company, 2) == 0 && + memcmp(sourceDir.fileName, otherDir.fileName, 0x20) == 0) { + return -7; + } + file = file + 1; + } while (file < 0x7F); + return 0; +} + +extern "C" void CardSequence_813D3D14(s32 slot, s16 fileNo, s32 command) { + CARDDir createStatus; + CARDDir checkedStatus; + CARDDir ioStatus; + CARDDir sourceStatus; + CARDDir renamedStatus; + CARDDir moveStatus; + s16 destinationFileNo = -1; + s32 destinationSlot; + u32 destinationSectorSize; + u32 sourceSectorSize; + s32 commonSectorSize; + BOOL temporaryCreated = FALSE; + BOOL metadataCopied = FALSE; + BOOL cancelSent; + s32 result; + s32 sectorError; + s32 statusResult; + s32 stage; + + result = CardSequence_813D3C38(slot, fileNo); + + if (result < 0) { + CardSequence_813D32A8(slot, command, result); + goto finish; + } + + commonSectorSize = 0; + result = CARDGetSectorSize(0, &sourceSectorSize); + if (result < 0) { + sectorError = result; + goto sectorSizeCheck; + } + result = CARDGetSectorSize(1, &destinationSectorSize); + if (result < 0) { + sectorError = result; + goto sectorSizeCheck; + } + if (sourceSectorSize != destinationSectorSize) { + sectorError = -27; + } else { + commonSectorSize = sourceSectorSize; + sectorError = 0; + } +sectorSizeCheck: + if (sectorError >= 0) { + goto sectorSizeDone; + } +sectorSizeError: + CardSequence_813D32A8(slot, command, sectorError); + goto finish; +sectorSizeDone: + ; + + result = 0; + statusResult = __CARDGetStatusEx(slot, fileNo, &checkedStatus); + if (statusResult >= 0) { + switch (command) { + case 2: + if (checkedStatus.permission & 8) { + result = -10; + } + break; + case 3: + if (checkedStatus.permission & 0x10) { + result = -10; + } + break; + } + } else { + result = CARDGetResultCode(slot); + } + if (result < 0) { + CardSequence_813D32A8(slot, command, result); + goto finish; + } + + result = __CARDGetStatusEx(slot, fileNo, &createStatus); + if (result < 0) { + goto cleanup; + } + + destinationSlot = slot ^ 1; + u32 createSize = (u32)createStatus.length * commonSectorSize; + { + s32 attempt = 0; + do { + sprintf(sThread->temporaryFileName, "Broken File%03d", attempt); + result = CARDCreate(destinationSlot, sThread->temporaryFileName, + createSize, &sThread->destinationFile); + if (result < 0 && result != -7) { + OSReport("Can't Create temp File with Error."); + destinationFileNo = result; + goto createDone; + } + ++attempt; + } while (result != 0 && attempt < 0x80); + if (attempt < 0x80) { + destinationFileNo = sThread->destinationFile.fileNo; + } else { + destinationFileNo = -0x80; + } +createDone: + ; + } + if (destinationFileNo < 0) { + CardSequence_813D32A8(destinationSlot, command, destinationFileNo); + goto finish; + } + + temporaryCreated = TRUE; + stage = 0; + cancelSent = FALSE; + { + u32 sectorSize; + u32 maxBlocks; + s32 block; + s32 ioResult; + + ioResult = __CARDGetStatusEx(slot, fileNo, &ioStatus); + if (ioResult < 0) { + goto copyIoFailure; + } + stage = 1; + ioResult = CARDGetSectorSize(slot, §orSize); + if (ioResult < 0) { + goto copyIoFailure; + } + maxBlocks = 0x40000 / sectorSize; + stage = 2; + ioResult = CARDFastOpen(slot, fileNo, &sThread->sourceFile); + if (ioResult < 0) { + goto copyIoFailure; + } + block = 0; + stage = 3; + for (; block < ioStatus.length; block += maxBlocks) { + u32 blocksRemaining = (u32)ioStatus.length - block; + u32 blocks = blocksRemaining > maxBlocks ? maxBlocks : blocksRemaining; + s32 offset = block * sectorSize; + s32 size = blocks * sectorSize; + ioResult = CARDRead(&sThread->sourceFile, + sThread->transferBuffer, size, offset); + if (ioResult < 0) { + goto copyIoFailure; + } + sThread->writePending = 1; + ioResult = CARDWriteAsync(&sThread->destinationFile, + sThread->transferBuffer, size, offset, + CardSequence_813D3C24); + if (ioResult < 0) { + goto copyIoFailure; + } + while (sThread->writePending != 0) { + if (CARDProbe(slot) == 0 && !cancelSent) { + CARDCancel(&sThread->destinationFile); + cancelSent = TRUE; + } + } + } + stage = 4; + ioResult = CARDClose(&sThread->sourceFile); + if (ioResult < 0) { + goto copyIoFailure; + } + stage = 5; + ioResult = CARDClose(&sThread->destinationFile); + if (ioResult >= 0) { + result = 0; + goto copyIoDone; + } + +copyIoFailure: + switch (stage) { + case 0: + OSReport("Can't get status of Copy source File.\n"); + break; + case 1: + OSReport("Can't get sector size of Copy source card.\n"); + break; + case 2: + OSReport("Can't Open Copy source File.\n"); + break; + case 3: + OSReport("Can't read or write data.\n"); + break; + case 4: + OSReport("Can't Close Copy src File.\n"); + break; + case 5: + OSReport("Can't Close Copy dst File.\n"); + break; + } + CARDFastDelete(destinationSlot, sThread->destinationFile.fileNo); + result = ioResult; +copyIoDone: + ; + } + + if (result >= 0) { + if (command == 2) { + result = __CARDGetStatusEx(slot, fileNo, &sourceStatus); + if (result < 0) { + OSReport("Can't get status for src file\n"); + } else { + ++sourceStatus.copyTimes; + result = __CARDSetStatusEx(slot, fileNo, &sourceStatus); + if (result < 0) { + OSReport("Can't set status for src file %d\n", result); + } else { + u32 oldTime = sourceStatus.time; + sourceStatus.time = (u32)(OSGetTime() / (*(u32*)0x800000F8 >> 2)); + result = __CARDSetStatusEx(destinationSlot, destinationFileNo, + &sourceStatus); + if (result < 0) { + OSReport("Can't set status for dst file (src file copyTimes--)\n"); + if (result != -3) { + --sourceStatus.copyTimes; + } + sourceStatus.time = oldTime; + s32 restoreResult = __CARDSetStatusEx(slot, fileNo, &sourceStatus); + if (restoreResult < 0) { + OSReport("Can't set status for src file(src file copyTimes--)\n"); + } + } else { + metadataCopied = TRUE; + result = CardSequence_813D3424(destinationSlot, destinationFileNo, + &sourceStatus); + if (result < 0) { + OSReport("Can't read icon for move dstfile\n"); + } else { + result = 0; + } + if (result < 0) { + goto cleanup; + } + } + } + } + } else if (command == 3) { + s16 attempt = 0; + s32 stage = 0; + result = __CARDGetStatusEx(slot, fileNo, &moveStatus); + if (result >= 0) { + stage = 1; + renamedStatus = moveStatus; + memset(renamedStatus.company, 0, 2); + memset(renamedStatus.gameName, 0, 4); + memset(renamedStatus.fileName, 0, 0x20); + do { + sprintf((char*)renamedStatus.fileName, "Broken File%03d", attempt); + result = __CARDSetStatusEx(slot, fileNo, &renamedStatus); + if (result < 0 && result != -7) { + goto moveStatusError; + } + ++attempt; + } while (result != 0 && attempt < 0x80); + stage = 2; + result = __CARDSetStatusEx(destinationSlot, destinationFileNo, + &moveStatus); + if (result < 0) { + s32 setResult = result; + if (result == -3) { + CARDFastDelete(slot, fileNo); + CardSequence_813D2A74(slot, fileNo); + } else { + s32 restoreResult = __CARDSetStatusEx(slot, fileNo, &moveStatus); + if (restoreResult < 0) { + OSReport("Can't repair src file - carddir\n"); + } + } + result = setResult; + } else { + metadataCopied = TRUE; + stage = 3; + result = CARDFastDelete(slot, fileNo); + if (result >= 0) { + CardSequence_813D2A74(slot, fileNo); + stage = 4; + result = __CARDGetStatusEx(destinationSlot, + destinationFileNo, &moveStatus); + if (result >= 0) { + stage = 5; + if ((result = CardSequence_813D3424( + destinationSlot, destinationFileNo, &moveStatus)) < 0) { + goto moveStatusError; + } + result = 0; + goto moveStatusDone; + } + } + } + } +moveStatusError: + switch (stage) { + case 0: + OSReport("Cant' get status for src file.\n"); + break; + case 1: + OSReport("Can't set status for src file\n"); + break; + case 2: + OSReport("Can't rename dst file\n"); + break; + case 3: + OSReport("Can't Delete src file\n"); + break; + case 4: + OSReport("Cant' get status for dst file.\n"); + break; + case 5: + OSReport("Can't read icon for move dstfile\n"); + break; + } +moveStatusDone: + ; + } + } + + if (result >= 0) { + CardSequence_813D2B40(slot); + CardSequence_813D2B40(destinationSlot); + CardSequence_813D3380(slot, 0, fileNo & 0xFF); + CardSequence_813D3380(destinationSlot, command, destinationFileNo & 0xFF); + goto finish; + } + +cleanup: + destinationSlot = slot ^ 1; + result = CARDGetResultCode(destinationSlot); + if (result < 0) { + CardSequence_813D32A8(destinationSlot, command, result); + } else { + if (temporaryCreated && !metadataCopied) { + CARDFastDelete(destinationSlot, destinationFileNo); + } + if (metadataCopied) { + if (__CARDGetStatusEx(destinationSlot, destinationFileNo, + &createStatus) == 0) { + CardSequence_813D3424(destinationSlot, destinationFileNo, &createStatus); + } + } + CardSequence_813D2B40(destinationSlot); + CardSequence_813D3380(destinationSlot, 0, fileNo & 0xFF); + } + result = CARDGetResultCode(slot); + if (result < 0) { + CardSequence_813D32A8(slot, command, result); + } else { + CardSequence_813D2B40(slot); + CardSequence_813D3380(slot, 0, fileNo & 0xFF); + } + +finish: + return; +} + +} +} diff --git a/tools/decomp-assist/iplCardSequence.attempts.md b/tools/decomp-assist/iplCardSequence.attempts.md new file mode 100644 index 00000000..ce7c1717 --- /dev/null +++ b/tools/decomp-assist/iplCardSequence.attempts.md @@ -0,0 +1,89 @@ +# iplCardSequence matching attempts + +Baseline: absent source, 0/30 exact functions, 0/9852 exact code bytes. +Started from /tmp/iplCardSequence.candidate98.cpp and replaced all raw thread +field offsets with CardThreadState members and ordinary array indexing. +Reused FileInfo, CardState and IconState from iplMemoryCardLib.h. The inline +request helpers use the existing public signatures. Only this CPP defines +IPL_CARD_SEQUENCE_CPP; its guarded shutdown return declaration is BOOL. + +CardThreadState models the actual queues, OS thread, stack, file handles, +allocated buffers, per-file arrays and completion state. The embedded comment +read buffer is aligned to 32 bytes for CARDRead, giving the original 0xDBE0 +allocation size without dummy members or explicit filler bytes. + +Initial typed reconstruction: 23/30 exact functions, 3248/9852 exact code bytes. +All 43 strings were identical on the first build. After subsequent ordinary +code restructuring, every data section scores 100%: .data 1464, .bss 16, +.sbss 8 and .sdata 8 bytes. No new address symbols or artificial data exist. + +## sendCardCopyCmd / sendCardMoveCmd / sendCardDeleteCmd + +Each experiment was applied to all three functions; they differ only in the +command byte. All retained versions have 21/21 instructions, two differences: +the ori into the message register and li r5,0 occur in the opposite order. + +1. Initial packed high-byte message plus command at the send call: two differences. +2. Included command in the initial message expression: eight differences, + different packing instruction order and registers. +3. Used the public void signature and a final send statement: two differences. +4. Factored a real sendFileCommand inline helper: compiler emitted an out-of-line + helper, reducing wrappers to three instructions; reverted. +5. Packed file/slot/command with a union and bitfields: 22/21 instructions, + rlwimi and a register copy instead of the original ori. +6. Used addition of the command byte: 21/21, twelve differences, addi instead + of ori and different registers; reverted. +7. Separate final message |= command statement: same two differences. + Retained this clear form. Each final function is 90.47619%. + +## initCardThread + +1. Typed arrays indexed by a signed byte index divided by four: 173/167 + instructions, 90.69461%; signed division introduced srawi/addze. +2. Native array element index: 167/167, 27 differences. All differences are + in the two pointer-array initialization loops. +3. Ordinary ascending for loops and direct expressions in the second loop: + 167/167, 19 differences; second loop now matches. +4. Computed both buffer pointers before either store: 166/167; commoned loads + unlike the original; reverted the hoisting. +5. Unsigned image offset, comment offset and array index: still 167/167, + same 19 register differences in the first initialization loop. +Final: 98.952095%; first-loop register allocation remains open. + +## CardSequence_813D2C8C + +1. Typed fields and a command | (validState << 8) response: 300/301 instructions, + 98.106316%; compiler folded the response into ori rather than rlwimi. +2. Union response with an eight-bit validity field: 301/301, 78 differences. + Remaining differences are saved registers and response assembly scheduling. +3. Extracted sendValidityResponse as a real inline helper: identical 301/301 + result and 78 differences. Retained this readable packing helper. +Final: 97.9402%; saved-register assignment and three response instructions differ. + +## CardSequence_813D3424 + +1. Typed icon and pointer tables: 514/512 instructions, 87.08008%; signed + conversion of the byte palette offset introduced extra arithmetic, plus + different register allocation and image/comment error branches. +2. Inverted the comment-range boundary checks into a normal nested success + path: 509/512, 86.47461%; changed branch layout and error joins. +3. Indexed iconOffset by the real iconCount rather than dividing a byte + offset by four: 499/512, 87.23633%; removed the artificial signed-division + work and retained direct struct fields. Original helper/error boundaries + and register allocation still differ. +Final: 87.23633%; 499/512 instructions. Original code appears to retain +inlined helper return/error joins that this reconstruction optimizes away. + +## CardSequence_813D3D14 + +1. Typed thread fields with the original nested stages: 608/608, 97.804276%; + 186 differences, mostly saved registers, plus metadata error joins, + move-loop branch target and cleanup sign extensions. +2. Full-width destinationFileNo until API calls: 606/608, 97.458885%; + removed two original instructions and changed allocation; reverted. +3. Structured nested sector-size checks instead of label-based checks: + 604/608, 96.43915%; eliminated additional original branches; reverted. +Final: retained the first 608/608 version, 97.804276%. + +No remaining function lacks three distinct source-level attempts. No other +translation unit changes its output. Configure.py remains NonMatching.