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.

C/C++ > Calling the bios in pure c

#6247 - Maverick - Tue May 20, 2003 10:23 pm

It is described everywhere how to run a bios function using ASM

This is easy but i am wondering if anyone knows how to or wether it is possible to do it in pure c.

I know you can call the function using thumb etc.

I am not brilliant at ASM so can not really "translate" a function from any ASM examples.

Sorry if this is an inconvenience to anyone and many thanks for the help!


Maverick

#6250 - Daedro - Tue May 20, 2003 11:54 pm

I also am sorry for any inconvenience to anyone as I would be asking this question soon too.

Daedro

#6257 - sgeos - Wed May 21, 2003 3:41 am

Maverick wrote:
It is described everywhere how to run a bios function using ASM

This is easy but i am wondering if anyone knows how to or wether it is possible to do it in pure c.


See my recent rotation/scaling demo for an example of using a bios SWI in C code.

Maverick wrote:
I know you can call the function using thumb etc.


Use "SWI 0xnn" in thumb and "SWI 0xnn0000" in ARM, where nn is SWI number in hex. I've only tested the ARM version.

-Brendan

#6270 - sgeos - Wed May 21, 2003 7:50 am

There might be a better way of doing this, but here is an example with a return value:

Code:
unsigned short swi_atan(signed short x, signed short y)
{
  unsigned long result;

  asm volatile
  (
    "mov r0,%1\nmov r1,%2\nswi 0x0A0000\nmov %0,r0\n" :
    "=r" (result) :  /* result is set to %0, the return value */
    "r" (x), "r" (y) :  /* %1 set to x, %2 to y */
    "r0", "r1" /* registers destroyed */
  );
  return result;
}


Here is the ASM in an easier to read format:

Code:
mov r0,%1
mov r1,%2
swi 0x0A0000
mov %0,r0


The compiler will pick registers for %0, %1, and %2. We are using r0 and r1 explicitly, so we need to tell the compiler about that, so we tell the compiler that we are destroying the value of those registers. I'm not going to go into any more detail now, but I'll do my best to answer any questions people might have.

This stuff is documented at:
http://www.gnu.org/software/gcc/onlinedocs/
See extensions to the C language, "Assembler Instructions with C Expression Operands " for your version of gcc.

-Brendan

#6275 - Maverick - Wed May 21, 2003 10:37 am

Im sorry, i may have worded the question wrong

I already know how to do this. It is easy

But is it possible to do it in PURE C as in NO ASM.

Many thanks for all answers so far!



Maverick

#6290 - sgeos - Wed May 21, 2003 6:53 pm

Maverick wrote:
Im sorry, i may have worded the question wrong

I already know how to do this. It is easy

But is it possible to do it in PURE C as in NO ASM.


It is not possible without making the compiler aware of these 'optimizations'. Somebody would have to add some gba specific information for the compiler to take into accout, and you would not be able to access the bios functions directly. The bios functions are pretty much an ASM thing as far as I can tell.

-Brendan

#6385 - Jason Wilkins - Fri May 23, 2003 10:23 pm

The simple answer is no, you cannot use standard ANSI C to call the bios.

You must use assembly language. For a novice, it would be good to find a library which implements the bios calls as functions which can be called from C.

Does anyone know of such a simple library?
_________________
http://devkitadv.sourceforge.net

#6387 - darkcloud - Fri May 23, 2003 10:41 pm

If you search around, you can find a few functions but I haven't seen any type of library.

Someone should make one ;).
_________________
Maybe in order to understand mankind, we have to look at the word itself: "Mankind". Basically, it's made up of two separate words - "mank" and "ind". What do these words mean ? It's a mystery, and that's why so is mankind.

#6388 - sgeos - Fri May 23, 2003 10:56 pm

darkcloud wrote:
Someone should make one ;).


Did you just volunteer? Otherwise I could make one. I think the bios functions are fun =)

I don't have any application for many of them, which could be a problem.

-Brendan

#6389 - darkcloud - Fri May 23, 2003 11:01 pm

sgeos wrote:
darkcloud wrote:
Someone should make one ;).


Did you just volunteer?


No no lol. I'm not very good with ASM. Although it would probably be a good learning experience.
_________________
Maybe in order to understand mankind, we have to look at the word itself: "Mankind". Basically, it's made up of two separate words - "mank" and "ind". What do these words mean ? It's a mystery, and that's why so is mankind.

#6392 - sgeos - Sat May 24, 2003 12:35 am

darkcloud wrote:
No no lol. I'm not very good with ASM. Although it would probably be a good learning experience.


I'm not convinced that I'm very good at ASM. I'm also unconvinced that setting up C wrapper functions for the SWI calls is very difficult. Well... I'll give it a go. Worst case scenario I'll only finish part of the library.

-Brendan

#6399 - Maverick - Sat May 24, 2003 10:16 am

Thats ok, thanks for the help, i already know the bios in asm it is very simple even a novice could do it in their sleep.

It was meerely something i was wondering.

Thanks


Maverick

#6844 - joris-fr - Tue Jun 03, 2003 12:25 pm

What does SWI stand for ?
SoftWare Interrupt ?
_________________
Joris

#6849 - MojoJojo - Tue Jun 03, 2003 1:22 pm

Yes. Or it did on the BBC, but I haven't actually really looked at much assembler since then, but I would be surprised if its changed.

#6945 - Darkain - Wed Jun 04, 2003 10:43 pm

i dunno... but wouldnt it help if the C/C++ wrapper functions to the bios calls be inlined functions? this way to help w/ speed a little bit. when looking through all of the tutorials ive seen, i see very little, if not none at all that use inline for small simple functions like these.
_________________
-=- Darkain Dragoon -=-
http://www.darkain.com
DarkStar for Nintendo DS