Quote:
Originally Posted by colonelsalt
How would I go about finding free space in RAM to store temporary values and buffer strings? FBI, for example, references offset 0x02021D18 (location of displayed strings) a number of times in his tutorials as a reliable place to store temporary data because of the "vast amount of free space" there. After poking around with VBA-SDL-H, though, I don't exactly see any strikingly obvious reason why this is the case. Is finding this free RAM space, then, largely a process of trial and error, or are there distinctive patterns one can look for in the code to find suitable locations?
|
To expand on daniilS's anwser: You have to be aware that much of the space in EWRAM (0x02000000) is reserved for malloc. If you need a lot of space, you can always just use malloc. You give the function a size and it returns a pointer to some free RAM of the specified size. Remember to use "free" on that pointer when you're done with that memory though!
Otherwise, if you need temporary RAM, just use the stack.
Code:
sub sp, #100
mov r0, sp
@ Have a pointer to 100 bytes of free RAM in r0 for the duration of this function
add sp, #100
If you need it to last a bit longer than that, you can use space that's just overwritten, like the text buffer FBI uses. That is only overwritten when a message is displayed. There are no real techniques for finding stuff like this. It's more of an educated guess, confirmed by debugging. We all knew that the text functions used some RAM to expand strings (when there is a buffer, it needs to change that buffer to actual text before changing each byte to a tile). We also guessed it didn't really matter what was there if the text renderer wasn't running. A quick check in IDA would've confirmed this.
Another technique I use is to find padding bytes. Most memory must be word aligned, so there is often a free hword or byte at the end of structures in the RAM. The IWRAM (0x03000000) isn't used by malloc, so you can just look in IDA for unused RAM there. Unused RAM has no XREFs and is therefore safe. I generally find a free word there, and malloc more space if I need it.
Things you can use (off the top of my head):
- Banks in scripts (up to four free words until a new script is run)
- Script variables (especially 0x8000 - 0x800F)
- Text renderer space
- Battle struct data (probably overwritten at the start of a battle anyway, up to 0x58 * 4 bytes if this is the case)
- Other battle structures, see above
Be aware that most times, this memory is temporary. If you need the memory to persist over saves and continues, you'll need to find free save block space. Often, this needs JPANs save block hack. Larger amounts of contiguous space are substantially harder to find.
__________________

A Pokemon that is discriminated!
Support squirtle and make it everyone's favourite.