Beispiel #1
0
        private void OptimizeMemoryCalls()
        {
            Utilities.Utilities.VerbouseOut("OPTIMIZER", "Starting SWM optimization", System.ConsoleColor.Yellow);
            int lastSWM = 255;
            int removed = 0;

            for (int i = 1; i <= program.Count; i++)
            {
                IOpcode node = program[i].opcode;
                if (node is Swm)
                {
                    if (node.FastAdd.toInt() == lastSWM)
                    {
                        Utilities.Utilities.VerbouseOut("OPTIMIZER", "Removed SWM on line " + (i + removed), System.ConsoleColor.Yellow);
                        program.Remove(i);
                        removed++;
                    }
                    else
                    {
                        lastSWM = node.FastAdd.toInt();
                    }
                }
                if (node is Jmp || node is Jnc)
                {
                    lastSWM = 255;
                }
            }
        }
        public void PrintCode()
        {
            int length = Count;

            for (int i = 1; i <= length; i++)
            {
                Console.Write(i + ":\t");
                IOpcode line = Get(i).opcode;
                Console.Write(line.Name + "\t");
                if (line.Arg1 != null)
                {
                    Console.Write(line.Arg1 + "\t");
                }
                if (line is Mov)
                {
                    if (line.Arg2 != null)
                    {
                        Console.Write(line.Arg2 + "\t");
                    }
                }
                if (line.FastAdd != null)
                {
                    Console.Write(line.FastAdd.GetValue());
                }
                Console.WriteLine();
            }
        }
Beispiel #3
0
        public byte GetOperandSafe(IOpcode opcode)
        {
            if (!opcode.AddressingType.ParameterSize.HasValue)
            {
                return(0);
            }
            var parameterSize = opcode.AddressingType.ParameterSize.Value;

            State.Parameter       = Helper.CompositeInteger(Memory, State.ProgramCounter, parameterSize);
            State.ProgramCounter += parameterSize;
            return(opcode.AddressingType.GetOperand(this, State.Parameter));
        }
Beispiel #4
0
        private IOpcode GetOpCodeFromCurrentPointer()
        {
            var     opcodeValue = Convert.ToUInt32(new string(LocalComputerMemory.PointerValue().ToString().TakeLast(2).ToArray()));
            IOpcode opcode      = null;

            switch ((OpCode)opcodeValue)
            {
            case OpCode.Adds:
                opcode = new Add();
                break;

            case OpCode.Multiplie:
                opcode = new Multiply();
                break;

            case OpCode.Inputstuff:
                opcode = new Input();
                break;

            case OpCode.OutputStuff:
                opcode = new Output();
                break;

            case OpCode.JumpIfTrue:
                opcode = new JumpIfTrue();
                break;

            case OpCode.JumpIfFalse:
                opcode = new JumpIfFalse();
                break;

            case OpCode.isLessThen:
                opcode = new LessThan();
                break;

            case OpCode.isEqual:
                opcode = new Equal();
                break;

            case OpCode.RelativeBase:
                opcode = new RelativeBase();
                break;

            case OpCode.Exit:
                opcode = new Exit();
                break;

            default:
                break;
            }
            return(opcode);
        }
Beispiel #5
0
 public int Jr(byte flag, byte condition, byte jumpTo, IOpcode opcode)
 {
     if (flag == condition)
     {
         Registers.PC = (ushort)(Registers.PC + (sbyte)jumpTo + 2);
         return(opcode.Cycle);
     }
     else
     {
         Registers.PC += 2;
         return(opcode.VariableCycle);
     }
 }
Beispiel #6
0
        private int ExecuteNextOperation()
        {
            State.Opcode = Memory[State.ProgramCounter++];
            IOpcode handler = OpcodeFactory.GetOpcode(State.Opcode);

            if (handler == null)
            {
                throw new Exception(string.Format("Opcode {0} not implemented!", State.Opcode));
            }
            var operand = GetOperandSafe(handler);

            return(handler.Execute(this, operand));
        }
