private void PauseButton_Click(object sender, EventArgs e) { system.Pause(); }
/// <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(); } }