Author Topic: Include and call assembly within HuC  (Read 746 times)

megatron-uk

  • Full Member
  • ***
  • Posts: 219
Include and call assembly within HuC
« on: January 02, 2014, 03:06:25 AM »
Investigating a bit further now, does anyone have any examples of how to define/include assembly code within HuC and any calling conventions that are needed?

I see lots of demos/examples for pure C code for huc and the same for hu6280 assembly in pceas, but not much where the two are used together. Can someone point me in the direction of any code or demos that do this?

nodtveidt

  • Guest
Re: Include and call assembly within HuC
« Reply #1 on: January 02, 2014, 03:11:15 AM »
Code: [Select]
#asm
;assembly code goes here
#endasm
Simple as that. :)

megatron-uk

  • Full Member
  • ***
  • Posts: 219
Re: Include and call assembly within HuC
« Reply #2 on: January 02, 2014, 03:22:22 AM »
 ](*,)

Doh. Simple as that eh?

Cheers!

megatron-uk

  • Full Member
  • ***
  • Posts: 219
Re: Include and call assembly within HuC
« Reply #3 on: January 02, 2014, 03:47:16 AM »
Ah, found the C/ASM integration comments in docs/huc/huc_doc.htm - I've been using http://www.archaicpixels.com/ for the HuC/pceas reference so didn't spot it straight away in my local docs.

nodtveidt

  • Guest
Re: Include and call assembly within HuC
« Reply #4 on: January 02, 2014, 04:16:00 AM »
Archaic Pixels is a pretty good resource.

Bonknuts

  • Hero Member
  • *****
  • Posts: 3292
