Page 1 of 1
Adding functions from third party library to iwram
Posted: Fri Dec 29, 2023 3:00 pm
by thepursuer
I've been fooling around seeing what I can get to compile from cmake's fetch content for the gba when I got an idea after trying
this. It's a fixed precision math library and it compiles perfectly without modification which was very satisfying. Since it's math related I thought it would be a good idea to put it into iwram. However I realized I have no idea how to mark symbols from linked libraries to get put into the iwram section. Without adding IWRAM_DATA/IWRAM_CODE to the source of the library, is it possible to move some of the symbols into iwram at the time of linkage?
Re: Adding functions from third party library to iwram
Posted: Sun Dec 31, 2023 4:50 pm
by WinterMute
This isn't something we have any particular experience with. It may be possible using a custom linkscript but there are a number of caveats with this kind of process.
IWRAM on the GBA is only 32K and, with stock linkscripts anyway, this contains stack and uninitialised variables - space is very much at a premium here.
Code that runs from EWRAM and ROM is generally compiled as thumb - there's an access penalty for arm instructions in these areas since they only have a 16bit bus.
IWRAM has a full 32bit bus so arm instructions in here will run as fast as possible. Thumb will run a bit faster but I'm not sure it's worth the trouble.
Ultimately it would be best to pull functions out & compile them as arm rather than attempting to avoid modifying the original library.
Re: Adding functions from third party library to iwram
Posted: Fri Jan 12, 2024 9:31 am
by thepursuer
Thanks I think this may be a little beyond me at the moment after all. I ended up just writing a git patch for the library and added the sections attribute to the ones I wanted in iwram.
Is code marked with the EWRAM_CODE define automatically compiled as thumb by the toolchain? Also IWRAM_CODE and IWRAM_DATA can't compile together in the same translation unit. Am I supposed to just keep such code separate?
Sorry this is unfamiliar for me.
Re: Adding functions from third party library to iwram
Posted: Mon Jan 15, 2024 12:32 am
by WinterMute
Apologies. This is an issue that's appeared with newer gcc releases and we had a PR sitting there to address it that we never got round to merging. IWRAM_CODE and IWRAM_DATA should work fine together with latest libgba.
The compiler & the templates default to compiling code as thumb unless you tell it otherwise. The devkitPro compilation rules will compile source files named as *.iwram.c/cpp as arm and place resulting code in iwram. You can also use ARM_CODE to mark individual functions to be compiled as arm.
Re: Adding functions from third party library to iwram
Posted: Mon Jan 15, 2024 3:17 pm
by thepursuer
That's super helpful! Thank you so much!