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 > How to put C code in iwram?

#21253 - isildur - Wed May 26, 2004 2:26 pm

I know how to do it in assembly by using the .section .iwram directive but how do I put a C function in iwram?

#21254 - isildur - Wed May 26, 2004 2:50 pm

never mind, I figured it out :)

#21256 - isildur - Wed May 26, 2004 3:08 pm

I have another similar question... How can I specify that a certain C function be compiled in thumb?

#21259 - Lord Graga - Wed May 26, 2004 3:16 pm

Use the edit button, you looney! ;)

I would tell you how to do it if I knew :P

#21260 - isildur - Wed May 26, 2004 3:18 pm

Your reply is as silly as my not using the edit button :) Thanks anyways ;)

#21262 - NoMis - Wed May 26, 2004 3:31 pm

As far as i know its only possible to use different compile options at file basis. So if you want a specific function compiled in thumb and other functions in arm than put the function definitions in different files.

NoMis

#21263 - isildur - Wed May 26, 2004 3:39 pm

Ok, I will do it for a whole file. But when it is a C file, is there a directive I can put in it to make it compile as thumb?

#21264 - Lord Graga - Wed May 26, 2004 3:42 pm

isildur wrote:
Ok, I will do it for a whole file. But when it is a C file, is there a directive I can put in it to make it compile as thumb?
I think there's a function like --compile-thumb, or something. Look it up!

#21266 - poslundc - Wed May 26, 2004 3:59 pm

gcc -mthumb

Dan.

#21268 - isildur - Wed May 26, 2004 4:06 pm

poslundc wrote:
gcc -mthumb

Dan.


Yes, I know this one but is there a directive I can put in my C file to have it compiled automatically in thumb?

#21281 - wintermute - Wed May 26, 2004 5:31 pm

isildur wrote:
poslundc wrote:
gcc -mthumb

Dan.


Yes, I know this one but is there a directive I can put in my C file to have it compiled automatically in thumb?



Not that I'm aware of. and I have been looking around for something similar.

The most straigtforward way I've found is to use makefile rules


Code:

THUMB   :=   -mthumb -mthumb-interwork
ARM   :=   -mthumb-interwork

#---------------------------------------------------------------------------------
%_arm.o : %_arm.c
   @echo $(notdir $<)
   $(CC) -MMD $(ARM) $(CFLAGS) -o $@ -c $<

#---------------------------------------------------------------------------------
%.o : %.c
   @echo $(notdir $<)
   $(CC) -MMD $(THUMB) $(CFLAGS) -o $@ -c $<

#21283 - isildur - Wed May 26, 2004 5:43 pm

wintermute, thanks, I will go that way then...

#22940 - R0d Longfella - Thu Jul 01, 2004 5:03 pm

Perhaps you could make use of grep in your make file, and have it look for a useless define or comment. (such as #define THUMB or // use Thumb!) If either of the two are present you can switch your compiler statement.

#22967 - col - Thu Jul 01, 2004 10:36 pm

I use a similar approach to Wintermute. All the files to be compiled as arm have .arm.c or .arm.cpp

My makefile sorts the rest out.

FWIW, I posted my makefile in full a while ago:
http://forum.gbadev.org/viewtopic.php?t=2004&start=12

This version there is a bit out of date, but contains most of the goodness. The nice thing is that you can seperate all your source files into different folders.
You just tell the makefile where the folders are and it does the rest.
The only time you need to edit the makefile is if you add a new directory, or want to tweak the compiler switches. You don't need to edit it when you add new source files - the makefile scans the directories and sorts out all the source files and headers.
It handles all the dependency issues as well - only recompiles what is needed.
It compiles to arm and thumb using .thumb.c, .thumb.cpp, .arm.c, .arm.cpp - thumb is default, so you only need to to the .arm. bits
anyhow, give it a whirl :)

cheers

Col

