diff --git a/arm7/source/fvPlayer7.c b/arm7/source/fvPlayer7.c index 1be22ee..4909c98 100644 --- a/arm7/source/fvPlayer7.c +++ b/arm7/source/fvPlayer7.c @@ -5,6 +5,7 @@ #include "fat.h" #include "adpcm.h" #include "irqWait.h" +#include "../../common/twlwram.h" #include "fvPlayer7.h" #define FV_AUDIO_START_OFFSET 12 @@ -324,8 +325,13 @@ static void handleFifo(u32 value) } case IPC_CMD_HANDSHAKE: - fifoSendValue32(FIFO_USER_01, IPC_CMD_PACK(IPC_CMD_HANDSHAKE, 0)); + { + bool canUseWram = false; + if (isDSiMode()) + canUseWram = twr_isUnlocked(); + fifoSendValue32(FIFO_USER_01, IPC_CMD_PACK(IPC_CMD_HANDSHAKE, canUseWram ? 1 : 0)); break; + } } } diff --git a/arm7/source/main.c b/arm7/source/main.c index 362bef6..7fc898d 100644 --- a/arm7/source/main.c +++ b/arm7/source/main.c @@ -114,8 +114,14 @@ int main(void) if (isDSiMode()) { - twr_setBlockMapping(TWR_WRAM_BLOCK_B, 0x03100000, 0x40000, TWR_WRAM_BLOCK_IMAGE_SIZE_256K); - twr_setBlockMapping(TWR_WRAM_BLOCK_C, 0x03140000, 0x40000, TWR_WRAM_BLOCK_IMAGE_SIZE_256K); + if (twr_isUnlockable()) + twr_unlockAll(); + + if (twr_isUnlocked()) + { + twr_setBlockMapping(TWR_WRAM_BLOCK_B, 0x03100000, 0x40000, TWR_WRAM_BLOCK_IMAGE_SIZE_256K); + twr_setBlockMapping(TWR_WRAM_BLOCK_C, 0x03140000, 0x40000, TWR_WRAM_BLOCK_IMAGE_SIZE_256K); + } // switch to 47kHz output REG_SNDEXTCNT = 0; diff --git a/arm9/source/FastVideo/fvPlayer.c b/arm9/source/FastVideo/fvPlayer.c index 4f0cc47..2f458d4 100644 --- a/arm9/source/FastVideo/fvPlayer.c +++ b/arm9/source/FastVideo/fvPlayer.c @@ -199,10 +199,10 @@ static u32* makeMbRenderDl(u32* displayList, int height, u32 texParam) return displayList; } -bool fv_initPlayer(fv_player_t* player, const char* filePath) +bool fv_initPlayer(fv_player_t* player, const char* filePath, bool useWram) { memset(player, 0, sizeof(fv_player_t)); - if (isDSiMode()) + if (isDSiMode() && twr_isUnlocked() && useWram) player->dataBuffer = (u8*)twr_getBlockAddress(TWR_WRAM_BLOCK_B); else player->dataBuffer = memalign(32, FV_PLAYER_DATA_BUFFER_SIZE * FV_PLAYER_DATA_BUFFER_COUNT); diff --git a/arm9/source/FastVideo/fvPlayer.h b/arm9/source/FastVideo/fvPlayer.h index 6feff04..994f95e 100644 --- a/arm9/source/FastVideo/fvPlayer.h +++ b/arm9/source/FastVideo/fvPlayer.h @@ -46,7 +46,7 @@ typedef struct extern "C" { #endif -bool fv_initPlayer(fv_player_t* player, const char* filePath); +bool fv_initPlayer(fv_player_t* player, const char* filePath, bool useWram); void fv_destroyPlayer(fv_player_t* player); void fv_startPlayer(fv_player_t* player); void fv_updatePlayer(fv_player_t* player); diff --git a/arm9/source/main.cpp b/arm9/source/main.cpp index b6b1bbc..543166d 100644 --- a/arm9/source/main.cpp +++ b/arm9/source/main.cpp @@ -20,12 +20,14 @@ int main(int argc, char** argv) mpu_enableVramCache(); - if (isDSiMode()) + bool canUseWram = false; + if (isDSiMode() && twr_isUnlocked()) { twr_setBlockMapping(TWR_WRAM_BLOCK_A, 0x03000000, 0x40000, TWR_WRAM_BLOCK_IMAGE_SIZE_256K); twr_setBlockMapping(TWR_WRAM_BLOCK_B, 0x03100000, 0x40000, TWR_WRAM_BLOCK_IMAGE_SIZE_256K); twr_setBlockMapping(TWR_WRAM_BLOCK_C, 0x03140000, 0x40000, TWR_WRAM_BLOCK_IMAGE_SIZE_256K); mpu_enableTwlWramCache(); + canUseWram = true; } fifoSetValue32Handler(FIFO_USER_01, NULL, NULL); @@ -33,7 +35,10 @@ int main(int argc, char** argv) // handshake fifoSendValue32(FIFO_USER_01, IPC_CMD_PACK(IPC_CMD_HANDSHAKE, 0)); fifoWaitValue32(FIFO_USER_01); - fifoGetValue32(FIFO_USER_01); + u32 handShake = fifoGetValue32(FIFO_USER_01); + + if (canUseWram && (handShake & IPC_CMD_ARG_MASK) == 0) + canUseWram = false; if (!isDSiMode()) { @@ -72,7 +77,7 @@ int main(int argc, char** argv) filePath = argv[1]; // iprintf("Playing %s\n", filePath); - if (fv_initPlayer(&sPlayer, filePath)) + if (fv_initPlayer(&sPlayer, filePath, canUseWram)) { sPlayerController = new PlayerController(&sPlayer); sPlayerController->Initialize(); diff --git a/common/twlwram.h b/common/twlwram.h index 8651ffb..edbebc3 100644 --- a/common/twlwram.h +++ b/common/twlwram.h @@ -93,6 +93,22 @@ void twr_mapWramCSlot(int slot, TWRWramCSlotMaster master, int offset, bool enab #endif +bool twr_isUnlocked(void); + +#ifdef ARM7 + +static inline bool twr_isUnlockable(void) +{ + return (REG_SCFG_EXT & 0x80000000) != 0; +} + +static inline void twr_unlockAll(void) +{ + REG_MBK9 = 0; +} + +#endif + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/common/twlwram.twl.c b/common/twlwram.twl.c index 3dd3dd3..0771251 100644 --- a/common/twlwram.twl.c +++ b/common/twlwram.twl.c @@ -66,3 +66,14 @@ void twr_mapWramCSlot(int slot, TWRWramCSlotMaster master, int offset, bool enab } #endif + +bool twr_isUnlocked(void) +{ + if ((REG_SCFG_EXT & 0x80000000) == 0) + return false; // SCFG and MBK registers are permanently locked + + if ((REG_MBK9 & 0xFFFF0F) != 0) + return false; // One or more MBK registers are locked + + return true; +}