Exemple #1
0
 protected virtual void OnInstructionExecuted(NESEventArgs e)
 {
     if (InstructionExecuted != null)
     {
         InstructionExecuted.Invoke(this, e);
         Execution.WaitOne();
     }
 }
Exemple #2
0
        public void Execute(byte[] program, CancellationToken cancellationToken)
        {
            _memory.Load(program);
            _dissassembler.SetPointer();

            while (true)
            {
                var instruction = _dissassembler.GetNextInstruction();

                if (cancellationToken.IsCancellationRequested || instruction == null)
                {
                    break;
                }

                instruction.Execute(_memory, _registers);

                InstructionExecuted?.Invoke(this, instruction);
            }
        }
Exemple #3
0
        // Event invocation, break handling and so on
        private void Execute(IExecutionEnvironment environment, IList <IInstruction> instructions, int waitMs)
        {
            Reset();
            IsFinished = false;
            IsAborted  = false;
            IsHalted   = false;

            ExecutionStarted?.Invoke(this, new EventArgs());

            while (!IsFinished && !IsAborted)
            {
                // Set the instruction for this cycle
                SetCurrentInstruction(environment, instructions);

                // Check if instruction is marked as a breakpoint and if it is active
                if (_breakPoints.ContainsKey(CurrentInstruction) && _breakPoints[CurrentInstruction])
                {
                    BreakExecution(CurrentInstruction, instructions.IndexOf(CurrentInstruction));
                }
                else if (IsHalted)
                {
                    // If instruction is not a breakpoint and IsHalted was already true, we stepped one instruction further
                    ExecutionStepped?.Invoke(this, new InstructionExecuteEventArgs(CurrentInstruction, CurrentInstructionIndex));
                }

                // Halt the thread if a breakpoint was reached
                if (IsHalted)
                {
                    try
                    {
                        Thread.Sleep(Timeout.Infinite);
                    }
                    catch (ThreadInterruptedException)
                    {
                        // Through an interrupt, the thread will continue from here
                    }
                    catch (ThreadAbortException)
                    {
                        // The further execution will be aborted
                        IsAborted = true;
                        break;
                    }
                }

                // Execute instruction
                InstructionExecuting?.Invoke(this, new InstructionExecuteEventArgs(CurrentInstruction, CurrentInstructionIndex));

                Thread.Sleep(waitMs);
                try
                {
                    ExecuteInternal(environment, instructions);
                }
                catch (Exception e)
                {
                    // Abort execution when the instruction throws
                    Logger.Log(LogLevel.Fatal, e.Message);
                    AbortExecution(true);
                    break;
                }

                InstructionExecuted?.Invoke(this, new InstructionExecuteEventArgs(CurrentInstruction, CurrentInstructionIndex));
            }

            if (!IsAborted)
            {
                ExecutionFinished?.Invoke(this, new EventArgs());
            }
        }
 internal void NotifyInstructionExecuted()
 {
     InstructionExecuted?.Invoke(Cpu, new InstructionExecutedEventArgs(Opcode));
 }