From 0cd564f597ff26e6b8e6e05c79be110a92e7eef1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferit=20Yi=C4=9Fit=20BALABAN?= Date: Fri, 29 Oct 2021 01:13:53 +0300 Subject: [PATCH] Load hex program code to ram --- MainForm.Designer.cs | 51 +++++++++++++++++++----------------- MainForm.cs | 61 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 25 deletions(-) diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index 6c973bf..5fa0068 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -31,6 +31,8 @@ namespace processor { this.splitContainer1 = new System.Windows.Forms.SplitContainer(); this.groupBox2 = new System.Windows.Forms.GroupBox(); + this.radHex = new System.Windows.Forms.RadioButton(); + this.radBin = new System.Windows.Forms.RadioButton(); this.btnClear = new System.Windows.Forms.Button(); this.btnLoad = new System.Windows.Forms.Button(); this.txtCode = new System.Windows.Forms.RichTextBox(); @@ -84,8 +86,6 @@ namespace processor this.txtr6 = new System.Windows.Forms.TextBox(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.ramList = new System.Windows.Forms.ListBox(); - this.radBin = new System.Windows.Forms.RadioButton(); - this.radHex = new System.Windows.Forms.RadioButton(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout(); @@ -141,6 +141,29 @@ namespace processor this.groupBox2.TabStop = false; this.groupBox2.Text = "Input"; // + // radHex + // + this.radHex.AutoSize = true; + this.radHex.Checked = true; + this.radHex.Location = new System.Drawing.Point(70, 318); + this.radHex.Name = "radHex"; + this.radHex.Size = new System.Drawing.Size(94, 19); + this.radHex.TabIndex = 10; + this.radHex.TabStop = true; + this.radHex.Text = "Hexadecimal"; + this.radHex.UseVisualStyleBackColor = true; + // + // radBin + // + this.radBin.AutoSize = true; + this.radBin.Location = new System.Drawing.Point(6, 318); + this.radBin.Name = "radBin"; + this.radBin.Size = new System.Drawing.Size(58, 19); + this.radBin.TabIndex = 9; + this.radBin.TabStop = true; + this.radBin.Text = "Binary"; + this.radBin.UseVisualStyleBackColor = true; + // // btnClear // this.btnClear.Location = new System.Drawing.Point(187, 316); @@ -149,6 +172,7 @@ namespace processor this.btnClear.TabIndex = 8; this.btnClear.Text = "Clear"; this.btnClear.UseVisualStyleBackColor = true; + this.btnClear.Click += new System.EventHandler(this.btnClear_Click); // // btnLoad // @@ -158,6 +182,7 @@ namespace processor this.btnLoad.TabIndex = 7; this.btnLoad.Text = "Load"; this.btnLoad.UseVisualStyleBackColor = true; + this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click); // // txtCode // @@ -692,28 +717,6 @@ namespace processor this.ramList.Size = new System.Drawing.Size(199, 428); this.ramList.TabIndex = 0; // - // radBin - // - this.radBin.AutoSize = true; - this.radBin.Location = new System.Drawing.Point(6, 318); - this.radBin.Name = "radBin"; - this.radBin.Size = new System.Drawing.Size(58, 19); - this.radBin.TabIndex = 9; - this.radBin.TabStop = true; - this.radBin.Text = "Binary"; - this.radBin.UseVisualStyleBackColor = true; - // - // radHex - // - this.radHex.AutoSize = true; - this.radHex.Location = new System.Drawing.Point(70, 318); - this.radHex.Name = "radHex"; - this.radHex.Size = new System.Drawing.Size(94, 19); - this.radHex.TabIndex = 10; - this.radHex.TabStop = true; - this.radHex.Text = "Hexadecimal"; - this.radHex.UseVisualStyleBackColor = true; - // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); diff --git a/MainForm.cs b/MainForm.cs index c905955..24a07f0 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -8,6 +8,8 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using static processor.Extensions; + namespace processor { public partial class MainForm : Form @@ -23,7 +25,64 @@ namespace processor { ramList.Items.Clear(); for (int i = 0; i < 256; i++) - ramList.Items.Add(string.Format("{0}: {1}", Convert.ToString(i, 2).PadLeft(8, '0'), "00000000")); + ramList.Items.Add(string.Format("{0}: {1}", Convert.ToString(i, 16).PadLeft(2, '0'), "00")); } + + #region RAM + private void ReadRam(int address, string value) + { + + } + + private void WriteRam(int address, string hexValue) + { + ramList.Items[address] = string.Format("{0}: {1}", CnvRespectfully(address.ToString()), hexValue); + } + #endregion + + private string CnvRespectfully(string value) + { + if (radHex.Checked is true) return value.Hex2Int().ToHex2(); + else throw new NotImplementedException(); + } + + /// + /// Parses and loads instructions to ramList + /// + /// + /// + private void btnLoad_Click(object sender, EventArgs e) + { + List instructionList = new List(); + + if (!string.IsNullOrWhiteSpace(txtCode.Text)) + { + List inputLines = txtCode.Text.Trim().Split((char)10, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList(); + inputLines = (from x in inputLines select RemoveComment(x)).ToList(); // removes comments from lines + instructionList = (from x in inputLines where string.IsNullOrEmpty(x) is false select x).ToList(); // drops empty elements + } + + if (radHex.Checked is true) + { + instructionList = (from x in instructionList select RemoveLeading(x)).ToList(); + List inst = new(); + for (int i = 0; i < instructionList.Count; i++) + { + IEnumerable enm = instructionList[i].Split2(); + inst.Add(enm.ElementAt(0)); + inst.Add(enm.ElementAt(1)); // i am quite sure that there is a much better approach but it's 1 am and i'm tired af + } + for (int i = 0; i < inst.Count; i++) + { + WriteRam(i, inst[i]); + } + } + else + { + + } + } + + private void btnClear_Click(object sender, EventArgs e) => InitializeRamList(); } }