Example #1
0
        private int InstructionExecutionLoopCore(bool isSingleInstruction)
        {
            ClockSynchronizer.Start();
            executionContext = new InstructionExecutionContext();
            StopReason       = StopReason.NotApplicable;
            State            = ProcessorState.Running;
            var totalTStates = 0;

            while (!executionContext.MustStop)
            {
                executionContext.StartNewInstruction();

                FireBeforeInstructionFetchEvent();
                if (executionContext.MustStop)
                {
                    break;
                }

                var executionTStates = ExecuteNextOpcode();

                totalTStates              = executionTStates + executionContext.AccummulatedMemoryWaitStates;
                TStatesElapsedSinceStart += (ulong)totalTStates;
                TStatesElapsedSinceReset += (ulong)totalTStates;

                ThrowIfNoFetchFinishedEventFired();

                if (!isSingleInstruction)
                {
                    CheckAutoStopForHaltOnDi();
                    CheckForAutoStopForRetWithStackEmpty();
                    CheckForLdSpInstruction();
                }

                FireAfterInstructionExecutionEvent(totalTStates);

                if (!IsHalted)
                {
                    IsHalted = executionContext.IsHaltInstruction;
                }

                var interruptTStates = AcceptPendingInterrupt();
                TStatesElapsedSinceStart += (ulong)interruptTStates;
                TStatesElapsedSinceReset += (ulong)interruptTStates;

                if (isSingleInstruction)
                {
                    executionContext.StopReason = StopReason.ExecuteNextInstructionInvoked;
                }
                else
                {
                    ClockSynchronizer.TryWait(totalTStates);
                }
            }

            ClockSynchronizer.Stop();
            this.StopReason = executionContext.StopReason;
            this.State      =
                StopReason == StopReason.PauseInvoked
                    ? ProcessorState.Paused
                    : ProcessorState.Stopped;

            executionContext = null;

            return(totalTStates);
        }