GCMemcard: Rename BlockAlloc variables to match our naming conventions.

This commit is contained in:
Admiral H. Curtiss 2018-11-18 16:22:19 +01:00
parent 17208f4c89
commit 9f03c2f358
3 changed files with 41 additions and 37 deletions

View File

@ -219,7 +219,7 @@ void GCMemcard::InitDirBatPointers()
CurrentDir = &dir_backup; CurrentDir = &dir_backup;
PreviousDir = &dir; PreviousDir = &dir;
} }
if (BE16(bat.UpdateCounter) > BE16(bat_backup.UpdateCounter)) if (BE16(bat.m_update_counter) > BE16(bat_backup.m_update_counter))
{ {
CurrentBat = &bat; CurrentBat = &bat;
PreviousBat = &bat_backup; PreviousBat = &bat_backup;
@ -295,11 +295,11 @@ u32 GCMemcard::TestChecksums() const
results |= 4; results |= 4;
calc_checksumsBE((u16*)(((u8*)&bat) + 4), 0xFFE, &csum, &csum_inv); calc_checksumsBE((u16*)(((u8*)&bat) + 4), 0xFFE, &csum, &csum_inv);
if ((bat.Checksum != csum) || (bat.Checksum_Inv != csum_inv)) if ((bat.m_checksum != csum) || (bat.m_checksum_inv != csum_inv))
results |= 8; results |= 8;
calc_checksumsBE((u16*)(((u8*)&bat_backup) + 4), 0xFFE, &csum, &csum_inv); calc_checksumsBE((u16*)(((u8*)&bat_backup) + 4), 0xFFE, &csum, &csum_inv);
if ((bat_backup.Checksum != csum) || (bat_backup.Checksum_Inv != csum_inv)) if ((bat_backup.m_checksum != csum) || (bat_backup.m_checksum_inv != csum_inv))
results |= 16; results |= 16;
return results; return results;
@ -313,8 +313,9 @@ bool GCMemcard::FixChecksums()
calc_checksumsBE((u16*)&hdr, 0xFE, &hdr.m_checksum, &hdr.m_checksum_inv); calc_checksumsBE((u16*)&hdr, 0xFE, &hdr.m_checksum, &hdr.m_checksum_inv);
calc_checksumsBE((u16*)&dir, 0xFFE, &dir.m_checksum, &dir.m_checksum_inv); calc_checksumsBE((u16*)&dir, 0xFFE, &dir.m_checksum, &dir.m_checksum_inv);
calc_checksumsBE((u16*)&dir_backup, 0xFFE, &dir_backup.m_checksum, &dir_backup.m_checksum_inv); calc_checksumsBE((u16*)&dir_backup, 0xFFE, &dir_backup.m_checksum, &dir_backup.m_checksum_inv);
calc_checksumsBE((u16*)&bat + 2, 0xFFE, &bat.Checksum, &bat.Checksum_Inv); calc_checksumsBE((u16*)&bat + 2, 0xFFE, &bat.m_checksum, &bat.m_checksum_inv);
calc_checksumsBE((u16*)&bat_backup + 2, 0xFFE, &bat_backup.Checksum, &bat_backup.Checksum_Inv); calc_checksumsBE((u16*)&bat_backup + 2, 0xFFE, &bat_backup.m_checksum,
&bat_backup.m_checksum_inv);
return true; return true;
} }
@ -358,7 +359,7 @@ u16 GCMemcard::GetFreeBlocks() const
if (!m_valid) if (!m_valid)
return 0; return 0;
return BE16(CurrentBat->FreeBlocks); return BE16(CurrentBat->m_free_blocks);
} }
u8 GCMemcard::TitlePresent(const DEntry& d) const u8 GCMemcard::TitlePresent(const DEntry& d) const
@ -574,23 +575,23 @@ u16 BlockAlloc::GetNextBlock(u16 Block) const
if ((Block < MC_FST_BLOCKS) || (Block > 4091)) if ((Block < MC_FST_BLOCKS) || (Block > 4091))
return 0; return 0;
return Common::swap16(Map[Block - MC_FST_BLOCKS]); return Common::swap16(m_map[Block - MC_FST_BLOCKS]);
} }
// Parameters and return value are expected as memory card block index, // Parameters and return value are expected as memory card block index,
// not BAT index; that is, block 5 is the first file data block. // not BAT index; that is, block 5 is the first file data block.
u16 BlockAlloc::NextFreeBlock(u16 MaxBlock, u16 StartingBlock) const u16 BlockAlloc::NextFreeBlock(u16 MaxBlock, u16 StartingBlock) const
{ {
if (FreeBlocks) if (m_free_blocks)
{ {
StartingBlock = MathUtil::Clamp<u16>(StartingBlock, MC_FST_BLOCKS, BAT_SIZE + MC_FST_BLOCKS); 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); MaxBlock = MathUtil::Clamp<u16>(MaxBlock, MC_FST_BLOCKS, BAT_SIZE + MC_FST_BLOCKS);
for (u16 i = StartingBlock; i < MaxBlock; ++i) for (u16 i = StartingBlock; i < MaxBlock; ++i)
if (Map[i - MC_FST_BLOCKS] == 0) if (m_map[i - MC_FST_BLOCKS] == 0)
return i; return i;
for (u16 i = MC_FST_BLOCKS; i < StartingBlock; ++i) for (u16 i = MC_FST_BLOCKS; i < StartingBlock; ++i)
if (Map[i - MC_FST_BLOCKS] == 0) if (m_map[i - MC_FST_BLOCKS] == 0)
return i; return i;
} }
return 0xFFFF; return 0xFFFF;
@ -612,8 +613,8 @@ bool BlockAlloc::ClearBlocks(u16 FirstBlock, u16 BlockCount)
return false; return false;
} }
for (unsigned int i = 0; i < length; ++i) for (unsigned int i = 0; i < length; ++i)
Map[blocks.at(i) - MC_FST_BLOCKS] = 0; m_map[blocks.at(i) - MC_FST_BLOCKS] = 0;
FreeBlocks = BE16(BE16(FreeBlocks) + BlockCount); m_free_blocks = BE16(BE16(m_free_blocks) + BlockCount);
return true; return true;
} }
@ -655,7 +656,7 @@ u32 GCMemcard::ImportFile(const DEntry& direntry, std::vector<GCMBlock>& saveBlo
{ {
return OUTOFDIRENTRIES; return OUTOFDIRENTRIES;
} }
if (BE16(CurrentBat->FreeBlocks) < BE16(direntry.m_block_count)) if (BE16(CurrentBat->m_free_blocks) < BE16(direntry.m_block_count))
{ {
return OUTOFBLOCKS; return OUTOFBLOCKS;
} }
@ -665,7 +666,8 @@ u32 GCMemcard::ImportFile(const DEntry& direntry, std::vector<GCMBlock>& saveBlo
} }
// find first free data block // find first free data block
u16 firstBlock = CurrentBat->NextFreeBlock(maxBlock, BE16(CurrentBat->LastAllocated)); u16 firstBlock =
CurrentBat->NextFreeBlock(maxBlock, BE16(CurrentBat->m_last_allocated_block));
if (firstBlock == 0xFFFF) if (firstBlock == 0xFFFF)
return OUTOFBLOCKS; return OUTOFBLOCKS;
Directory UpdatedDir = *CurrentDir; Directory UpdatedDir = *CurrentDir;
@ -711,13 +713,13 @@ u32 GCMemcard::ImportFile(const DEntry& direntry, std::vector<GCMBlock>& saveBlo
nextBlock = 0xFFFF; nextBlock = 0xFFFF;
else else
nextBlock = UpdatedBat.NextFreeBlock(maxBlock, firstBlock + 1); nextBlock = UpdatedBat.NextFreeBlock(maxBlock, firstBlock + 1);
UpdatedBat.Map[firstBlock - MC_FST_BLOCKS] = BE16(nextBlock); UpdatedBat.m_map[firstBlock - MC_FST_BLOCKS] = BE16(nextBlock);
UpdatedBat.LastAllocated = BE16(firstBlock); UpdatedBat.m_last_allocated_block = BE16(firstBlock);
firstBlock = nextBlock; firstBlock = nextBlock;
} }
UpdatedBat.FreeBlocks = BE16(BE16(UpdatedBat.FreeBlocks) - fileBlocks); UpdatedBat.m_free_blocks = BE16(BE16(UpdatedBat.m_free_blocks) - fileBlocks);
UpdatedBat.UpdateCounter = BE16(BE16(UpdatedBat.UpdateCounter) + 1); UpdatedBat.m_update_counter = BE16(BE16(UpdatedBat.m_update_counter) + 1);
*PreviousBat = UpdatedBat; *PreviousBat = UpdatedBat;
if (PreviousBat == &bat) if (PreviousBat == &bat)
{ {
@ -748,7 +750,7 @@ u32 GCMemcard::RemoveFile(u8 index) // index in the directory array
BlockAlloc UpdatedBat = *CurrentBat; BlockAlloc UpdatedBat = *CurrentBat;
if (!UpdatedBat.ClearBlocks(startingblock, numberofblocks)) if (!UpdatedBat.ClearBlocks(startingblock, numberofblocks))
return DELETE_FAIL; return DELETE_FAIL;
UpdatedBat.UpdateCounter = BE16(BE16(UpdatedBat.UpdateCounter) + 1); UpdatedBat.m_update_counter = BE16(BE16(UpdatedBat.m_update_counter) + 1);
*PreviousBat = UpdatedBat; *PreviousBat = UpdatedBat;
if (PreviousBat == &bat) if (PreviousBat == &bat)
{ {

View File

@ -241,38 +241,40 @@ static_assert(sizeof(Directory) == BLOCK_SIZE);
struct BlockAlloc struct BlockAlloc
{ {
u16 Checksum; // 0x0000 2 Additive Checksum u16 m_checksum; // 0x0000 2 Additive Checksum
u16 Checksum_Inv; // 0x0002 2 Inverse Checksum u16 m_checksum_inv; // 0x0002 2 Inverse Checksum
u16 UpdateCounter; // 0x0004 2 Update Counter u16 m_update_counter; // 0x0004 2 Update Counter
u16 FreeBlocks; // 0x0006 2 Free Blocks u16 m_free_blocks; // 0x0006 2 Free Blocks
u16 LastAllocated; // 0x0008 2 Last allocated Block u16 m_last_allocated_block; // 0x0008 2 Last allocated Block
u16 Map[BAT_SIZE]; // 0x000a 0x1ff8 Map of allocated Blocks u16 m_map[BAT_SIZE]; // 0x000a 0x1ff8 Map of allocated Blocks
u16 GetNextBlock(u16 Block) const; u16 GetNextBlock(u16 Block) const;
u16 NextFreeBlock(u16 MaxBlock, u16 StartingBlock = MC_FST_BLOCKS) const; u16 NextFreeBlock(u16 MaxBlock, u16 StartingBlock = MC_FST_BLOCKS) const;
bool ClearBlocks(u16 StartingBlock, u16 Length); bool ClearBlocks(u16 StartingBlock, u16 Length);
void fixChecksums() { calc_checksumsBE((u16*)&UpdateCounter, 0xFFE, &Checksum, &Checksum_Inv); } void fixChecksums()
{
calc_checksumsBE((u16*)&m_update_counter, 0xFFE, &m_checksum, &m_checksum_inv);
}
explicit BlockAlloc(u16 sizeMb = MemCard2043Mb) explicit BlockAlloc(u16 sizeMb = MemCard2043Mb)
{ {
memset(this, 0, BLOCK_SIZE); memset(this, 0, BLOCK_SIZE);
// UpdateCounter = 0; m_free_blocks = BE16((sizeMb * MBIT_TO_BLOCKS) - MC_FST_BLOCKS);
FreeBlocks = BE16((sizeMb * MBIT_TO_BLOCKS) - MC_FST_BLOCKS); m_last_allocated_block = BE16(4);
LastAllocated = BE16(4);
fixChecksums(); fixChecksums();
} }
u16 AssignBlocksContiguous(u16 length) u16 AssignBlocksContiguous(u16 length)
{ {
u16 starting = BE16(LastAllocated) + 1; u16 starting = BE16(m_last_allocated_block) + 1;
if (length > BE16(FreeBlocks)) if (length > BE16(m_free_blocks))
return 0xFFFF; return 0xFFFF;
u16 current = starting; u16 current = starting;
while ((current - starting + 1) < length) while ((current - starting + 1) < length)
{ {
Map[current - 5] = BE16(current + 1); m_map[current - 5] = BE16(current + 1);
current++; current++;
} }
Map[current - 5] = 0xFFFF; m_map[current - 5] = 0xFFFF;
LastAllocated = BE16(current); m_last_allocated_block = BE16(current);
FreeBlocks = BE16(BE16(FreeBlocks) - length); m_free_blocks = BE16(BE16(m_free_blocks) - length);
fixChecksums(); fixChecksums();
return BE16(starting); return BE16(starting);
} }

View File

@ -90,7 +90,7 @@ int GCMemcardDirectory::LoadGCI(const std::string& file_name, bool current_game_
return NO_INDEX; return NO_INDEX;
} }
int total_blocks = BE16(m_hdr.m_size_mb) * MBIT_TO_BLOCKS - MC_FST_BLOCKS; int total_blocks = BE16(m_hdr.m_size_mb) * MBIT_TO_BLOCKS - MC_FST_BLOCKS;
int free_blocks = BE16(m_bat1.FreeBlocks); int free_blocks = BE16(m_bat1.m_free_blocks);
if (total_blocks > free_blocks * 10) if (total_blocks > free_blocks * 10)
{ {
PanicAlertT("%s\nwas not loaded because there is less than 10%% free blocks available on " PanicAlertT("%s\nwas not loaded because there is less than 10%% free blocks available on "
@ -546,7 +546,7 @@ s32 GCMemcardDirectory::DirectoryWrite(u32 dest_address, u32 length, const u8* s
bool GCMemcardDirectory::SetUsedBlocks(int save_index) bool GCMemcardDirectory::SetUsedBlocks(int save_index)
{ {
BlockAlloc* current_bat; BlockAlloc* current_bat;
if (BE16(m_bat2.UpdateCounter) > BE16(m_bat1.UpdateCounter)) if (BE16(m_bat2.m_update_counter) > BE16(m_bat1.m_update_counter))
current_bat = &m_bat2; current_bat = &m_bat2;
else else
current_bat = &m_bat1; current_bat = &m_bat1;