public void ReadInstructions(string[] instructions) { foreach (string input in instructions) { try { memory.AddInstruction(input); } catch (AssemblerException ex) { window.Error(ex.InstrctionNumber, ex.Message); } } window.Console.WriteLine("---------------------------------------------"); }
/// <summary> /// Execute new instruction in the thread /// </summary> /// <param name="memory">System memory</param> /// <param name="window">UVSim Controller</param> public void Execute(MemoryManager memory, IUVSimController window) { // Check if thread was terminated if (Terminated) { return; } // Create executioners BasicML directExecution = new BasicMLDirect(window, memory); BasicML indirectExecution = new BasicMLIndirect(window, memory); BasicMLMath directMath = new BasicMLMathDirect(window, memory); BasicMLMath indirectMath = new BasicMLMathIndirect(window, memory); // Retrieve pc value from memory int pc = memory[THREAD_OFFSET * ThreadNumber + (THREAD_OFFSET - 1)]; pc = THREAD_OFFSET * ThreadNumber + pc; //Retrieve register from memory int memoryLocation = THREAD_OFFSET * ThreadNumber + (THREAD_OFFSET - 3); Accumilator.Instance.Value = directExecution.ComposeDWORD(ref memoryLocation); try { int opCode; // Get the opCode opCode = memory[pc] / 100; // Switch statment to execute the targeted instruction based on the opCode. switch (opCode) { //----------------------------------------------------------------------------------------------------------------------- // Coded by: Josh Cooley // READ case 10: window.Console.Write($"[Thread #{ThreadNumber}] "); directExecution.Read(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Josh Cooley // WRITE case 11: window.Console.Write($"[Thread #{ThreadNumber}] "); directExecution.Write(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Josh Cooley // READ - INDIRECT case 12: window.Console.Write($"[Thread #{ThreadNumber}] "); indirectExecution.Read(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Josh Cooley // WRITE - INDIRECT case 13: window.Console.Write($"[Thread #{ThreadNumber}] "); indirectExecution.Write(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Josh Cooley // LOAD case 20: directExecution.Load(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Nikita Pestin // STORE case 21: directExecution.Store(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Nikita Pestin // LOAD - INDIRECT case 22: indirectExecution.Load(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Nikita Pestin // STORE - INDIRECT case 23: indirectExecution.Load(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Nikita Pestin // ADD case 30: directMath.Add(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Nikita Pestin // SUBTRACT case 31: directMath.Subtract(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Caleb Hansen // DIVIDE case 32: directMath.Divide(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Caleb Hansen // MULTIPLY case 33: directMath.Multiply(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Nikita Pestin // ADD - INDIRECT case 34: indirectMath.Add(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Nikita Pestin // SUBTRACT - INDIRECT case 35: indirectMath.Subtract(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Nikita Pestin // DIVIDE - INDIRECT case 36: indirectMath.Divide(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Nikita Pestin // MULTIPLY - INDIRECT case 37: indirectMath.Multiply(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Caleb Hansen // BRANCH case 40: directExecution.Branch(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Ali Alabdlmohsen // BRANCHNEG case 41: directExecution.BranchNeg(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Ali Alabdlmohsen // BRANCHZERO case 42: directExecution.BranchZero(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Ali Alabdlmohsen // HALT case 43: // End the execution. window.Console.WriteLine("---------------------------------------------"); terminated = true; break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Caleb Hansen // BRANCH - Josh Cooley case 44: indirectExecution.Branch(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Ali Alabdlmohsen // BRANCHNEG - INDIRECT case 45: indirectExecution.BranchNeg(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Ali Alabdlmohsen // BRANCHZERO - INDIRECT case 46: indirectExecution.BranchZero(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Ali Alabdlmohsen // REMINDER case 50: directMath.Reminder(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Ali Alabdlmohsen // EXPONENTIATION case 51: directMath.Exponential(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Ali Alabdlmohsen // REMINDER - INDIRECT case 52: indirectMath.Reminder(ref pc); break; //----------------------------------------------------------------------------------------------------------------------- // Coded by: Ali Alabdlmohsen // EXPONENTIATION - INDIRECT case 53: indirectMath.Exponential(ref pc); break; // Defult. default: // Increment the program counter. pc++; break; } } catch (Exception ex) { window.Console.Write($"[Thread #{ThreadNumber}] "); window.Error(ex.Message); terminated = true; } // Check termination if ((pc % THREAD_OFFSET) >= (THREAD_OFFSET - 3)) { terminated = true; } // Save program counter and accumulator to memory pc %= THREAD_OFFSET; memory[THREAD_OFFSET * ThreadNumber + (THREAD_OFFSET - 1)] = pc; directExecution.SaveDWORD(THREAD_OFFSET * ThreadNumber + (THREAD_OFFSET - 3), Accumilator.Instance.Value); }
/// <summary> /// Compile program /// </summary> public void Compile() { // Write first lines builder.AppendLine("using System;"); builder.AppendLine(); builder.AppendLine("namespace UVSim"); builder.AppendLine("{"); builder.AppendLine("\tclass Program"); builder.AppendLine("\t{"); builder.AppendLine("\t\tstatic void Main(string[] args)"); builder.AppendLine("\t\t{"); builder.AppendLine("\t\t\tint acc = 0;"); try { // Make run for (int i = 0; i < Thread.THREAD_OFFSET; i++) { // Get the opCode int opCode = memory[i] / 100; int operand = memory[i] % 100; // Check is this instruction is variable or number then miss it if (isNumber.Contains(i) || isVariable.ContainsKey(i) || isVariable.ContainsKey(i - 1)) { continue; } // Switch statment to execute the targeted instruction based on the opCode. switch (opCode) { // READ case 10: builder.AppendLine($"Label{i}:\tConsole.Write(\"Enter an integer: \");"); builder.AppendLine($"\t\t\t{GetVar(operand)} = int.Parse(Console.ReadLine());"); break; // WRITE case 11: builder.AppendLine($"Label{i}:\tConsole.WriteLine({GetVar(operand)});"); break; // READ - INDIRECT case 12: i++; operand = memory[i]; builder.AppendLine($"Label{i}:\tConsole.Write(\"Enter an integer: \");"); builder.AppendLine($"\t\t\t{GetVar(operand)} = int.Parse(Console.ReadLine());"); break; // WRITE - INDIRECT case 13: i++; operand = memory[i]; builder.AppendLine($"Label{i}:\tConsole.WriteLine({GetVar(operand)});"); break; // LOAD case 20: builder.AppendLine($"Label{i}:\tacc = {GetVar(operand)};"); break; // STORE case 21: builder.AppendLine($"Label{i}:\t{GetVar(operand)} = acc;"); break; // LOAD - INDIRECT case 22: i++; operand = memory[i]; builder.AppendLine($"Label{i}:\tacc = {GetVar(operand)};"); break; // STORE - INDIRECT case 23: i++; operand = memory[i]; builder.AppendLine($"Label{i}:\t{GetVar(operand)} = acc;"); break; // ADD case 30: builder.AppendLine($"Label{i}:\tacc+= {GetVar(operand)};"); break; // SUBTRACT case 31: builder.AppendLine($"Label{i}:\tacc-= {GetVar(operand)};"); break; // DIVIDE case 32: builder.AppendLine($"Label{i}:\tacc/= {GetVar(operand)};"); break; // MULTIPLY case 33: builder.AppendLine($"Label{i}:\tacc*= {GetVar(operand)};"); break; // ADD - INDIRECT case 34: i++; operand = memory[i]; builder.AppendLine($"Label{i}:\tacc+= {GetVar(operand)};"); break; // SUBTRACT - INDIRECT case 35: i++; operand = memory[i]; builder.AppendLine($"Label{i}:\tacc-= {GetVar(operand)};"); break; // DIVIDE - INDIRECT case 36: i++; operand = memory[i]; builder.AppendLine($"Label{i}:\tacc/= {GetVar(operand)};"); break; // MULTIPLY - INDIRECT case 37: i++; operand = memory[i]; builder.AppendLine($"Label{i}:\tacc*= {GetVar(operand)};"); break; // BRANCH case 40: builder.AppendLine($"Label{i}:\tgoto Label{operand}"); break; // BRANCHNEG case 41: builder.AppendLine($"Label{i}:\tif (acc < 0) goto Label{operand};"); break; // BRANCHZERO case 42: builder.AppendLine($"Label{i}:\tif (acc == 0) goto Label{operand};"); break; // HALT case 43: builder.AppendLine($"Label{i}:\treturn;"); break; // BRANCH - INDIRECT case 44: i++; operand = memory[i]; builder.AppendLine($"Label{i}:\tgoto Label{operand}"); break; // BRANCHNEG - INDIRECT case 45: i++; operand = memory[i]; builder.AppendLine($"Label{i}:\tif (acc < 0) goto Label{operand};"); break; // BRANCHZERO - INDIRECT case 46: i++; operand = memory[i]; builder.AppendLine($"Label{i}:\tif (acc == 0) goto Label{operand};"); break; // REMINDER case 50: builder.AppendLine($"Label{i}:\tacc%= {GetVar(operand)};"); break; // EXPONENTIATION case 51: builder.AppendLine($"Label{i}:\tacc = (int)Math.Pow(acc, {GetVar(operand)});"); break; // REMINDER - INDIRECT case 52: i++; operand = memory[i]; builder.AppendLine($"Label{i}:\tacc%= {GetVar(operand)};"); break; // EXPONENTIATION - INDIRECT case 53: i++; operand = memory[i]; builder.AppendLine($"Label{i}:\tacc = (int)Math.Pow(acc, {GetVar(operand)});"); break; // Defult. default: break; } } window.Console.WriteLine("Compilation successful!"); } catch (Exception ex) { window.Error(ex.Message); } // Iterate through all variables and initialize them foreach (int key in isVariable.Keys) { int location = key; builder.Insert(113, $"\t\t\tint {isVariable[key]} = {ComposeDWORD(ref location)};\r\n"); } builder.AppendLine("\t\t}"); builder.AppendLine("\t}"); builder.AppendLine("}"); }