This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Using asm functions in C that don't start with an underscore

Using asm functions in C that don't start with an underscore
by on (#157916)
What can I do when I use a library (like FamiTone) and I want to call the functions from within C in CC65?
The function names don't start with an underscore, so unless I modify the source code myself, I don't see a way to use the FamiTone functions in C.

I created a wrapper file that does the following since the FamiTone library doesn't include the export commands:
Code:
.segment "CODE"

   .include "famitone2.s"
   .export FamiToneInit
   .export FamiToneUpdate
   .export FamiToneMusicPlay
   .export FamiToneMusicStop

Now, is there a way to export the functions with an alias?
After all, this version here is not the best solution:
Code:
_FamiToneMusicPlay:
   JSR FamiToneMusicPlay
   RTS

_FamiToneMusicStop:
   JSR FamiToneMusicStop
   RTS

And I don't really want to use inline assembly in my code for a mere function call if I can avoid it.
Re: Using asm functions in C that don't start with an unders
by on (#157917)
Something to try:
Code:
.segment "CODE"

   .include "famitone2.s"
   _FamiToneInit=FamiToneInit
   _FamiToneUpdate=FamiToneUpdate
   _FamiToneMusicPlay=FamiToneMusicPlay
   _FamiToneMusicStop=FamiToneMusicStop
   .export _FamiToneInit
   .export _FamiToneUpdate
   .export _FamiToneMusicPlay
   .export _FamiToneMusicStop
Re: Using asm functions in C that don't start with an unders
by on (#157918)
Thanks. Yeah, that works.
Re: Using asm functions in C that don't start with an unders
by on (#157919)
tepples wrote:
Something to try:

This can be simplified down to:
Code:
.segment "CODE"

   .include "famitone2.s"
   .export _FamiToneInit=FamiToneInit
   .export _FamiToneUpdate=FamiToneUpdate
   .export _FamiToneMusicPlay=FamiToneMusicPlay
   .export _FamiToneMusicStop=FamiToneMusicStop

(You could also use ":=" since it's a label, but there's no technical difference.)

And of course, make sure that the calling conventions match.
Re: Using asm functions in C that don't start with an unders
by on (#157920)
Yup, that's even more compact.
Re: Using asm functions in C that don't start with an unders
by on (#157922)
If the function has been defined elsewhere, you should be able to throw this in a header:

Code:
#ifndef FAMITONE_INTERFACE_H
#define FAMITONE_INTERFACE_H

extern void FamiToneInit(uint8_t arg);
// So on and so forth - modify arguments and return types appropriately based on what stack jazz the functions are doing

#endif


Include a header like that, and since famitone2.s has defined the functions this should allow them to be used. This is how I've integrated a few assembly functions into my Genesis project and I didn't need the .export directive. Perhaps that's a better way, though, so I ought to look into that.
Re: Using asm functions in C that don't start with an unders
by on (#157937)
I know. But the problem is that CC65 transforms every function and variable name into the same name, but with an underscore. Therefore, an assembly function has to have an undersore to be usable with C.

Also, the .export command is necessary so that the function is usable in other translation units. I don't know how you can use an Assembly function in C (or in another assembly translation unit) without having the original code file use .export.
Re: Using asm functions in C that don't start with an unders
by on (#157942)
To put it another way: A label that is not exported is treated as if it were static.
Re: Using asm functions in C that don't start with an unders
by on (#157945)
tepples wrote:
To put it another way: A label that is not exported is treated as if it were static.

I realize I've forgotten the ever-crucial reason mine worked; rather than .export, I was using .global.

Source (for Kosinki decompression algorithm): https://github.com/Mikejmoffitt/LICS/bl ... stem/kos.s

And on line 25 of this source file you can see the function being externally declared: https://github.com/Mikejmoffitt/LICS/bl ... /cdaudio.c