Instruction decoder

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

Registers:
==========
REG1
REG2

Instructions:
=============
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?

Cheers,

/Ulrik





Ulrik_LindbladAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SalteCommented:
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:

op0tbl[*instr++](comp);

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.

Alf
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ulrik_LindbladAuthor Commented:
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!

/Ulrik
0
tinchosCommented:
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.