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
BeforeInstructionFetch
event 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.Execute
method is invoked.
The code for InstructionExecutor.Execute
does then the following:
- The
ProcessorAgent.FetchNextOpcode
method is invoked, if necessary, in order to retrieve additional opcde bytes for the instruction. - The
InstructionExecutor.InstructionFetchFinished
event is fired. This causesIZ80Processor
to fire theBeforeInstructionExecution
event. - 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 invokingProcessorAgent.Stop
(the default implementation ofIZ80InstructionExecutor
does never do this). - The method terminates, returning the count of T states required to execute the instruction.
Control returns then to IZ80Processor
:
- The
AfterInstructionExecution
event is triggered. The code that handles the event has an opportunity to request execution termination by invokingAfterInstructionExecutionEventArgs.ExecutionStopper.Stop
. IClockSynchronizer.TryWait
is executed, having the value returned byInstructionExecutor.Execute
plus any additional extra wait states passed as the T states count.TStatesElapsedSinceStart
andTStatesElapsedSinceReset
are 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.