EDK - Clock
-----------

Although system clock could be implemented with an o2 Line, but this would be too slow. There has to be another solution.

Basically, all Chips are linked into a chain. For example, a chain of four Chips is looking the following way:

o2 high phase |                  o2 low phase
    .-----.   |   .----------------.     .------.     .------.
    |     |   |   | 1. Next Clock  |     |      |     |      |
.-> | CPU | ----> | 2. Fire Timers | --> | CIA1 | --> | CIA2 | -.
|   |     |   |   | 3. VIC         |     |      |     |      |  |
|   '-----'   |   '----------------'     '------'     '------'  |
'---------------------------------------------------------------'

When one of these Chips has finished its task for the current clock, it jumps to the next Chip in the list. The code to switch from one Chip to another is:

  mov [ESI]Chip.pfnContinue,offset NextClock  // 1.5
  mov ESI,[ESI]Chip.pNextChip                 // 0.5
  jmp [ESI]Chip.pfnOnClock                    // 2 + 1 AGI + 3 BPU
NextClock:

This executes in 8 cyles on a Pentium, when both code and data are in the L1 cache. The time needed for overhead may be reduced to 3 cycles at best:

  mov EBP,[ESI]Chip.pNextChip                 // 0.5
  nop                                         // replace by
  nop                                         // something useful
  mov ESI,EBP                                 // 0.5
  jmp [EBP]Chip.pfnOnClock                    // 2

In this example, the label NextClock is constant, so [ESI]Chip.pfnOnClock doesn't need to be reloaded. This will also avoid the 3 clock BPU penalty for a mispredicted jump (Actually, the saving happens in the previous Chip). Finally, using EBP as a temporary register will avoid the AGI stall and save another 0.5 cycles.

See http://announce.com/agner/assem for more information on Pentium optimization.

In the current version of the EDK, the VIC performs also the job of the 985248 Hz Clock in order to save time. This will be changed in the future to let the C64 and the VIC1541 run at slightly different clock rates.
