I've been working on an Intel 8086 emulator for about a month now. I've decided to start counting cycles to make emulation more accurate and synchronize it correctly with the PIT.
The clock cycles used for each instruction are detailed in Intel's User Manual but I'd like to know how they're calculated. For example, I've deduced the following steps for the XCHG mem8,reg8
instruction - which takes exactly 17 clock cycles according to the manual:
But I'm probably completely wrong as my reasoning doesn't seem to work for all instructions. For instance, I can't comprehend why the PUSH reg
instruction takes 11 clock cycles, whereas the POP reg
instruction only takes 8 clock cycles.
So, could you tell me how clock cycles are spent in each instruction, or rather a general method to understand where those numbers come from?
Thank you.
How are cycles calculated and what does actually the clock
do was a mystery to me as well, until I had the chance to work together with hardware guys and I could see what kind of models they work with. The answer lies in the hardware
CPU
is parallel machine and although to programmers it's design is usually described in some simplifying terms explaining the pipeline or the microinstructions needed to implement it etc. CPU
remains to be parallel machine.
For an instruction to complete, many tiny bit-size
signals must flow through from one end to another. At some spots the processing units must wait till all the input bit
s arrive. This coordinated movement from one stage
to another is driven by the clock-signal
which is sent centrally to all the many parts. Each such move drummed by the clock-signal
is called cycle
.
So in order to know how many cycle
s are really needed to finish the work, you must take into account how are the wire
s connected and where the bit
s must flow through and where and how many are the required synchronization points.
I doubt if the Intel 8086
schematic is publicly available and even if it was then I doubt that it would be readable. But the only correct answer lies there. Everything else is just a simplification and to reproduce the exact hardware behavior in software, you would have to simulate/interpret the CPU
s hardware
See also:
CPU
s in various hardware description languages where you can see how exactly is the clock signal used