sega16 wrote:oofwill wrote:
now i have a question : is there any way to save score (in order to use it with a original pcb then) with SGDK?
Yes you could make a pcb with sram and cr2032 battery but if you just want to save the score it would make the pcb cost less to come up with a password system. An idea I had was to xor encrypt the score and have a simple hash as part of the password that is xor encrypted with another value. This will prevent players from trying to get a higher score by entering a false password.
I think he means how do you read/write sram using SGDK. There's no built in support - you have to edit the header in rom_head.c, then use pointers to access the sram.
The rom header is normally this (from rom_head.c)
Code: Select all
char console[16]; /* Console Name (16) */
char copyright[16]; /* Copyright Information (16) */
char title_local[48]; /* Domestic Name (48) */
char title_int[48]; /* Overseas Name (48) */
char serial[16]; /* Serial Number (2, 14) */
u16 checksum; /* Checksum (2) */
char IOSupport[16]; /* I/O Support (16) */
u32 rom_start; /* ROM Start Address (4) */
u32 rom_end; /* ROM End Address (4) */
u32 ram_start; /* Start of Backup RAM (4) */
u32 ram_end; /* End of Backup RAM (4) */
char modem_support[24]; /* Modem Support (24) */
char notes[40]; /* Memo (40) */
char region[16]; /* Country Support (16) */
When using sram, it should be
Code: Select all
char console[16]; /* Console Name (16) */
char copyright[16]; /* Copyright Information (16) */
char title_local[48]; /* Domestic Name (48) */
char title_int[48]; /* Overseas Name (48) */
char serial[16]; /* Serial Number (2, 14) */
u16 checksum; /* Checksum (2) */
char IOSupport[16]; /* I/O Support (16) */
u32 rom_start; /* ROM Start Address (4) */
u32 rom_end; /* ROM End Address (4) */
u32 ram_start; /* Start of Backup RAM (4) */
u32 ram_end; /* End of Backup RAM (4) */
char sram_sig[2]; /* "RA" for save ram (2) */
u16 sram_type; /* 0xF820 for save ram on odd bytes (2) */
u32 sram_start; /* SRAM start address - normally 0x200001 (4) */
u32 sram_end; /* SRAM end address - start + 2*sram_size (4) */
char modem_support[12]; /* Modem Support (12) */
char notes[40]; /* Memo (40) */
char region[16]; /* Country Support (16) */
Then as long as your rom is less than or equal to 2MBytes, you simply turn on the sram and then read/write it directly.
turn on sram
Code: Select all
*(vu8*)0xA130F1 = 1; /* enable sram */
If your rom is bigger than 2MBytes, you need to turn off the sram unless you are using it. When you do use it, you need to disable ints, turn on the sram, read/write it, turn off the sram, then enable ints.
turn off sram
Code: Select all
*(vu8*)0xA130F1 = 0; /* disable sram */
Note - you can turn on the sram but have it write-protected (read only) by doing
Code: Select all
*(vu8*)0xA130F1 = 3; /* enable sram, write-protected */
Setting it to 2 disables the sram and write-protects it. Most folks don't worry about the write-protect status and just use 0/1. (Note - also, some PCBs don't support write-protection)
To read/write the sram, read/write bytes at odd positions.
read
Code: Select all
u8 read_sram(int offset)
{
return *(vu8*)(0x200001 + offset*2);
}
write
Code: Select all
void write_sram(int offset, u8 val)
{
*(vu8*)(0x200001 + offset*2) = val;
}