From 871671b1a4caa6aaee76a5b1ce11decd08057a87 Mon Sep 17 00:00:00 2001 From: profi200 Date: Sat, 20 Jun 2020 18:00:43 +0200 Subject: [PATCH] Updated documentation. Use fsQuickRead()/fsQuickWrite(). --- include/fsutil.h | 3 ++- source/arm9/hardware/lgy.c | 20 +++++++------------- source/fsutil.c | 30 ++++++++++++++++-------------- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/include/fsutil.h b/include/fsutil.h index 80f0983..99ad63d 100644 --- a/include/fsutil.h +++ b/include/fsutil.h @@ -23,4 +23,5 @@ -Result fsQuickRead(const char *const path, u32 off, void *buf, u32 size); +Result fsQuickRead(void *const buf, const char *const path, u32 size); +Result fsQuickWrite(void *const buf, const char *const path, u32 size); diff --git a/source/arm9/hardware/lgy.c b/source/arm9/hardware/lgy.c index 53c6dde..fe74b42 100644 --- a/source/arm9/hardware/lgy.c +++ b/source/arm9/hardware/lgy.c @@ -6,7 +6,7 @@ #include "mmio.h" #include "arm9/hardware/ndma.h" #include "arm9/arm7_stub.h" -#include "fs.h" +#include "fsutil.h" #include "arm9/debug.h" #include "arm9/hardware/crypto.h" #include "util.h" @@ -20,7 +20,7 @@ #define REG_LGY_GBA_RTC_CNT *((vu16*)(LGY_REGS_BASE + 0x108)) #define REG_LGY_GBA_RTC_BCD_DATE *((vu32*)(LGY_REGS_BASE + 0x110)) #define REG_LGY_GBA_RTC_BCD_TIME *((vu32*)(LGY_REGS_BASE + 0x114)) -#define REG_LGY_GBA_RTC_HEX_TIME *((vu32*)(LGY_REGS_BASE + 0x118)) // Writing bit 7 at runtime completely deadlocks the ARM7. +#define REG_LGY_GBA_RTC_HEX_TIME *((vu32*)(LGY_REGS_BASE + 0x118)) // Writing bit 7 completely hangs all(?) GBA hardware. #define REG_LGY_GBA_RTC_HEX_DATE *((vu32*)(LGY_REGS_BASE + 0x11C)) #define REGs_LGY_GBA_SAVE_TIMING ((vu32*)(LGY_REGS_BASE + 0x120)) @@ -87,13 +87,12 @@ Result LGY_prepareGbaMode(bool biosIntro, u16 saveType, const char *const savePa Result res = RES_OK; if(g_saveSize != 0) { - FHandle f; - if(fOpen(&f, savePath, FA_OPEN_EXISTING | FA_READ) == RES_OK) + res = fsQuickRead((void*)SAVE_LOC, savePath, MAX_SAVE_SIZE); + if(res == RES_FR_NO_FILE) { - res = fRead(f, (void*)SAVE_LOC, MAX_SAVE_SIZE, NULL); - fClose(f); + res = RES_OK; // Ignore a missing save file. + NDMA_fill((u32*)SAVE_LOC, 0xFFFFFFFFu, g_saveSize); } - else NDMA_fill((u32*)SAVE_LOC, 0xFFFFFFFFu, g_saveSize); // Hash the savegame so it's only backed up when changed. sha((u32*)SAVE_LOC, g_saveSize, g_saveHash, SHA_INPUT_BIG | SHA_MODE_256, SHA_OUTPUT_BIG); @@ -163,12 +162,7 @@ Result LGY_backupGbaSave(void) // Update hash. memcpy(g_saveHash, newHash, 32); - FHandle f; - if((res = fOpen(&f, g_savePath, FA_OPEN_ALWAYS | FA_WRITE)) == RES_OK) - { - res = fWrite(f, (void*)SAVE_LOC, g_saveSize, NULL); - fClose(f); - } + res = fsQuickWrite((void*)SAVE_LOC, g_savePath, g_saveSize); } // Disable savegame mem region. diff --git a/source/fsutil.c b/source/fsutil.c index d42da94..96de284 100644 --- a/source/fsutil.c +++ b/source/fsutil.c @@ -21,26 +21,28 @@ #include "fs.h" -#define BLOCK_SIZE (1024u * 1024) - - -Result fsQuickRead(const char *const path, u32 off, void *buf, u32 size) +Result fsQuickRead(void *const buf, const char *const path, u32 size) { Result res; FHandle f; if((res = fOpen(&f, path, FA_OPEN_EXISTING | FA_READ)) == RES_OK) { - if((res = fLseek(f, off)) == RES_OK) - { - u32 totalRead = 0; - u32 read; - while(size > totalRead && (res = fRead(f, buf, BLOCK_SIZE, &read)) == RES_OK && read == BLOCK_SIZE) - { - buf += read; - totalRead += read; - } - } + res = fRead(f, buf, size, NULL); + + fClose(f); + } + + return res; +} + +Result fsQuickWrite(void *const buf, const char *const path, u32 size) +{ + Result res; + FHandle f; + if((res = fOpen(&f, path, FA_OPEN_ALWAYS | FA_WRITE)) == RES_OK) + { + res = fWrite(f, buf, size, NULL); fClose(f); }