Beispiel #7
0
 public int Ret(byte flag, byte condition, IOpcode opcode)
 {
     if (flag == condition)
     {
         Registers.PC = Pop();
         return(opcode.Cycle);
     }
     else
     {
         Registers.PC++;
         return(opcode.VariableCycle);
     }
 }
Beispiel #8
0
 public int Jp(byte flag, byte condition, ushort jumpTo, IOpcode opcode)
 {
     if (flag == condition)
     {
         Registers.PC = jumpTo;
         return(opcode.Cycle);
     }
     else
     {
         Registers.PC += 3;
         return(opcode.VariableCycle);
     }
 }
Beispiel #9
0
        public void InstructionList_Completion()
        {
            int missingCount = 0;

            for (int i = 0; i < OpcodeList.Length; i++)
            {
                IOpcode opcode = OpcodeFactory.GetOpcode(OpcodeList[i]);
                if (opcode == null)
                {
                    missingCount++;
                    Debug.WriteLine("Opcode {0:X2} is missing!", OpcodeList[i]);
                }
            }
            if (missingCount > 0)
            {
                Debug.WriteLine("Opcode completion {0:###.00}%", 100D - (100D * missingCount) / OpcodeList.Length);
            }
            Assert.AreEqual(missingCount, 0, String.Format("{0} opcode(s) are missing", missingCount));
        }
Beispiel #10
0
            void Render(IOpcode opcode)
            {
                //set coordinates type
                if (opcode.CoordinatesType == CoordinatesType.Relative)
                {
                    absoluteCoordinates = false;
                }
                else
                {
                    absoluteCoordinates = true;
                }

                if (opcode is SetVisibillityOn)
                {
                    currentIsVisible = true;
                }
                else if (opcode is SetCurrentPoint)
                {
                    var op = (SetCurrentPoint)opcode;
                    currentPoint = new Point(op.X, op.Y);
                }
                else if (opcode is DrawPolylineShort)
                {
                    var op = (DrawPolylineShort)opcode;
                    DrawPolyline(op.Points);
                }
                else if (opcode is DrawPolylineLong)
                {
                    var op = (DrawPolylineLong)opcode;
                    DrawPolyline(op.Points);
                }
                else if (opcode is DrawPolymarkerShort)
                {
                    var op = (DrawPolymarkerShort)opcode;
                    DrawPolymarker(op.Points);
                }
                else if (opcode is DrawPolymarkerLong)
                {
                    var op = (DrawPolymarkerLong)opcode;
                    DrawPolymarker(op.Points);
                }
                else if (opcode is DrawLineShort)
                {
                    var op = (DrawLineShort)opcode;
                    DrawLine(op.StartingPoint, op.EndPoint);
                }
                else if (opcode is DrawLineLong)
                {
                    var op = (DrawLineLong)opcode;
                    DrawLine(op.StartingPoint, op.EndPoint);
                }
                else if (opcode is SetVisibillityOff)
                {
                    currentIsVisible = false;
                }
                else if (opcode is DrawTextBasic)
                {
                    var op = (DrawTextBasic)opcode;
                    DrawTextBasic(op.Position, op.Text);
                }
                else if (opcode is SetColorIndex)
                {
                    var op = (SetColorIndex)opcode;
                    currentColor = DefaultColorMap.Default[op.Index];
                }
                else if (opcode is SetUrlLink)
                {
                    var op = (SetUrlLink)opcode;

                    if (currentUrls.Count > 0)
                    {
                        string divText = "<div style=\"background-color:pink; position:absolute; top:{0}px; left: {1}px; width: {2}px; height: {3}px;\"></div>";
                        outputString.Append(string.Format(divText,
                                                          currentGeometryStartPoint.Y,
                                                          currentGeometryStartPoint.X,
                                                          Math.Max(10, currentGeometryEndPoint.X  - currentGeometryStartPoint.X),
                                                          Math.Max(10, currentGeometryEndPoint.Y - currentGeometryStartPoint.Y)));
                    }

                    currentUrls.Clear();
                    currentGeometryStartPoint = Point.Zero;
                    currentGeometryEndPoint = Point.Zero;

                    if (op.Index != 0)
                    {
                        currentUrls = new List<Tuple<string,string>> { urlsDictionary[op.Index] };
                        return;
                    }
                    else if (op.Links != null && op.Links.Length > 0)
                    {
                        foreach (var link in op.Links)
                        {
                            urlsDictionary[link.Index] = Tuple.Create(link.Address, link.Name);

                            currentUrls.Add(urlsDictionary[link.Index]);
                        }

                        return;
                    }

                }
                else if (opcode is UnrecognizedOpcode)
                {
                    var op = (UnrecognizedOpcode)opcode;

                    Console.WriteLine("unrecognized opcode:" + op.OpcodeId);
                }
                else
                {
                    Console.WriteLine("unimplemented opcode:" + opcode);
                }
            }
