Exemple #1
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");
                    //}
                }
            });
        }