Ejemplo n.º 1
0
        public static void load(string fileName, Memory toLoad)
        {
            Txt txtFile = new Txt(fileName);

            string[] lines = txtFile.Read();

            if (lines != null)
            {
                int counter = 0;

                for (int i = 0; i < lines.Length; i++)
                {
                    int parsed = stringToInt(lines[i]);
                    toLoad.SetL0((short)counter, (short)parsed);
                    counter++;
                }

                Console.WriteLine("Loaded " + counter + " words.");
                Console.WriteLine("Enter run to start loaded program.");
            }
        }
Ejemplo n.º 2
0
        public static void assembleFile(string param0)
        {
            asmFile     = new Txt(param0);
            asmFileName = param0;
            symbolTable = new Dictionary <string, symbol>();
            goToTable   = new Dictionary <string, symbol>();

            string[] fileLines = asmFile.Read();

            if (fileLines != null)
            {
                asmLines = new List <string>();
                for (int i = 0; i < fileLines.Length; i++)
                {
                    asmLines.Add(fileLines[i]);
                }
            }
            else
            {
                return;
            }

            Txt output = null;

            if (param0.Contains("."))
            {
                output = new Txt(param0.Substring(0, param0.IndexOf('.')) + ".bin");
            }
            else
            {
                output = new Txt(param0 + ".bin");
            }


            List <int> toWrite = new List <int>();

            for (int i = 0; i < fileLines.Length; i++)
            {
                string[] line = fileLines[i].Split(' ');

                if (line.Length > 0)
                {
                    if (line[0][0] == '$')
                    {
                        symbolTable.Add(line[0].Trim(), new symbol(line[0].Substring(1)));
                    }
                    else if (line[0][0] == '@')
                    {
                        goToTable.Add(line[0].Trim(), new symbol(line[0].Substring(1), toWrite.Count));
                    }
                    else
                    {
                        Cpu.instructions current = Cpu.stringToInstructions(line[0]);
                        int toGrab = Cpu.instructionsParamsLength(current);

                        toWrite.Add((int)current);

                        if (line.Length == toGrab + 1)
                        {
                            for (int j = 1; j <= toGrab; j++)
                            {
                                if (current == Cpu.instructions.setL0 || current == Cpu.instructions.readL0 || current == Cpu.instructions.write)
                                {
                                    if (j == 1)
                                    {
                                        int pointer = getSymbolPointer(line[j]);
                                        if (pointer != -1)
                                        {
                                            toWrite.Add(pointer);
                                        }
                                        else
                                        {
                                            Console.WriteLine("Error @" + i + " line = " + line[0] + " " + line[1]);
                                            Console.WriteLine("Problem Loading Symbol.");
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        int parsed = stringToInt(line[j]);
                                        toWrite.Add(stringToInt(line[j]));
                                    }
                                }
                                else if (current == Cpu.instructions.jump || current == Cpu.instructions.jzr || current == Cpu.instructions.jnz)
                                {
                                    int pointer = getGoToPointer(line[1]);
                                    if (pointer != -1)
                                    {
                                        toWrite.Add(pointer);
                                    }
                                    else
                                    {
                                        Console.WriteLine("Error @" + i + " line = " + line[0] + " " + line[1]);
                                        Console.WriteLine("Problem Loading GoTo.");
                                        return;
                                    }
                                }
                                else
                                {
                                    int parsed = stringToInt(line[j]);
                                    if (parsed != -1)
                                    {
                                        toWrite.Add(parsed);
                                    }
                                    else
                                    {
                                        Console.WriteLine("Error @" + i + " line[0] = " + line[0]);
                                        Console.WriteLine("Problem Parsing Line");
                                        return;
                                    }
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("Error @" + i + " line[0] = " + line[0]);
                            Console.WriteLine("No Enough Instruction Params For: " + current);
                            return;
                        }
                    }
                }
            }

            if (toWrite.Count < symbol.tablePointer)
            {
                List <string> outputLines = new List <string>();
                for (int i = 0; i < toWrite.Count; i++)
                {
                    outputLines.Add(toWrite[i] + "");
                }

                output.Write(outputLines);
            }
            else
            {
                Console.WriteLine("Error Program Size > symbolTablePointer");
                Console.WriteLine("Program Size: " + toWrite.Count);
                return;
            }
        }
Ejemplo n.º 3
0
        public static void edit(string param0)
        {
            file     = new Txt(param0);
            fileName = param0;

            string[] fileLines = file.Read();

            if (fileLines == null)
            {
                lines = new List <string>();
            }
            else
            {
                lines = new List <string>();
                for (int i = 0; i < fileLines.Length; i++)
                {
                    lines.Add(fileLines[i]);
                }
            }

            if (param0 == "help")
            {
                lines = helpTXT;
            }

            scrollPos = 0;


            bool run = true;

            while (run)
            {
                printLines();

                string toProcess = Console.ReadLine();

                string[] sections = toProcess.Split(' ');

                switch (sections[0].ToLower())
                {
                case "save":
                    file.Write(lines);
                    break;

                case "del":
                    lines.RemoveAt(scrollPos);
                    break;

                case "g":
                    int lineNumber = -1;

                    if (int.TryParse(sections[1], out lineNumber))
                    {
                        if (lines.Count() < lineNumber)
                        {
                            scrollPos = lines.Count();
                        }
                        else
                        {
                            scrollPos = lineNumber;
                        }
                    }

                    break;

                case "u":
                    if (scrollPos > 0)
                    {
                        scrollPos--;
                    }
                    break;

                case "clr":
                    if (lines.Count > scrollPos)
                    {
                        lines[scrollPos] = string.Empty;
                    }
                    break;

                case "d":
                    if (scrollPos < lines.Count - 1)
                    {
                        scrollPos++;
                    }
                    break;

                case "add":
                    List <string> newLines = new List <string>();
                    for (int i = 0; i < lines.Count; i++)
                    {
                        if (i == scrollPos)
                        {
                            newLines.Add("");
                        }
                        newLines.Add(lines[i]);
                    }

                    lines = newLines;
                    break;

                case "exit":
                    run = false;
                    break;

                default:
                    if (lines.Count() > scrollPos)
                    {
                        lines[scrollPos] = toProcess;
                    }
                    else
                    {
                        lines.Add(toProcess);
                    }
                    scrollPos++;
                    break;
                }
            }
        }