I added computed goto support to cc65 for a project. It's a GCC extension that lets you use fast jump tables from C, inside a function.
https://cc65.github.io/doc/cc65.html#s5
That could be interesting. For now I'm not sure how I could use it yet so what would be a good use case for a jump table in C? For now I'm not using that paradigm in asm either but I think the famitracker driver uses it to call the proper method to parse the current effect.
The more I see examples and ideas, the more I can see how I could use it in my own code.
A canonical example is a "drawEnemy(u8 enemytype)" function.
Nice! I like how on CMOS 6502s it uses the jmp (abs,x) instruction.
(for anyone else curious,
here's the diff that adds support)
@calima
I see. I guess I'm not used to think in tables yet ^^;;; I'm designing the entities and instead of doing it with tables, my first idea is the spawner would have a reference to the method that knows how to handle that specific entity type. Maybe that too complicated for nes and requires too much data processing and should go with something more simple. Nothing coded yet, I'm still throwing ideas at the wall.
edit:
I'm reading the comments on the diff on github and it says that it improve performance compared to switch/if statement. I have a case were I use a lot of switch/if to check which animation to do based on movement. Maybe it can be improved later as a table of goto instead? This is the kind of optimization that would help my current code and wouldn't have thought about using tables. Still, unless I profile it, I cannot say for sure how much faster it would be (not even sure how to profile it on the nes anyway ^^;;)
edit2:
What would be the performance difference between a jump table containing labels (I didn't realize at first, I was sure it was a list of function) and a pointer to function to go to specific code? I never use label in C, only in ASM, so for now that would mean to code with a different paradigm where in the same function all entities would be processed somehow.
The project I made this for is for SNES, hence the jmp (abs,x). It just happens to be supported by earlier cpus as well
Quote:
What would be the performance difference between a jump table containing labels (I didn't realize at first, I was sure it was a list of function) and a pointer to function to go to specific code?
cc65 calls indirect functions kind of slow. Then you have to consider if the functions take parameters or not. Unlike with switch/if-cascade where the difference can be huge, a function jump table with functions not taking parameters is just a bit slower. Maybe a few ten cycles.
edit: You can profile on the NES via the color bits. You'll see on screen how many scanlines the code in between takes.
I see, for snes then. I guess for my current project since I don't use such paradigm and still using 2.17, it may be hard to use to some degree. Still, I will keep it in mind to experiment later since anything that is faster in C to avoid asm is always a good thing.
Even function from asm that may requires parameters, I try to avoid to put them on the software stack and define them as special zero page parameters to be used later.
C support for computed goto is great!
calima wrote:
The project I made this for is for SNES, hence the jmp (abs,x). It just happens to be supported by earlier cpus as well
I didn't think 6502 assembly had support for the jmp (abs,x) instruction?
So to clarify, it works on the NES's plain 6502, but is more efficient on 65sc02 and everything above, including 65816. For 6502 a series of instructions are used instead of the jmp (abs,x).
Aha 65C02 that's what you meant by CMOS version.