From bba688c0dfc4ac70bbc3c9ee331536047a3c21d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferit=20Yi=C4=9Fit=20BALABAN?= Date: Wed, 3 Nov 2021 08:29:34 +0300 Subject: [PATCH] Refactoring and clean up 3 --- src/MainForm.cs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/MainForm.cs b/src/MainForm.cs index 1d8ca34..fa2f77c 100644 --- a/src/MainForm.cs +++ b/src/MainForm.cs @@ -52,19 +52,22 @@ namespace processor /// /// /// - private int ReadRam(int address) => CnvRespectfully(ramList.Items[address].ToString().Split(':', StringSplitOptions.TrimEntries)[1]); + 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 + /// Reads the amount of memory cells supplied in "count", starts reading from "address". count = 1 only reads startAddress, count = 2 reads startAddress and the next cell. /// - /// - /// + /// + /// /// - private int ReadRam(int address, int count = 0) + private int ReadConsecutive(int startAddress, int readCount = 1) { + if (readCount < 1) + throw new ArgumentOutOfRangeException(nameof(readCount)); + string append = string.Empty; - for (int i = 0; i <= count; i++) - append += ramList.Items[address + i].ToString().Split(':', StringSplitOptions.TrimEntries)[1]; + for (int i = startAddress; i <= startAddress + readCount; i++) + append += ramList.Items[i].ToString()?.Split(':', StringSplitOptions.TrimEntries)[1]; return CnvRespectfully(append); } @@ -72,15 +75,15 @@ namespace processor /// Used by loader for writing hexadecimal/binary instructions /// /// - /// - private void LoadCode(int address, string value) => ramList.Items[address] = $"{CnvRespectfully(address, 2, 8)}: {value}"; + /// + private void LoadCode(int address, string value) => ramList.Items[address] = $"{CnvRespectfully(address)}: {value}"; /// /// Write data to addressed memory cell /// /// /// - private void WriteRam(int address, int value) => ramList.Items[address] = $"{CnvRespectfully(address, 2, 8)}: {CnvRespectfully(value, 2, 8)}"; + private void WriteRam(int address, int value) => ramList.Items[address] = $"{CnvRespectfully(address)}: {CnvRespectfully(value)}"; #endregion #region Registers @@ -94,7 +97,7 @@ namespace processor private void SetCounter(int value) { txtProgCounter.Text = CnvRespectfully(value); - SetInstRegister(ReadRam(ReadCounter(), 1)); + SetInstRegister(ReadConsecutive(ReadCounter(), 2)); } private int[] ReadInstRegister() @@ -126,8 +129,6 @@ 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 /// @@ -149,7 +150,7 @@ namespace processor } else { - + throw new NotImplementedException(); } } @@ -173,11 +174,10 @@ namespace processor /// Writes data to RAM without using CPU instructions /// /// - private void ExecuteRamWrite(List codes) + private void ExecuteRamWrite(IEnumerable codes) { - foreach (string code in codes) // code is a 2 byte hexadecimal instruction: 1A34 + foreach (string[] ia in codes.Select(code => code.Split2().ToArray())) { - string[] ia = code.Split2().ToArray(); WriteRam(CnvRespectfully(ia[0]), CnvRespectfully(ia[1])); } } @@ -241,4 +241,4 @@ namespace processor } } } -} +} \ No newline at end of file