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.

C/C++ > Saving RAW Data

#13969 - dagamer34 - Fri Dec 26, 2003 6:24 pm

Does anybody have code to save RAW data (like a struct)? I have tried to implement it by saving bytes but my code seems totally off. Any help here?
_________________
Little kids and Playstation 2's don't mix. :(

#13972 - jma - Fri Dec 26, 2003 7:57 pm

Where would you like to save them to? Obviously the GBA doesn't have files, but if you have a couple areas of memory set aside, you could use memcpy() or memmove() to do it for you:

Code:
MY_STRUCT mySource;
MY_STRUCT myDest;

memcpy(&myDest,&mySource,sizeof(MY_STRUCT));


This copies, perfectly, all the data from one structure into another. The destination need not be a structure either, but just a reserved array of bytes (that is big enough to host the source data).

Is this what you needed?

Jeff
_________________
massung@gmail.com
http://www.retrobyte.org

#13991 - dagamer34 - Sat Dec 27, 2003 1:02 am

It seems that memcpy isn't defined. So is there any other way to copy raw data to SRAM?
_________________
Little kids and Playstation 2's don't mix. :(

#13993 - tepples - Sat Dec 27, 2003 1:18 am

To use a function, you must both declare it and define it. To declare memcpy(), place the line #include <stdlib.h> near the top of any C file that calls memcpy(). To define memcpy(), link libc into your program. If you're using arm-agb-elf-gcc, this is done automatically; if you're using arm-agb-elf-ld directly, you have to make sure that your linker command includes -lc -lgcc.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#13994 - dagamer34 - Sat Dec 27, 2003 1:24 am

Never mind, i figured it out.

If anybody wants the code, here it is:

Code:

// Saves raw data to SRAM
void SaveRaw (u16 offset, u8* rawdata, u16 size)
{
   u8* temp = (u8*)rawdata;
   u16 loop;

   for (loop = 0; loop < size; loop++)
   {
      *(u8 *)(SRAM + offset + loop) = temp [loop];
   }
}

// Loads raw data from SRAM
void LoadRaw (u16 offset, u8* rawdata, u16 size)
{
   u8* temp = (u8*)rawdata;
   u16 loop;

   for (loop = 0; loop < size; loop++)
   {
       temp [loop] = *(u8 *)(SRAM + offset + loop);
   }
}


And to use it:
Code:


SaveRaw (3, (u8*)&test, sizeof (Test));
LoadRaw (3, (u8*)&test, sizeof (Test));


And there you have it! Make sure to keep in mind where everything is located, don't overwrite other stuff you've saved.
_________________
Little kids and Playstation 2's don't mix. :(

#14011 - sajiimori - Sat Dec 27, 2003 9:35 am

Congratulations on rewriting memcpy() in a slower and less flexible form. Avoid such redundant code by learning the standard C library.

http://www-ccs.ucsd.edu/c/lib_over.html

#14015 - tom - Sat Dec 27, 2003 12:34 pm

memcpy() is exactly what you shouldn't use when accessing the sram.

memcpy usually tries to be smart and does 32 bit accesses whenever possible, and the sram can only be accessed bytewise.

#14041 - dagamer34 - Sun Dec 28, 2003 2:09 am

Don't you just feel great when you do something you thought you couldn't do to only realize that it is slow and there is a faster way to do it?

Well.. then, it works... and its not like you will be saving data every frame (what if someone takes that cart out??)

Besides, some of us are NOT expert coders... no need to go around *insulting* others.
_________________
Little kids and Playstation 2's don't mix. :(

#14046 - sajiimori - Sun Dec 28, 2003 7:35 am

No need to feel insulted...if tom is right (I don't know much about gba sram), you'll have a good use for your code after all.

#14094 - dagamer34 - Mon Dec 29, 2003 8:01 pm

How do you link in a library? Because I added in those options into my makefile (i am using the GBA Appwizard one) and i still get an undefine.
_________________
Little kids and Playstation 2's don't mix. :(

#14098 - sajiimori - Mon Dec 29, 2003 8:52 pm

If you use the gcc front end to link (instead of ld), it will automatically include the standard library. With ld you have to use -lc (or -l<library_name> in general).

#14187 - dagamer34 - Thu Jan 01, 2004 12:10 am

According to GBATek, SRAM can ONLY be read and written from 8-bits at a time, so my code really does have some use.
_________________
Little kids and Playstation 2's don't mix. :(