예제 #1
0
        public static InstructionSet Disassemble(string path)
        {
            InstructionSet instructionSet = new InstructionSet();

            using (var file = File.Open(path, FileMode.Open)) {
                using (var reader = new BeBinaryReader(file)) {
                    while (reader.BaseStream.Position != reader.BaseStream.Length)
                    {
                        byte operation       = reader.ReadByte();
                        var  parameterSchema = parameterSchemas[operation];
                        var  parameters      = new List <TypedNumber>();
                        for (var i = 0; i < parameterSchema.Length; i++)
                        {
                            var parameter = parameterSchema[i];
                            switch (parameter)
                            {
                            case ParameterType.size_8:
                                parameters.Add(TypedNumber.TypedByte(
                                                   reader.ReadByte()
                                                   ));
                                break;

                            case ParameterType.size_16:
                                parameters.Add(TypedNumber.TypedShort(
                                                   reader.ReadUInt16()
                                                   ));
                                break;

                            case ParameterType.size_32:
                                parameters.Add(TypedNumber.TypedInt(
                                                   reader.ReadUInt32()
                                                   ));
                                break;
                            }
                        }
                        instructionSet.Add(new Instruction(
                                               opcode: operation,
                                               parameters: parameters.ToArray()
                                               ));
                    }
                }
            }
            return(instructionSet);
        }
예제 #2
0
        internal void Run(InstructionSet instructionSet)
        {
            registers = new IRegister[] {
                new Register1(this),
                new Register2(this),
                new Register3(this),
                new Register4(this),
                new Register5(this),
                new Register6(this),
                new Register7(this),
                new Register8(this),
                new Register9(this),
                new Register10(this)
            };
            var       instructions     = instructionSet.GetInstructions();
            int       instructionCount = instructions.Length;
            uint      index            = 0;
            long      operationCount   = 0;
            Stopwatch stopwatch        = new Stopwatch();

            stopwatch.Start();
            while (index < instructionCount)
            {
                operationCount++;
                var instruction = instructions[index];
                index++;
                switch (instruction.opcode)
                {
                case 0:
                    ADD();
                    break;

                case 1:
                    SUB();
                    break;

                case 2:
                    MTP();
                    break;

                case 3:
                    DIV();
                    break;

                case 4:
                    MOD();
                    break;

                case 5:
                    AND();
                    break;

                case 6:
                    OR();
                    break;

                case 7:
                    XOR();
                    break;

                case 8:
                    NOT();
                    break;

                case 9:
                    L_SHIFT();
                    break;

                case 10:
                    R_SHIFT();
                    break;

                case 11:
                    LOAD_8(instruction.parameters[0].value_8, instruction.parameters[1].value_8);
                    break;

                case 12:
                    LOAD_16(instruction.parameters[0].value_8, instruction.parameters[1].value_16);
                    break;

                case 13:
                    LOAD_32(instruction.parameters[0].value_8, instruction.parameters[1].value_32);
                    break;

                case 14:
                    PRELOAD_8(instruction.parameters[0].value_8);
                    break;

                case 15:
                    PRELOAD_16(instruction.parameters[0].value_8);
                    break;

                case 16:
                    PRELOAD_32(instruction.parameters[0].value_8);
                    break;

                case 17:
                    PRELOAD_BLOCK(instruction.parameters[0].value_8);
                    break;

                case 18:
                    FREE_ADR_8(instruction.parameters[0].value_8);
                    break;

                case 19:
                    FREE_ADR_16(instruction.parameters[0].value_8);
                    break;

                case 20:
                    FREE_ADR_32(instruction.parameters[0].value_8);
                    break;

                case 21:
                    FREE_BLOCK(instruction.parameters[0].value_8, instruction.parameters[1].value_8);
                    break;

                case 22:
                    COPY_REG(instruction.parameters[0].value_8, instruction.parameters[1].value_8);
                    break;

                case 23:
                    SWAP_REG(instruction.parameters[0].value_8, instruction.parameters[1].value_8);
                    break;

                case 24:
                    SET_ADR_8(instruction.parameters[0].value_8, instruction.parameters[1].value_8);
                    break;

                case 25:
                    SET_ADR_16(instruction.parameters[0].value_8, instruction.parameters[1].value_8);
                    break;

                case 26:
                    SET_ADR_32(instruction.parameters[0].value_8, instruction.parameters[1].value_8);
                    break;

                case 27:
                    GET_ADR_8(instruction.parameters[0].value_8, instruction.parameters[1].value_8);
                    break;

                case 28:
                    GET_ADR_16(instruction.parameters[0].value_8, instruction.parameters[1].value_8);
                    break;

                case 29:
                    GET_ADR_32(instruction.parameters[0].value_8, instruction.parameters[1].value_8);
                    break;

                case 30:
                    SET_REG_8(instruction.parameters[0].value_8, instruction.parameters[1].value_8);
                    break;

                case 31:
                    SET_REG_16(instruction.parameters[0].value_8, instruction.parameters[1].value_16);
                    break;

                case 32:
                    SET_REG_32(instruction.parameters[0].value_8, instruction.parameters[1].value_32);
                    break;

                case 33:
                    CMP();
                    break;

                case 34:
                    index = JMP();
                    break;

                case 35:
                    var conditionalJumpResult = CON_JMP();
                    if (conditionalJumpResult.HasValue)
                    {
                        index = conditionalJumpResult.Value;
                    }
                    break;

                case 37:
                    stack.Push(new StackFrame(index, getRegisterValues()));
                    index = CALL(instruction.parameters[0].value_8);
                    break;

                case 38:
                    var stackFrame = stack.Pop();
                    index = stackFrame.address;
                    setRegisterValues(stackFrame.registerValues);
                    break;

                case 39:
                    IN();
                    break;

                case 40:
                    OUT();
                    break;

                default:
                    break;
                }
            }
            stopwatch.Stop();

            var completionTime = (double)stopwatch.ElapsedMilliseconds / 1000;

            Console.WriteLine($"Program finished with {operationCount} operations fired");
            Console.WriteLine($"Completion time (seconds): {completionTime}");
            Console.WriteLine($"Operations per second: {operationCount / completionTime}");
        }
예제 #3
0
        public static void ExecuteAssembly(InstructionSet instructionSet)
        {
            var processor = new Processor();

            processor.Run(instructionSet);
        }