Added SRAM save game functionality
This commit is contained in:
@@ -23,6 +23,7 @@ namespace Core.Memory
|
||||
|
||||
// A flag to handle cartridges that don't use paging (like early 32KB games)
|
||||
private bool _isCartridgeLoaded = false;
|
||||
public bool SramUsed { get; private set; } = false;
|
||||
|
||||
public void LoadCartridge(byte[] romData)
|
||||
{
|
||||
@@ -57,7 +58,7 @@ namespace Core.Memory
|
||||
if ((_mapperControl & 0x08) != 0)
|
||||
{
|
||||
int ramBank = (_mapperControl & 0x04) != 0 ? 1 : 0;
|
||||
int ramOffset = (ramBank * 0x4000) + (address & 0x3FFF);
|
||||
int ramOffset = (ramBank * 0x4000) + (address & 0x1FFF);
|
||||
return _cartridgeRam[ramOffset];
|
||||
}
|
||||
|
||||
@@ -139,8 +140,9 @@ namespace Core.Memory
|
||||
// Bypass the lock if they are trying to save their game!
|
||||
if (address >= 0x8000 && (_mapperControl & 0x08) != 0)
|
||||
{
|
||||
SramUsed = true;
|
||||
int ramBank = (_mapperControl & 0x04) != 0 ? 1 : 0;
|
||||
int ramOffset = (ramBank * 0x4000) + (address & 0x3FFF);
|
||||
int ramOffset = (ramBank * 0x4000) + (address & 0x1FFF);
|
||||
_cartridgeRam[ramOffset] = value;
|
||||
return;
|
||||
}
|
||||
@@ -196,11 +198,37 @@ namespace Core.Memory
|
||||
|
||||
return _cartridgeRom[absoluteAddress];
|
||||
}
|
||||
public void LoadSaveData(string filePath)
|
||||
{
|
||||
if (System.IO.File.Exists(filePath))
|
||||
{
|
||||
byte[] saveData = System.IO.File.ReadAllBytes(filePath);
|
||||
Array.Copy(saveData, _cartridgeRam, Math.Min(saveData.Length, _cartridgeRam.Length));
|
||||
SramUsed = true; // We loaded a save, so it's active!
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < _cartridgeRam.Length; i++)
|
||||
{
|
||||
_cartridgeRam[i] = 0xFF;
|
||||
}
|
||||
SramUsed = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveSaveData(string filePath)
|
||||
{
|
||||
// Only write a file to the hard drive if the game actually used the Save RAM!
|
||||
if (SramUsed)
|
||||
{
|
||||
System.IO.File.WriteAllBytes(filePath, _cartridgeRam);
|
||||
}
|
||||
}
|
||||
|
||||
public void CleanRAMData()
|
||||
{
|
||||
Array.Clear(_workRam, 0, _workRam.Length);
|
||||
Array.Clear(_cartridgeRam, 0, _cartridgeRam.Length);
|
||||
_mapperControl = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user