Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #330 from LPFaint99/GCMemcard_addsave_fix
GCMemcard: fix edge case of adding to a fragmented memcard.
  • Loading branch information
Sonicadvance1 committed May 5, 2014
2 parents edbf90f + b549ec7 commit 0bfec02
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions Source/Core/Core/HW/GCMemcard.cpp
Expand Up @@ -554,11 +554,12 @@ u16 GCMemcard::BlockAlloc::GetNextBlock(u16 Block) const
return Common::swap16(Map[Block-MC_FST_BLOCKS]);
}

u16 GCMemcard::BlockAlloc::NextFreeBlock(u16 StartingBlock) const
u16 GCMemcard::BlockAlloc::NextFreeBlock(u16 MaxBlock, u16 StartingBlock) const
{
if (FreeBlocks)
{
for (u16 i = StartingBlock; i < BAT_SIZE; ++i)
MaxBlock = std::min<u16>(MaxBlock, BAT_SIZE);
for (u16 i = StartingBlock; i < MaxBlock; ++i)
if (Map[i-MC_FST_BLOCKS] == 0)
return i;

Expand Down Expand Up @@ -638,7 +639,7 @@ u32 GCMemcard::ImportFile(DEntry& direntry, std::vector<GCMBlock> &saveBlocks)
}

// find first free data block
u16 firstBlock = CurrentBat->NextFreeBlock(BE16(CurrentBat->LastAllocated));
u16 firstBlock = CurrentBat->NextFreeBlock(maxBlock - MC_FST_BLOCKS, BE16(CurrentBat->LastAllocated));
if (firstBlock == 0xFFFF)
return OUTOFBLOCKS;
Directory UpdatedDir = *CurrentDir;
Expand Down Expand Up @@ -683,7 +684,7 @@ u32 GCMemcard::ImportFile(DEntry& direntry, std::vector<GCMBlock> &saveBlocks)
if (i == fileBlocks-1)
nextBlock = 0xFFFF;
else
nextBlock = UpdatedBat.NextFreeBlock(firstBlock+1);
nextBlock = UpdatedBat.NextFreeBlock(maxBlock - MC_FST_BLOCKS, firstBlock + 1);
UpdatedBat.Map[firstBlock - MC_FST_BLOCKS] = BE16(nextBlock);
UpdatedBat.LastAllocated = BE16(firstBlock);
firstBlock = nextBlock;
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/GCMemcard.h
Expand Up @@ -158,7 +158,7 @@ class GCMemcard : NonCopyable
u16 LastAllocated; //0x0008 2 Last allocated Block
u16 Map[BAT_SIZE]; //0x000a 0x1ff8 Map of allocated Blocks
u16 GetNextBlock(u16 Block) const;
u16 NextFreeBlock(u16 StartingBlock=MC_FST_BLOCKS) const;
u16 NextFreeBlock(u16 MaxBlock, u16 StartingBlock = MC_FST_BLOCKS) const;
bool ClearBlocks(u16 StartingBlock, u16 Length);
} bat,bat_backup;

Expand Down

0 comments on commit 0bfec02

Please sign in to comment.