Beispiel #11
0
 public ASTNode(IOpcode opcode)
 {
     this.opcode = opcode;
 }
Beispiel #12
0
 public void Add(IOpcode opcode)
 {
     AddChild(new ASTNode(opcode));
 }
Beispiel #13
0
        private void OptimizeRegistersTransactions()
        {
            Utilities.Utilities.VerbouseOut("OPTIMIZER", "Starting registers transactions optimization", System.ConsoleColor.Yellow);
            int  lastA   = 255;
            int  lastB   = 255;
            bool lockA   = false;
            bool lockB   = false;
            bool remove  = false;
            int  removed = 0;

            for (int i = 1; i <= program.Count; i++)
            {
                IOpcode node = program[i].opcode;
                if (node is Mov)
                {
                    if (node.Arg1 == "a")
                    {
                        if (node.Arg2 == "b")
                        {
                            if (lastA == lastB && !lockA)
                            {
                                remove = true;
                            }
                            else
                            {
                                lastA = lastB;
                            }
                        }
                        else if (node.FastAdd.toInt() == lastA && !lockA)
                        {
                            remove = true;
                        }
                        else
                        {
                            lastA = node.FastAdd.toInt();
                            lockA = false;
                        }
                    }
                    else if (node.Arg1 == "b")
                    {
                        if (node.Arg2 == "a")
                        {
                            if (lastA == lastB && !lockB)
                            {
                                remove = true;
                            }
                            else
                            {
                                lastB = lastA;
                            }
                        }
                        else if (node.FastAdd.toInt() == lastB && !lockB)
                        {
                            remove = true;
                        }
                        else
                        {
                            lastB = node.FastAdd.toInt();
                            lockB = false;
                        }
                    }
                }
                else if (node is Jmp || node is Jnc)
                {
                    lastA = 255;
                    lastB = 255;
                }
                else if (node is In || node is Ld || node is Add)
                {
                    if (node is In)
                    {
                        if (node.Arg1 == "a")
                        {
                            lockA = true;
                        }
                        else if (node.Arg1 == "b")
                        {
                            lockB = true;
                        }
                    }
                    else if (node is Ld)
                    {
                        lockB = true;
                    }
                    else if (node is Add)
                    {
                        if (node.Arg1 == "a")
                        {
                            if (node.FastAdd.toInt() != 0)
                            {
                                lockA = true;
                            }
                            if (node.FastAdd.toInt() == 0)
                            {
                                remove = true;
                            }
                        }
                        else if (node.Arg2 == "b")
                        {
                            if (node.FastAdd.toInt() != 0)
                            {
                                lockB = true;
                            }
                            if (node.FastAdd.toInt() == 0)
                            {
                                remove = true;
                            }
                        }
                    }
                }
                if (remove)
                {
                    Utilities.Utilities.VerbouseOut("OPTIMIZER", "Removed reducent MOV on line " + (i + removed), System.ConsoleColor.Yellow);
                    program.Remove(i);
                    i--;
                    removed++;
                    remove = false;
                }
            }
        }
        public Binary Linker()
        {
            int maxPage = Program.eightBit ? 256 : 16;
            int maxWord = maxPage;

            if (Program.useTracer)
            {
                Utilities.Utilities.VerbouseOut("CODE STACKER", "Starting...");
                IOpcode[][] binary;
                binary = new IOpcode[maxPage][];
                for (int i = 0; i < binary.Length; i++)
                {
                    binary[i] = new IOpcode[maxWord];
                }
                int pc = 1;
                for (int i = 0; i < maxPage; i++)
                {
                    for (int j = 0; j < maxPage; j++)
                    {
                        if (j != maxPage - 1)
                        {
                            if (program[pc] != null)
                            {
                                program[pc].opcode.Page = i;
                                program[pc].opcode.Word = j;
                                binary[i][j]            = program[pc].opcode;
                            }
                            else
                            {
                                program.Add(new Add("a", "0"));
                                program[pc].opcode.Page = i;
                                program[pc].opcode.Word = j;
                                binary[i][j]            = program[pc].opcode;
                            }
                            pc++;
                        }
                        else
                        {
                            if (i != (Program.eightBit ? 255 : 15))
                            {
                                if (program[pc] != null)
                                {
                                    program[pc] = new ASTNode(new Swi(i + 1));
                                }
                                else
                                {
                                    program.Add(new Swi(i + 1));
                                }
                                program[pc].opcode.Page = i;
                                program[pc].opcode.Word = j;
                                binary[i][j]            = program[pc].opcode;
                                pc++;
                            }
                            else
                            {
                                binary[i][j] = new Add("a", "0");
                            }
                        }
                    }
                }
                Utilities.Utilities.VerbouseOut("CODE STACKER", "Finished");
                return(new Binary(binary));
            }
            else
            {
                IOpcode[][] binary;
                binary = new IOpcode[maxPage][];
                for (int i = 0; i < binary.Length; i++)
                {
                    binary[i] = new IOpcode[maxWord];
                }
                int pc = 1;
                for (int i = 0; i < maxPage; i++)
                {
                    for (int j = 0; j < maxPage; j++)
                    {
                        if (program[pc] != null)
                        {
                            program[pc].opcode.Page = i;
                            program[pc].opcode.Word = j;
                            binary[i][j]            = program[pc].opcode;
                            pc++;
                        }
                    }
                }
                return(new Binary(binary));
            }
        }
