#18361 - regularkid - Wed Mar 24, 2004 11:19 pm
Hi!
I'm currently trying to debug some memory corruption in my game and I think the culprit might be that my IWRAM usage is just getting to large at runtime and overwritting some other stuff (because the stack gets too large).
What address does the stack start at, and is there a way to determine the size of the stack at runtime?
Anyone else ever experience anything like this? Thanks!
_________________
- RegularKid
#18363 - regularkid - Wed Mar 24, 2004 11:53 pm
I did some more searching and found that the stack starts at 0300:7F00. Does this grow upwards towards 0300:8000, or downwards towards 0300:0000?
_________________
- RegularKid
#18365 - Miked0801 - Thu Mar 25, 2004 12:02 am
You can set the address anywhere and all stacks grown backwards from high addr to low.
#18366 - regularkid - Thu Mar 25, 2004 12:16 am
Is there a way to display at runtime the size of the stack? Actually, the best thing would be if it's possible to keep track of the max size that the stack has grown to during the program, then I can figure out if it is getting too big and overwritting other stuff in RAM.
_________________
- RegularKid
#18371 - torne - Thu Mar 25, 2004 1:49 am
You can find the current value of the stack pointer by, uhm, reading the stack pointer register (this will require inline asm from C). The GBA doesn't have stack limit checking; my GBA OS kernel checks for stacks growing too large by having magic values written near the bottom of the stack segment and checking that they are intact on context switches. You could do something similar.
#18420 - Miked0801 - Thu Mar 25, 2004 8:40 pm
For stack growth watching, I place a known value in the stack space on bootup (being careful not to munge the stack were it already is) - soemthing like DEADBEEF or C0CAC01A or whatever. Then, whenever you want to see where your stack is, stop the game in an emulator and look for where the pattern begins. You could also do this in VBlank every tic in code and place that vlaue on screen.
#18490 - Derek - Sat Mar 27, 2004 5:59 pm
Quote: |
Actually, the best thing would be if it's possible to keep track of the max size that the stack has grown to during the program |
Have you tried this system. Store a pointer to a main variable on startup. Just make up a dummy if you are using AGBMain().
Use a function with another dummy variable and subtract the two pointers to get the current stack size. Works a treat and is standard C.
Code: |
int* stack_start;
int stack_size(int dummy)
{
return (int)stack_start - (int)&dummy;
}
int main(int argc, char* argv[])
{
stack_start = &argc;
printf("%d\n", stack_size(0));
return 0;
} |
_________________
Yeti3D Portable 3D Engine