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

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
User avatar
DRW
Posts: 2225
Joined: Sat Sep 07, 2013 2:59 pm

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

Post by DRW »

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: Select all

.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: Select all

_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.
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Using asm functions in C that don't start with an unders

Post by tepples »

Something to try:

Code: Select all

.segment "CODE"

   .include "famitone2.s"
   _FamiToneInit=FamiToneInit
   _FamiToneUpdate=FamiToneUpdate
   _FamiToneMusicPlay=FamiToneMusicPlay
   _FamiToneMusicStop=FamiToneMusicStop
   .export _FamiToneInit
   .export _FamiToneUpdate
   .export _FamiToneMusicPlay
   .export _FamiToneMusicStop
User avatar
DRW
Posts: 2225
Joined: Sat Sep 07, 2013 2:59 pm

Re: Using asm functions in C that don't start with an unders

Post by DRW »

Thanks. Yeah, that works.
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: Using asm functions in C that don't start with an unders

Post by thefox »

tepples wrote:Something to try:
This can be simplified down to:

Code: Select all

.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.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
DRW
Posts: 2225
Joined: Sat Sep 07, 2013 2:59 pm

Re: Using asm functions in C that don't start with an unders

Post by DRW »

Yup, that's even more compact.
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
User avatar
mikejmoffitt
Posts: 1353
Joined: Sun May 27, 2012 8:43 pm

Re: Using asm functions in C that don't start with an unders

Post by mikejmoffitt »

If the function has been defined elsewhere, you should be able to throw this in a header:

Code: Select all

#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.
User avatar
DRW
Posts: 2225
Joined: Sat Sep 07, 2013 2:59 pm

Re: Using asm functions in C that don't start with an unders

Post by DRW »

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.
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Using asm functions in C that don't start with an unders

Post by tepples »

To put it another way: A label that is not exported is treated as if it were static.
User avatar
mikejmoffitt
Posts: 1353
Joined: Sun May 27, 2012 8:43 pm

Re: Using asm functions in C that don't start with an unders

Post by mikejmoffitt »

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
Post Reply