?
Solved

Instruction decoder

Posted on 2003-03-28
4
Medium Priority
?
1,004 Views
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):

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





0
Comment
Question by:Ulrik_Lindblad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 12

Accepted Solution

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

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
 

Author Comment

by:Ulrik_Lindblad
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!

/Ulrik
0
 
LVL 9

Expert Comment

by:tinchos
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

777 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