cc65 now supports computed goto

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
calima
Posts: 1021
Joined: Tue Oct 06, 2015 10:16 am

cc65 now supports computed goto

Post by calima » Thu May 02, 2019 5:49 am

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

User avatar
Banshaku
Posts: 2328
Joined: Tue Jun 24, 2008 8:38 pm
Location: Fukuoka, Japan
Contact:

Re: cc65 now supports computed goto

Post by Banshaku » Thu May 02, 2019 6:05 am

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.

calima
Posts: 1021
Joined: Tue Oct 06, 2015 10:16 am

Re: cc65 now supports computed goto

Post by calima » Thu May 02, 2019 7:03 am

A canonical example is a "drawEnemy(u8 enemytype)" function.

lidnariq
Posts: 8791
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: cc65 now supports computed goto

Post by lidnariq » Thu May 02, 2019 11:00 am

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)

User avatar
Banshaku
Posts: 2328
Joined: Tue Jun 24, 2008 8:38 pm
Location: Fukuoka, Japan
Contact:

Re: cc65 now supports computed goto

Post by Banshaku » Thu May 02, 2019 7:57 pm

@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.

calima
Posts: 1021
Joined: Tue Oct 06, 2015 10:16 am

Re: cc65 now supports computed goto

Post by calima » Fri May 03, 2019 2:04 am

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 ;)
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.

User avatar
Banshaku
Posts: 2328
Joined: Tue Jun 24, 2008 8:38 pm
Location: Fukuoka, Japan
Contact:

Re: cc65 now supports computed goto

Post by Banshaku » Fri May 03, 2019 8:35 am

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.

Pokun
Posts: 1274
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: cc65 now supports computed goto

Post by Pokun » Sat May 04, 2019 2:05 am

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?

calima
Posts: 1021
Joined: Tue Oct 06, 2015 10:16 am

Re: cc65 now supports computed goto

Post by calima » Sat May 04, 2019 3:26 am

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).

Pokun
Posts: 1274
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: cc65 now supports computed goto

Post by Pokun » Sat May 04, 2019 4:19 am

Aha 65C02 that's what you meant by CMOS version.

Post Reply