It is currently Mon Oct 23, 2017 1:36 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: CPU Emulator Design
PostPosted: Sat Jan 04, 2014 4:50 pm 
Offline

Joined: Wed Dec 11, 2013 9:54 pm
Posts: 26
Which design do you guys find to be better of the following two when fetching the instruction:

1) A jump table with all of the opcodes (function pointers in the right places in the table) that are referenced.

2) A large switch loop with a case for each opcode.

At present, I have mine doing things with a jump table but am curious of which way is better. I have an individual function for each opcode. Way I see it is I only had to write the code just once.


Top
 Profile  
 
 Post subject: Re: CPU Emulator Design
PostPosted: Sat Jan 04, 2014 5:54 pm 
Offline
User avatar

Joined: Sun Sep 19, 2004 9:28 pm
Posts: 3192
Location: Mountain View, CA, USA
Most compilers will turn a "gigantic big-ass switch/case statement" into a jump table (while, if I remember correctly, a small number of switch/case statements results in a series of mov/cmp statements). It's your responsibility to determine which happens by looking at the generated assembly code from the compiler, and/or by doing profiling.

If you don't know whether or not to trust the compiler, then your own jump/function table is perfectly fine.


Top
 Profile  
 
 Post subject: Re: CPU Emulator Design
PostPosted: Sat Jan 04, 2014 6:18 pm 
Offline
User avatar

Joined: Mon Sep 27, 2004 8:33 am
Posts: 3715
Location: Central Texas, USA
Quote:
Which design do you guys find to be better of the following two when fetching the instruction:

Whichever meets your design goals. A good starting place is whichever is most clear/maintainable. Surely one is easier to write than the other. Use that.


Top
 Profile  
 
 Post subject: Re: CPU Emulator Design
PostPosted: Sat Jan 04, 2014 6:49 pm 
Offline
Formerly Fx3
User avatar

Joined: Fri Nov 12, 2004 4:59 pm
Posts: 3064
Location: Brazil
I use two jump tables. The first one decodes the addressing mode and the argument (immediate byte, indirect etc), the next one do the opcode operations. With this system, most of opcodes can be condensed in just one.


Top
 Profile  
 
 Post subject: Re: CPU Emulator Design
PostPosted: Sun Jan 05, 2014 4:17 am 
Offline
User avatar

Joined: Thu Sep 23, 2010 7:28 pm
Posts: 232
Zepper wrote:
I use two jump tables. The first one decodes the addressing mode and the argument (immediate byte, indirect etc), the next one do the opcode operations. With this system, most of opcodes can be condensed in just one.


This is how I do it too. It makes for extremely clean code that's easy to manage and others to understand.


Top
 Profile  
 
 Post subject: Re: CPU Emulator Design
PostPosted: Sun Jan 05, 2014 4:43 am 
Offline

Joined: Thu Sep 15, 2005 9:23 am
Posts: 1194
Location: Behind you with a knife!
miker00lz wrote:
Zepper wrote:
I use two jump tables. The first one decodes the addressing mode and the argument (immediate byte, indirect etc), the next one do the opcode operations. With this system, most of opcodes can be condensed in just one.


This is how I do it too. It makes for extremely clean code that's easy to manage and others to understand.


I thought I was the only one!!! Its so easy to manage and the readability is out of this world.

_________________
http://www.jamesturner.de/


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

All times are UTC - 7 hours


Who is online

Users browsing this forum: Bing [Bot] and 7 guests


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