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 > Writing SRAM failed - 0x80 or 0x8080 ?

#56109 - Didou - Thu Oct 06, 2005 7:09 am

Hi,

I'm currently trying to add scores saving in ReboNDS. But I've tried tons of things, what I get is either no save or a frozen DS...
However I tried to follow examples...

First of all, amongst threads, Wiki and tutos, it appeard that some advised to write WAIT_CR with the mask 0x8080 and some others with only 0x80. So which one is the right one ?

Following the code that freeze my DS when it is reached...
Any idea ? I must miss something... While debuging it appeared that the crashing code is exactly the bytes copy in the SRAM, not the WAIT_CR modifications.

I also tried using 0x80 as masks. In this case, the DS is not frozen, but no save is done...

Thank's

Code:
static void bytes_memcpy (char* dest, char *src, int size)
{ while (size--) *dest++ = *src++ ; }


void save_scores ()
{
  u16 i ;

  tmp_data.magic_tag = MAGICNUMBER ;
  /* Scores. */
  for (i = 0; i < sizeof (struct highscores); i++)
    ((u8*) &tmp_data.highscores)[i] = ((u8*) &global_highscores)[i] ;
  WAIT_CR &= ~0x80 ;
  /* It is now safe to write GBA Cartridge memory */
  bytes_memcpy ((char*) SRAM, (char*) &tmp_data, sizeof (struct save_data)) ;
  WAIT_CR |= 0x80 ;
}

#56113 - FluBBa - Thu Oct 06, 2005 9:04 am

What is SRAM declared as?
_________________
I probably suck, my not is a programmer.

#56117 - Didou - Thu Oct 06, 2005 10:04 am

Hi,

Quote:
What is SRAM declared as?

It comes from the NDSLib's define. I didn't check it value, but I hope it to be 0x0A000000.

-- Didou

#56208 - cory1492 - Fri Oct 07, 2005 3:36 am

Code:
#define SRAM          ((uint8*)0x0A000000)
(from ndslib source)

I have been using this to write/read SRAM with ETool:
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++;}
}

A little bloated but I found it reliable... the CR that I am setting here gives access to both SRAM and the DS card. Never really looked into unsetting the wait cr though...

0x80 should give access to gba cart only (as in doublec's tutorial)

#59397 - Didou - Tue Nov 01, 2005 2:38 pm

Hi,

Please, don't laugh :)))

A few days ago, I updated ReboNDS and told that highscores were working thank's to the R17. Then later I corrected because in fact they looked not working... I thought I mistook...

I tried again today ... then it works once, twice and not anymore. I mean that my scores got saved a short time, then nothing...

You know what ... my RAM-battery was depleted :)))) I tried on my old FlashAdvance, and it works finally fine with the good old ~=0x0880 !

Thank you to all of you who helped me !

-- Didou