//returns control of the main thread to the form. private void EndRun(object sender, CompRunFinishedEventArgs e) { if (this.InvokeRequired) { this.BeginInvoke(new EndRunDelegate(EndRun), e); return; } }//display a char form the Computer class.
//sets up the signal to the main thread that this thread is finished public void onFinishExecution(CompRunFinishedEventArgs e) { EventHandler <CompRunFinishedEventArgs> handler = CompRunFinished; if (handler != null) { handler(this, e); } }
//handles a step cycle for the computer public void Step() { if (dontrun) { CompRunFinishedEventArgs arg1 = new CompRunFinishedEventArgs(); arg1.end = 4; onFinishExecution(arg1); return; } run = true; //if (!trace) trace = true; Loader.Log("Computer: Executing Step Cycle", false, false); uint val = cpu.fetch(); uint insval = Registers.getPC(); Registers.setRegister(15, Registers.getPC() + 4); Computer x = this; Instruction ins = cpu.decode(val, ref x); cpu.execute(ins); CompRunFinishedEventArgs arg = new CompRunFinishedEventArgs(); arg.end = 3; if (trace) { try { writeTrace(insval); } catch { } } TraceStep++; onFinishExecution(arg); run = false; }
//update the panels on the main window. <e> contains a status code that will help determine which message to show the user, based on how execution endec //i.e. clicking on stop, hitting breakpoints etc. private void EndRun(CompRunFinishedEventArgs e) { if (e.end == 4) { RunBtn.Enabled = true; MessageBox.Show("Error: Illegal step/run performed. The program has already executed. Please hit reset and try again."); return; } Loader.Log("Form1: Step/Execute cycle finished.", false, false); uint entry = comp.Registers.getPC(); setRegister(15, entry); SetProgMode(); int x1 = 0; int fi = 1; while (fi < 5) { uint flag = comp.Registers.cpsr; bool fl = Memory.TestBit(32 - fi, flag); if (fl == true) { flagView.Rows[0].Cells[fi - 1].Value = "1"; } else { flagView.Rows[0].Cells[fi - 1].Value = "0"; } fi++; } if (Memory.TestBit(7, comp.Registers.cpsr)) { flagView.Rows[0].Cells[4].Value = "1"; } else { flagView.Rows[0].Cells[4].Value = "0"; } while (x1 < 15) { uint val = comp.getReg(x1); setRegister(x1, val); x1++; } var x = disassemblyView.Rows; disassemblyView.ClearSelection(); bool selected = false; foreach (DataGridViewRow y in x) { if (y.Cells[0].Value == null) { continue; } string iii = (string)y.Cells[0].Value; var nupoint = UInt32.Parse(iii.Substring(2), System.Globalization.NumberStyles.HexNumber); if (nupoint == entry) { int rindex = y.Index; if (rindex > 2) { selected = true; if (rindex != disassemblyView.Rows[disassemblyView.Rows.Count - 1].Index) { disassemblyView.FirstDisplayedScrollingRowIndex = rindex - 2; } else { disassemblyView.FirstDisplayedScrollingRowIndex = disassemblyView.Rows.Count - 4; } } else { selected = true; } y.Selected = true; disassemblyView.Update(); break; } } if (!selected) { disassemblyView.FirstDisplayedScrollingRowIndex = disassemblyView.Rows.Count - 4; disassemblyView.Rows[disassemblyView.Rows.Count - 1].Selected = true; } if (e.end == 0) { MessageBox.Show("Execution complete"); opModePanel.Text = "Operating Mode: None"; comp.resetCounter(); } else if (e.end == 1) { MessageBox.Show("Breakpoint Reached"); opModePanel.Text = "Operating Mode: Breakpoint"; } else if (e.end == 2) { MessageBox.Show("Execution stopped by user"); opModePanel.Text = "Operating Mode: User Stop"; } else if (e.end == 3) { opModePanel.Text = "Operating Mode: None"; } else if (e.end == 4) { MessageBox.Show("Error: Illegal step/run performed. The program has already executed. Please hit reset and try again."); } else if (e.end == 5) { MessageBox.Show("Internal simulator error. Run aborted."); } addStack(); resetMemory(entry); RunBtn.Enabled = true; }
//handles a run cycle for the computer public void Run() { if (dontrun) { CompRunFinishedEventArgs arg1 = new CompRunFinishedEventArgs(); arg1.end = 4; onFinishExecution(arg1); return; } run = true; interrupt = false; endrun = false; Loader.Log("Computer: Executing Run Cycle", false, false); uint val = 9001; try { //if (!trace) { trace = true; sw = new StreamWriter("trace.log"); } while (val != 0 && stop == false && interrupt == false && halt == false) { val = cpu.fetch(); uint insval = Registers.getPC(); if (insval == RAM.begin2) { tr = true; } Registers.setRegister(15, Registers.getPC() + 4); Computer x = this; Instruction ins = cpu.decode(val, ref x); if (val != uint.MaxValue) { cpu.execute(ins); } else { Registers.setRegister(15, Registers.getPC() - 4); stop = true; } if (trace && val != uint.MaxValue && val != 0) { try { writeTrace(insval); } catch { } } TraceStep++; if (cpu.irq == 1 && !Memory.TestBit(7, Registers.cpsr)) {//toggle the IRQ bit on and process input Registers.swapRegisters(2); cpu.irq = 0; Memory.SetBitinInt(Registers.cpsr, 7, true); } } if (val != uint.MaxValue) { Registers.setRegister(15, Registers.getPC() - 4); } CompRunFinishedEventArgs arg = new CompRunFinishedEventArgs(); if (interrupt) { arg.end = 4; } if (!interrupt) { if (val == uint.MaxValue) { Loader.Log("Computer: Breakpoint hit", false, false); stop = false; arg.end = 1; } else { arg.end = 0; endrun = true; halt = false; if (stop) { Loader.Log("Computer: Stopping at user's command.", false, false); arg.end = 2; stop = false; } if (arg.end == 0) { dontrun = true; } } } interrupt = false; run = false; onFinishExecution(arg); } catch { interrupt = false; run = false; stop = false; CompRunFinishedEventArgs arg = new CompRunFinishedEventArgs(); arg.end = 5; onFinishExecution(arg); } }