SRAM read / write

SGDK only sub forum

Moderator: Stef

nemezes
Very interested
Posts: 69
Joined: Sat Mar 31, 2018 1:09 pm

Re: SRAM read / write

Post by nemezes » Mon Apr 27, 2020 6:50 pm

cloudstrifer wrote:
Mon Apr 27, 2020 5:56 pm
Example
I don't know if it's the best way to do, but it works.

Code: Select all


u32 sRamOffSet = 0x0000;

sRamOffSet ++;
This was my main doubt, how to get another values for sRamOffset. Thanks. I will try it. :)

cloudstrifer
Very interested
Posts: 118
Joined: Mon Feb 19, 2018 7:31 pm

Re: SRAM read / write

Post by cloudstrifer » Mon Apr 27, 2020 7:10 pm

I think sRamOffSet is the start address used by a sram read or write.

So sRamOffSet can be from 0 to sram size.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: SRAM read / write

Post by Stef » Mon Apr 27, 2020 7:45 pm

It's really simple, offset is just the address where you want to write in SRAM but the restriction is that you can read/write only 1 value at once.
Did you tested ? that should work :)

nemezes
Very interested
Posts: 69
Joined: Sat Mar 31, 2018 1:09 pm

Re: SRAM read / write

Post by nemezes » Mon Apr 27, 2020 8:45 pm

Stef wrote:
Mon Apr 27, 2020 7:45 pm
It's really simple, offset is just the address where you want to write in SRAM but the restriction is that you can read/write only 1 value at once.
Did you tested ? that should work :)
I did what cloudstrifer said. I made the first offset sRAMoffset = 0x0000, then the others I use sRAMoffset2 = sRAMoffset+1.

it worked using Fusion. blastem and gens I got some issues, I dont know if they support SRAM.

Sik
Very interested
Posts: 939
Joined: Thu Apr 10, 2008 3:03 pm
Contact:

Re: SRAM read / write

Post by Sik » Mon Apr 27, 2020 10:30 pm

You know how you have 32KB worth of SRAM, right? sRamOffSet is the location within SRAM.

sRamOffSet = 0x0000 would be the 1st byte, 0x0001 would be the 2nd byte, 0x0002 would be the 3rd byte, etc. (up to 0x7FFF, the last byte).
Sik is pronounced as "seek", not as "sick".

nemezes
Very interested
Posts: 69
Joined: Sat Mar 31, 2018 1:09 pm

Re: SRAM read / write

Post by nemezes » Mon Apr 27, 2020 10:58 pm

Sik wrote:
Mon Apr 27, 2020 10:30 pm
You know how you have 32KB worth of SRAM, right? sRamOffSet is the location within SRAM.

sRamOffSet = 0x0000 would be the 1st byte, 0x0001 would be the 2nd byte, 0x0002 would be the 3rd byte, etc. (up to 0x7FFF, the last byte).
I didnt know about that.

So I just have to use one sRamOffSet for all the values?

I really dont know how to measure bytes and so on.

How many u8 variables will fit in 1 byte?

nemezes
Very interested
Posts: 69
Joined: Sat Mar 31, 2018 1:09 pm

Re: SRAM read / write

Post by nemezes » Mon Apr 27, 2020 11:57 pm

I think I figured out.

I made the first offset 0x0000, then the next.one should be +256, as I saw that 1 byte is like 256.

So I also may learnt that 1 byte = 1 u8 variable.

As I see sik wrote that F in the 0x7FFF kind of number, it should be base 16.

nemezes
Very interested
Posts: 69
Joined: Sat Mar 31, 2018 1:09 pm

Re: SRAM read / write

Post by nemezes » Tue Apr 28, 2020 2:00 am

SRAM works in gens and fusion now, but wont work in blastem.

dont know why.

is there some init command to be made? some variable to adjust in rom_head.c file?

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Re: SRAM read / write

Post by Chilly Willy » Tue Apr 28, 2020 1:22 pm

nemezes wrote:
Mon Apr 27, 2020 11:57 pm
I think I figured out.

I made the first offset 0x0000, then the next.one should be +256, as I saw that 1 byte is like 256.

So I also may learnt that 1 byte = 1 u8 variable.

As I see sik wrote that F in the 0x7FFF kind of number, it should be base 16.
Totally wrong. A u8 variable is an unsigned byte, which is 8 bits. It holds a VALUE of 0 to 255. The value passed to the sram functions is an offset - an INDEX associated with the position inside an array. That offset can be 0 to 32767. You can pass it via a variable or constant or expression. If you use a variable to hold the offset for use later, that variable must be at least a word since the offset VALUE is 0 to 32767 (a u16 or s16 should be fine).

Doing +256 on the offset skips 255 spaces inside the sram each time, wasting a lot of sram space. You can do it and it will work, but you'll only have 128 spaces instead of 32768 spaces with the rest being wasted.

nemezes
Very interested
Posts: 69
Joined: Sat Mar 31, 2018 1:09 pm

Re: SRAM read / write

Post by nemezes » Wed Apr 29, 2020 12:51 pm

Chilly Willy wrote:
Tue Apr 28, 2020 1:22 pm
nemezes wrote:
Mon Apr 27, 2020 11:57 pm
I think I figured out.

I made the first offset 0x0000, then the next.one should be +256, as I saw that 1 byte is like 256.

So I also may learnt that 1 byte = 1 u8 variable.

