public void Test_LDA_PROG_2() { string expectedResult = "10101010"; List <string> program = new List <string>() { "00001111", "11100000", "11110000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "10101010", }; EngineProc engine = new EngineProc(); engine.Init(new RAMProgram(program), _decoder); engine.Run(); string output = engine.GetOutputReg(); Assert.AreEqual(expectedResult, output); }
public void Test_JIC_PROG_1() { string expectedResult = "00000000"; List <string> program = new List <string>() { "00001111", // LDA F "00011110", // ADD E "10010100", // JIC 4 "00001101", // LDA D (should miss) "11100000", "11110000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "10101010", "00000001", "11111111", }; EngineProc engine = new EngineProc(); engine.Init(new RAMProgram(program), _decoder); engine.Run(); string output = engine.GetOutputReg(); Assert.AreEqual(expectedResult, output); }
public void Test_JLT_PROG_1() { string expectedResult = "11111111"; List <string> program = new List <string>() { "00001111", "01110011", // Should not jump, 1 > 0 "00001110", "11100000", "11110000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "11111111", "00000001" }; EngineProc engine = new EngineProc(); engine.Init(new RAMProgram(program), _decoder); engine.Run(); string output = engine.GetOutputReg(); Assert.AreEqual(expectedResult, output); }
public void Test_JNQ_PROG_1() { string expectedResult = "00000001"; List <string> program = new List <string>() { "00001111", "01100011", "00001110", // This LDA should be skipped "11100000", "11110000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "11111111", // should never be loaded into A "00000001" }; EngineProc engine = new EngineProc(); engine.Init(new RAMProgram(program), _decoder); engine.Run(); string output = engine.GetOutputReg(); Assert.AreEqual(expectedResult, output); }
public void Test_JEQ_PROG_1() { string expectedResult = "00000000"; List <string> program = new List <string>() { "00001111", "01010000", // JEQ - shouldn't jump, if it does, it will infinite loop "00001101", "01010101", // JEQ should jump "00001110", // should never be hit, A=RAM[14] "11100000", "11110000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "00000000", "10101010", "11111111", }; EngineProc engine = new EngineProc(); engine.Init(new RAMProgram(program), _decoder); engine.Run(); string output = engine.GetOutputReg(); Assert.AreEqual(expectedResult, output); }
public static void Main(string[] args) { IDecoder _decoder = new InstructionDecoder(); _ = Parser.Default.ParseArguments <Options>(args) .WithParsed(o => { List <string> source_file_contents = new List <string>();; FileType fileType = FileType.B; if (!string.IsNullOrEmpty(o.SourceFile)) { if (!File.Exists(o.SourceFile)) { Console.Error.WriteLine($"SAP1EMU: error: {o.SourceFile}: No such file"); Console.Error.WriteLine($"SAP1EMU: fatal error: no input file"); Console.Error.WriteLine("emulation terminated"); CheckEnvAndExit(); } else // Check if file is valid { source_file_contents = new List <string>(File.ReadAllLines(o.SourceFile)); int loc = source_file_contents.Count; if (o.SourceFile.Length >= 3) { string ftype = o.SourceFile.Substring(o.SourceFile.Length - 2, 2); if (ftype == ".s") { fileType = FileType.S; } else if (ftype == ".b") { fileType = FileType.B; } else { Console.Error.WriteLine($"SAP1EMU: error: {o.SourceFile}: Invalid file extension: Must be <FileName>.s or <FileName>.b"); Console.Error.WriteLine($"SAP1EMU: fatal error: no valid input file"); Console.Error.WriteLine("emulation terminated."); CheckEnvAndExit(); } } if (loc == 0) { Console.Error.WriteLine($"SAP1EMU: error: {o.SourceFile}: File is empty"); Console.Error.WriteLine($"SAP1EMU: fatal error: no valid input file"); Console.Error.WriteLine("emulation terminated"); CheckEnvAndExit(); } if (loc > 16) { Console.Error.WriteLine($"SAP1EMU: error: {o.SourceFile}: invalid file: Contains more than 16 lines of code."); Console.Error.WriteLine($"SAP1EMU: fatal error: no valid input file"); Console.Error.WriteLine("emulation terminated"); CheckEnvAndExit(); } } if (!string.IsNullOrEmpty(o.FOframe)) { if (o.FOframe.ToLower() != "no-format" && o.FOframe.ToLower() != "std") { Console.Error.WriteLine($"SAP1EMU: warning: {o.SourceFile}: invalid format argument {o.FOframe}: Defaulting to \"std\"."); o.FOframe = "std"; } } if (o.InstructionSetName.ToLower() != "sap1emu" && o.InstructionSetName.ToLower() != "malvino" && o.InstructionSetName.ToLower() != "beneater") { Console.Error.WriteLine($"SAP1EMU: warning: {o.InstructionSetName}: invalid argument: Defaulting to \"SAP1Emu\"."); o.InstructionSetName = "SAP1Emu"; } List <string> compiled_binary = null; if (fileType == FileType.S) { try { compiled_binary = Assemble.Parse(source_file_contents, o.InstructionSetName); } catch (ParseException pe) { //Console.SetOut(new StreamWriter(Console.OpenStandardOutput())); //Console.SetError(new StreamWriter(Console.OpenStandardError())); var tempColor = Console.ForegroundColor; if (Console.BackgroundColor == ConsoleColor.Red) { Console.ForegroundColor = ConsoleColor.Cyan; } else { Console.ForegroundColor = ConsoleColor.Red; } Console.Error.WriteLine($"SAP1ASM: fatal error: " + pe.Message + " " + pe.InnerException.Message); Console.ForegroundColor = tempColor; Console.Error.WriteLine("assembly terminated"); Console.Error.Flush(); CheckEnvAndExit(); } } else { compiled_binary = source_file_contents; } RAMProgram rmp = new RAMProgram(compiled_binary); EngineProc engine = new EngineProc(); //StringBuilder sb_out = new StringBuilder(); //TextWriter writer_out = new StringWriter(sb_out); //Console.SetOut(writer_out); //StringBuilder sb_error = new StringBuilder(); //TextWriter writer_error = new StringWriter(sb_error); //Console.SetError(writer_error); engine.Init(rmp, _decoder, o.InstructionSetName); try { engine.Run(); } catch (EngineRuntimeException ere) { var tempColor = Console.ForegroundColor; if (Console.BackgroundColor == ConsoleColor.Red) { Console.ForegroundColor = ConsoleColor.Cyan; } else { Console.ForegroundColor = ConsoleColor.Red; } //Console.SetOut(new StreamWriter(Console.OpenStandardOutput())); //Console.SetError(new StreamWriter(Console.OpenStandardError())); Console.Error.WriteLine($"SAP1EMU: fatal error: " + ere.Message); Console.ForegroundColor = tempColor; Console.Error.WriteLine("emulation terminated"); Console.Error.Flush(); CheckEnvAndExit(); } string engine_output = "************************************************************\n" + "Final Output Register Value: " + engine.GetOutputReg() + "\n************************************************************\n\n"; List <Frame> FrameStack = engine.FrameStack(); if (o.fframe) { engine_output += "\n" + engine.FinalFrame(); } else if (o.Fframe) { StringBuilder sb = new StringBuilder(); StringWriter fw = new StringWriter(sb); foreach (Frame frame in FrameStack) { fw.WriteLine(frame.ToString()); } fw.Flush(); engine_output += "\n" + sb.ToString(); } else if (o.FOframe != null) { engine_output = null; // Clear the output StringBuilder sb = new StringBuilder(); StringWriter fw = new StringWriter(sb); foreach (Frame frame in FrameStack) { if (frame.TState == 6) { if (o.FOframe.ToLower() == "std") { fw.WriteLine(frame.OutputRegister()); } else if (o.FOframe.ToLower() == "no-format") { string temp = frame.OReg; if (string.IsNullOrEmpty(temp)) { temp = "00000000"; } fw.WriteLine(temp); } } } fw.Flush(); engine_output += sb.ToString(); } var standardOutput = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }; Console.SetOut(standardOutput); var standardError = new StreamWriter(Console.OpenStandardError()) { AutoFlush = true }; Console.SetError(standardError); //string stdout = sb_out.ToString(); //string stderror = sb_error.ToString(); File.WriteAllText(o.OutputFile, engine_output); // Start the Single Stepping Debug Session if Debug Flag is set Debug_Proc(o, source_file_contents, FrameStack); Console.Out.WriteLine("Debug Session Complete"); //foreach(Frame f in FrameStack) //{ // foreach(string s in f.RAM) // { // Console.Out.WriteLine(s); // } // Console.Out.WriteLine("\n"); //} } }); }