Skip to content

Commit

Permalink
SNES: Improved SPC file compatibility
Browse files Browse the repository at this point in the history
Try to figure out which of the 100C0-100FF or 101C0-101FF sections contains the actual RAM
  • Loading branch information
SourMesen committed May 12, 2023
1 parent da0825a commit 932c62f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Core/SNES/Spc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,28 @@ void Spc::LoadSpcFile(SpcFileData* data)
{
memcpy(_ram, data->SpcRam, Spc::SpcRamSize);

if(data->HasExtraRam) {
bool extraRamContainsIpl = memcmp(data->SpcExtraRam, _spcBios, 0x40) == 0;
if(!extraRamContainsIpl) {
bool ramContainsIpl = memcmp(data->SpcRam + 0xFFC0, _spcBios, 0x40) == 0;
bool isSpcRamEmpty = true;
bool isExtraRamEmpty = true;
for(int i = 0; i < 0x40; i++) {
if(data->SpcExtraRam[i] != 0 && data->SpcExtraRam[i] != 0xFF) {
isExtraRamEmpty = false;
}
if(data->SpcRam[i+0xFFC0] != 0 && data->SpcRam[i + 0xFFC0] != 0xFF) {
isSpcRamEmpty = false;
}
}

if(ramContainsIpl || (isSpcRamEmpty && !isExtraRamEmpty)) {
//Use extra ram section only if the main spc FFC0-FFFF section is empty or contains the IPL code
memcpy(_ram + 0xFFC0, data->SpcExtraRam, 0x40);
}
}
}

_dsp->LoadSpcFileRegs(data->DspRegs);

_state.PC = data->PC;
Expand Down
6 changes: 5 additions & 1 deletion Core/SNES/SpcFileData.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class SpcFileData
uint8_t DspRegs[128];
uint8_t SpcRam[0x10000];

bool HasExtraRam = false;
uint8_t SpcExtraRam[0x40] = {};

uint32_t TrackLength;
uint32_t FadeLength;

Expand Down Expand Up @@ -65,7 +68,8 @@ class SpcFileData
memcpy(SpcRam, spcData + 0x100, 0x10000);
if(size >= 0x10200) {
//Some SPC files don't have this data (0x10180 bytes instead of 0x10200 bytes)
memcpy(SpcRam + 0xFFC0, spcData + 0x101C0, 0x40);
memcpy(SpcExtraRam, spcData + 0x101C0, 0x40);
HasExtraRam = true;
}

memcpy(DspRegs, spcData + 0x10100, 128);
Expand Down

0 comments on commit 932c62f

Please sign in to comment.