diff options
| author | Pierre Bourdon <delroth@gmail.com> | 2018-11-22 06:54:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-11-22 06:54:52 +0100 |
| commit | 8d20a173f091ebcd14b0db9492bde158df3f0822 (patch) | |
| tree | c66e3be5875317c52a9853fa0d80ef32065421dd /Source/Core | |
| parent | 2bb61afe6ea94d31d763aa2c210e53b079ce7490 (diff) | |
| parent | 353e289fbf7b422e528a8dd7ec2e1b62a820b355 (diff) | |
Merge pull request #7579 from AdmiralCurtiss/next-free-block-fix
GCMemcard: Fix mixed memcard-based and bat-based indices in definition and usage of NextFreeBlock().
Diffstat (limited to 'Source/Core')
| -rw-r--r-- | Source/Core/Core/HW/GCMemcard/GCMemcard.cpp | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/Source/Core/Core/HW/GCMemcard/GCMemcard.cpp b/Source/Core/Core/HW/GCMemcard/GCMemcard.cpp index fd04f69063..28e7225845 100644 --- a/Source/Core/Core/HW/GCMemcard/GCMemcard.cpp +++ b/Source/Core/Core/HW/GCMemcard/GCMemcard.cpp @@ -14,6 +14,7 @@ #include "Common/CommonPaths.h" #include "Common/CommonTypes.h" #include "Common/File.h" +#include "Common/MathUtil.h" #include "Common/MsgHandler.h" #include "Common/StringUtil.h" #include "Common/Swap.h" @@ -576,11 +577,14 @@ u16 BlockAlloc::GetNextBlock(u16 Block) const return Common::swap16(Map[Block - MC_FST_BLOCKS]); } +// Parameters and return value are expected as memory card block index, +// not BAT index; that is, block 5 is the first file data block. u16 BlockAlloc::NextFreeBlock(u16 MaxBlock, u16 StartingBlock) const { if (FreeBlocks) { - MaxBlock = std::min<u16>(MaxBlock, BAT_SIZE); + StartingBlock = MathUtil::Clamp<u16>(StartingBlock, MC_FST_BLOCKS, BAT_SIZE + MC_FST_BLOCKS); + MaxBlock = MathUtil::Clamp<u16>(MaxBlock, MC_FST_BLOCKS, BAT_SIZE + MC_FST_BLOCKS); for (u16 i = StartingBlock; i < MaxBlock; ++i) if (Map[i - MC_FST_BLOCKS] == 0) return i; @@ -661,8 +665,7 @@ u32 GCMemcard::ImportFile(const DEntry& direntry, std::vector<GCMBlock>& saveBlo } // find first free data block - u16 firstBlock = - CurrentBat->NextFreeBlock(maxBlock - MC_FST_BLOCKS, BE16(CurrentBat->LastAllocated)); + u16 firstBlock = CurrentBat->NextFreeBlock(maxBlock, BE16(CurrentBat->LastAllocated)); if (firstBlock == 0xFFFF) return OUTOFBLOCKS; Directory UpdatedDir = *CurrentDir; @@ -707,7 +710,7 @@ u32 GCMemcard::ImportFile(const DEntry& direntry, std::vector<GCMBlock>& saveBlo if (i == fileBlocks - 1) nextBlock = 0xFFFF; else - nextBlock = UpdatedBat.NextFreeBlock(maxBlock - MC_FST_BLOCKS, firstBlock + 1); + nextBlock = UpdatedBat.NextFreeBlock(maxBlock, firstBlock + 1); UpdatedBat.Map[firstBlock - MC_FST_BLOCKS] = BE16(nextBlock); UpdatedBat.LastAllocated = BE16(firstBlock); firstBlock = nextBlock; |
