Skip to content

Latest commit

 

History

History
24 lines (17 loc) · 2.79 KB

InstructionExecutionFlow.md

File metadata and controls

24 lines (17 loc) · 2.79 KB

Instruction execution flow

The following is the chain of operations performed by IZ80Processor when executing one instruction, either as part of the instructions execution loop or when executing one single instruction (see how execution works).

  1. The BeforeInstructionFetch event is triggered. The code that handles the event has an opportunity to request execution termination by invoking BeforeInstructionFetchEventArgs.ExecutionStopper.Stop.
  2. The first opcode byte of the instruction to be executed is retrieved by reading Memory[Registers.PC].
  3. The InstructionExecutor.Execute method is invoked.

The code for InstructionExecutor.Execute does then the following:

  1. The ProcessorAgent.FetchNextOpcode method is invoked, if necessary, in order to retrieve additional opcde bytes for the instruction.
  2. The InstructionExecutor.InstructionFetchFinished event is fired. This causes IZ80Processor to fire the BeforeInstructionExecution event.
  3. The instruction is processed, using the members of ProcessorAgent as appropriate to access registers, memory and ports. Execution termination can be requested at this point by invoking ProcessorAgent.Stop (the default implementation of IZ80InstructionExecutor does never do this).
  4. The method terminates, returning the count of T states required to execute the instruction.

Control returns then to IZ80Processor:

  1. The AfterInstructionExecution event is triggered. The code that handles the event has an opportunity to request execution termination by invoking AfterInstructionExecutionEventArgs.ExecutionStopper.Stop.
  2. IClockSynchronizer.TryWait is executed, having the value returned by InstructionExecutor.Execute plus any additional extra wait states passed as the T states count.
  3. TStatesElapsedSinceStart and TStatesElapsedSinceReset are increased by the same value calculated in the previous step.
  4. If one of the execution stop conditions is met, the method that initiated the execution returns.
  5. If inside an instruction execution loop, the flow starts again for the next instruction.

Note that BeforeInstructionFetchEventArgs, BeforeInstructionExecutionEventArgs and AfterInstructionExecutionEventArgs inherit from ProcessorEventArgs, which defines the LocalUserState property. This property is propagated from the 'before fetch' event to the 'before execution' event and from this one to the 'after execution' event, and can be used by the events handling code at its convenience.