Edit - i forgot to add, this makefile depends on gnu make specific features, so you will need to use gnu make.

#22993 - batblaster - Fri Jul 02, 2004 9:04 am

Hi ,

I'm coding some different thiongs and sometimes i'm putting my function in IWRAM , to do i've taked some help from HAM doing this:

Code:


void function(void) MEM_FUNC_IN_IWRAM;



MEM_FUNC_IN_IWRAM void function(void)
{
...
}



This work perfectly, tested many many times on my small 3d routines and tested with No$GBA too...

To know how the macro work you need to watch the HAM dev kit...
_________________
Batblaster / 7 Raven Studios Co. Ltd
------------------------------------------

#24000 - Yodajr - Mon Jul 26, 2004 8:09 am

batblaster wrote:
This work perfectly


batblaster, your projet is under the 256ko ?
Because if my rom overtake the multiboot limit, when I use this macro, I have this error :
Code:
relocation truncated to fit: R_ARM_PC24


Are you do something else ?

#24034 - batblaster - Mon Jul 26, 2004 9:19 pm

Nope , my code in 17.5 megabits and works perfect , i dont know what is the difference if you use the macro on HAM or on devkitadv...

My demo is out and many of my routine are in IWRAM, if you wan to see the demo take from here...


http://www.ngine.de/roms/RasterMania.rar

I hope you like and i hope GBADEV put online very very soon...

On my demo all raster and 3d routines are in IWRAM using the macro...

CU...
_________________
Batblaster / 7 Raven Studios Co. Ltd
------------------------------------------

#24057 - Yodajr - Tue Jul 27, 2004 4:03 am

If I like it ? I love it ! :D
Congratulations, man, your demo is impressive !

I don't understand why I have this error... I use HAM and VHAM :-/

How are you initialise the variables wich used in the routines in IWRAM ?

u8 pos_x MEM_IN_IWRAM; ?

Are you using any "include" ?

Thanks ;)

#24062 - DekuTree64 - Tue Jul 27, 2004 8:11 am

Hey there Batblaster, great demo! Glad to see that you're still at it. Demo coders are rare these days, seems like most people here are just working on games now. Not that that's a bad thing, the experience gained from making a game is surely more valuable than that from making a demo, but heck, demos are more fun, and are much more feasable to finish.
My new spare-time project is a standard tunnel effect, casting one ray per 8x8 pixels, and interpolating the texture coordinates between them. Not too interesting, but since thats the classic method, and GBA uses tiles just the right size, I couldn't resist.

Anyway, so as not to be entirely off the original topic, Yodajr, your problem is that you're not using long calls. EWRAM and IWRAM are close enough together that you can do regular branches between them, but when you move the EWRAM code into ROM, then it's too far away so you have to tell the compiler to deal with it properly. I always used my own long-call function to do it, because I wasn't using the standard libraries, which the compiler expects to have when using automatic long calls.
Usually there are two defines, MEM_IN_IWRAM and CODE_IN_IWRAM, or something like that. The mem version just specifies to put it in IWRAM, the code version also specifies to use a long call as well, so if you have both versions, just switch it to the code one, if not, I'll go dig up a reference on exactly how to set the attributes for it.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#24089 - dagamer34 - Tue Jul 27, 2004 8:12 pm

Code:

#define CODE_IN_IWRAM    __attribute__ ((section (".iwram"), long_call))


No need to look it up Deku, i did it for you. ;)
_________________
Little kids and Playstation 2's don't mix. :(

#24254 - batblaster - Fri Jul 30, 2004 6:00 pm

Deku, you are my asm Teacher heheheh Many many thanks you got a time to watch my demo... My 3D routine for the endpart are not so well like yours but Krawall take so much time...

I'm working now on some new things , i don't know if i will relase but i like hehehe...

Thanks to all people who found time to watch my demo... thanks a lot...
_________________
Batblaster / 7 Raven Studios Co. Ltd
------------------------------------------