Пример #1
0
        public void EngineRun_LDA170()
        {
            EngineProc engine = new EngineProc();

            engine.Init(RP_LDA170, _decoder);
            engine.Run();
        }
Пример #2
0
        public void EngineRun_FIB5()
        {
            EngineProc engine = new EngineProc();

            engine.Init(RP_FIB, _decoder);
            engine.Run();
        }
Пример #3
0
        public void EngineRun_HLT()
        {
            EngineProc engine = new EngineProc();

            engine.Init(RP_HLT, _decoder);
            engine.Run();
        }
Пример #4
0
        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);
        }
Пример #5
0
        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);
        }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }
Пример #8
0
        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);
        }
Пример #9
0
        public void Infinite_Loop_Test()
        {
            List <string> program = new List <string>()
            {
                "00001111",
                "01000000",      // JEQ - shouldn't jump, if it does, it will infinite loop
                "11100000",
                "11110000",      // JEQ should jump
                "00000000",      // should never be hit, A=RAM[14]
                "00000000",
                "00000000",
                "00000000",
                "00000000",
                "00000000",
                "00000000",
                "00000000",
                "00000000",
                "00000000",
                "00000000",
                "11111111",
            };

            EngineProc engine = new EngineProc();

            bool caught = false;

            engine.Init(new RAMProgram(program), _decoder);
            try
            {
                engine.Run();
            }
            catch (EngineRuntimeException)
            {
                caught = true;
            }

            if (!caught)
            {
                Assert.Fail();
            }
        }
Пример #10
0
        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");
                    //}
                }
            });
        }