Re: Include and call assembly within HuC
« Reply #5 on: January 02, 2014, 04:57:43 AM »
There's support in HuC for Pragma Fastcall, but no ever mentions or documents it. I've used it extensively. As you've probably already seen, the library is actually in all assembly. You can make your own assembly functions. Normally, when you make a C function in HuC, the compiler uses a software stack to pass arguments (and no, it doesn't modify the real stack - too small). Vars defined inside that function, also use a software stack (IIRC, it's been forever). You can get around it by using Global variables. But a much better method, once you get your code prototyped in C, is make an assembly version of it.

 Fastcall allows you to bypass the internal stack and use either A/X/Y and dedicated Zeropage bytes, for passing arguments (as if they're PROCs in magickit, but they actually don't use the proc directive). You can even do argument overloading with fastcall; one function - multiple number of arguments. You can also define the size of ~each~ argument (byte, word, 24bit, 32bit). And lastly, fastcall also allows direct code placement - sort of. You can define a fastcall function that has no code. I.e. instead of passing the arguments to Acc/X/Y or zeropage, it passed the arguments directly to a port. I did this for arcade card reg writes.

 You have to prototype your fastcall argument in C code, and then the actual code goes (usually) in the first library bank (because this bank is fixed, and is 'near' code). If you run out of room in the first Lib bank, remove some stuff, or put a 'far' code call in there (which is done for code in the second Lib bank).

 As Old Rov said, simply just placing #asm/#endasm directives anyway will give you quick access to assembly code. Not that global variables defined in HuC can be accessed with the same name, just add an under score in front of it. Accessing internal function defines vars with #ASM, is more complicated.

 Beware of pointer or array access in HuC. It's all treated as 'far' memory access, even static mapped ram, and code generated to access it is huge and slooow. Logic shifts are also pretty slow as well in HuC (go figure).

 Anyway, that's my 2 cents worth :P

nodtveidt

  • Guest
Re: Include and call assembly within HuC
« Reply #6 on: January 02, 2014, 05:42:47 AM »
Aye... array performance is very bad. To get around it for code that would otherwise greatly benefit from it, I use single variables and end up duplicating a lot of code, or use "variable copy" methods. Code size suffers for it, but it's way faster than using straightforward code that utilizes arrays. Out of all the things that need to be addressed in HuC, array access is one of the most important.

Bonknuts

  • Hero Member
  • *****
  • Posts: 3292
Re: Include and call assembly within HuC
« Reply #7 on: January 02, 2014, 07:04:31 AM »
How did you guys handle array/point access?

 I did two things. One, was just to write the array/pointer access with #asm/#endasm. The other was to create pragma fastcall functions that would return the byte or word in Acc or Acc:X.

 This:
var=array[index];

into:

var =get_array(array,index);

 etc. They were short/small functions that were fast. Since the top layer was Small C, you could easily do var=get_array(array,index++); or such. I wrote a bunch of little ones, depending on what I needed it to do. At some point, I was actually working on macro support - so the fastcall functions could be inlined. Though I think the inlined part, the way it was setup, had to be added to the source code of HuC. I added some support to HuC (the internal code itself, not the asm lib), but I never released it. You HuC guys seemed to not need it, and I didn't really need to use HuC for my own stuffs (though the idea of prototyping PCE code in C first, is appealing to me).

Arkhan

  • Hero Member
  • *****
  • Posts: 14142
  • Fuck Elmer.
    • Incessant Negativity Software
Re: Include and call assembly within HuC
« Reply #8 on: January 02, 2014, 07:48:58 AM »
Insanity had a get/set that was just a c function that called inline assembly.
[Fri 19:34]<nectarsis> been wanting to try that one for awhile now Ope
[Fri 19:33]<Opethian> l;ol huge dong

I'm a max level Forum Warrior.  I'm immortal.
If you're not ready to defend your claims, don't post em.

Bonknuts

  • Hero Member
  • *****
  • Posts: 3292
Re: Include and call assembly within HuC
« Reply #9 on: January 02, 2014, 01:24:38 PM »
How do you guys handle that nowadays? Just lots of asm support?

Arkhan

  • Hero Member
  • *****
  • Posts: 14142
  • Fuck Elmer.
    • Incessant Negativity Software
Re: Include and call assembly within HuC
« Reply #10 on: January 02, 2014, 02:51:36 PM »
How do you guys handle that nowadays? Just lots of asm support?

Same way basically.   It works and isn't that invasive.   I think OldMan has some other way to do it now.

[Fri 19:34]<nectarsis> been wanting to try that one for awhile now Ope
[Fri 19:33]<Opethian> l;ol huge dong

I'm a max level Forum Warrior.  I'm immortal.
If you're not ready to defend your claims, don't post em.

touko

  • Hero Member
  • *****
  • Posts: 953
Re: Include and call assembly within HuC
« Reply #11 on: January 03, 2014, 12:09:03 AM »
me apart , someone uses Huc with massive inline asm ???

Arkhan

  • Hero Member
  • *****
  • Posts: 14142
  • Fuck Elmer.
    • Incessant Negativity Software
Re: Include and call assembly within HuC
« Reply #12 on: January 03, 2014, 09:32:25 AM »
me apart , someone uses Huc with massive inline asm ???

I put more and more inline assembly where it's needed.  Basically anywhere that does heavy array access is a good starting point for fixing.

It's still easier to write a complete game in C than it will ever be in assembly.   It always has been and always will be.
[Fri 19:34]<nectarsis> been wanting to try that one for awhile now Ope
[Fri 19:33]<Opethian> l;ol huge dong

I'm a max level Forum Warrior.  I'm immortal.
If you're not ready to defend your claims, don't post em.

touko

  • Hero Member
  • *****
  • Posts: 953
Re: Include and call assembly within HuC
« Reply #13 on: January 03, 2014, 10:10:49 AM »
I have started like that, but now is more easy and faster to write directly in ASM .
The code size is also lighter in ASM, more lighter ;-) ..

And like you said, huc is the only compiler where 9+1=12 lol ..

nodtveidt

  • Guest
Re: Include and call assembly within HuC
« Reply #14 on: January 03, 2014, 11:03:33 AM »
I still don't get the 9+1=12 thing. :lol: