private Computer CreateComputer(double[] program, double inputValue, List<string> log) { var computer = new Computer(100, program); computer.NotifyMicroAction += (sender, e) => log.Add(e.ActionDescription); computer.IO.InputRequested += (sender, e) => computer.IO.Input(inputValue); return computer; }
private void btnLoad_Click(object sender, RoutedEventArgs ea) { var data = OpenFile(); if (data == null) return; txtRegAValue.Background = Brushes.White; txtRegDValue.Background = Brushes.White; txtRegPCValue.Background = Brushes.White; txtRegPValue.Background = Brushes.White; txtRegXValue.Background = Brushes.White; txtRegYValue.Background = Brushes.White; txtRegZValue.Background = Brushes.White; txtRegIValue.Background = Brushes.White; txtRegOValue.Background = Brushes.White; txtOutputLog.Background = Brushes.White; txtLogMicroActions.Text = string.Empty; txtInputValue.Background = Brushes.White; txtInputValue.IsEnabled = false; foreach (var txtData in this.values) { txtData.Text = "0"; txtData.FontWeight = FontWeights.Normal; txtData.Background = Brushes.White; } foreach (var txtAddress in this.addresses) { txtAddress.Background = Brushes.White; } txtAddress0.Background = Brushes.LightBlue; for (int i = 0; i < data.Length; i++) { this.values[i].Text = data[i].ToString(); this.values[i].FontWeight = FontWeights.Bold; } txtRegAValue.Text = "0"; txtRegDValue.Text = "0"; txtRegPCValue.Text = "0"; txtRegPValue.Text = "0"; txtRegXValue.Text = "0"; txtRegYValue.Text = "0"; txtRegZValue.Text = "0"; txtRegIValue.Text = "0"; txtRegOValue.Text = "0"; txtInputLog.Text = string.Empty; txtOutputLog.Text = string.Empty; this.computer = new Computer(100, data); this.computer.IO.InputRequested += (s, e) => { txtInputValue.IsEnabled = true; txtInputValue.Focus(); txtInputValue.Background = Brushes.Pink; btnStep.IsEnabled = false; }; this.computer.IO.OutputPerformed += (s, e) => { if (string.IsNullOrEmpty(txtOutputLog.Text)) txtOutputLog.Text = e.Value.ToString(); else txtOutputLog.Text = txtOutputLog.Text + "\r\n" + e.Value.ToString(); txtOutputLog.Background = Brushes.Yellow; }; this.computer.Memory.CellValueChanged += (s, e) => { this.values[e.Address].Text = e.NewValue.ToString(); this.values[e.Address].Background = Brushes.Yellow; this.values[e.Address].FontWeight = FontWeights.Bold; }; this.computer.Registers.RegisterValueChanged += (s, e) => { switch (e.Register) { case Register.A: txtRegAValue.Text = e.NewValue.ToString(); txtRegAValue.Background = Brushes.Yellow; break; case Register.D: txtRegDValue.Text = e.NewValue.ToString(); txtRegDValue.Background = Brushes.Yellow; break; case Register.I: txtRegIValue.Text = e.NewValue.ToString(); txtRegIValue.Background = Brushes.Yellow; break; case Register.O: txtRegOValue.Text = e.NewValue.ToString(); txtRegOValue.Background = Brushes.Yellow; break; case Register.PC: txtRegPCValue.Text = e.NewValue.ToString(); txtRegPCValue.Background = Brushes.Yellow; foreach (var txtAddress in this.addresses) { txtAddress.Background = Brushes.White; } this.addresses[(int)this.computer.Registers[Register.PC]].Background = Brushes.LightBlue; break; case Register.P: txtRegPValue.Text = e.NewValue.ToString(); txtRegPValue.Background = Brushes.Yellow; break; case Register.X: txtRegXValue.Text = e.NewValue.ToString(); txtRegXValue.Background = Brushes.Yellow; break; case Register.Y: txtRegYValue.Text = e.NewValue.ToString(); txtRegYValue.Background = Brushes.Yellow; break; case Register.Z: txtRegZValue.Text = e.NewValue.ToString(); txtRegZValue.Background = Brushes.Yellow; break; } }; this.computer.NotifyMicroAction += (s, e) => { if (string.IsNullOrEmpty(this.txtLogMicroActions.Text)) { txtLogMicroActions.Text = e.ActionDescription; } else { txtLogMicroActions.Text += " => " + e.ActionDescription; } }; this.btnStep.IsEnabled = true; }