diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index 5fa0068..678ff8d 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -221,6 +221,7 @@ namespace processor this.btnStart.TabIndex = 2; this.btnStart.Text = "Start"; this.btnStart.UseVisualStyleBackColor = true; + this.btnStart.Click += new System.EventHandler(this.btnStart_Click); // // btnStep // @@ -230,6 +231,7 @@ namespace processor this.btnStep.TabIndex = 3; this.btnStep.Text = "Step"; this.btnStep.UseVisualStyleBackColor = true; + this.btnStep.Click += new System.EventHandler(this.btnStep_Click); // // btnHalt // diff --git a/MainForm.cs b/MainForm.cs index c793d5a..486ae69 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -4,10 +4,14 @@ using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; +using System.Reflection.Emit; using System.Text; +using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; +using processor.Properties; + using static processor.Extensions; namespace processor @@ -59,6 +63,20 @@ namespace processor /// private int ReadRam(int address) => CnvRespectfully(ramList.Items[address].ToString().Split(':', StringSplitOptions.TrimEntries)[1]); + /// + /// Reads the amount of memory cells supplied in "count", starts reading from "address". 0 for count only reads addressed memory, 1 reads addressed and next cell + /// + /// + /// + /// + private int ReadRam(int address, int count = 0) + { + string append = string.Empty; + for (int i = 0; i <= count; i++) + append += ramList.Items[address + i].ToString().Split(':', StringSplitOptions.TrimEntries)[1]; + return CnvRespectfully(append); + } + /// /// Used by loader for writing hexadecimal/binary instructions /// @@ -74,6 +92,33 @@ namespace processor private void WriteRam(int address, int value) => ramList.Items[address] = $"{CnvRespectfully(address)}: {CnvRespectfully(value)}"; #endregion + #region Registers + + private int ReadRegister(int address) => CnvRespectfully(Controls.Find($"txtr{address:x}".ToLower(), true)[0].Text); + + private void WriteRegister(int address, int value) => Controls.Find($"txtr{address:x}".ToLower(), true)[0].Text = CnvRespectfully(value); + + private int ReadCounter() => CnvRespectfully(txtProgCounter.Text); + + private void SetCounter(int value) + { + txtProgCounter.Text = CnvRespectfully(value); + SetInstRegister(ReadRam(ReadCounter(), 1)); + } + + private int[] ReadInstRegister() + { + int[] r = new int[4]; + string[] a = radHex.Checked ? txtrInstReg.Text.Split1() : txtrInstReg.Text.Split4().ToArray(); + for (int i = 0; i < 4; i++) + r[i] = CnvRespectfully(a[i]); + return r; + } + + private void SetInstRegister(int value) => txtrInstReg.Text = CnvRespectfully4(value); + + #endregion + /// /// Converts integer to hexadecimal or binary representation in text according to selected view mode /// @@ -81,6 +126,10 @@ namespace processor /// private string CnvRespectfully(int value) => radHex.Checked ? Convert.ToString(value, 16).PadLeft(2, '0').ToUpper() : Convert.ToString(value, 2).PadLeft(8, '0'); + private string CnvRespectfully4(int value) => radHex.Checked ? Convert.ToString(value, 16).PadLeft(4, '0').ToUpper() : Convert.ToString(value, 2).PadLeft(8, '0'); + + private string CnvRespectfully(int value, int hexPad = 2, int binPad = 8) => radHex.Checked ? Convert.ToString(value, 16).PadLeft(hexPad, '0').ToUpper() : Convert.ToString(value, 2).PadLeft(binPad, '0'); + /// /// Converts hexadecimal or binary representation to integer according to selected view mode /// @@ -88,6 +137,8 @@ namespace processor /// private int CnvRespectfully(string value) => radHex.Checked ? Convert.ToInt32(value, 16) : Convert.ToInt32(value, 2); + private string AppendOperand(int x, int y) => radHex.Checked ? CnvRespectfully((x * 16) + y, 2, 8) : CnvRespectfully(5, 2, 4) + CnvRespectfully(3, 2, 4); + /// /// Parses and loads instructions to ramList /// @@ -126,7 +177,10 @@ namespace processor private void btnStart_Click(object sender, EventArgs e) { - + } + + private void btnStep_Click(object sender, EventArgs e) + { } } }