Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            string[] recursiveFileSearch(string pth)
            {
                List <string> files = new List <string>();

                foreach (string s in Directory.EnumerateFiles(pth))
                {
                    try
                    {
                        files.Add(s);
                    }
                    catch
                    {
                    }
                }
                foreach (string s in Directory.EnumerateDirectories(pth))
                {
                    try
                    {
                        files.AddRange(recursiveFileSearch(s));
                    }
                    catch
                    {
                    }
                }
                return(files.ToArray());
            }

            //CHelper.ASCIIArt("Hello World!", Console.ReadLine()));
            Operation op;
            string    path;
            string    pathOut;
            string    csvPath;

            if (args.Length >= 4)
            {
                path    = args[0];
                pathOut = args[1];
                csvPath = args[2];
                op      = (Operation)Enum.Parse(typeof(Operation), args[3], true);
            }
            else
            {
                Console.WriteLine(CHelper.ASCIIArt("PAssembler", CHelper.LoadASCIIFont(@"C:\Users\PeterHusman\Documents\FontFile.json"))); //, @"C:\Users\PeterHusman\Documents\FontFile.json"));
                                                                                                                                           //               Console.WriteLine(@"                                  _     _
                                                                                                                                           //    /\                          | |   | |
                                                                                                                                           //   /  \   ___ ___  ___ _ __ ___ | |__ | | ___ _ __
                                                                                                                                           //  / /\ \ / __/ __|/ _ \ '_ ` _ \| '_ \| |/ _ \ '__|
                                                                                                                                           // / ____ \\__ \__ \  __/ | | | | | |_) | |  __/ |
                ///_/    \_\___/___/\___|_| |_| |_|_.__/|_|\___|_|  ");
                int choice = CHelper.SelectorMenu("Please select an action.", new string[] { "Assemble", "Dissassemble", "Pseudoassemble", "Pseudoassemble and assemble" }, true, ConsoleColor.Yellow, ConsoleColor.Gray, ConsoleColor.Magenta);
                Console.WriteLine();
                string[] recFiles = recursiveFileSearch($@"C:\Users\{Environment.UserName}");
                path    = CHelper.RequestInput("What is the input file path?", true, ConsoleColor.Yellow, ConsoleColor.Gray, recFiles);
                pathOut = CHelper.RequestInput("What is the output file path?", true, ConsoleColor.Yellow, ConsoleColor.Gray, recFiles);
                csvPath = CHelper.RequestInput("What is the .csv file path?", true, ConsoleColor.Yellow, ConsoleColor.Gray, recFiles);
                op      = (Operation)choice;
            }
            var csv = GetOpCodesFromCSV(csvPath);

            switch (op)
            {
            case Operation.assemble:
                Assemble(path, csv, pathOut, true);
                break;

            case Operation.dissassemble:
                Dissassemble(path, csv, pathOut);
                break;

            case Operation.passemble:
                HelperToAsm(path, pathOut);
                break;

            case Operation.fullassemble:
                HelperToAsm(path, pathOut);
                Assemble(pathOut, csv, pathOut, true);
                break;
            }
            if (args.Length < 4)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Finished");
                Console.ReadKey(true);
            }
            //var csv2 = GetOpCodesFromCSV(csvPath);
            //HelperToAsm(path, pathOut);
            //Assemble(pathOut, csv, pathOut, true);

            //string[] lines = File.ReadAllLines(path);
            //Dictionary<string, uint> labels = new Dictionary<string, uint>();
            //List<byte> output = new List<byte>();
            //for(int i = 0; i < lines.Length; i++)
            //{
            //    lines[i] = lines[i].Split(';')[0];
            //    lines[i] = lines[i].Replace("r", "");
            //    if(lines[i].EndsWith(":"))
            //    {
            //        labels.Add(lines[i].Remove(lines[i].Length-1,1), (uint)i);
            //    }
            //}
            //for(int i = 0; i < lines.Length; i++)
            //{

            //}
            //File.WriteAllBytes(pathOut, output.ToArray());
            //Console.ReadKey();
        }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            string[] recursiveFileSearch(string pth)
            {
                List <string> files = new List <string>();

                foreach (string s in Directory.EnumerateFiles(pth))
                {
                    try
                    {
                        files.Add(s);
                    }
                    catch
                    {
                    }
                }
                foreach (string s in Directory.EnumerateDirectories(pth))
                {
                    try
                    {
                        files.AddRange(recursiveFileSearch(s));
                    }
                    catch
                    {
                    }
                }
                return(files.ToArray());
            }

            ushort flipEndian(ushort input)
            {
                byte[] inst9 = MemoryMarshal.AsBytes <ushort>(new ushort[] { input }).ToArray();
                inst9 = inst9.Reverse().ToArray();
                return(MemoryMarshal.Cast <byte, ushort>(inst9)[0]);
            }

            ushort flipEndianBytes(byte[] input)
            {
                byte[] inst9 = input.ToArray();
                inst9 = inst9.Reverse().ToArray();
                return(MemoryMarshal.Cast <byte, ushort>(inst9)[0]);
            }

            ushort[] registers = new ushort[0x100];
            registers[0] = 0x0000;
            Span <byte> registerSpan = MemoryMarshal.AsBytes(registers.AsSpan());
            string      filePath;
            bool        debug = false;

            if (args.Length > 0)
            {
                filePath = args[0];
                if (args.Length > 1)
                {
                    debug = args[1] == "debug";
                }
            }
            else
            {
                Console.WriteLine(CHelper.ASCIIArt("PEmulator", CHelper.LoadASCIIFont(@"C:\Users\PeterHusman\Documents\FontFile.json")));
                filePath = CHelper.RequestInput("Please enter the file path of the program to emulate.", true, ConsoleColor.Yellow, ConsoleColor.Gray, recursiveFileSearch($@"C:\Users\{Environment.UserName}"));
                debug    = CHelper.SelectorMenu("Please choose an extra parameter.", new string[] { "None", "Debug" }, true, ConsoleColor.Yellow, ConsoleColor.Gray, ConsoleColor.Magenta) == 1;
                Console.WriteLine();
            }
            Console.ForegroundColor = ConsoleColor.Gray;
            if (debug)
            {
                Console.WriteLine("Debug mode entered. Enter a blank line to step through the program and enter Q followed by a register number to query the value of the register.");
            }
            byte[]    program   = File.ReadAllBytes(filePath);
            Random    rand      = new Random();
            MemoryMap memoryMap = new MemoryMap(program);

            while (memoryMap[0x0000] == 0)
            {
                if (debug)
                {
                    string s = "a";
                    do
                    {
                        s = Console.ReadLine();
                        if (s.ToLower().StartsWith("q"))
                        {
                            Console.WriteLine($"r{s.Remove(0, 1)} = {registers[int.Parse(s.Remove(0, 1), System.Globalization.NumberStyles.HexNumber)]:X}");
                        }
                    } while (s != "");
                    Span <byte> instr = memoryMap.GetInstruction(registers[0] * 2 + 0x8000);
                    Console.Write($"{((OpCodes)instr[0]).ToString()} {instr[1]:X2} {instr[2]:X2} {instr[3]:X2}");
                }
                memoryMap[0x0003] = (ushort)(rand.NextDouble() * ushort.MaxValue);
                if (memoryMap[0x0002] != 0)
                {
                    memoryMap[0x0004] = ushort.Parse(Console.ReadLine());
                    memoryMap[0x0002] = 0;
                }
                if (memoryMap[0x0008] != 0)
                {
                    Console.WriteLine(memoryMap[0x0007].ToString("X"));
                    memoryMap[0x0008] = 0;
                }
                if (memoryMap[0x0006] != 0)
                {
                    Console.Write((char)(memoryMap[0x0005]));
                    memoryMap[0x0006] = 0;
                }



                Span <byte> instruction = memoryMap.GetInstruction(registers[0] * 2 + 0x8000);
                switch ((OpCodes)instruction[0])
                {
                case OpCodes.nop:
                    break;

                case OpCodes.add:
                    registers[instruction[1]] = (ushort)(registers[instruction[2]] + registers[instruction[3]]);
                    break;

                case OpCodes.sub:
                    registers[instruction[1]] = (ushort)(registers[instruction[2]] - registers[instruction[3]]);
                    break;

                case OpCodes.mul:
                    registers[instruction[1]] = (ushort)(registers[instruction[2]] * registers[instruction[3]]);
                    break;

                case OpCodes.div:
                    registers[instruction[1]] = (ushort)(registers[instruction[2]] / registers[instruction[3]]);
                    break;

                case OpCodes.mod:
                    registers[instruction[1]] = (ushort)(registers[instruction[2]] % registers[instruction[3]]);
                    break;

                case OpCodes.and:
                    registers[instruction[1]] = (ushort)(registers[instruction[2]] & registers[instruction[3]]);
                    break;

                case OpCodes.orx:
                    registers[instruction[1]] = (ushort)(registers[instruction[2]] | registers[instruction[3]]);
                    break;

                case OpCodes.not:
                    registers[instruction[1]] = (ushort)(~registers[instruction[2]]);
                    break;

                case OpCodes.xor:
                    registers[instruction[1]] = (ushort)(registers[instruction[2]] ^ registers[instruction[3]]);
                    break;

                case OpCodes.jmp:
                    registers[0] = (ushort)(MemoryMarshal.Cast <byte, ushort>(instruction.Slice(2))[0] - 1);
                    break;

                case OpCodes.j*z:
                    if (registers[instruction[1]] == 0)
                    {
                        registers[0] = (ushort)(MemoryMarshal.Cast <byte, ushort>(instruction.Slice(2))[0] - 1);
                    }
                    break;

                case OpCodes.jnz:
                    if (registers[instruction[1]] != 0)
                    {
                        registers[0] = (ushort)(MemoryMarshal.Cast <byte, ushort>(instruction.Slice(2))[0] - 1);
                    }
                    break;

                case OpCodes.set:
                    registers[instruction[1]] = (ushort)(MemoryMarshal.Cast <byte, ushort>(instruction.Slice(2))[0]);
                    break;

                case OpCodes.mov:
                    registers[instruction[1]] = registers[instruction[2]];
                    break;

                case OpCodes.equ:
                    registers[instruction[1]] = Convert.ToUInt16(registers[instruction[2]] == registers[instruction[3]]);
                    break;

                case OpCodes.lth:
                    registers[instruction[1]] = Convert.ToUInt16(registers[instruction[2]] < registers[instruction[3]]);
                    break;

                case OpCodes.leq:
                    registers[instruction[1]] = Convert.ToUInt16(registers[instruction[2]] <= registers[instruction[3]]);
                    break;

                case OpCodes.lod:
                    registers[instruction[1]] = memoryMap[(ushort)(MemoryMarshal.Cast <byte, ushort>(instruction.Slice(2))[0])];
                    break;

                case OpCodes.str:
                    memoryMap[(ushort)(MemoryMarshal.Cast <byte, ushort>(instruction.Slice(2))[0])] = registers[instruction[1]];
                    break;

                case OpCodes.psh:
                    registers[0x20]--;
                    memoryMap[registers[0x20]] = registers[instruction[1]];
                    break;

                case OpCodes.pop:
                    registers[instruction[1]] = memoryMap[registers[0x20]];
                    registers[0x20]++;
                    break;

                case OpCodes.pek:
                    registers[instruction[1]] = memoryMap[registers[0x20] + (ushort)(MemoryMarshal.Cast <byte, ushort>(instruction.Slice(2))[0])];
                    break;

                case OpCodes.cal:
                    registers[0x20]--;
                    memoryMap[registers[0x20]] = registers[0];
                    registers[0] = (ushort)(MemoryMarshal.Cast <byte, ushort>(instruction.Slice(2))[0] - 1);
                    break;

                case OpCodes.ret:
                    registers[0]     = (ushort)(memoryMap[registers[0x20]]);
                    registers[0x20] += (ushort)(MemoryMarshal.Cast <byte, ushort>(instruction.Slice(2))[0] + 1);
                    break;

                case OpCodes.jmi:
                    registers[0] = (ushort)(registers[instruction[3]] - 1);
                    break;

                case OpCodes.jzi:
                    if (registers[instruction[1]] == 0)
                    {
                        registers[0] = (ushort)(registers[instruction[3]] - 1);
                    }
                    break;

                case OpCodes.jni:
                    if (registers[instruction[1]] != 0)
                    {
                        registers[0] = (ushort)(registers[instruction[3]] - 1);
                    }
                    break;

                case OpCodes.sti:
                    memoryMap[registers[instruction[3]]] = registers[instruction[1]];
                    break;

                case OpCodes.ldi:
                    registers[instruction[1]] = (ushort)(memoryMap[registers[instruction[2]] - instruction[3]]);
                    break;

                case OpCodes.cli:
                    registers[0x20]--;
                    memoryMap[registers[0x20]] = registers[0];
                    registers[0] = (ushort)(registers[instruction[3]] - 1);
                    break;

                case OpCodes.shl:
                    registers[instruction[1]] = (ushort)(registers[instruction[2]] << registers[instruction[3]]);
                    break;

                case OpCodes.shr:
                    registers[instruction[1]] = (ushort)(registers[instruction[2]] >> registers[instruction[3]]);
                    break;

                case OpCodes.lpm:
                    registers[instruction[1]] = (ushort)(MemoryMarshal.Cast <byte, ushort>(instruction.Slice(2))[0] * 2 + 0x8000);
                    break;

                default:
                    throw new InvalidOperationException();
                }
                registers[0]++;
            }
            if (args.Length <= 0)
            {
                Console.ReadKey();
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            string[] recursiveFileSearch(string pth)
            {
                List <string> files = new List <string>();

                foreach (string s in Directory.EnumerateFiles(pth))
                {
                    try
                    {
                        files.Add(s);
                    }
                    catch
                    {
                    }
                }
                foreach (string s in Directory.EnumerateDirectories(pth))
                {
                    try
                    {
                        files.AddRange(recursiveFileSearch(s));
                    }
                    catch
                    {
                    }
                }
                return(files.ToArray());
            }

            while (true)
            {
                Console.WriteLine(CHelper.ASCIIArt("PSuite", CHelper.LoadASCIIFont(@"C:\Users\PeterHusman\Documents\FontFile.json")));
                switch (CHelper.SelectorMenu("Please select an action.", new string[] { "Open Assembler", "Open Emulator", "Assemble and emulate a file" }, true, ConsoleColor.Yellow, ConsoleColor.Gray, ConsoleColor.Magenta))
                {
                case 0:
                    Console.Clear();
                    CAAssembler.Program.Main(new string[0]);
                    break;

                case 1:
                    Console.Clear();
                    CAEmulator.Program.Main(new string[0]);
                    break;

                case 2:
                    Console.WriteLine();
                    string[] options    = new string[] { "None", "Debug" };
                    string   extraParam = options[CHelper.SelectorMenu("Choose an extra parameter to provide to the emulator.", options, true, ConsoleColor.Yellow, ConsoleColor.Gray, ConsoleColor.Magenta)];
                    Console.WriteLine();
                    string filePath = CHelper.RequestInput("Please enter the file path of the file to assemble and emulate.", true, ConsoleColor.Yellow, ConsoleColor.Gray, recursiveFileSearch($@"C:\Users\{Environment.UserName}"));
                    CAAssembler.Program.Main(new string[] { filePath, filePath + ".temp", @"C:\Users\PeterHusman\Downloads\Assembly To Machine Code Chart - Sheet1 (4).csv", "fullassemble" });
                    if (extraParam == "None")
                    {
                        CAEmulator.Program.Main(new string[] { filePath + ".temp" });
                    }
                    else
                    {
                        CAEmulator.Program.Main(new string[] { filePath + ".temp", extraParam.ToLower() });
                    }
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Finished");
                    Console.ReadKey(true);
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.Clear();
                    break;
                }
            }
        }