Posted on 2003-03-28
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"
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?