Beispiel #15
0
        void Render(IOpcode opcode)
        {
            if (opcode is SetVisibillityOn)
            {
                currentIsVisible = true;
            }
            else if (opcode is SetCurrentPoint)
            {
                var op = (SetCurrentPoint)opcode;
                currentPoint = new Point(op.X, op.Y);
            }
            else if (opcode is SetLineWeight)
            {
                //var op = (SetLineWeight)opcode;
                //currentLineWeight = op.Weight / 20; //TODO: Just some constant to try and get a sane line weight
            }
            else if (opcode is DrawPolylineShort)
            {
                polyCount++;
                absoluteCoordinates = false;
                var op = (DrawPolylineShort)opcode;
                DrawPolyline(op.Points);
            }
            else if (opcode is DrawPolylineLong)
            {
                absoluteCoordinates = false;
                var op = (DrawPolylineLong)opcode;
                DrawPolyline(op.Points);
            }
            else if (opcode is DrawPolymarkerShort)
            {
                absoluteCoordinates = false;
                var op = (DrawPolymarkerShort)opcode;
                DrawPolymarker(op.Points);
            }
            else if (opcode is DrawPolymarkerLong)
            {
                absoluteCoordinates = false;
                var op = (DrawPolymarkerLong)opcode;
                DrawPolymarker(op.Points);
            }
            else if (opcode is DrawLineShort)
            {
                absoluteCoordinates = false;
                var op = (DrawLineShort)opcode;
                DrawLine(op.StartingPoint, op.EndPoint);
            }
            else if (opcode is DrawLineLong)
            {
                absoluteCoordinates = false;
                var op = (DrawLineLong)opcode;
                DrawLine(op.StartingPoint, op.EndPoint);
            }
            else if (opcode is SetVisibillityOff)
            {
                currentIsVisible = false;
            }
            else if (opcode is SetColorIndex)
            {
                var op = (SetColorIndex)opcode;
                SetColorIndex(op.Index);
            }
            else if (opcode is SetColorRgba)
            {
                var op = (SetColorRgba)opcode;
                SetColorRgba(Color.FromArgb(op.A, op.R, op.G, op.B));
            }
            else if (opcode is DrawTextBasic)
            {
                var op = (DrawTextBasic)opcode;
                DrawTextBasic(op.Position, op.Text);
            }
            else
            {
                var op = (UnrecognizedOpcode)opcode;

                if (op.OpcodeId == "") return;
                if (op.OpcodeId == "URL") return;

                Console.WriteLine("unrecognized opcode:" + op.OpcodeId);

                return;
            }
        }
 public void Add(IOpcode opcode)
 {
     lastNode.Add(opcode);
     lastNode = lastNode.Child;
     count++;
 }
        public Assembly(string programName)
        {
            this.programName = programName;

            string[] lines = CodeIO.LoadFile(programName);
            //Clearing Code
            //Removing comments and trimming
            Utilities.Utilities.VerbouseOut("-=-=Clearing code from comments=-=-");
            lines = ClearCode(lines);
            Utilities.Utilities.VerbouseOut("-=-=Parsing lines=-=-");
            parsed = SplitCode(lines);
            //Get hat
            Utilities.Utilities.VerbouseOut("-=-=Parsing libraries imports=-=-");
            ArrayList imports = GetImports(parsed);

            //Catching imports
            Utilities.Utilities.VerbouseOut("Importing");
            importManager = new Preprocessor(imports);
            //Calling preprocessor
            Utilities.Utilities.VerbouseOut("PREPROCESSOR", "Inserting macroses...");
            for (int i = 0; i < importManager.CountMacroses(); i++)
            {
                parsed = InsertAllMacro(parsed);
            }
            Utilities.Utilities.VerbouseOut("PREPROCESSOR", "Updated code: ");
            if (Program.verboseMode)
            {
                for (int j = 0; j < parsed.Length; j++)
                {
                    Console.Write(j + ":\t");
                    for (int k = 0; k < parsed[j].Length; k++)
                    {
                        Console.Write(parsed[j][k] + " ");
                    }
                    Console.WriteLine();
                }
            }
            Utilities.Utilities.VerbouseOut("PREPROCESSOR", "Finding definitions...");
            parsed = importManager.CatchDefines(parsed);
            var safer = new List <string[]>();

            for (int i = 0; i < parsed.Length; i++)
            {
                if (parsed[i] != null)
                {
                    safer.Add(parsed[i]);
                }
            }
            parsed = safer.ToArray();
            Utilities.Utilities.VerbouseOut("-=-=Parsing pExts=-=-");
            ArrayList pexts = GetPexts(parsed);

            importManager.ImportPexts(pexts);
            Utilities.Utilities.VerbouseOut("Removing preprocessor code from source...");
            parsed = ClearHatAfterImport(parsed);
            //Catching labels
            Utilities.Utilities.VerbouseOut("Parsing labels...");
            CodeLine[] code = LabelCatcher(parsed);
            //Converting code to object form

            Utilities.Utilities.VerbouseOut("PREPROCESSOR", "Cleared code: ");
            if (Program.verboseMode)
            {
                for (int j = 0; j < parsed.Length; j++)
                {
                    Console.Write(j + ":\t");
                    for (int k = 0; k < parsed[j].Length; k++)
                    {
                        Console.Write(parsed[j][k] + " ");
                    }
                    Console.WriteLine();
                }
            }

            Utilities.Utilities.VerbouseOut("Converting code to object form");
            for (int i = 0; i < code.Length; i++)
            {
                string opcode = code[i].code[0].ToLower();
                string label  = code[i].label;
                if (opcode == "add")
                {
                    program.Add(new Add(code[i].code[1], code[i].code[2]));
                }
                else if (opcode == "in")
                {
                    if (code[i].code.Length == 2)
                    {
                        program.Add(new In(code[i].code[1]));
                    }
                    else if (code[i].code.Length > 2)
                    {
                        program.Add(new In(code[i].code[1], code[i].code[2]));
                    }
                }
                else if (opcode == "jmp")
                {
                    program.Add(new Jmp(code[i].code[1]));
                }
                else if (opcode == "jnc")
                {
                    program.Add(new Jnc(code[i].code[1]));
                }
                else if (opcode == "ld")
                {
                    program.Add(new Ld(code[i].code[1]));
                }
                else if (opcode == "mov")
                {
                    if (code[i].code.Length == 3)
                    {
                        program.Add(new Mov(code[i].code[1], code[i].code[2]));
                    }
                    else if (code[i].code.Length > 3)
                    {
                        program.Add(new Mov(code[i].code[1], code[i].code[2], code[i].code[3]));
                    }
                }
                else if (opcode == "out")
                {
                    if (code[i].code.Length == 2)
                    {
                        program.Add(new Out(code[i].code[1]));
                    }
                    else if (code[i].code.Length > 2)
                    {
                        program.Add(new Out(code[i].code[1], code[i].code[2]));
                    }
                }
                else if (opcode == "st")
                {
                    program.Add(new St(code[i].code[1]));
                }
                else if (opcode == "swi")
                {
                    program.Add(new Swi(code[i].code[1]));
                }
                else if (opcode == "swm")
                {
                    program.Add(new Swm(code[i].code[1]));
                }
                else
                {
                    program.InsertSubTree(program.Count, importManager.LookUpPext(opcode, code[i].code));
                }
                if (label != null)
                {
                    program.AddLabel(label);
                }
            }
            Utilities.Utilities.VerbouseOut("Linking labels...");
            for (int i = 0; i <= program.Count; i++)
            {
                if (program[i].opcode is Jmp || program[i].opcode is Jnc)
                {
                    if (program[i].opcode.Arg1 == null)
                    {
                        ((PCChanger)program[i].opcode).Link = program[program[i].opcode.FastAdd.toInt() + 1];
                        Utilities.Utilities.VerbouseOut("LABEL_LINKER", "Linked: " + program[i].opcode.Name + " " + program[i].opcode.FastAdd.ToString() + " to " + ((PCChanger)program[i].opcode).Link.ToString());
                    }
                    else
                    {
                        ((PCChanger)program[i].opcode).Link = program.GetLabel(program[i].opcode.Arg1);
                        Utilities.Utilities.VerbouseOut("LABEL_LINKER", "Linked: " + program[i].opcode.Name + " " + program[i].opcode.Arg1.ToString() + " to " + ((PCChanger)program[i].opcode).Link.ToString());
                    }
                }
            }
            if (Program.verboseMode)
            {
                Console.WriteLine("---Final code---");
                int length = program.Count;
                for (int i = 1; i <= length; i++)
                {
                    Console.Write(i + ":\t");
                    IOpcode line = program.Get(i).opcode;
                    Console.Write(line.Name + "\t");
                    if (line.Arg1 != null)
                    {
                        Console.Write(line.Arg1 + "\t");
                    }
                    if (line is Mov)
                    {
                        if (line.Arg2 != null)
                        {
                            Console.Write(line.Arg2 + "\t");
                        }
                    }
                    if (line.FastAdd != null)
                    {
                        Console.Write(line.FastAdd.GetValue());
                    }
                    Console.WriteLine();
                }
            }
        }