Page 1 of 1

Storing game data

Posted: Thu Aug 02, 2007 2:46 am
by mbrenaman
I was wondering... What is the best way to store game data (in a NES game) that can be easily loaded and edited by a computer program (written in something like C)?

There are many types of data in the game, some fixed sized entries, as well as variable sized entries. I want to keep game data on 2 fixed 16kb banks, and to just be two big files inc'ed into the needed bank.

Should I just use lookup table at the start of each data bank, or is there a better method?

I think the easiest to deal with would be lookup tables NOT split into two one byte list. Seems it would be easier to code and manage in my opinion.

Any comments welcome.

Posted: Thu Aug 02, 2007 8:04 am
by kyuusaku
If you're going to be writing a C program, why not allocate space in the game for the data and have the C program directly edit the game? This could be later expanded to much more too like a game editor or something.

I would do the table route. For data of a defined size, store a table of structures containing the bank number (for switching) and a pointer to the address of the data. For variable size data, I'd say do the same but add a size constant.

Posted: Thu Aug 02, 2007 8:18 am
by never-obsolete
for variable length strings, i've seen things like this:

Code: Select all

	asl				; A=string entry index
	tax

	lda PtrTable, X			; get pointer
	sta DataPtr
	lda PtrTable + 1, X
	sta DataPtr + 1

	lda PtrTable + 2, X		; get length
	sec
	sbc PtrTable, X
	tax
where the length is calculated by subtracting the low byte of the needed entry from the low byte of the entry that follows. you would also need a "terminating" pointer at the end of the table so your last string ends at the correct point.

Posted: Thu Aug 02, 2007 8:33 am
by mbrenaman
If you're going to be writing a C program, why not allocate space in the game for the data and have the C program directly edit the game? This could be later expanded to much more too like a game editor or something.
That was kind of my plan.
I would do the table route. For data of a defined size, store a table of structures containing the bank number (for switching) and a pointer to the address of the data. For variable size data, I'd say do the same but add a size constant.
I'm not sure I follow you there.

I figured I'd have main pointer table at the start of each bank. A routine on the fixed bank where the vectors are, would fetch the address by a one byte index value. Some of the main table entries can lead to other pointer tables on the same bank for variable length things like strings, etc.
Which would be load be the same routine.

When I need to load the first/next byte, just use a simple routine on the vectors bank.

Does this sound ok?

Posted: Thu Aug 02, 2007 10:38 am
by kyuusaku
Here's what I meant in C:

Code: Select all

struct {
byte bank; /*the bank the data is in*/
word pointer; /*6502 address to start of data */
word size; /*size of data */
} entry;

entry table[256]; /*table of structures containing information on how to retrieve data items 0-255 */

write_bank(table[datanumber].bank); /* bankswitch to bank containing data */
size = table[datanumber].size;
address = table[datanumber].address;

while (size != 0) {
do_something(get(*address++));
size--;
}

Posted: Thu Aug 02, 2007 11:35 am
by mbrenaman
Oh. I see completly what you meant. Sorry.

Well, thanks for the help. It's very much appreciated.