Page 1 of 1

[C] Passing parameters by registers

Posted: Fri Jul 17, 2015 8:56 am
by tryphon
Hello,

my question is not really SGDK related, but I've read some sources of SGDK and saw some functions implemented in asm.

But it seems that the parameters of the functions are sent using the stack.

Is it possible to make the compiler send parameters using registers, as it's more often done when programming in asm ?

Re: [C] Passing parameters by registers

Posted: Fri Jul 17, 2015 11:51 am
by Stef
tryphon wrote:Hello,

my question is not really SGDK related, but I've read some sources of SGDK and saw some functions implemented in asm.

But it seems that the parameters of the functions are sent using the stack.

Is it possible to make the compiler send parameters using registers, as it's more often done when programming in asm ?
Actually the GCC version provided in SGDK does not allow it even by forcing maximum optimization level or using the fastcall keyword.
I tried to force it by using some asm pragma but it was not really easy to implement your function then.
The problem with fastcall is that is not a standardized calling convention method and maybe for 68000 it doesn't even exist (which explain why it doesn't make any difference). Honestly except for function you use more than one hundred of time *per frame* it is not a big issue of passing parameters through the stack. When you have many parameters (more than 4/5 parameters) it may be better to pass a pointer to a structure owing all the parameters though.

Posted: Sat Jul 18, 2015 6:39 pm
by tryphon
OK, not a big deal for the moment.
Thanks :)

Posted: Sun Jul 19, 2015 5:53 pm
by Count SymphoniC
Wait. So it couldn't be done by including pre assembled .asm code? Would I have trouble mixing asm and C using SGDK?

Posted: Sun Jul 19, 2015 8:50 pm
by Stef
Count SymphoniC wrote:Wait. So it couldn't be done by including pre assembled .asm code? Would I have trouble mixing asm and C using SGDK?
Of course you can do it by using assembly code, it's just that you cannot force C code to use registers when calling functions.

Posted: Mon Jul 20, 2015 4:04 am
by Count SymphoniC
Stef wrote:
Count SymphoniC wrote:Wait. So it couldn't be done by including pre assembled .asm code? Would I have trouble mixing asm and C using SGDK?
Of course you can do it by using assembly code, it's just that you cannot force C code to use registers when calling functions.
Hey Stef :D

Interesting... well it's not something I've tried yet, but plan on doing so very soon. So if I was linking asm with my C code and I was calling an asm subroutine from within C, would I want to make sure that I wiped my data and address registers before returning to the C portion or does it really even matter? Btw, did you ever solve the .xgm pausing instruments issue?

Posted: Mon Jul 20, 2015 9:21 am
by Stef
Count SymphoniC wrote: Hey Stef :D

Interesting... well it's not something I've tried yet, but plan on doing so very soon. So if I was linking asm with my C code and I was calling an asm subroutine from within C, would I want to make sure that I wiped my data and address registers before returning to the C portion or does it really even matter?
When you call asm subroutine from C, you have to make sure the subroutine you call fetch parameters from stack.
What you can do is to have sort of 2 methods enter point depending you call it from C or asm :

Code: Select all

myMethodC:
    move.l (sp)+, d0-d1/a0-a1

myMethodASM:
    ...
Do when you call 'myMethodC' you take care of loading registers with the parameters as myMethodASM expect it.
Btw, did you ever solve the .xgm pausing instruments issue?
I don't specifically remember this issue, do you have a music example producing it ? I made some fixes on the XGM driver (and still i am) so hopefully i got this one fixed =)

Posted: Tue Jul 21, 2015 5:43 am
by Count SymphoniC
No I remember you asking how to get the song to stop properly when pausing. I wasn't sure if you ever got that figured out but it was last year so I don't blame you for not remembering.

Posted: Tue Jul 21, 2015 8:16 am
by Stef
Oh yeah this one, it's fixed since a long time now ;)
Actually in some very few case, you may still heard a little buzzing note staying but setting min D1L / max RR + key OFF is enough to stop a channel.