It is currently Tue Jul 23, 2019 2:31 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Thu May 02, 2019 5:49 am 
Offline

Joined: Tue Oct 06, 2015 10:16 am
Posts: 956
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


Top
 Profile  
 
PostPosted: Thu May 02, 2019 6:05 am 
Offline
User avatar

Joined: Tue Jun 24, 2008 8:38 pm
Posts: 2303
Location: Fukuoka, Japan
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.


Top
 Profile  
 
PostPosted: Thu May 02, 2019 7:03 am 
Offline

Joined: Tue Oct 06, 2015 10:16 am
Posts: 956
A canonical example is a "drawEnemy(u8 enemytype)" function.


Top
 Profile  
 
PostPosted: Thu May 02, 2019 11:00 am 
Offline

Joined: Sun Apr 13, 2008 11:12 am
Posts: 8488
Location: Seattle
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)


Top
 Profile  
 
PostPosted: Thu May 02, 2019 7:57 pm 
Offline
User avatar

Joined: Tue Jun 24, 2008 8:38 pm
Posts: 2303
Location: Fukuoka, Japan
@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.


Top
 Profile  
 
PostPosted: Fri May 03, 2019 2:04 am 
Offline

Joined: Tue Oct 06, 2015 10:16 am
Posts: 956
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.


Top
 Profile  
 
PostPosted: Fri May 03, 2019 8:35 am 
Offline
User avatar

Joined: Tue Jun 24, 2008 8:38 pm
Posts: 2303
Location: Fukuoka, Japan
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.


Top
 Profile  
 
PostPosted: Sat May 04, 2019 2:05 am 
Offline

Joined: Tue May 28, 2013 5:49 am
Posts: 1177
Location: Hokkaido, Japan
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?


Top
 Profile  
 
PostPosted: Sat May 04, 2019 3:26 am 
Offline

Joined: Tue Oct 06, 2015 10:16 am
Posts: 956
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).


Top
 Profile  
 
PostPosted: Sat May 04, 2019 4:19 am 
Offline

Joined: Tue May 28, 2013 5:49 am
Posts: 1177
Location: Hokkaido, Japan
Aha 65C02 that's what you meant by CMOS version.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group