Load hex program code to ram

This commit is contained in:
Ferit Yiğit BALABAN
2021-10-29 01:13:53 +03:00
parent 07c2a32cdd
commit 0cd564f597
2 changed files with 87 additions and 25 deletions

51
MainForm.Designer.cs generated
View File

@@ -31,6 +31,8 @@ namespace processor
{ {
this.splitContainer1 = new System.Windows.Forms.SplitContainer(); this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.groupBox2 = new System.Windows.Forms.GroupBox(); 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.btnClear = new System.Windows.Forms.Button();
this.btnLoad = new System.Windows.Forms.Button(); this.btnLoad = new System.Windows.Forms.Button();
this.txtCode = new System.Windows.Forms.RichTextBox(); this.txtCode = new System.Windows.Forms.RichTextBox();
@@ -84,8 +86,6 @@ namespace processor
this.txtr6 = new System.Windows.Forms.TextBox(); this.txtr6 = new System.Windows.Forms.TextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox(); this.groupBox3 = new System.Windows.Forms.GroupBox();
this.ramList = new System.Windows.Forms.ListBox(); 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(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout();
@@ -141,6 +141,29 @@ namespace processor
this.groupBox2.TabStop = false; this.groupBox2.TabStop = false;
this.groupBox2.Text = "Input"; 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 // btnClear
// //
this.btnClear.Location = new System.Drawing.Point(187, 316); this.btnClear.Location = new System.Drawing.Point(187, 316);
@@ -149,6 +172,7 @@ namespace processor
this.btnClear.TabIndex = 8; this.btnClear.TabIndex = 8;
this.btnClear.Text = "Clear"; this.btnClear.Text = "Clear";
this.btnClear.UseVisualStyleBackColor = true; this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
// //
// btnLoad // btnLoad
// //
@@ -158,6 +182,7 @@ namespace processor
this.btnLoad.TabIndex = 7; this.btnLoad.TabIndex = 7;
this.btnLoad.Text = "Load"; this.btnLoad.Text = "Load";
this.btnLoad.UseVisualStyleBackColor = true; this.btnLoad.UseVisualStyleBackColor = true;
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
// //
// txtCode // txtCode
// //
@@ -692,28 +717,6 @@ namespace processor
this.ramList.Size = new System.Drawing.Size(199, 428); this.ramList.Size = new System.Drawing.Size(199, 428);
this.ramList.TabIndex = 0; 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 // MainForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);

View File

@@ -8,6 +8,8 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using static processor.Extensions;
namespace processor namespace processor
{ {
public partial class MainForm : Form public partial class MainForm : Form
@@ -23,7 +25,64 @@ namespace processor
{ {
ramList.Items.Clear(); ramList.Items.Clear();
for (int i = 0; i < 256; i++) 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();
}
/// <summary>
/// Parses and loads instructions to ramList
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLoad_Click(object sender, EventArgs e)
{
List<string> instructionList = new List<string>();
if (!string.IsNullOrWhiteSpace(txtCode.Text))
{
List<string> 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<string> inst = new();
for (int i = 0; i < instructionList.Count; i++)
{
IEnumerable<string> 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();
} }
} }