Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Instruction decoder

Posted on 2003-03-28
Medium Priority
Last Modified: 2012-08-13
Hi all,

I am planning to do some generic processor simulator, and one of the tasks that always
will occur is an instruction decoder. Now, if creating a processor simulator for a fairly
complexed processor I want ALL parts of it to be quite fast (sometimes we are talking
hours for a single simulation). So - to start with - lets talk about the decoder.

Lets say that we have a processor with 2 registers that is supporting only 2 (!) instructions
(dont care about the details, it is the principle that is important):


ADD S,D with opcode "0sd"
SUB S,D with opcode "1sd"

Register decoding:

s = 0 -> S = REG1
s = 1 -> S = REG2

d = 0 -> D = REG2
d = 1 -> D = REG1

So in this case the possible instructions encountered in a program would be:

Opcode 000: ADD REG1,REG2
Opcode 001: ADD REG1,REG1 (2*REG1)
Opcode 010: ...
Opcode 100: SUB REG1,REG2

And so on...

And now to the discussion. How would this be implemented in a fast way? (consider an instruction
set with up to 80 instructions, opcodes 24 bits (or even more for VLIW architectures)). One should
be able to gain something because of the fact that  every opcode corresponds to an unique instruction
(and it's arguments).

Even though quite memory consuming, I (or rather - a program that I will write in that case)
am thinking about doing an explicit enumeration, so that - at run-time - decoding of an intruction will
only be a simple (and therefore fast?) table lookup (that will be indexed using the opcode,
and that will return a function pointer to the "instruction" (that in C++ of course will
be implemented as a function) to be run. Is this a good idea do you think? And in that case, which
container should I use?

std::map <int, InstrExecFunc*> ? (The mapping of an opcode (represented in some datatype) to an "int" need
in that case only to be done once, before the actual execution (simulation) starts, so it will add no overhead
(or even, I create it once and then store it - in some way - as a part of the program)).

Thoughts, ideas suggestions?

Another question, anyone has experience of simulating fixed point arithmetics? I am having problems choosing
representation that allows me to simulate, for example, a ripple carry adder that will set condition codes
and stuff like that. I was in a project where we did this in C, and then we represented data as arrays of
chars, but there should be faster and better ways I think. Ideas? URL's?



Question by:Ulrik_Lindblad
LVL 12

Accepted Solution

Salte earned 80 total points
ID: 8225017
It partly depends on varios circumstances.

If you want to make a simulator for real computers they often tend to have the various fields at specific positions and so there's no way around but to use masking and unmasking bits. I.e. & (AND), | (OR), ^ (XOR), ~(COMPLEMENT) etc are your friends.

So a typical code would be something like this:

switch (*instr & OP_MASK) {
case OP_A:  ....
case OP_B:

At other times you can use a table of function pointers:

void (* op0tbl[256])(Computer * comp) = {
  lots of functions for each byte.

which is called by something like:


Typically the function called get a pointer to a 'Computer' object which holds data for all registers, RAM, I/O channels etc etc.

You could make this an array of member function pointers but it probably is just as well to let it be regular function pointers and just declare the functions to be static functions inside the class if you want them to be in a class.

Of course a class' vtable is also a table of function pointers and so you could use that but I think it is a bad idea, you typically index yourself in this table using the opcode or some other code from the instruction code and so an array of function pointers is more to the mark.

Also, note that although the presence of such a table can be considered generic for any CPU they tend to be very differnet from CPU to CPU and as such I would rather just let each CPU type have it's own data structures for instruction decoding.

So my advice is use switch() at places where that makes sense and use an array of function pointers where that makes sense. Typically the more bits is used to index into different branhes there more it makes sense to have a table of function pointers and the fewer cases you have the more it makes sense to have a switch or perhaps even only a simple if test.


Author Comment

ID: 8226246
Hi Alf (and the rest of you),

Thanks for the comments Alf! I am realizing, though, that I have not been clear enough when explaining what I want
(or maybe it s like aways: "I don't really know WHAT I want" =) ). I will get back to you with a better explanation on what I mean with "generic" (I am going
for a dinner tonight (as if you were interested... =) ))later, and I think you (and others) really can give me some
really good feedback and good ideas. As for now: thanks again, "talk" to you later!


Expert Comment

ID: 9566363
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: Salte

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question