using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static processor.Extensions; namespace processor { public partial class MainForm : Form { public MainForm() => InitializeComponent(); private void MainForm_Load(object sender, EventArgs e) { InitializeRamList(); InitializeCpu(); } private void InitializeRamList() { ramList.Items.Clear(); if (radHex.Checked is true) for (int i = 0; i < 256; i++) ramList.Items.Add(string.Format("{0}: {1}", Convert.ToString(i, 16).PadLeft(2, '0').ToUpper(), "00")); else for (int i = 0; i < 256; i++) ramList.Items.Add(string.Format("{0}: {1}", Convert.ToString(i, 2).PadLeft(8, '0'), "00000000")); } private void InitializeCpu() { if (radHex.Checked is true) { txtrInstReg.Text = "00"; txtProgCounter.Text = "00"; for (int i = 0; i < 16; i++) Controls.Find($"txtr{i:x}", true)[0].Text = "00"; } else { txtrInstReg.Text = "00000000"; txtProgCounter.Text = "00000000"; for (int i = 0; i < 16; i++) Controls.Find($"txtr{i:x}", true)[0].Text = "00000000"; } } #region RAM /// /// Returns data as integers hold in addressed memory cell /// /// /// private int ReadRam(int address) { } private void WriteRam(int address, string hexValue) { ramList.Items[address] = string.Format("{0}: {1}", CnvRespectfully(address.ToString()), hexValue); } #endregion /// /// Converts integer to hexadecimal or binary representation in text according to selected view mode /// /// /// private string CnvRespectfully(int value) => radHex.Checked ? Convert.ToString(value, 16).PadLeft(2, '0') : Convert.ToString(value, 2).PadLeft(8, '0'); /// /// Converts hexadecimal or binary representation to integer according to selected view mode /// /// /// private int CnvRespectfully(string value) => radHex.Checked ? Convert.ToInt32(value, 16) : Convert.ToInt32(value, 2); /// /// Parses and loads instructions to ramList /// /// /// private void btnLoad_Click(object sender, EventArgs e) { List instructionList = new(); 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(); int j = 0; for (int i = 0; i < instructionList.Count; i++) { string[] enm = instructionList[i].Split2().ToArray(); WriteRam(j, enm[0]); WriteRam(j+1, enm[1]); j += 2; } } else { } } private void btnClear_Click(object sender, EventArgs e) => InitializeRamList(); private void btnStart_Click(object sender, EventArgs e) { } } }