Example #1
0
        internal IInstruction Instruction(int opcode)
        {
            // Get instruction with params via opcode (etc: LD n, n)
            // using maps in InstructionMap
            Type type = InstructionMap.GetInstruction(opcode);

            if (type != null)
            {
                // If instruction is found, create instruction instance as Instruction
                var instruction = Activator.CreateInstance(type) as IInstruction;

                // Assign opcode
                instruction.Opcode = opcode;

                // Fetch params using maps in ParamOneMap and ParamTwoMap
                instruction.Params = ParamMap.GetParams(this, opcode);

                // Fetch initial Ticks information
                instruction.Ticks = TicksMap.Ticks[opcode];

                // Return Instruction object
                return(instruction);
            }
            // If no instruction is found, throw missing opcode exception
            // TODO: Create Missing Opcode Exception, for now return null
            return(null);
        }
Example #2
0
        static void Main(string[] args)
        {
            var ruleExample = new Rule
                              (
                id: 1,
                format: new ITokenType[]
            {
                new FixedString("if"),
                new FixedString("("),
                new BasicPlaceHolder(new PlaceHolder(1, "Condition", new TableID(3, 'A'))),
                new FixedString(")"),
                new FixedString("then"),
                new AsmPlaceHolder(new PlaceHolder(1, "Label", new TableID(2, 'A')))
            }
                              );
            var instruction = new Instruction
                              (
                Name: "BREQ",
                Id: 1,
                AsmParams: new List <AsmParameter>
            {
                new AsmParameter(new Parameter("k", new RangeConstraint(new Range(-64, 63))))
            },
                BasicParams: new List <BasicParameter>
            {
                new BasicParameter(new Parameter("", new ConstantConstraint("Equal")))
            },
                Rule: ruleExample
                              );
            var instruction2 = new Instruction
                               (
                Name: "BRLO",
                Id: 2,
                AsmParams: new List <AsmParameter>
            {
                new AsmParameter(new Parameter("k", new RangeConstraint(new Range(-64, 63))))
            },
                BasicParams: new List <BasicParameter>
            {
                new BasicParameter(new Parameter("", new ConstantConstraint("Lower")))
            },
                Rule: ruleExample
                               );
            //var instruction3 =
            var instructionMap = new InstructionMap(new List <Instruction> {
                instruction, instruction2
            });
            var found =
                instructionMap.FindMatch(
                    ruleExample,
                    asmParamsValue: new List <IExprValue> {
                new IntegerValue(33)
            },
                    basicParamsValue: new List <IExprValue> {
                new ConstStringValue("Equal")
            }
                    );
            var found2 =
                instructionMap.FindMatch(
                    ruleExample,
                    asmParamsValue: new List <IExprValue> {
                new IntegerValue(33)
            },
                    basicParamsValue: new List <IExprValue> {
                new ConstStringValue("Lower")
            }
                    );

            Console.WriteLine(found != null);

            JsonSerializerSettings jss = new JsonSerializerSettings();

            jss.TypeNameHandling = TypeNameHandling.All;
            var         foundSerialized   = JsonConvert.SerializeObject(found, jss);
            Instruction foundDeserialized = JsonConvert.DeserializeObject <Instruction>(foundSerialized);

            Console.WriteLine(foundDeserialized.Name);
        }