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.

Coding > OOP in GBA Dev

#45454 - merodia - Fri Jun 10, 2005 8:35 pm

Hi,

Just wondering. When you use objects in programs the compiler allocates new memory for every instance of a class, but that is on the PC which has way more memory than a GBA. What i would like to know is? where would the instances be stored on the GBAs memory if it is possible to do so.

Thank you

#45459 - poslundc - Fri Jun 10, 2005 9:43 pm

Global objects and objects declared inside of functions go into IWRAM, which is only 32K, as you observed.

Instances created dynamically with new or malloc go into EWRAM, which you have 256K of, but is much slower than IWRAM.

(This behaviour is modifiable, but the above is the default.)

Dan.

#46428 - Steve++ - Fri Jun 24, 2005 5:31 pm

Ah crap.... I'm creating large object pools off the stack (haven't compiled anything yet), and you're telling me that will be done in IWRAM? I don't know anything about modifying the behaviour (I'm assuming crt0.s has something to do with it). Can I make local objects allocatble from EWRAM and local variables (primitives) allocatable from IWRAM, or is there just one stack for locals? Is it possible to add an attribute to a local object to make it allocatable from EWRAM?

Perhaps I should just bite the bullet and dynamically allocate the object pool. I was trying to be a stack allocation purist, but I guess that won't happen now.

#46430 - DekuTree64 - Fri Jun 24, 2005 5:49 pm

One trick you cuold use is to make a secondary EWRAM stack. Make a global pointer and set it to the top of EWRAM when you start your game. When you want to allocate from it, subtract the size of your object, and then create it there with placement new.
When you're done with the object, just delete it like normal, and then add the size back onto the EWRAM stack pointer.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#46432 - poslundc - Fri Jun 24, 2005 5:52 pm

In GCC you can use the __attribute__ directive to specify where you want a variable to be placed, eg.

Code:
int variableInEWRAM __attribute__ ((section (".ewram")));     // Placed in EWRAM


I believe that you can use .sbss in DevKitARM instead of .ewram to have the same effect but leave the data uninitialized (which keeps your ROM size down).

Alternatively, if you're not using dynamic allocation or multiboot, EWRAM should be left untouched by the compiler, so you can just grab a pointer and do whatever you want with it. Common uses include mapping EWRAM to a global struct, or taking a pointer to the end of EWRAM and growing your own stack down from it.

Dan.

Edit: DekuTree beat me to it... :P

#46434 - DekuTree64 - Fri Jun 24, 2005 6:32 pm

Oh, and how 'bout this for even more fun:

Code:
// void CallWithCustomStack(void *funcAddr, void *stackAddr)

.global CallWithCustomStack
.arm
.align 2
CallWithCustomStack:
mov  r2, sp          // Preserve original stack
mov  sp, r1          // Set stack pointer to stackAddr arg
stmfd sp!, {r2, lr}  // Save old stack and lr on the custom stack
mov  lr, pc          // Call the function
bx   r0
ldmfd sp, {sp, lr}   // Sneaky, pop the stack off the stack :)
bx   lr


It'll need to be more complicated if you need to pass arguments to your function, but that will allocate any locals in the function from the stack pointer you pass in, which could be in EWRAM.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#46449 - merodia - Sat Jun 25, 2005 1:08 am

Thanx alot for the replies, I think this is a really important topic. When having to implement algorithms for intelligence in games.

Thank you very much.