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 > gba software interrupts

#166329 - moonlightcheese - Wed Feb 04, 2009 8:47 pm

i'm attempting to build an elf loader and i think i've got the file read and placed in memory properly and i need to send an interrupt to begin execution at the pointer to the memory segments i populated with the loader. are there any particular examples or tutorials regarding software interrupts on the gba? i'm not even sure where to start and any advice or reference would be helpful. interrupts are supposed to be the most difficult part of OS coding and i can definitely see the truth in that... :D

#166331 - Dwedit - Wed Feb 04, 2009 9:12 pm

I don't think you need any form of software interrupt to load an ELF file, rather you would just jump to the entry point with the proper lr value in case it decides to return.
But you may also need to ensure that the ctr0 file used by the program you wish to run indeed supports returning.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#166334 - moonlightcheese - Wed Feb 04, 2009 9:41 pm

i'm not worried about returning really. i just want to make the process image run. everything is coded in C and i have no clue how to just branch to an address in C. i'm told that using inline assembly is a no-no for switching context due to some irregularities the compiler causes in cpu registers.

#166335 - Maxxie - Wed Feb 04, 2009 9:50 pm

moonlightcheese wrote:
i just want to make the process image run. everything is coded in C and i have no clue how to just branch to an address in C.


Declare a procedure pointer type, and create an instance like you would do with any other pointer. You can then just call it as you are used to with functions available at compiletime.

Code:

typedef int (* mainfunction_type)(int argc, char **argv) ;

mainfunction_type *mainfn = (mainfunction_type *)address ;
int retval = mainfn(argc,argv) ;

_________________
Trying to bring more detail into understanding the wireless hardware

#166336 - Dwedit - Wed Feb 04, 2009 9:56 pm

The easiest way to call an arbitrary address in pure C is to make a function pointer, then call it.

declare a function pointer:
void (*mypointer)();
assign a function pointer:
*(u32*)mypointer=0x02000000;
call a function pointer:
mypointer();

To do a context switch on a GBA, just save and restore the complete set of registers, r0-r12, sp, lr, maybe pc, as well as the processor status register, and also which CPU mode you were in before. You need a separate stack for each 'thread'.
You can also use the standard C library functions setjmp and longjmp.

If you are writing a GBA operating system which loads ELF files, and plan on ever having the OS interrupt the program, you may want to ensure that the client program does not clear all memory and reset the stack address. Usually those tasks are done in the crt0 code which executes before main(). But if it's just an elf loader that boots once then lets the program have free reign, that's not necessary.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#166337 - samel - Wed Feb 04, 2009 10:35 pm

May be these link can help you

//gba thread by DarkPhantom

http://www.gbadev.org/index.php?showinfo=986

//ds thread modified from DarkPhantom by me

http://donotjava.netsons.org/?page_id=67

ThreaDS work fine with DevkitPro r21 [if i remember right 8p] but should have no [BIG] problem with newer version.

If you have any question just ask.

#166339 - moonlightcheese - Thu Feb 05, 2009 12:24 am

ok i got it to jump properly but the executable won't load. time for some debugging. thanks for the help :D

#166340 - kusma - Thu Feb 05, 2009 2:03 am

Perhaps the image needs a bit of relocating? I'd reccomend looking at it all in a debugger.

#166345 - moonlightcheese - Thu Feb 05, 2009 3:28 pm

kusma wrote:
Perhaps the image needs a bit of relocating? I'd reccomend looking at it all in a debugger.

i read in a post on osdev.org that you have to use section headers rather than program headers on systems that do not have an MMU. i'm trying to work through an example but it's got virtual memory implemented and i'm just building a loader for purposes of example with physical addresses. i'm gonna try to figure out how to use the section headers.