Skip to content

Commit

Permalink
Add support for mapping more than 128KByte SRAM
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertymodo committed Aug 20, 2021
1 parent 9398d21 commit e66acce
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
22 changes: 11 additions & 11 deletions getset.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ inline uint8 S9xGetByte (uint32 Address)

case CMemory::MAP_HIROM_SRAM:
case CMemory::MAP_RONLY_SRAM:
byte = *(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask));
byte = *(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0x1f0000) >> 3)) & Memory.SRAMMask));
addCyclesInMemoryAccess;
return (byte);

Expand Down Expand Up @@ -241,10 +241,10 @@ inline uint16 S9xGetWord (uint32 Address, enum s9xwrap_t w = WRAP_NONE)
case CMemory::MAP_HIROM_SRAM:
case CMemory::MAP_RONLY_SRAM:
if (Memory.SRAMMask >= MEMMAP_MASK)
word = READ_WORD(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask));
word = READ_WORD(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0x1f0000) >> 3)) & Memory.SRAMMask));
else
word = (*(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask)) |
(*(Memory.SRAM + ((((Address + 1) & 0x7fff) - 0x6000 + (((Address + 1) & 0xf0000) >> 3)) & Memory.SRAMMask)) << 8));
word = (*(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0x1f0000) >> 3)) & Memory.SRAMMask)) |
(*(Memory.SRAM + ((((Address + 1) & 0x7fff) - 0x6000 + (((Address + 1) & 0x1f0000) >> 3)) & Memory.SRAMMask)) << 8));
addCyclesInMemoryAccess_x2;
return (word);

Expand Down Expand Up @@ -368,7 +368,7 @@ inline void S9xSetByte (uint8 Byte, uint32 Address)
case CMemory::MAP_HIROM_SRAM:
if (Memory.SRAMMask)
{
*(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask)) = Byte;
*(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0x1f0000) >> 3)) & Memory.SRAMMask)) = Byte;
CPU.SRAMModified = TRUE;
}

Expand Down Expand Up @@ -555,11 +555,11 @@ inline void S9xSetWord (uint16 Word, uint32 Address, enum s9xwrap_t w = WRAP_NON
if (Memory.SRAMMask)
{
if (Memory.SRAMMask >= MEMMAP_MASK)
WRITE_WORD(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask), Word);
WRITE_WORD(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0x1f0000) >> 3)) & Memory.SRAMMask), Word);
else
{
*(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask)) = (uint8) Word;
*(Memory.SRAM + ((((Address + 1) & 0x7fff) - 0x6000 + (((Address + 1) & 0xf0000) >> 3)) & Memory.SRAMMask)) = Word >> 8;
*(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0x1f0000) >> 3)) & Memory.SRAMMask)) = (uint8) Word;
*(Memory.SRAM + ((((Address + 1) & 0x7fff) - 0x6000 + (((Address + 1) & 0x1f0000) >> 3)) & Memory.SRAMMask)) = Word >> 8;
}

CPU.SRAMModified = TRUE;
Expand Down Expand Up @@ -730,7 +730,7 @@ inline void S9xSetPCBase (uint32 Address)
if ((Memory.SRAMMask & MEMMAP_MASK) != MEMMAP_MASK)
CPU.PCBase = NULL;
else
CPU.PCBase = Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask) - (Address & 0xffff);
CPU.PCBase = Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0x1f0000) >> 3)) & Memory.SRAMMask) - (Address & 0xffff);
return;

case CMemory::MAP_BWRAM:
Expand Down Expand Up @@ -786,7 +786,7 @@ inline uint8 * S9xGetBasePointer (uint32 Address)
case CMemory::MAP_HIROM_SRAM:
if ((Memory.SRAMMask & MEMMAP_MASK) != MEMMAP_MASK)
return (NULL);
return (Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask) - (Address & 0xffff));
return (Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0x1f0000) >> 3)) & Memory.SRAMMask) - (Address & 0xffff));

