gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > SRAM Writing, particularly on Supercard Lite

#104037 - Moeity - Tue Sep 26, 2006 2:57 am

I'm trying to save a simple single uint32 value to SRAM. I'm using some standard write and read functions:
Code:

void writeSram(int offset, char const* src, int size) {
        WAIT_CR &= ~0x0880;
        char* dest = (char*)SRAM;
        if(offset>0){
                while(offset--){
                        *dest++;
                }
        }
        int x = 0;
        while(size--){
                *dest++ = (uint8)(src[x]); x++;
        }
}

void readSram(int offset, char* dest, int size) {
        WAIT_CR &= ~0x0880;
        char const* src = (char const*)SRAM;
        if(offset>0){
                while(offset--){
                        *src++;
                }
        }
        int x = 0;
        while(size--){
                *dest++ = (uint8)(src[x]);
                x++;
        }
}


Then using:
Code:

     uint32 now_index;
     const int S_INDEX_LOC = 0;
                ...
     void save(){
                writeSram(S_INDEX_LOC, (char*)&now_index, sizeof(now_index));
    }
    void load(){
        readSram(S_INDEX_LOC, (char*)&now_index, sizeof(now_index));
    }


It appears to write, and load back fine when testing, however after powering off I always get a constant value that is not what I saved back. Am I doing anything obviously wrong in code?

It could also be I'm not setting up my Supercard Lite properly... I compiled in devkitarm, copied the program.nds file to my SD card as /program.nds, and copied the default .sav file from the Supercard program directory as /program.sav.

Any ideas? Once this is working I should have a version 0.1 of a working ebook reader with loadable aa fonts, run-time font-resizing and coloring, switchable potrait/landscape modes, and automatic page saving whose souce I can give to anyone interested. Hope to add some very basic html tag parsing and filesytem reading for version 0.2, at which time it should be a decent ebook reader.

#104039 - Moeity - Tue Sep 26, 2006 3:08 am

The code is fine, I was mistakenly QPC saving the wrong save file after rebooting the system. Sorry for the post, problem solved. Two days of being puzzled, then figured it out right after I posted here, of course...

#104053 - HyperHacker - Tue Sep 26, 2006 6:52 am

Moeity wrote:
Two days of being puzzled, then figured it out right after I posted here, of course...

I find the best way to find the solution to a problem is to ask for help. Maybe 80% of the time I find it within half an hour of posting or even while writing the post.
_________________
I'm a PSP hacker now, but I still <3 DS.

#104769 - sasq - Mon Oct 02, 2006 2:54 pm

Not to for nothing, but that code is a bit longish. This should be enough:

Code:

void writeSram(int offset,  const char *src, int size) {
        char *dest = ((char*)SRAM) + offset;
        while(size--) *dest++ = *src++;
}


EDIT:

or even
Code:

void writeSram(int offset,  const char *src, int size) {
        while(size--) ((char*)SRAM)[offset+size] = src[size];
}

if you want to really write short hard to read code :)

#104789 - sumiguchi - Mon Oct 02, 2006 5:52 pm

So does this really persist after a power recycle? I found that to persist the data, I had to do a quick power cycle (off then on in < 2 or 3 seconds) and then go to the Saver screen and manually write the current SRAM to the file.

It loads automatically but I haven't been able to get it to save automatically (so that it works after a power cycle)...

#104793 - sasq - Mon Oct 02, 2006 6:08 pm

I've never had such problems with SRAM. Simple 8bit writes has always been enough - do you use some non-standard GBA cart?

#104794 - Sektor - Mon Oct 02, 2006 6:30 pm

Yes, he uses a supercard. It doesn't have a battery to keep the SRAM, so unless you use the built-in feature to write the SRAM to CF/SD then it will be lost after power off.
_________________
GTAMP.com/DS