Esempio n. 1
0
        private static void RegisterConstructor(ConstructorInfo info, Type forClass)
        {
            ParameterInfo[] parameterInfo = info.GetParameters();

            InstructionRegister.RegisterConstructor($"{forClass.Name}", (InterpreterRuntime runtime, int registerIndex) => {
                if (runtime.GetObject(registerIndex, out object result))
                {
                    if (parameterInfo.Length > 0)
                    {
                        object[] objects = new object[parameterInfo.Length];
                        for (int i = 0; i < parameterInfo.Length; i++)
                        {
                            if (!runtime.Pop(out objects[parameterInfo.Length - 1 - i]))
                            {
                                break;
                            }
                        }

                        // Since it will break but not assign objects[i] if the pop goes wrong we can just check if it has set it
                        if (objects[objects.Length - 1] != null)
                        {
                            if (runtime.Push(info.Invoke(result, objects), true))
                            {
                                return;
                            }
                        }
                    }
                    else if (runtime.Push(info.Invoke(result, null), true))
                    {
                        return;
                    }
                }
                Log.Error($"{forClass.Name} Constructor failed");
            }, parameterInfo.Select(x => InstructionRegister.GetParamType(x.ParameterType)).ToArray());
        }
Esempio n. 2
0
        public void InstructionRegisterOutputsOperand()
        {
            DataBus   bus     = new DataBus();
            SignalBus signals = new SignalBus();

            Register instruction = new InstructionRegister(bus, signals);

            instruction.Value = STA.OP_CODE + 15;
            signals.IO        = true;

            instruction.WriteToBus();
            Assert.AreEqual(15, bus.Value);
        }
Esempio n. 3
0
        public void InstructionRegisterInputsInstructionAndOperand()
        {
            DataBus   bus     = new DataBus();
            SignalBus signals = new SignalBus();

            Register instruction = new InstructionRegister(bus, signals);

            bus.Value  = STA.OP_CODE + 15;
            signals.II = true;

            instruction.ReadFromBus();
            Assert.AreEqual(STA.OP_CODE + 15, instruction.Value);
        }
