[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Switch Case Loop Slower For Emulator



Bryan Parkoff wrote:

>    I have noticed that every programmers use Switch Case Loop for Emulator.
>Switch Case has to scan 256 opcode each time.  I think that Switch Case Loop
>is very long for scanning.  There has to be another way to reduce Switch
>Case Loop by removing too much case for scanning each opcodes.
>    It has to use clever algorithm that will reduce Intel's cycle and
>improve performance.

The preferred implementation for a CASE statement depends on
the density of the switch variable values.  If it is a 2-byte variable
with only a few "cases" defined, then a branch tree may be an
efficient implementation.  However, if 240 of the 256 possibilities
for the "opcode" variable are case tags, then an indexed indirect
branch is a much better choice.  Any good compiler should use
the most appropriate code.  A search loop would almost never
be appropriate.

In this case, since the opcode map is relatively dense, the preferred
approach is to index with the opcode into a table of execute routine
addresses.  There is no loop involved in finding the execute routine.

Since there are no "looping" instructions (like move string) in the
6502 repertoire, and no "indefinite indirection", there will never be
a need for a loop in any execute routine, either.

The only loop in the emulator core is the "fetch-execute" loop
that progresses from one emulated instruction to the next, and
that is the loop formed by the final jump at the end of each
instruction "execute routine".

Since the only action of the fetch routine common to all instructions
is to read the byte pointed to by the simulated program counter and
do an indexed branch to the appropriate execute routine, an effecient
emulator will frequently append the "fetch" code to the end of each
"execute routine", so that there is only one unconditional branch
of the host machine executed for each emulated instruction.  In this
case the fetch-execute "loop" is virtual, having no common code
that is executed for each instruction (except accidentally, if two
successive emulated instructions have the same execute routine).

-michael

Check out amazing quality 8-bit Apple sound on my
Home page:  http://members.aol.com/MJMahon/