Search code examples
assemblycpu-architecturecpu-speedz80

What exactly is a machine cycle?


I'm kinda confused about the exact definition of a "machine cycle".

For example, some source online say that it is:

The four steps which the CPU carries out for each machine language instruction: fetch, decode, execute, and store.

While the book Programming the Z80, which supposedly has a good reputation, says:

We have seen that all instructions are executed in three phases: FETCH, DECODE, EXECUTE. We now need to introduce some definitions. Each of these phases will require several clock cycles. The Z80 executes each phase in one or more logical cycles, called a "machine cycle."

Now what confuses me here is that - from what I understood - the first one is basically saying: A machine cycle is a fetch-decode-execute cycle, while the book is basically saying: A machine cycle is the one - or more - clock cycle that happens in each fetch, decode or execute cycle individually.

So, what is what?


Solution

  • z80 has two different concepts of "cycle". The distinction matters, because z80 is a multi-cycle architecture, and it uses both multiple clock cycles per "step" and (often) multiple "steps" per instruction.

    The "steps" are called machine cycles (M-cycles), they do "high level" tasks such as reading from memory, doing an ALU operation, etc. Complex instructions such as inc (iy+42) take many machine cycles, for decoding the prefix, the main opcode, reading the offset, adding it to iy, doing the increment, and writing back the result. Conditional jumps even have a varying number of M-cycles, they omit the M-cycle that does that actual jumping if the condition is false.

    Each M-cycle then takes multiple (3 to 6) clock cycles (aka T-cycle or T-state, this terminology has mostly died unless referring to old multicycle processors). For example, a memory read will take 3 cycles, an opcode decode typically takes 4, some internal operations take 5, and 16bit increments seem to extend the OCF by an other 2 cycles somehow.

    That's all quite z80-specific.

    Elsewhere the term "machine cycle" has been used to refer some sort of "complete trip" of an instruction from start to finish. That's not what would be meant in a z80 context.