case CMemory::MAP_BWRAM:
return (Memory.BWRAM - 0x6000 - (Address & 0x8000));
Expand Down Expand Up @@ -831,7 +831,7 @@ inline uint8 * S9xGetMemPointer (uint32 Address)
case CMemory::MAP_HIROM_SRAM:
if ((Memory.SRAMMask & MEMMAP_MASK) != MEMMAP_MASK)
return (NULL);
return (Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask));
return (Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0x1f0000) >> 3)) & Memory.SRAMMask));

case CMemory::MAP_BWRAM:
return (Memory.BWRAM - 0x6000 + (Address & 0x7fff));
Expand Down
18 changes: 11 additions & 7 deletions memmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1916,7 +1916,7 @@ void CMemory::ClearSRAM (bool8 onlyNonSavedSRAM)
if (!(Settings.SuperFX && ROMType < 0x15) && !(Settings.SA1 && ROMType == 0x34)) // can have SRAM
return;

memset(SRAM, SNESGameFixes.SRAMInitialValue, 0x20000);
memset(SRAM, SNESGameFixes.SRAMInitialValue, sizeof(SRAM));
}

bool8 CMemory::LoadSRAM (const char *filename)
Expand Down Expand Up @@ -1951,15 +1951,17 @@ bool8 CMemory::LoadSRAM (const char *filename)
}

size = SRAMSize ? (1 << (SRAMSize + 3)) * 128 : 0;
if (size > 0x20000)
size = 0x20000;
if (LoROM)
size = size < 0x70000 ? size : 0x70000;
else if (HiROM)
size = size < 0x40000 ? size : 0x40000;

if (size)
{
file = fopen(sramName, "rb");
if (file)
{
len = fread((char *) SRAM, 1, 0x20000, file);
len = fread((char *) SRAM, 1, size, file);
fclose(file);
if (len - size == 512)
memmove(SRAM, SRAM + 512, size);
Expand All @@ -1983,7 +1985,7 @@ bool8 CMemory::LoadSRAM (const char *filename)
file = fopen(path, "rb");
if (file)
{
len = fread((char *) SRAM, 1, 0x20000, file);
len = fread((char *) SRAM, 1, size, file);
fclose(file);
if (len - size == 512)
memmove(SRAM, SRAM + 512, size);
Expand Down Expand Up @@ -2040,8 +2042,10 @@ bool8 CMemory::SaveSRAM (const char *filename)
}

size = SRAMSize ? (1 << (SRAMSize + 3)) * 128 : 0;
if (size > 0x20000)
size = 0x20000;
if (LoROM)
size = size < 0x70000 ? size : 0x70000;
else if (HiROM)
size = size < 0x40000 ? size : 0x40000;

if (size)
{
Expand Down
7 changes: 5 additions & 2 deletions server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,8 +1169,11 @@ void S9xNPSendSRAMToClient (int c)
uint8 sram [7];
int SRAMSize = Memory.SRAMSize ?
(1 << (Memory.SRAMSize + 3)) * 128 : 0;
if (SRAMSize > 0x10000)
SRAMSize = 0x10000;
if (Memory.LoROM)
SRAMSize = SRAMSize < 0x70000 ? SRAMSize : 0x70000;
else if (Memory.HiROM)
SRAMSize = SRAMSize < 0x40000 ? SRAMSize : 0x40000;

This comment has been minimized.

Copy link
@OV2

OV2 Aug 25, 2021

Collaborator

This will be a problem, since the client does not accept sram > 0x10000 (S9xNPGetSRAMData). But I'm not sure if netplay is even working right not.

This comment has been minimized.

Copy link
@bearoso

bearoso Aug 25, 2021

Collaborator

Netplay never worked well, so I'm not that concerned. I'd just add a hard error if someone tries to start it with more than 128kb, rather than give a semblance that it's going to work. The netplay code needs a complete rewrite that's more adaptive.

int len = 7 + SRAMSize;

sprintf (NetPlay.ActionMsg, "SERVER: Sending S-RAM to player %d...", c + 1);
Expand Down

2 comments on commit e66acce

@OV2
Copy link
Collaborator

@OV2 OV2 commented on e66acce Aug 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably also adjust this in cheats2.cpp

@qwertymodo
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheats and netplay client have been added in 46f11f6

Please sign in to comment.