Adding functions from third party library to iwram

Post Reply
thepursuer
Posts: 10
Joined: Sat Mar 11, 2023 5:53 am

Adding functions from third party library to iwram

Post by thepursuer » Fri Dec 29, 2023 3:00 pm

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?

WinterMute
Site Admin
Posts: 1859
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Adding functions from third party library to iwram

Post by WinterMute » Sun Dec 31, 2023 4:50 pm

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.
Help keep devkitPro toolchains free, Donate today

Personal Blog

thepursuer
Posts: 10
Joined: Sat Mar 11, 2023 5:53 am

Re: Adding functions from third party library to iwram

Post by thepursuer » Fri Jan 12, 2024 9:31 am

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.

WinterMute
Site Admin
Posts: 1859
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Adding functions from third party library to iwram

Post by WinterMute » Mon Jan 15, 2024 12:32 am

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.
Help keep devkitPro toolchains free, Donate today

Personal Blog

thepursuer
Posts: 10
Joined: Sat Mar 11, 2023 5:53 am

Re: Adding functions from third party library to iwram

Post by thepursuer » Mon Jan 15, 2024 3:17 pm

That's super helpful! Thank you so much!

Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests