Skip to content

Commit

Permalink
Merge branch 'develop' into feature/rom-gpio
Browse files Browse the repository at this point in the history
  • Loading branch information
Gericom committed Oct 5, 2024
2 parents 69e8678 + 3b5f49a commit 4ea794f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
9 changes: 5 additions & 4 deletions code/core/arm7/source/IpcServices/FsIpcService.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "common.h"
#include <libtwl/ipc/ipcSync.h>
#include <string.h>
#include "dldi.h"
#include "FsIpcService.h"
Expand Down Expand Up @@ -57,23 +58,23 @@ void FsIpcService::SetupDldi(const fs_ipc_cmd_t* cmd) const
void FsIpcService::DldiReadSectors(const fs_ipc_cmd_t* cmd) const
{
_DLDI_readSectors_ptr(cmd->sector, cmd->count, cmd->buffer);
SendResponseMessage(0);
ipc_setArm7SyncBits(ipc_getArm9SyncBits());
}

void FsIpcService::DldiWriteSectors(const fs_ipc_cmd_t* cmd) const
{
_DLDI_writeSectors_ptr(cmd->sector, cmd->count, cmd->buffer);
SendResponseMessage(0);
ipc_setArm7SyncBits(ipc_getArm9SyncBits());
}

void FsIpcService::DsiSdReadSectors(const fs_ipc_cmd_t* cmd) const
{
sdmmc_sdcard_readsectors(cmd->sector, cmd->count, cmd->buffer);
SendResponseMessage(0);
ipc_setArm7SyncBits(ipc_getArm9SyncBits());
}

void FsIpcService::DsiSdWriteSectors(const fs_ipc_cmd_t* cmd) const
{
sdmmc_sdcard_writesectors(cmd->sector, cmd->count, cmd->buffer);
SendResponseMessage(0);
ipc_setArm7SyncBits(ipc_getArm9SyncBits());
}
32 changes: 20 additions & 12 deletions code/core/arm9/source/Fat/FsIpc.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "common.h"
#include <libtwl/ipc/ipcFifoSystem.h>
#include <libtwl/ipc/ipcFifo.h>
#include <libtwl/ipc/ipcSync.h>
#include <string.h>
#include "IpcChannels.h"
#include "FsIpcCommand.h"
Expand All @@ -16,6 +17,12 @@ alignas(32) static u8 sTempBuffers[2][512];

static FsWaitToken* volatile sCurrentWaitToken = nullptr;

static bool isArm7FsOperationComplete()
{
u32 ipcSync = REG_IPCSYNC;
return ((ipcSync >> IPCSYNC_LOCAL_DATA_SHIFT) & 0xF) == (ipcSync & IPCSYNC_REMOTE_DATA_MASK);
}

extern "C"
[[gnu::noinline]]
u32 fs_waitForCompletion(FsWaitToken* waitToken, bool keepIrqsDisabled)
Expand All @@ -32,9 +39,8 @@ u32 fs_waitForCompletion(FsWaitToken* waitToken, bool keepIrqsDisabled)
{
break;
}
else if (!ipc_isRecvFifoEmpty())
else if (isArm7FsOperationComplete())
{
ipc_recvWordDirect();
currentWaitToken->transactionComplete = true;
sCurrentWaitToken = nullptr;
break;
Expand All @@ -57,6 +63,13 @@ extern "C" u32 fs_waitForCompletionOfCurrentTransaction(bool keepIrqsDisabled)
return fs_waitForCompletion(nullptr, keepIrqsDisabled);
}

static void sendIpcCommand()
{
dc_flushRange(&sIpcCommand, sizeof(sIpcCommand));
ipc_setArm9SyncBits(ipc_getArm7SyncBits() + 1);
ipc_sendWordDirect(((((u32)&sIpcCommand) >> 2) << IPC_FIFO_MSG_CHANNEL_BITS) | IPC_CHANNEL_FS);
}

static void executeIpcCommandAsync(u32 cmd, void* buffer, u32 sector, u32 count, FsWaitToken* waitToken)
{
waitToken->transactionComplete = false;
Expand All @@ -66,8 +79,7 @@ static void executeIpcCommandAsync(u32 cmd, void* buffer, u32 sector, u32 count,
sIpcCommand.buffer = buffer;
sIpcCommand.sector = sector;
sIpcCommand.count = count;
dc_flushRange(&sIpcCommand, sizeof(sIpcCommand));
ipc_sendWordDirect(((((u32)&sIpcCommand) >> 2) << IPC_FIFO_MSG_CHANNEL_BITS) | IPC_CHANNEL_FS);
sendIpcCommand();
sCurrentWaitToken = waitToken;
}
arm_restoreIrqs(irqs);
Expand All @@ -91,14 +103,12 @@ static void readSectorsNotCacheAligned(FsDevice device, void* buffer, u32 sector
{
sIpcCommand.buffer = sTempBuffers[i & 1];
sIpcCommand.sector = sector + i;
dc_flushRange(&sIpcCommand, sizeof(sIpcCommand));
ipc_sendWordDirect(((((u32)&sIpcCommand) >> 2) << IPC_FIFO_MSG_CHANNEL_BITS) | IPC_CHANNEL_FS);
sendIpcCommand();
if (i != 0)
memcpy((u8*)buffer + 512 * (i - 1), sTempBuffers[(i - 1) & 1], 512);
if (i != count - 1)
dc_invalidateRange(sTempBuffers[(i + 1) & 1], 512);
while (ipc_isRecvFifoEmpty());
ipc_recvWordDirect();
while (!isArm7FsOperationComplete());
}
memcpy((u8*)buffer + 512 * (count - 1), sTempBuffers[(count - 1) & 1], 512);
}
Expand Down Expand Up @@ -139,15 +149,13 @@ static void writeSectorsNotCacheAligned(FsDevice device, const void* buffer, u32
{
sIpcCommand.buffer = sTempBuffers[i & 1];
sIpcCommand.sector = sector + i;
dc_flushRange(&sIpcCommand, sizeof(sIpcCommand));
ipc_sendWordDirect(((((u32)&sIpcCommand) >> 2) << IPC_FIFO_MSG_CHANNEL_BITS) | IPC_CHANNEL_FS);
sendIpcCommand();
if (i != count - 1)
{
memcpy(sTempBuffers[(i + 1) & 1], (u8*)buffer + 512 * (i + 1), 512);
dc_flushRange(sTempBuffers[(i + 1) & 1], 512);
}
while (ipc_isRecvFifoEmpty());
ipc_recvWordDirect();
while (!isArm7FsOperationComplete());
}
memcpy((u8*)buffer + 512 * (count - 1), sTempBuffers[(count - 1) & 1], 512);
}
Expand Down

0 comments on commit 4ea794f

Please sign in to comment.