Пример #11
0
        public ActionResult Post([FromBody] EmulatorPacket emulatorPacket)
        {
            EntityEntry <EmulationSessionMap> session;

            try
            {
                session = _sap1EmuContext.Add <EmulationSessionMap>(new EmulationSessionMap
                {
                    EmulationID      = Guid.NewGuid(),
                    ConnectionID     = null,
                    SessionStart     = DateTime.UtcNow,
                    EmulatorId       = _emulatorId,
                    StatusId         = StatusFactory.GetStatus(StatusType.Pending).Id,
                    InstructionSetId = _instructionSets[emulatorPacket.SetName]
                });
                _sap1EmuContext.CodeSubmissions.Add(new CodeSubmission()
                {
                    EmulationID = session.Entity.EmulationID,
                    Code        = emulatorPacket.CodeList,
                });
                _sap1EmuContext.SaveChanges();
            }
            catch (KeyNotFoundException)
            {
                return(BadRequest(
                           // TODO: Set Type to supported_sets url
                           new ProblemDetails()
                {
                    Status = StatusCodes.Status400BadRequest,
                    Type = "",
                    Title = "Invalid SetName...",
                    Detail = $"The Instruction Set `{emulatorPacket.SetName}` does not exist.",
                    Instance = HttpContext.Request.Path
                }
                           ));
            }


            try
            {
                List <string> compiled_binary = Assemble.Parse(emulatorPacket.CodeList, emulatorPacket.SetName);
                RAMProgram    rmp             = new RAMProgram(compiled_binary);

                EngineProc engine = new EngineProc();
                engine.Init(rmp, _decoder, emulatorPacket.SetName);
                engine.Run();

                session.Entity.SessionEnd = DateTime.UtcNow;
                session.Entity.StatusId   = StatusFactory.GetStatus(StatusType.Ok).Id;

                _sap1EmuContext.SaveChanges();
                return(Ok(engine.FrameStack()));
            }
            catch (ParseException pe)
            {
                session.Entity.SessionEnd = DateTime.UtcNow;
                session.Entity.StatusId   = StatusFactory.GetStatus(StatusType.ParsingError).Id;


                if (pe.InnerException != null)
                {
                    string msg = (pe.Message + " " + pe.InnerException.Message);
                    _sap1EmuContext.ErrorLog.Add(new ErrorLog()
                    {
                        EmulationID = session.Entity.EmulationID,
                        ErrorMsg    = msg
                    });

                    _sap1EmuContext.SaveChanges();
                    return(BadRequest(msg));
                }
                else
                {
                    _sap1EmuContext.ErrorLog.Add(new ErrorLog()
                    {
                        EmulationID = session.Entity.EmulationID,
                        ErrorMsg    = pe.Message
                    });

                    _sap1EmuContext.SaveChanges();
                    return(BadRequest(pe.Message));
                }
            }
            catch (EngineRuntimeException ere)
            {
                session.Entity.SessionEnd = DateTime.UtcNow;
                session.Entity.StatusId   = StatusFactory.GetStatus(StatusType.EmulationError).Id;

                if (ere.InnerException != null)
                {
                    string msg = (ere.Message + " " + ere.InnerException.Message);
                    _sap1EmuContext.ErrorLog.Add(new ErrorLog()
                    {
                        EmulationID = session.Entity.EmulationID,
                        ErrorMsg    = msg
                    });

                    _sap1EmuContext.SaveChanges();
                    return(BadRequest(ere.Message + " " + ere.InnerException.Message));
                }
                else
                {
                    _sap1EmuContext.ErrorLog.Add(new ErrorLog()
                    {
                        EmulationID = session.Entity.EmulationID,
                        ErrorMsg    = ere.Message
                    });

                    _sap1EmuContext.SaveChanges();
                    return(BadRequest(ere.Message));
                }
            }
            catch (Exception e)
            {
                session.Entity.SessionEnd = DateTime.UtcNow;
                session.Entity.StatusId   = StatusFactory.GetStatus(StatusType.SystemError).Id;

                _sap1EmuContext.ErrorLog.Add(new ErrorLog()
                {
                    EmulationID = session.Entity.EmulationID,
                    ErrorMsg    = e.Message
                });

                _sap1EmuContext.SaveChanges();
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }
Пример #12
0
        public IActionResult StartEmulation([FromBody] SAP2CodePacket sap2CodePacket)
        {
            EmulationSessionMap session = null;

            try
            {
                session = _sap1EmuContext.EmulationSessionMaps
                          .Single(esm => esm.EmulationID == sap2CodePacket.EmulationID);
            }
            catch (InvalidOperationException)
            {
                return(BadRequest(sap2CodePacket.EmulationID));
            }


            if (session.StatusId != (int)StatusType.Pending)
            {
                string message = string.Empty;
                switch ((StatusType)session.StatusId)
                {
                case StatusType.Ok:
                    message = "Emulation Complete: Please use 'GET: /session/{id}/recall' instead";
                    break;

                case StatusType.SQLError:
                case StatusType.ParsingError:
                case StatusType.EmulationError:
                case StatusType.SystemError:
                    message = "Emulation Errored: Please create new session";
                    break;

                case StatusType.InProgress:
                    message = "Emulation In Progress: Please use 'POST: /{id}/resume' instead";
                    break;

                default:
                    message = "Unknown Host Error";
                    break;
                }

                return(BadRequest(
                           new
                {
                    EmulationID = sap2CodePacket.EmulationID,
                    Status = StatusFactory.GetStatus((StatusType)session.StatusId),
                    Message = message
                }
                           ));
            }


            // This will save the plain code
            try
            {
                _sap1EmuContext.CodeSubmissions.Add(
                    new CodeSubmission()
                {
                    EmulationID = session.EmulationID,
                    Code        = sap2CodePacket.Code
                }
                    );
                _sap1EmuContext.SaveChanges();
            }
            catch (Exception e)
            {
                _sap1EmuContext.ErrorLog.Add(
                    new ErrorLog
                {
                    EmulationID = session.EmulationID,
                    ErrorMsg    = "NON-FATAL SQL ERROR:\t" + e.Message + (e.InnerException != null ? "\t" + e.InnerException.Message : "")
                }
                    );
                //session.SessionEnd = DateTime.UtcNow;
                session.StatusId = (int)StatusType.SQLError;

                _sap1EmuContext.SaveChanges();
            }


            SAP2BinaryPacket sap2BinaryPacket;

            // Assemble
            try
            {
                sap2BinaryPacket = new SAP2BinaryPacket()
                {
                    EmulationID = sap2CodePacket.EmulationID,
                    Code        = Assemble.Parse((List <string>)sap2CodePacket.Code),
                    SetName     = sap2CodePacket.SetName
                };
            }
            catch (ParseException pe)
            {
                session.StatusId   = (int)StatusType.ParsingError;
                session.SessionEnd = DateTime.UtcNow;

                string errorMsg = pe.Message + (pe.InnerException != null ? "\n" + pe.InnerException.Message : "");
                _sap1EmuContext.ErrorLog.Add(
                    new ErrorLog
                {
                    EmulationID = session.EmulationID,
                    ErrorMsg    = errorMsg
                }
                    );

                _sap1EmuContext.SaveChanges();
                return(BadRequest(
                           new
                {
                    EmulationID = sap2CodePacket.EmulationID,
                    Status = StatusFactory.GetStatus((StatusType)session.StatusId),
                    Message = errorMsg
                }
                           ));
            }


            // Save Binary
            try
            {
                _sap1EmuContext.CodeSubmissionsBinary.Add(
                    new CodeSubmission()
                {
                    EmulationID = session.EmulationID,
                    Code        = sap2BinaryPacket.Code
                }
                    );
                _sap1EmuContext.SaveChanges();
            }
            catch (Exception e)
            {
                _sap1EmuContext.ErrorLog.Add(
                    new ErrorLog
                {
                    EmulationID = session.EmulationID,
                    ErrorMsg    = "NON-FATAL SQL ERROR:\t" + e.Message + (e.InnerException != null ? "\t" + e.InnerException.Message : "")
                }
                    );

                session.SessionEnd = DateTime.UtcNow;
                session.StatusId   = (int)StatusType.SystemError;

                _sap1EmuContext.SaveChanges();
            }


            // Run Emulator
            try
            {
                RAMProgram rmp = new RAMProgram((List <string>)sap2BinaryPacket.Code);

                EngineProc engine = new EngineProc();
                engine.Init(rmp, _decoder, sap2BinaryPacket.SetName);
                engine.Run();

                session.StatusId   = (int)StatusType.Ok;
                session.SessionEnd = DateTime.UtcNow;
                _sap1EmuContext.SaveChanges();

                return(Ok(engine.FrameStack()));
            }
            catch (EngineRuntimeException ere)
            {
                session.StatusId   = (int)StatusType.EmulationError;
                session.SessionEnd = DateTime.UtcNow;

                string errorMsg = ere.Message + (ere.InnerException != null ? "\n" + ere.InnerException.Message : "");
                _sap1EmuContext.ErrorLog.Add(
                    new ErrorLog
                {
                    EmulationID = session.EmulationID,
                    ErrorMsg    = errorMsg
                }
                    );
                _sap1EmuContext.SaveChanges();
                return(BadRequest(
                           new
                {
                    EmulationID = sap2CodePacket.EmulationID,
                    Status = StatusFactory.GetStatus((StatusType)session.StatusId),
                    Message = errorMsg
                }
                           ));
            }
        }