ROM vs RAM

SGDK only sub forum

Moderator: Stef

Post Reply
themrcul
Very interested
Posts: 116
Joined: Fri Apr 15, 2016 2:21 pm

ROM vs RAM

Post by themrcul » Wed Apr 19, 2017 9:22 am

Hi all,
I'm sorry to ask what is probably such a stupid question, but I am very new to all this!

I am wondering what the rules around modifying global variables in code are.

I have set up a synthetic test which declares a string in global space like this:

Input.c:

Code: Select all

char variable[] = "blah";
Then later, in an Update() function, I modify the string by setting it's contents like this:

Code: Select all

void Update()
{
	if (joystate & BUTTON_A) 
	{
	variable[2] = 'a';
	variable[3] = 'h';
	}
}
In another update function, I am printing the variable to screen with:

Code: Select all

VDP_drawTextBG(PLAN_A, variable, 1, 15);
It works.
The string updates on the screen. But I was under the impression that all of the code is stored in ROM, and would therefore not be modifiable?

Do I need to declare this variable in RAM by calling malloc() and setting the returned contents to the string I want for modifyable variables? Or is it ok to modify any global variable once the game starts, and it be ok as in my test?

The reason I am asking is because I am exporting sprite definitions into .c files and want to export most of it's data in tact, but want to set function pointers into them at runtime. Is that possible? Or do I need to malloc the size of memory needed to hold the sprite definition, set the data into that malloced memory and go from there?

I would really like to just set the data into the variable directly without mallocing everything but want to make sure I am doing the right thing before everything explodes!

Thanks guys.

Grind
Very interested
Posts: 69
Joined: Fri Jun 13, 2014 1:26 pm
Location: US
Contact:

Re: ROM vs RAM

Post by Grind » Wed Apr 19, 2017 11:57 am

All global variables are loaded into RAM unless they are declared const. If you are using SGDK the boot/sega.s file loads that stuff for you on startup or a hard reset.

I've never had to modify a SpriteDefinition, which rescomp creates as const (so ROM). There is probably a much better way to do whatever you are trying to do. Is it because you want different animation speeds? In that case you may want to set the speed to 0 in the res file and advance frames manually.

Otherwise you are gonna be calling MEM_alloc() and copying the sprite def into it. And not just the definition but the many pointers to varying numbers of frames, animations, etc which are all their own structures. That would be a waste of memory on top of being complex.

themrcul
Very interested
Posts: 116
Joined: Fri Apr 15, 2016 2:21 pm

Re: ROM vs RAM

Post by themrcul » Wed Apr 19, 2017 1:27 pm

Hi Grind,
Thanks so much for your detailed reply.

That would explain why the code didn't crash and worked!

I am building my own sprite engine, so I'm sorry I likely created confusion talking about SpriteDefinition's - that's the name of one of my structs that holds sprite animation info data.

Depending on how I structure the data though, I may get away with having some parts const and the "modifiable" parts non-const. For example, everything about playing the animation is const, but the x and y positions, health, etc, are in a non const format:

Code: Select all

typedef struct SpriteAnimation
{
	// info about sprites, anim speed, etc
}SpriteAnimation;

typedef struct SpritePlacement
{
	const SpriteAnimation* currentAnimation;
	void* UpdateFunction;
	void* CollisionFunction;
	u16 health;
	u16 x;
	u16 y;
}SpritePlacement;

SpritePlacement placement = { ... };
Not the best example but you get the idea!

This means I'll have to watch my memory usage more carefully...

Anyway, thanks again Grind.

Post Reply