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 Highscores!!!!????

#71427 - lewisrevill - Sun Feb 12, 2006 8:51 pm

Hey people

Hope your all well. Anyway I was wondering if anyone one has any advice on how to save highscores onto the cartidge? Code would be a god send!

#71431 - tepples - Sun Feb 12, 2006 9:45 pm

Search the GBA beginners' FAQ for "saving".
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#71447 - lewisrevill - Sun Feb 12, 2006 11:38 pm

Yeah, I have looked at the FAQ and its about as clear as mud to me! Can anyone explain it differently to me and maybe supply some code thats better to understand? Thank you

#71449 - keldon - Sun Feb 12, 2006 11:46 pm

What don't you understand?

#71455 - lewisrevill - Mon Feb 13, 2006 12:14 am

Most of it. Im quite new to this part of the programming. I know there is a set pointer for SRAM which I know. But then I dont know how to write it or Read it? Any help?

#71463 - keldon - Mon Feb 13, 2006 12:45 am

The general idea of it is that you are (a) writing to an area of memory to save, and (b) reading from an area of memory to read.

The Plan
I am going to assume you have a text system set up; and I want you to implement a basic saving / loading system function. The basic idea is that when the player presses "A" then the text "Bulldog" is stored; and whatever pressing "B" will display 8 whatever 8 characters at the memory in the save area.

Activity
Code:
// address of SRAM
#define SRAM ((unsigned char *)0x0e000000)

The FAQ gives you the address of SRAM. Your job is to read and write to it. When the player presses "A", then write bulldog using bytecopy.

Code:
// using bytecopy to write
bytecpy(SRAM + 4, &text, sizeof(text));


When the player presses "B", then read bulldog using bytecopy. Write 0 to byte 9 of your char* so that the string is null terminated.

Code:
// using bytecopy to read 8 bytes
bytecpy(&text, SRAM + 4, 8); 



Reflection on activity
There is not that much to this activity; as you are just reading/writing a single piece of text. However the main part is covered: reading and writing to and from SRAM.

You use the bytecpy method as they copies bytes each time, as you can only read/write one byte at a time with SRAM.

You read and write to and from (SRAM+4). This is because the first and last byte of SRAM usually becomes corrupt, so we try to avoid accessing it.

Facts etc.
SRAM only allows byte transfers from the CPU and does not allow DMA access.

Your one true guide to the gba inner workings is found here and in the FAQ and forum

Going further
Now you can experiment further by storing more important data like a room structure in a bigger environment, such as a room with a person which moves around. Sometimes you have to work with your data when the data you wish to store contains addresses, etc. as these addresses will most likely not be valid when you load. But in general you have one large array to store your data in so just think about how that is going to be done.

#71570 - lewisrevill - Mon Feb 13, 2006 2:38 pm

Thank you Keldon

Thats clearer now. And forgive my nieveness but the 'text' is a variable with the data in? And how do you create the bytecpy() command. Again i have read the FAQ and dont understand what are variables and what is the set code. can you help me with this? Thank you again it is appreciated! You da man!

#71581 - keldon - Mon Feb 13, 2006 3:59 pm

Code:
// taken from method in FAQ
void bytecpy(void *restrict in_dst, const void *restrict in_src, unsigned int length)
{
  unsigned char *restrict src = (unsigned char *)in_src;
  unsigned char *restrict dst = (unsigned char *)in_dst;

  for(; length > 0; length--)
    *dst++ = *src++;
}


Quote:
And forgive my nieveness but the 'text' is a variable with the data in?

Yes, just a text string.

Quote:
dont understand what are variables and what is the set code

Just look at the prototype of the bytecpy method:
Code:
void bytecpy(void *dest, const void *source, unsigned int length)

When setting/saving data the dest is SRAM; and when loading the source is SRAM.

#71617 - lewisrevill - Mon Feb 13, 2006 7:38 pm

Im still struggling im afraid, How would the code actually be written? Exactly how it is shown? What needs to be changed?

#71632 - tepples - Mon Feb 13, 2006 8:41 pm

Do you know what memcpy() does? The bytecpy() function does the same thing but always accesses SRAM in a safe manner.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#75929 - Dilyias - Fri Mar 17, 2006 1:25 am

lewisrevill wrote:
Im still struggling im afraid, How would the code actually be written? Exactly how it is shown? What needs to be changed?


I've been programming GBA for about a week and I checked out the FAQ. It's crystal clear to me. It has all the code you need, all you have to do is fill in the values of the example structure, data, before you call the bytecpy command to store the data.

It is evidence that you do not have a good working knowledge of C and you probably have not gone through any of the GBA tutorials.

I strongly suggest tackling these two first before moving on. You need a good foundation or you will end up wasting a lot of time.

Once you have a handle on C, go over this tutorial several times. Make sure you understand all of it (except maybe the Advanced section). Once you master this, stuff like saving will be a breeze. :-)

http://user.chem.tue.nl/jakvijn/tonc/index.htm

Bookmark it! :-)

Eric