1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2025-02-23 15:55:47 +00:00
oot/src/boot/z_std_dma.c

631 lines
22 KiB
C
Raw Normal View History

/**
* @file z_std_dma.c
*
* This file implements a system for structuring the ROM image and retrieving data. It is designed to have the same
* external interface regardless of whether the ROM segments are compressed or not.
*
* The ROM image is partitioned into regions that are entered into the DMA data table `gDmaDataTable`. External code
* does not directly address locations on the ROM image directly, instead a "Virtual ROM" addressing is used. Virtual
* ROM is defined to be the ROM address of a segment in a totally uncompressed ROM. For uncompressed ROMs, "physical"
* ROM and VROM addresses coincide. The DMA data table converts VROM to ROM addresses so that code may exclusively use
* VROM addresses even if the ROM is compressed.
*
* External code typically submits requests to the DMA Manager asking for a transfer in terms of Virtual ROM; the DMA
* Manager translates this to the physical ROM address, performs the transfer to RAM and decompresses the data if
* required.
* Requests are processed in the order they are received and may be submitted both synchronously and asynchronously.
*
* There are some additional provisions to ensure that audio DMA is particularly high-speed, the audio data is assumed
* to be uncompressed and the request queue and address translation is skipped.
*/
#include "global.h"
#include "terminal.h"
2020-03-17 00:31:30 -04:00
StackEntry sDmaMgrStackInfo;
OSMesgQueue sDmaMgrMsgQueue;
OSMesg sDmaMgrMsgBuf[32];
2020-03-17 00:31:30 -04:00
OSThread sDmaMgrThread;
STACK(sDmaMgrStack, 0x500);
2020-03-17 00:31:30 -04:00
const char* sDmaMgrCurFileName;
s32 sDmaMgrCurFileLine;
u32 gDmaMgrVerbose = 0;
size_t gDmaMgrDmaBuffSize = DMAMGR_DEFAULT_BUFSIZE;
u32 sDmaMgrIsRomCompressed = false;
// dmadata filenames
#define DEFINE_DMA_ENTRY(_0, nameString) nameString,
#if OOT_DEBUG
const char* sDmaMgrFileNames[] = {
#include "tables/dmadata_table.h"
2020-03-17 00:31:30 -04:00
};
#endif
2020-03-17 00:31:30 -04:00
#undef DEFINE_DMA_ENTRY
#if OOT_DEBUG
/**
* Compares `str1` and `str2`.
*
* @return
* 0 if str1 and str2 are the same,
* -1 if the first character that does not match has a smaller value in str1 than str2,
* +1 if the first character that does not match has a greater value in str1 than str2
*/
s32 DmaMgr_StrCmp(const char* str1, const char* str2) {
while (*str1 != '\0') {
if (*str1 > *str2) {
2020-03-17 00:31:30 -04:00
return 1;
2020-03-22 22:19:43 +01:00
}
if (*str1 < *str2) {
2020-03-17 00:31:30 -04:00
return -1;
2020-03-22 22:19:43 +01:00
}
str1++;
str2++;
2020-03-17 00:31:30 -04:00
}
if (*str2 > '\0') {
2020-03-17 00:31:30 -04:00
return -1;
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
return 0;
}
#endif
2020-03-17 00:31:30 -04:00
/**
* Transfer `size` bytes from physical ROM address `rom` to `ram`.
*
* This function is intended for internal use only, however it is possible to use this function externally in which
* case it behaves as a synchronous transfer, data is available as soon as this function returns.
*
* Transfers are divided into chunks based on the current value of `gDmaMgrDmaBuffSize` to avoid congestion of the PI
* so that higher priority transfers can still be carried out in a timely manner. The transfers are sent in a queue to
* the OS PI Manager which performs the transfer.
*
* @return 0 if successful, -1 if the DMA could not be queued with the PI Manager.
*/
s32 DmaMgr_DmaRomToRam(uintptr_t rom, void* ram, size_t size) {
2020-03-17 00:31:30 -04:00
OSIoMesg ioMsg;
OSMesgQueue queue;
OSMesg msg;
s32 ret;
size_t buffSize = gDmaMgrDmaBuffSize;
2020-03-17 00:31:30 -04:00
2020-03-22 22:19:43 +01:00
if (buffSize == 0) {
buffSize = DMAMGR_DEFAULT_BUFSIZE;
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
osInvalICache(ram, size);
osInvalDCache(ram, size);
2020-03-17 00:31:30 -04:00
osCreateMesgQueue(&queue, &msg, 1);
2020-03-22 22:19:43 +01:00
while (size > buffSize) {
// The system avoids large DMAs as these would stall the PI for too long, potentially causing issues with
// audio. To allow audio to continue to DMA whenever it needs to, other DMAs are split into manageable chunks.
2020-03-17 00:31:30 -04:00
if (1) {} // Necessary to match
ioMsg.hdr.pri = OS_MESG_PRI_NORMAL;
ioMsg.hdr.retQueue = &queue;
ioMsg.devAddr = rom;
ioMsg.dramAddr = ram;
2020-03-17 00:31:30 -04:00
ioMsg.size = buffSize;
if (gDmaMgrVerbose == 10) {
PRINTF("%10lld ノーマルDMA %08x %08x %08x (%d)\n", OS_CYCLES_TO_USEC(osGetTime()), ioMsg.dramAddr,
ioMsg.devAddr, ioMsg.size, MQ_GET_COUNT(&gPiMgrCmdQueue));
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
ret = osEPiStartDma(gCartHandle, &ioMsg, OS_READ);
if (ret != 0) {
goto end;
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
if (gDmaMgrVerbose == 10) {
PRINTF("%10lld ノーマルDMA START (%d)\n", OS_CYCLES_TO_USEC(osGetTime()), MQ_GET_COUNT(&gPiMgrCmdQueue));
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
osRecvMesg(&queue, NULL, OS_MESG_BLOCK);
if (gDmaMgrVerbose == 10) {
PRINTF("%10lld ノーマルDMA END (%d)\n", OS_CYCLES_TO_USEC(osGetTime()), MQ_GET_COUNT(&gPiMgrCmdQueue));
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
size -= buffSize;
rom += buffSize;
ram = (u8*)ram + buffSize;
2020-03-17 00:31:30 -04:00
}
if (1) { // Also necessary to match
s32 pad[2];
}
2020-03-17 00:31:30 -04:00
ioMsg.hdr.pri = OS_MESG_PRI_NORMAL;
ioMsg.hdr.retQueue = &queue;
ioMsg.devAddr = rom;
ioMsg.dramAddr = ram;
2020-03-17 00:31:30 -04:00
ioMsg.size = size;
if (gDmaMgrVerbose == 10) {
PRINTF("%10lld ノーマルDMA %08x %08x %08x (%d)\n", OS_CYCLES_TO_USEC(osGetTime()), ioMsg.dramAddr,
ioMsg.devAddr, ioMsg.size, MQ_GET_COUNT(&gPiMgrCmdQueue));
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
ret = osEPiStartDma(gCartHandle, &ioMsg, OS_READ);
if (ret != 0) {
goto end;
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
osRecvMesg(&queue, NULL, OS_MESG_BLOCK);
if (gDmaMgrVerbose == 10) {
PRINTF("%10lld ノーマルDMA END (%d)\n", OS_CYCLES_TO_USEC(osGetTime()), MQ_GET_COUNT(&gPiMgrCmdQueue));
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
end:
osInvalICache(ram, size);
osInvalDCache(ram, size);
2020-03-17 00:31:30 -04:00
return ret;
}
/**
* Callback function to facilitate audio DMA. Audio DMA does not use the request queue as audio data is often needed
* very soon after the request is sent, requiring a higher priority method for enqueueing a DMA on the OS PI command
* queue.
*
* @param pihandle Cartridge ROM PI Handle.
* @param mb IO Message describing the transfer.
* @param direction Read or write. (Only read is allowed)
* @return 0 if the IO Message was successfully put on the OS PI command queue, < 0 otherwise
*/
s32 DmaMgr_AudioDmaHandler(OSPiHandle* pihandle, OSIoMesg* mb, s32 direction) {
2020-03-17 00:31:30 -04:00
s32 ret;
ASSERT(pihandle == gCartHandle, "pihandle == carthandle", "../z_std_dma.c", 530);
ASSERT(direction == OS_READ, "direction == OS_READ", "../z_std_dma.c", 531);
ASSERT(mb != NULL, "mb != NULL", "../z_std_dma.c", 532);
2020-03-17 00:31:30 -04:00
if (gDmaMgrVerbose == 10) {
PRINTF("%10lld サウンドDMA %08x %08x %08x (%d)\n", OS_CYCLES_TO_USEC(osGetTime()), mb->dramAddr, mb->devAddr,
mb->size, MQ_GET_COUNT(&gPiMgrCmdQueue));
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
ret = osEPiStartDma(pihandle, mb, direction);
if (ret != 0) {
PRINTF("OOPS!!\n");
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
return ret;
}
/**
* DMA read from disk drive. Blocks the current thread until DMA completes.
*
* @param ram RAM address to write data to.
* @param rom ROM address to read from.
* @param size Size of transfer.
*/
void DmaMgr_DmaFromDriveRom(void* ram, uintptr_t rom, size_t size) {
OSPiHandle* handle = osDriveRomInit();
2020-03-17 00:31:30 -04:00
OSMesgQueue queue;
OSMesg msg;
OSIoMesg ioMsg;
osInvalICache(ram, size);
osInvalDCache(ram, size);
2020-03-17 00:31:30 -04:00
osCreateMesgQueue(&queue, &msg, 1);
ioMsg.hdr.retQueue = &queue;
ioMsg.hdr.pri = OS_MESG_PRI_NORMAL;
ioMsg.devAddr = rom;
ioMsg.dramAddr = ram;
2020-03-17 00:31:30 -04:00
ioMsg.size = size;
handle->transferInfo.cmdType = 2;
osEPiStartDma(handle, &ioMsg, OS_READ);
osRecvMesg(&queue, NULL, OS_MESG_BLOCK);
2020-03-17 00:31:30 -04:00
}
#if OOT_DEBUG
/**
* DMA error encountered, print error messages and bring up the crash screen.
*
* @param req DMA Request causing the error.
* @param filename DMA data filename associated with the operation that errored.
* @param errorName Error name string.
* @param errorDesc Error description string.
*
* This function does not return.
*/
NORETURN void DmaMgr_Error(DmaRequest* req, const char* filename, const char* errorName, const char* errorDesc) {
uintptr_t vrom = req->vromAddr;
void* ram = req->dramAddr;
size_t size = req->size;
2020-03-22 22:19:43 +01:00
char buff1[80];
char buff2[80];
2020-03-17 00:31:30 -04:00
PRINTF("%c", BEL);
PRINTF(VT_FGCOL(RED));
// "DMA Fatal Error"
PRINTF("DMA致命的エラー(%s)\nROM:%X RAM:%X SIZE:%X %s\n",
errorDesc != NULL ? errorDesc : (errorName != NULL ? errorName : "???"), vrom, ram, size,
filename != NULL ? filename : "???");
2020-03-17 00:31:30 -04:00
if (req->filename != NULL) { // Source file name that issued the DMA request
PRINTF("DMA ERROR: %s %d", req->filename, req->line);
} else if (sDmaMgrCurFileName != NULL) {
PRINTF("DMA ERROR: %s %d", sDmaMgrCurFileName, sDmaMgrCurFileLine);
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
PRINTF(VT_RST);
2020-03-17 00:31:30 -04:00
if (req->filename != NULL) {
2020-03-17 00:31:30 -04:00
sprintf(buff1, "DMA ERROR: %s %d", req->filename, req->line);
} else if (sDmaMgrCurFileName != NULL) {
2020-03-17 00:31:30 -04:00
sprintf(buff1, "DMA ERROR: %s %d", sDmaMgrCurFileName, sDmaMgrCurFileLine);
2020-03-22 22:19:43 +01:00
} else {
Fix/cleanup/rephrase miscellaneous stuff (#983) * Add parens around params usage in VEC_SET macro * Remove unnecessary space character in a xml * Use defines instead of magic values in head/tail magic comments * Use `OS_USEC_TO_CYCLES` to make a time look better in `Graph_TaskSet00` * `0x25800` -> `sizeof(u16[SCREEN_HEIGHT][SCREEN_WIDTH])` * `0x803DA800` -> `0x80400000 - frame buffer size` * Use `OS_VI_` defines instead of hex * Add empty line after some variable declarations * Remove unused `extern CutsceneData` in `z_bg_dy_yoseizo.c` * `Matrix_MtxFToYXZRotS` does not use `MTXMODE_` * Use `MTXMODE_` more * Remove `ASCII_TO_U32`, use `'IS64'` * Add explicit `!= NULL` in some ternaries * Use `INV_CONTENT`, `AMMO` macros more * Use `PLAYER_AP_` enum more to compare to `Player#heldItemActionParam` * Get rid of lowercase hex (outside libultra) * `gWindMill*` -> `gWindmill*` * Format and small fix enums in `z_boss_mo.h` * Use `CHECK_BTN_ANY` more * Fix xz/xy mistake in comment in tektite * Rephrase comments mentioning "the devs" in a more neutral way * Clean-up some objectively useless parens * Fix some negative values written as u16 instead of s16 in ichains * `SKJ_ACTON_` -> `SKJ_ACTION_` * Run formatter * Fix unk_ offset of `TransformUpdateIndex#unk_10` -> `unk_0E` * Remove comments using in-game text * Remove `U` suffix from integer literals * Revert "Remove `ASCII_TO_U32`, use `'IS64'`" This reverts commit c801337dde9fe5e8b7a7ecf85ad3629bf5b87aaf. * Use `PLAYER_STR_*` to compare to `CUR_UPG_VALUE(UPG_STRENGTH)` * Add empty line after decl x2 * Revert "Use `PLAYER_STR_*` to compare to `CUR_UPG_VALUE(UPG_STRENGTH)`" This reverts commit d80bdb32da449edc74e02b8ab3f5a2c532e74bdb. * Make `CUR_UPG_VALUE(UPG_STRENGTH)` compare to integers (eventually needs its own enum) * Only use `PLAYER_SHIELD_` enum with `Player#currentShield` * Only use `PLAYER_TUNIC_` enum with `Player#currentTunic`
2021-10-03 05:17:09 +02:00
sprintf(buff1, "DMA ERROR: %s", errorName != NULL ? errorName : "???");
2020-03-22 22:19:43 +01:00
}
sprintf(buff2, "%07X %08X %X %s", vrom, ram, size, filename != NULL ? filename : "???");
2020-03-17 00:31:30 -04:00
Fault_AddHungupAndCrashImpl(buff1, buff2);
}
#define DMA_ERROR(req, filename, errorName, errorDesc, file, line) DmaMgr_Error(req, filename, errorName, errorDesc)
#else
#define DMA_ERROR(req, filename, errorName, errorDesc, file, line) Fault_AddHungupAndCrash(file, line)
#endif
/**
* Searches the filesystem for the entry containing the address `vrom`. Retrieves the name of this entry from
* the array of file names.
*
* @param vrom Virtual ROM location
* @return Pointer to associated filename
*/
const char* DmaMgr_FindFileName(uintptr_t vrom) {
#if OOT_DEBUG
DmaEntry* iter = gDmaDataTable;
const char** name = sDmaMgrFileNames;
2020-03-17 00:31:30 -04:00
while (iter->vromEnd != 0) {
2020-03-22 22:19:43 +01:00
if (vrom >= iter->vromStart && vrom < iter->vromEnd) {
2020-03-17 00:31:30 -04:00
return *name;
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
iter++;
name++;
}
Fix/cleanup/rephrase miscellaneous stuff (#983) * Add parens around params usage in VEC_SET macro * Remove unnecessary space character in a xml * Use defines instead of magic values in head/tail magic comments * Use `OS_USEC_TO_CYCLES` to make a time look better in `Graph_TaskSet00` * `0x25800` -> `sizeof(u16[SCREEN_HEIGHT][SCREEN_WIDTH])` * `0x803DA800` -> `0x80400000 - frame buffer size` * Use `OS_VI_` defines instead of hex * Add empty line after some variable declarations * Remove unused `extern CutsceneData` in `z_bg_dy_yoseizo.c` * `Matrix_MtxFToYXZRotS` does not use `MTXMODE_` * Use `MTXMODE_` more * Remove `ASCII_TO_U32`, use `'IS64'` * Add explicit `!= NULL` in some ternaries * Use `INV_CONTENT`, `AMMO` macros more * Use `PLAYER_AP_` enum more to compare to `Player#heldItemActionParam` * Get rid of lowercase hex (outside libultra) * `gWindMill*` -> `gWindmill*` * Format and small fix enums in `z_boss_mo.h` * Use `CHECK_BTN_ANY` more * Fix xz/xy mistake in comment in tektite * Rephrase comments mentioning "the devs" in a more neutral way * Clean-up some objectively useless parens * Fix some negative values written as u16 instead of s16 in ichains * `SKJ_ACTON_` -> `SKJ_ACTION_` * Run formatter * Fix unk_ offset of `TransformUpdateIndex#unk_10` -> `unk_0E` * Remove comments using in-game text * Remove `U` suffix from integer literals * Revert "Remove `ASCII_TO_U32`, use `'IS64'`" This reverts commit c801337dde9fe5e8b7a7ecf85ad3629bf5b87aaf. * Use `PLAYER_STR_*` to compare to `CUR_UPG_VALUE(UPG_STRENGTH)` * Add empty line after decl x2 * Revert "Use `PLAYER_STR_*` to compare to `CUR_UPG_VALUE(UPG_STRENGTH)`" This reverts commit d80bdb32da449edc74e02b8ab3f5a2c532e74bdb. * Make `CUR_UPG_VALUE(UPG_STRENGTH)` compare to integers (eventually needs its own enum) * Only use `PLAYER_SHIELD_` enum with `Player#currentShield` * Only use `PLAYER_TUNIC_` enum with `Player#currentTunic`
2021-10-03 05:17:09 +02:00
//! @bug Since there is no return, in case the file isn't found, the return value will be a pointer to the end
2020-03-22 22:19:43 +01:00
// of gDmaDataTable
#ifdef AVOID_UB
return "";
#endif
#else
return NULL;
#endif
2020-03-17 00:31:30 -04:00
}
const char* DmaMgr_GetFileName(uintptr_t vrom) {
#if OOT_DEBUG
const char* ret = DmaMgr_FindFileName(vrom);
if (ret == NULL) {
2020-03-17 00:31:30 -04:00
return "(unknown)";
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
if (DmaMgr_StrCmp(ret, "kanji") == 0 || DmaMgr_StrCmp(ret, "link_animetion") == 0) {
// This check may be related to these files being too large to be loaded all at once, however a NULL filename
// does not prevent them from being loaded.
2020-03-17 00:31:30 -04:00
return NULL;
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
return ret;
#else
return "";
#endif
2020-03-17 00:31:30 -04:00
}
void DmaMgr_ProcessRequest(DmaRequest* req) {
uintptr_t vrom = req->vromAddr;
void* ram = req->dramAddr;
size_t size = req->size;
uintptr_t romStart;
size_t romSize;
u8 found = false;
2020-03-17 00:31:30 -04:00
DmaEntry* iter;
const char* filename;
#if OOT_DEBUG
// Get the filename (for debugging)
2020-03-17 00:31:30 -04:00
filename = DmaMgr_GetFileName(vrom);
#else
// An unused empty string is defined in .rodata of retail builds, suggesting it was used near here.
filename = "";
#endif
2020-03-17 00:31:30 -04:00
// Iterate through the DMA data table until the region containing the vrom address for this request is found
iter = gDmaDataTable;
while (iter->vromEnd != 0) {
2020-03-22 22:19:43 +01:00
if (vrom >= iter->vromStart && vrom < iter->vromEnd) {
// Found the region this request falls into
if (0) {
// The string is defined in .rodata of debug builds but not used, suggesting a debug print is here
// but was optimized out in some way.
PRINTF("DMA ROM:%08X RAM:%08X SIZE:%08X %s\n", vrom, ram, size, filename);
}
2020-03-17 00:31:30 -04:00
2020-03-22 22:19:43 +01:00
if (iter->romEnd == 0) {
// romEnd of 0 indicates that the file is uncompressed. Files that are stored uncompressed can have
// only part of their content loaded into RAM, so DMA only the requested region.
2020-03-22 22:19:43 +01:00
if (iter->vromEnd < vrom + size) {
// Error, vrom + size ends up in a different file than it started in
// "DMA transfers cannot cross segment boundaries"
DMA_ERROR(req, filename, "Segment Alignment Error",
"セグメント境界をまたがってDMA転送することはできません", "../z_std_dma.c", 726);
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
DmaMgr_DmaRomToRam(iter->romStart + (vrom - iter->vromStart), ram, size);
2020-03-17 00:31:30 -04:00
found = true;
if (0) {
PRINTF("No Press ROM:%08X RAM:%08X SIZE:%08X\n", vrom, ram, size);
}
2020-03-22 22:19:43 +01:00
} else {
// File is compressed. Files that are stored compressed must be loaded into RAM all at once.
2020-03-17 00:31:30 -04:00
romStart = iter->romStart;
romSize = iter->romEnd - iter->romStart;
2020-03-22 22:19:43 +01:00
if (vrom != iter->vromStart) {
// Error, requested vrom is not the start of a file
// "DMA transfer cannot be performed from the middle of a compressed segment"
DMA_ERROR(req, filename, "Can't Transfer Segment",
"圧縮されたセグメントの途中からはDMA転送することはできません", "../z_std_dma.c", 746);
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
2020-03-22 22:19:43 +01:00
if (size != iter->vromEnd - iter->vromStart) {
// Error, only part of the file was requested
// "It is not possible to DMA only part of a compressed segment"
DMA_ERROR(req, filename, "Can't Transfer Segment",
"圧縮されたセグメントの一部だけをDMA転送することはできません", "../z_std_dma.c", 752);
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
// Reduce the thread priority and decompress the file, the decompression routine handles the DMA
// in chunks. Restores the thread priority when done.
osSetThreadPri(NULL, THREAD_PRI_DMAMGR_LOW);
2020-03-17 00:31:30 -04:00
Yaz0_Decompress(romStart, ram, romSize);
osSetThreadPri(NULL, THREAD_PRI_DMAMGR);
2020-03-17 00:31:30 -04:00
found = true;
if (0) {
PRINTF(" Press ROM:%X RAM:%X SIZE:%X\n", vrom, ram, size);
}
2020-03-17 00:31:30 -04:00
}
break;
}
iter++;
}
2020-03-22 22:19:43 +01:00
if (!found) {
// Requested region was not found in the filesystem
if (sDmaMgrIsRomCompressed) {
// Error, rom is compressed so DMA may only be requested within the filesystem bounds
// "Corresponding data does not exist"
DMA_ERROR(req, NULL, "DATA DON'T EXIST", "該当するデータが存在しません", "../z_std_dma.c", 771);
2020-03-17 00:31:30 -04:00
return;
} else {
// ROM is uncompressed, allow arbitrary DMA even if the region is not marked in the filesystem
DmaMgr_DmaRomToRam(vrom, ram, size);
2020-03-17 00:31:30 -04:00
if (0) {
PRINTF("No Press ROM:%08X RAM:%08X SIZE:%08X (非公式)\n", vrom, ram, size);
}
}
2020-03-17 00:31:30 -04:00
}
}
void DmaMgr_ThreadEntry(void* arg) {
2020-03-17 00:31:30 -04:00
OSMesg msg;
DmaRequest* req;
2020-03-22 22:19:43 +01:00
// "DMA manager thread execution start"
PRINTF("DMAマネージャスレッド実行開始\n");
2020-03-22 22:19:43 +01:00
while (true) {
// Wait for DMA Requests to arrive from other threads
osRecvMesg(&sDmaMgrMsgQueue, &msg, OS_MESG_BLOCK);
2020-03-17 00:31:30 -04:00
req = (DmaRequest*)msg;
if (req == NULL) {
2020-03-17 00:31:30 -04:00
break;
2020-03-22 22:19:43 +01:00
}
if (0) {
PRINTF("DMA登録受付 dmap=%08x\n", req);
}
2020-03-17 00:31:30 -04:00
// Process the DMA request
DmaMgr_ProcessRequest(req);
// Notify the sender that the request has been processed
if (req->notifyQueue != NULL) {
osSendMesg(req->notifyQueue, req->notifyMsg, OS_MESG_NOBLOCK);
if (0) {
PRINTF("osSendMesg: dmap=%08x, mq=%08x, m=%08x \n", req, req->notifyQueue, req->notifyMsg);
}
2020-03-17 00:31:30 -04:00
}
}
// "DMA manager thread execution end"
PRINTF("DMAマネージャスレッド実行終了\n");
2020-03-17 00:31:30 -04:00
}
/**
* Submit an asynchronous DMA request. Unlike other DMA requests, this will not block the current thread. Data arrival
* is not immediate however, ensure that the request has completed by awaiting a message sent to `queue` when the DMA
* operation has completed.
*
* @param req DMA request structure, filled out internally.
* @param ram Location in DRAM for data to be written.
* @param vrom Virtual ROM location for data to be read.
* @param size Transfer size.
* @param queue Message queue to notify with `msg` once the transfer is complete.
* @param msg Message to send to `queue` once the transfer is complete.
* @return 0
*/
s32 DmaMgr_RequestAsync(DmaRequest* req, void* ram, uintptr_t vrom, size_t size, u32 unk, OSMesgQueue* queue,
OSMesg msg) {
2020-03-17 00:31:30 -04:00
static s32 sDmaMgrQueueFullLogged = 0;
#if OOT_DEBUG
if ((ram == NULL) || (osMemSize < OS_K0_TO_PHYSICAL(ram) + size) || (vrom & 1) || (vrom > 0x4000000) ||
(size == 0) || (size & 1)) {
// The line numbers for `DMA_ERROR` are only used in retail builds, but this usage was removed so
// its line number is unknown.
//! @bug `req` is passed to `DMA_ERROR` without rom, ram and size being set
DMA_ERROR(req, NULL, "ILLIGAL DMA-FUNCTION CALL", "パラメータ異常です", "../z_std_dma.c", 0);
2020-03-22 22:19:43 +01:00
}
#endif
2020-03-17 00:31:30 -04:00
req->vromAddr = vrom;
req->dramAddr = ram;
2020-03-17 00:31:30 -04:00
req->size = size;
req->unk_14 = 0;
req->notifyQueue = queue;
req->notifyMsg = msg;
#if OOT_DEBUG
if (1 && (sDmaMgrQueueFullLogged == 0) && MQ_IS_FULL(&sDmaMgrMsgQueue)) {
sDmaMgrQueueFullLogged++;
PRINTF("%c", BEL);
PRINTF(VT_FGCOL(RED));
// "dmaEntryMsgQ is full. Reconsider your queue size."
PRINTF("dmaEntryMsgQが一杯です。キューサイズの再検討をおすすめします。");
LOG_NUM("(sizeof(dmaEntryMsgBufs) / sizeof(dmaEntryMsgBufs[0]))", ARRAY_COUNT(sDmaMgrMsgBuf), "../z_std_dma.c",
952);
PRINTF(VT_RST);
2020-03-17 00:31:30 -04:00
}
#endif
2020-03-17 00:31:30 -04:00
osSendMesg(&sDmaMgrMsgQueue, (OSMesg)req, OS_MESG_BLOCK);
2020-03-17 00:31:30 -04:00
return 0;
}
/**
* Submit a synchronous DMA request. This will block the current thread until the requested transfer is complete. Data
* is immediately available as soon as this function returns.
*
* @param ram Location in DRAM for data to be written.
* @param vrom Virtual ROM location for data to be read.
* @param size Transfer size.
* @return 0
*/
s32 DmaMgr_RequestSync(void* ram, uintptr_t vrom, size_t size) {
2020-03-17 00:31:30 -04:00
DmaRequest req;
OSMesgQueue queue;
OSMesg msg;
s32 ret;
osCreateMesgQueue(&queue, &msg, 1);
ret = DmaMgr_RequestAsync(&req, ram, vrom, size, 0, &queue, NULL);
if (ret == -1) { // DmaMgr_RequestAsync only returns 0
2020-03-17 00:31:30 -04:00
return ret;
2020-03-22 22:19:43 +01:00
}
osRecvMesg(&queue, NULL, OS_MESG_BLOCK);
2020-03-17 00:31:30 -04:00
return 0;
}
void DmaMgr_Init(void) {
2020-03-17 00:31:30 -04:00
const char** name;
s32 idx;
DmaEntry* iter;
// DMA the dma data table to RAM
DmaMgr_DmaRomToRam((uintptr_t)_dmadataSegmentRomStart, _dmadataSegmentStart,
(u32)(_dmadataSegmentRomEnd - _dmadataSegmentRomStart));
PRINTF("dma_rom_ad[]\n");
2020-03-17 00:31:30 -04:00
#if OOT_DEBUG
2020-03-17 00:31:30 -04:00
name = sDmaMgrFileNames;
iter = gDmaDataTable;
idx = 0;
// Check if the ROM is compressed (romEnd not 0)
sDmaMgrIsRomCompressed = false;
2020-03-22 22:19:43 +01:00
while (iter->vromEnd != 0) {
if (iter->romEnd != 0) {
sDmaMgrIsRomCompressed = true;
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
PRINTF("%3d %08x %08x %08x %08x %08x %c %s\n", idx, iter->vromStart, iter->vromEnd, iter->romStart,
iter->romEnd, (iter->romEnd != 0) ? iter->romEnd - iter->romStart : iter->vromEnd - iter->vromStart,
(((iter->romEnd != 0) ? iter->romEnd - iter->romStart : 0) > 0x10000) ? '*' : ' ', name ? *name : "");
2020-03-17 00:31:30 -04:00
idx++;
iter++;
if (name != NULL) {
2020-03-17 00:31:30 -04:00
name++;
2020-03-22 22:19:43 +01:00
}
2020-03-17 00:31:30 -04:00
}
#endif
2020-03-17 00:31:30 -04:00
// Ensure that the boot segment always follows after the makerom segment.
if ((uintptr_t)_bootSegmentRomStart != gDmaDataTable[0].vromEnd) {
PRINTF("_bootSegmentRomStart(%08x) != dma_rom_ad[0].rom_b(%08x)\n", _bootSegmentRomStart,
gDmaDataTable[0].vromEnd);
//! @bug The main code file where fault.c resides is not yet loaded
2020-03-17 00:31:30 -04:00
Fault_AddHungupAndCrash("../z_std_dma.c", 1055);
}
// Start the DMA manager
osCreateMesgQueue(&sDmaMgrMsgQueue, sDmaMgrMsgBuf, ARRAY_COUNT(sDmaMgrMsgBuf));
StackCheck_Init(&sDmaMgrStackInfo, sDmaMgrStack, STACK_TOP(sDmaMgrStack), 0, 0x100, "dmamgr");
osCreateThread(&sDmaMgrThread, THREAD_ID_DMAMGR, DmaMgr_ThreadEntry, NULL, STACK_TOP(sDmaMgrStack),
THREAD_PRI_DMAMGR);
2020-03-17 00:31:30 -04:00
osStartThread(&sDmaMgrThread);
}
#if OOT_DEBUG
/**
* Asynchronous DMA Request with source file and line info for debugging.
*
* @see DmaMgr_RequestAsync
*/
s32 DmaMgr_RequestAsyncDebug(DmaRequest* req, void* ram, uintptr_t vrom, size_t size, u32 unk5, OSMesgQueue* queue,
OSMesg msg, const char* file, s32 line) {
2020-03-17 00:31:30 -04:00
req->filename = file;
req->line = line;
return DmaMgr_RequestAsync(req, ram, vrom, size, unk5, queue, msg);
2020-03-17 00:31:30 -04:00
}
/**
* Synchronous DMA Request with source file and line info for debugging.
*
* @see DmaMgr_RequestSync
*/
s32 DmaMgr_RequestSyncDebug(void* ram, uintptr_t vrom, size_t size, const char* file, s32 line) {
2020-03-17 00:31:30 -04:00
DmaRequest req;
s32 ret;
OSMesgQueue queue;
OSMesg msg;
s32 pad;
2020-03-17 00:31:30 -04:00
req.filename = file;
req.line = line;
osCreateMesgQueue(&queue, &msg, 1);
ret = DmaMgr_RequestAsync(&req, ram, vrom, size, 0, &queue, NULL);
if (ret == -1) { // DmaMgr_RequestAsync only returns 0
2020-03-17 00:31:30 -04:00
return ret;
2020-03-22 22:19:43 +01:00
}
osRecvMesg(&queue, NULL, OS_MESG_BLOCK);
2020-03-17 00:31:30 -04:00
return 0;
}
#endif