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).
- The
BeforeInstructionFetchevent is triggered. The code that handles the event has an opportunity to request execution termination by invokingBeforeInstructionFetchEventArgs.ExecutionStopper.Stop. - The first opcode byte of the instruction to be executed is retrieved by reading
Memory[Registers.PC]. - The
InstructionExecutor.Executemethod is invoked.
The code for InstructionExecutor.Execute does then the following:
- The
ProcessorAgent.FetchNextOpcodemethod is invoked, if necessary, in order to retrieve additional opcde bytes for the instruction. - The
InstructionExecutor.InstructionFetchFinishedevent is fired. This causesIZ80Processorto fire theBeforeInstructionExecutionevent. - The instruction is processed, using the members of
ProcessorAgentas appropriate to access registers, memory and ports. Execution termination can be requested at this point by invokingProcessorAgent.Stop(the default implementation ofIZ80InstructionExecutordoes never do this). - The method terminates, returning the count of T states required to execute the instruction.
Control returns then to IZ80Processor:
- The
AfterInstructionExecutionevent is triggered. The code that handles the event has an opportunity to request execution termination by invokingAfterInstructionExecutionEventArgs.ExecutionStopper.Stop. IClockSynchronizer.TryWaitis executed, having the value returned byInstructionExecutor.Executeplus any additional extra wait states passed as the T states count.TStatesElapsedSinceStartandTStatesElapsedSinceResetare increased by the same value calculated in the previous step.- If one of the execution stop conditions is met, the method that initiated the execution returns.
- 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.