Exemple #1
0
        /// <summary>
        /// Executes binary code step by step.
        /// </summary>
        private void executeStepByStep()
        {
            LinkedList <byte> instructionCode = new LinkedList <byte>();

            Thread.BeginCriticalRegion();
            executing = true;
            next      = 0;
            Thread.EndCriticalRegion();
            Stopwatch sw = new Stopwatch();

            sw.Start();
            try
            {
                vars = new Variables();
                vars.SetVariable("working", true);
                while (true)
                {
                    lock (Form1.LockObject)
                    {
                        int pc = cpu.Constants.GetRegister("pc").Val;
                        Console.WriteLine("Executing pc = " + pc);
                        if (separators.Contains(pc - entryPoint) && pc != entryPoint)
                        {
                            Thread.BeginCriticalRegion();
                            bool temp = stepByStepMode;
                            Thread.EndCriticalRegion();
                            // TODO Check for step by step mode
                            if (separators.Contains(pc - entryPoint))
                            {
                                for (int i = 0; i < separators.Count - 1; i++)
                                {
                                    if (separators.ElementAt(i) == pc - entryPoint)
                                    {
                                        Thread.BeginCriticalRegion();
                                        next = i;
                                        Thread.EndCriticalRegion();
                                    }
                                }
                            }
                            Instruction         inst = cpu.Constants.MatchInstruction(instructionCode.ToArray());
                            InstructionRegister ir   = new InstructionRegister(instructionCode.ToArray());
                            inst.ReadAddressingModes(binary.ToArray());
                            int[] operands = inst.FetchOperands(ir, cpu, vars);
                            int[] result   = inst.Execute(ir, cpu, vars, operands);
                            inst.StoreResult(ir, cpu, vars, result);
                            instructionCode.Clear();

                            cpu.CheckForInterupts(vars);

                            pc = cpu.Constants.GetRegister("pc").Val;

                            for (int i = 0; i < separators.Count - 1; i++)
                            {
                                if (separators.ElementAt(i) == pc - entryPoint)
                                {
                                    Thread.BeginCriticalRegion();
                                    next = i;
                                    Thread.EndCriticalRegion();
                                }
                            }

                            if (pc - entryPoint >= binary.Count() || (bool)vars.GetVariable("working") == false) // First condition might not work for addressing word larger than 1
                            {
                                Thread.BeginCriticalRegion();
                                executing = false;
                                Thread.EndCriticalRegion();
                                //instSem.Release(1);
                                Thread.Yield();
                                break;
                            }
                            //Thread.BeginCriticalRegion();
                            //temp = stepByStepMode;
                            //Thread.EndCriticalRegion();
                            //if (temp == true)
                            //{
                            //    //instSem.Release(1);
                            //    Thread.Yield();
                            //}
                        }
                        //byte[] readFromMemory = Program.Mem[(uint)pc];
                        Form1.Instance.InstructionReached(next);
                        if ((stepByStepMode || breakPoints.Contains(next)) && separators.Contains(pc - entryPoint))
                        {
                            //breakSem.WaitOne();
                            system.Pause();
                        }
                        byte[] readFromMemory = cpu.ReadFromMemory((uint)pc);
                        Console.WriteLine("Read from memory {0}", ConversionHelper.ConvertFromByteArrayToInt(readFromMemory));
                        pc++;
                        for (int k = 0; k < readFromMemory.Length; k++)
                        {
                            instructionCode.AddLast(readFromMemory[k]);
                        }
                        //binary.AddLast(binaryCode[pc++]);
                        cpu.Constants.GetRegister("pc").Val = pc;
                        Thread.EndCriticalRegion();
                    }
                }
                //breakSem.Release();
                system.EndWorking();
                writeToOutput(" Code executed successfully.\n");
                Form1.Instance.ExecutionStoped();
                sw.Stop();
                Console.WriteLine("Execution time: {0} ms.", sw.ElapsedMilliseconds);
            }
            catch (System.Reflection.TargetInvocationException ex)
            {
                writeToOutput(DateTime.Now + " Execution error: " + ex.InnerException.Message + " in " + ex.InnerException.TargetSite + "\n");
                File.AppendAllText("error.txt", ex.ToString());
                //instSem.Release(1);
                //breakSem.Release(1);
                StopDebugging();
                system.EndWorking();
                endExecution();
            }
            catch (Exception ex)
            {
                writeToOutput(DateTime.Now + " Execution error: " + ex.Message + "\n");
                File.AppendAllText("error.txt", ex.ToString());
                //instSem.Release(1);
                //breakSem.Release(1);
                StopDebugging();
                system.EndWorking();
                endExecution();
            }
        }
Exemple #2
0
 /// <summary>
 /// Method that executes binary code.
 /// </summary>
 public void Execute()
 {
     try
     {
         cpu.Constants.GetRegister("pc").Val = entryPoint;
         LinkedList <Instruction> instructions = new LinkedList <Instruction>();
         LinkedList <byte>        binary       = new LinkedList <byte>();
         Thread.BeginCriticalRegion();
         executing = true;
         Thread.EndCriticalRegion();
         Variables vars = new Variables();
         vars.SetVariable("working", true);
         while (true)
         {
             int pc = cpu.Constants.GetRegister("pc").Val;
             cpu.CheckForInterupts(vars);
             if (separators.Contains(pc) && pc != entryPoint)
             {
                 Instruction         inst = cpu.Constants.MatchInstruction(binary.ToArray());
                 InstructionRegister ir   = new InstructionRegister(binary.ToArray());
                 inst.ReadAddressingModes(binary.ToArray());
                 int[] operands = inst.FetchOperands(ir, cpu, vars);
                 int[] result   = inst.Execute(ir, cpu, vars, operands);
                 inst.StoreResult(ir, cpu, vars, result);
                 binary.Clear();
                 pc = cpu.Constants.GetRegister("pc").Val;
             }
             if (pc - entryPoint >= binary.Count() || (bool)vars.GetVariable("working") == false)
             {
                 Thread.BeginCriticalRegion();
                 executing = false;
                 Thread.EndCriticalRegion();
                 break;
             }
             //binary.AddLast(binaryCode[pc++]);
             //byte[] nextWord = Program.Mem[(uint)pc];
             byte[] nextWord = cpu.ReadFromMemory((uint)pc);
             for (int i = 0; i < nextWord.Length; i++)
             {
                 binary.AddLast(nextWord[i]);
             }
             pc++;
             cpu.Constants.GetRegister("pc").Val = pc;
         }
         system.EndWorking();
         output.Text += DateTime.Now.ToString() + " Code executed successfully.\n";
         output.ScrollToCaret();
     }
     catch (System.Reflection.TargetInvocationException ex)
     {
         writeToOutput(DateTime.Now + " Execution error: " + ex.InnerException.Message + " in " + ex.InnerException.TargetSite + "\n");
         File.AppendAllText("error.txt", ex.ToString());
         StopDebugging();
         system.EndWorking();
         endExecution();
     }
     catch (Exception ex)
     {
         writeToOutput(DateTime.Now + " Execution error: " + ex.Message + "\n");
         File.AppendAllText("error.txt", ex.ToString());
         StopDebugging();
         system.EndWorking();
         endExecution();
     }
 }