alekmaul / pvsneslib

PVSnesLib : A small, open and free development kit for the Nintendo SNES
MIT License
830 stars 73 forks source link

Wrong behavior for sram with hirom #286

Open alekmaul opened 2 months ago

alekmaul commented 2 months ago

SRAM mapping in hirom is a bit more complicated in then in lowram LoROM (unless SRAM is < 8KiByte when SRAM is > 8KiB in size) https://snes.nesdev.org/wiki/Memory_map#HiROM SNESdev Wiki Memory map The SNES natively provides access to RAM and I/O at specific locations. The cartridge is free to provide whatever it wants in the remaining space, and there are specific address ranges that are conventionally used to add access to additional hardware such as extra RAM or a coprocessor. Different address ranges are accessed at different speeds, a... For HIROM, you would want to change BANK_SRAM to $30 The sta 0,y in line 190 to sta $6000,y The lda 0,y in line 236 to lda $6000,y And that would get consoleCopySram() and consoleLoadSram() temporarily working for HIROM if SRAM size <= 8KiByte (I think) A proper fix would use ifdefs and use defines for SRAM bank, starting address and bank size.

DigiDwrf commented 2 months ago

Indeed. That's why I came up with a solution:

the HiRom header looks like this:

.MEMORYMAP                      ; Begin describing the system architecture.
  SLOTSIZE $10000               ; The slot is $10000 bytes in size. More details on slots later.
  DEFAULTSLOT 0                 ; There's only 1 slot in SNES, there are more in other consoles.
  SLOT 0 $0000                  ; Defines Slot 0's starting address.
  SLOT 1 $0 $2000               ; Used for low RAM allocation
  SLOT 2 $2000 $E000            ; Used for RAM allocation
  SLOT 3 $0 $10000              ; Used for global RAM allocation
  SLOT 4 $6000                  ; Used for SRAM storage. <---- JUST HERE!!
.ENDME                          ; End MemoryMap definition

So, SLOT 4 is used for SRAM storage. A RAM secton can be added inside an ASM file like this:

.RAMSECTION ".save_file" BANK $30 SLOT 4
    save_file        DSB  2048            ; There's a size limit of 2048 bytes for HiRom
.ENDS

At last, an extern variable inside any .c or .h file has to be declared in order to access data:

typedef struct
{
    u32 var1;
    u16 var2;
    u8 var3;
    // etc...
} custom_savefie_struct;

extern custom_savefie_struct save_file[3]; // you can declare an array if you use more that one save file inside your program.

This is just an example, data can be declared an modified in any other way of course. And that's it! any modification to that variable, will automatically write SRAM data, and no fuction is needed.