Esempio n. 4
0
        private static void RegisterField(FieldInfo info, Type forClass)
        {
            InstructionRegister.RegisterGetter(info.Name + "::" + forClass.Name, (InterpreterRuntime runtime, int registerIndex) => {
                if (!runtime.GetObject(registerIndex, out object result) || !runtime.Push(info.GetValue(result), true))
                {
                    Log.Error($"{forClass.Name}::{info.Name} Getter Failed");
                }
            }, new ParamType[0]);

            InstructionRegister.RegisterSetter(info.Name + "::" + forClass.Name, (InterpreterRuntime runtime, int registerIndex) => {
                if (!runtime.GetObject(registerIndex, out object result) || !runtime.Pop(out object valueToSet))
                {
                    Log.Error($"{forClass.Name}::{info.Name} Setter Failed");
                }
Esempio n. 5
0
 /**
  * @brief Links all the component so they
  *        can be used in micro instructions.
  */
 public void LinkCPUcomponents(
     ProgramCounter PC, MemoryAddressRegister MAR,
     MemoryDataRegister MDR, InstructionRegister IR,
     GeneralPurposeRegisterA GPA, GeneralPurposeRegisterB GPB,
     ProcessStatusRegister PSR,
     MemoryListControl memory, ArithmeticLogicUnit ALU,
     Clock clock, BusControl busSystem)
 {
     this.PC        = PC;
     this.MAR       = MAR;
     this.MDR       = MDR;
     this.IR        = IR;
     this.GPA       = GPA;
     this.GPB       = GPB;
     this.PSR       = PSR;
     this.memory    = memory;
     this.ALU       = ALU;
     this.clock     = clock;
     this.busSystem = busSystem;
 }
 private Opcode getInstruction()
 {
     return((Opcode)getHexValue(InstructionRegister.ToString("X4").Substring(0, 1)));
 }
 private int getOperand()
 {
     return(getHexValue(InstructionRegister.ToString("X4").Substring(1)));
 }
Esempio n. 8
0
        public static void Main(string[] args)
        {
            if (Directory.Exists(Directory.GetCurrentDirectory() + "/StaticBindings") == false)
            {
                Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/StaticBindings");
            }

            CreateBindings.DirectoryPath = Directory.GetCurrentDirectory() + "/StaticBindings";
            CreateBindings.Create <Color>();
            CreateBindings.CreateFinalFile();

            //DOML.LinkBindings();
            Console.SetWindowSize((int)(Console.LargestWindowWidth / 1.5f), (int)(Console.LargestWindowHeight / 1.5f));
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            using (StreamReader reader = new StreamReader(File.OpenRead("test.doml"))) {
                int c = reader.Read();
                while (c > 0)
                {
                    Console.Write((char)c);
                    c = reader.Read();
                }
            }
            TopLevelNode topLevel = new Parser().ParseAST(new StreamReader(File.OpenRead("test.doml")));

            topLevel.BasicCodegen(Console.Out, false);
            topLevel.BasicCodegen(Console.Out, true);
            foreach (Instruction instruction in topLevel.GetInstructions())
            {
                Console.Write(instruction.OpCode);
                foreach (object obj in instruction.Parameters)
                {
                    Console.Write(" " + obj);
                }
                Console.WriteLine();
            }

            topLevel.Print(Console.Out);
            Console.Read();
            return;

#if BenchmarkDotNet
            //Summary summary = BenchmarkRunner.Run<AllTests>();
            //Console.Read();
#elif StaticBindings
            if (Directory.Exists(Directory.GetCurrentDirectory() + "/StaticBindings") == false)
            {
                Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/StaticBindings");
            }

            CreateBindings.DirectoryPath = Directory.GetCurrentDirectory() + "/StaticBindings";
            CreateBindings.Create <Color>("System");
            CreateBindings.CreateFinalFile();
#elif TestBindings
            DOMLBindings.LinkBindings();


            Interpreter interpreter = Parser.GetInterpreterFromText(@"
            // This is a comment
            // Construct a new System.Color
            @ Test        = System.Color ...
            ;             .RGB             = 255, 64, 128 // Implicit 'array'

            @ TheSame     = System.Color ...
            ;             .RGB->Normalised = 1, 0.25, 0.5, // You can have trailing commas

            @ AgainSame   = System.Color ...
            ;             .RGB->Hex        = 0xFF4080
            ;             .Name            = ""OtherName""

            /* Multi Line Comment Blocks are great */
            @ Copy = System.Color...
            ;             .RGB = Test.R, Test.G, Test.B
            ;             .Name = ""Copy""
            ");

            string withComments;
            string withoutComments;

            IRWriter.EmitToLocation(interpreter, Directory.GetCurrentDirectory() + "/CompactOutput.IR", false, false);
            IRWriter.EmitToLocation(interpreter, Directory.GetCurrentDirectory() + "/Output.IR", false, true);

            StringBuilder builder = new StringBuilder();

            IRWriter.EmitToString(interpreter, builder, true);
            withComments = builder.ToString();
            builder.Clear();
            IRWriter.EmitToString(interpreter, builder, false);
            withoutComments = builder.ToString();

            Interpreter c;

            using (StringReader reader = new StringReader(withComments))
            {
                c = Parser.GetInterpreterFromIR(reader);
                c.HandleSafeInstruction(new Instruction());
            }

            using (StringReader reader = new StringReader(withoutComments))
            {
                c = Parser.GetInterpreterFromIR(reader);
                c.HandleSafeInstruction(new Instruction());
            }

            Console.Read();

            return;

            TestDOML.RunStringTest(@"
            // This is a comment
            // Construct a new System.Color
            @ Test        = System.Color ...
            ;             .RGB             = 255, 64, 128 // Implicit 'array'

            @ TheSame     = System.Color ...
            ;             .RGB->Normalised = 1, 0.25, 0.5, // You can have trailing commas

            @ AgainSame   = System.Color ...
            ;             .RGB->Hex        = 0xFF4080
            ;             .Name            = ""OtherName""

            /* Multi Line Comment Blocks are great */
            @ Copy = System.Color...
            ;             .RGB = Test.R, Test.G, Test.B
            ;             .Name = ""Copy""
            ", 5, Config.READ_EMIT);

            Console.Write(Log.HandleLogs);

            Console.Read();
#else
            Log.LogHandler += Log_LogHandler;

            Action <InterpreterRuntime> Set_RGB = (InterpreterRuntime runtime) =>
            {
                if (runtime.Pop(out Colour result))
                {
                    // Handle
                    if (!runtime.Pop(out result.B) || !runtime.Pop(out result.G) || !runtime.Pop(out result.R))
                    {
                        Log.Error("Pops failed");
                        return;
                    }

                    result.R /= 255;
                    result.G /= 255;
                    result.B /= 255;
                }
            };

            Action <InterpreterRuntime> Get_RGB = (InterpreterRuntime runtime) =>
            {
                if (!runtime.Pop(out Colour result) || !runtime.Push(result.R, true) || !runtime.Push(result.G, true) || !runtime.Push(result.B, true))
                {
                    Log.Error("Pushes failed");
                }
            };

            InstructionRegister.RegisterConstructor("System.Color", (InterpreterRuntime runtime) =>
            {
                Colour colour = new Colour();
                Colours.Add(colour);
                if (!runtime.Push(colour, true))
                {
                    Log.Error("Creation failed");
                }
            });

            InstructionRegister.RegisterSetter("RGB", "System.Color", 3, Set_RGB);
            InstructionRegister.RegisterGetter("RGB", "System.Color", 3, Get_RGB);
            InstructionRegister.RegisterSetter("RGB.Normalised", "System.Color", 3, (InterpreterRuntime runtime) =>
            {
                if (runtime.Pop(out Colour result))
                {
                    if (!runtime.Pop(out result.B) || !runtime.Pop(out result.G) || !runtime.Pop(out result.R))
                    {
                        Log.Error("Pops failed");
                    }
                }
            });

            Parser.GetInterpreterFromText(@"
                @ Test = System.Color ... // Comment
                ;      .RGB(Normalised) = 0.5, 0.25, 0.1,
                ").Execute(true);//Emit(Directory.GetCurrentDirectory() + "/Test.doml", true, true);

            /*TestDOML.RunStringTest(@"
             * @ Test = System.Color ... // Comment
             * ;      .RGB(Normalised) = 0.5, 0.25, 0.1,
             * ", 100, Config.ALL);*/

            Colours.ForEach(y => Log.Info($"Colour RGB: {y.R} {y.G} {y.B}"));

            Console.Read();
#endif
        }