As I see sik wrote that F in the 0x7FFF kind of number, it should be base 16.
Totally wrong. A u8 variable is an unsigned byte, which is 8 bits. It holds a VALUE of 0 to 255. The value passed to the sram functions is an offset - an INDEX associated with the position inside an array. That offset can be 0 to 32767. You can pass it via a variable or constant or expression. If you use a variable to hold the offset for use later, that variable must be at least a word since the offset VALUE is 0 to 32767 (a u16 or s16 should be fine).

Doing +256 on the offset skips 255 spaces inside the sram each time, wasting a lot of sram space. You can do it and it will work, but you'll only have 128 spaces instead of 32768 spaces with the rest being wasted.
Thanks!! After digging a lot and talking with a friend. We looked headers of other games and we figured out how to make the save works in blastem, gens, fusion and everdrive cart.

I will talk to other friend and see if he can test in a regular cart with sram save.

Also, I will write here what I did to make everything work (I hope it is a right way of doing it)

nemezes
Very interested
Posts: 69
Joined: Sat Mar 31, 2018 1:09 pm

mini tutorial SRAM read / write with SGDK

Post by nemezes » Wed Apr 29, 2020 4:05 pm

here is my mini tutorial of how I achived to use save with sram
(I can edit it, if someone find something wrong)

thanks to Chilly Willy, SIk, Stef and cloudstrifer.

---

1) to activate the sram save in your game, you should go to folder src/boot and look for file rom_head.c
you will find lines like these:

Code: Select all

...
0x00FF0000,
0x00FFFFFF,
"  ",
0x0000,
0x00200000,
0x002001FF,
"            ",
"DEMONSTRATION PROGRAM                   ",
"JUE             "
...
following the comments in this file, you should do this

Code: Select all

...
0x00FF0000,
0x00FFFFFF,
"RA",      /* "RA" for save ram (2) */
0xF820,    /* 0xF820 for save ram on odd bytes (2) */
0x200001,  /* SRAM start address - normally 0x200001 (4) */
0x2003FF,  /* SRAM end address - start + 2*sram_size (4)
              My comment: as Sik and Chilly Willy said, you can go up to 0x207FFF
              (this is a hexadecimal number, base 16, and is equal to 32767) */
"            ",
"DEMONSTRATION PROGRAM                   ",
"JUE             "
...
2) now that sram save is active, you can code what you want. :)
example how to save three u8 variables and to take what was stored there to other three u8 variables.

Code: Select all

...

u32 sRAMoffset1;
u32 sRAMoffset2;
u32 sRAMoffset3;

SRAMoffset1 = 0x0000;
SRAMoffset2 = 0x0001;
SRAMoffset3 = 0x0003; /* as, in this example, it is activate do use memory up to address 0x2003FF,
                         you can create 0x03FF=1023 SRAMoffset variables. */
                         
u8 variable1;
u8 variable2;
u8 variable3;

variable1 = 10;
variable2 = 255;
variable3 = 100;

SRAM_enable(); // to use SRAM, shoud enable it

SRAM_writeByte(SRAMoffset1,variable1); // will record the value of variable1 in SRAM at SRAMoffset1
SRAM_writeByte(SRAMoffset2,variable2); // will record the value of variable2 in SRAM at SRAMoffset2
SRAM_writeByte(SRAMoffset3,variable3); // will record the value of variable3 in SRAM at SRAMoffset3

SRAM_disable(); // after do what you want, should disable it

...

u8 var_value1;
u8 var_value2;
u8 var_value3;

SRAM_enable();

var_value1 = SRAM_readByte(SRAMoffset1); // will give the var_value1 the value stored in SRAMoffset1
var_value2 = SRAM_readByte(SRAMoffset1); // will give the var_value2 the value stored in SRAMoffset2
var_value3 = SRAM_readByte(SRAMoffset1); // will give the var_value3 the value stored in SRAMoffset3

SRAM_disable();

...

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Re: SRAM read / write

Post by Chilly Willy » Thu Apr 30, 2020 2:31 pm

Using multiple variables for offsets is a bit wasteful, as is making them all u32. :D But if you have ram to burn, it's not a big deal.

Another thing to remember is that if your rom is >2MB in size, enabling the save ram kills any rom above 2MB until it's disabled. Never put save ram code in the upper 2M space of the rom, never have interrupt code in the upper 2M, or data that interrupts may depend on. If you do, you also need to disable interrupts before you enable the save ram, then re-enable the interrupts after you disable the save ram. If your rom is less than or exactly equal to 2MB, this isn't an issue and you COULD just leave the save ram enabled. Sonic 3 does that.

nemezes
Very interested
Posts: 69
Joined: Sat Mar 31, 2018 1:09 pm

Re: SRAM read / write

Post by nemezes » Thu Apr 30, 2020 6:35 pm

Chilly Willy wrote:
Thu Apr 30, 2020 2:31 pm
Using multiple variables for offsets is a bit wasteful, as is making them all u32. :D But if you have ram to burn, it's not a big deal.

Another thing to remember is that if your rom is >2MB in size, enabling the save ram kills any rom above 2MB until it's disabled. Never put save ram code in the upper 2M space of the rom, never have interrupt code in the upper 2M, or data that interrupts may depend on. If you do, you also need to disable interrupts before you enable the save ram, then re-enable the interrupts after you disable the save ram. If your rom is less than or exactly equal to 2MB, this isn't an issue and you COULD just leave the save ram enabled. Sonic 3 does that.
I am not thinking of save memory space and so on, it is just a tip for those like me that was lost and dont know what to do to start using SRAM read/write.

thanks for the new info!

as for start, when I use save in my games, I will let it with less than 2MB to avoid any problem.

Post Reply