Esempio n. 1
0
            public StateClass(int ip, String instructionLine, int[] memory, int[] registers, Flags f, InstructionClass i)
            {
                this.instructionLine = instructionLine;
                this.ip = ip;

                memory.CopyTo(this.mem, 0);
                registers.CopyTo(this.reg, 0);
                this.f           = new Flags();
                this.f           = f;
                this.instruction = new InstructionClass();
                this.instruction = i;
            }
            public StateClass(long ip, bool inAddr, String instructionLine, long[] memory, long[] registers, Flags f, InstructionClass i)
            {
                this.instructionLine = instructionLine;
                this.ip = ip;

                memory.CopyTo(this.mem, 0);
                registers.CopyTo(this.reg, 0);
                this.flags               = new Flags();
                this.flags               = f;
                this.instruction         = new InstructionClass();
                this.instruction         = i;
                this.IndirrectAddressing = inAddr;
            }
Esempio n. 3
0
 public FakeInstruction(InstructionClass iClass, Operation operation, params MachineOperand[] ops)
 {
     this.iClass    = iClass;
     this.operation = operation;
     this.ops       = ops;
 }
Esempio n. 4
0
 public FakeInstruction(Operation operation, params MachineOperand[] ops)
 {
     this.operation = operation;
     this.ops       = ops;
     this.iClass    = InstructionClass.Invalid;
 }
Esempio n. 5
0
        public void runCode()
        {
            long  arg1 = 0, arg2 = 0, arg3 = 0;
            Int64 instruction;
            int   ip = 0;

            // open file for reading
            StreamReader fstr = new StreamReader(args[0]);
            String       instructionLine; //  line with instructions readed from file

            // initial state
            states.Add(new StateClass(ip, "Initial Status", memory, registers, new Flags(), new InstructionClass()));

            while ((instructionLine = fstr.ReadLine()) != null)
            {
                instruction = Convert.ToInt64(instructionLine);
                // init system
                Flags            f  = new Flags();
                InstructionClass ic = new InstructionClass();
                // get arguments
                // [ instruction | arg1 | arg2 | arg3 ]
                arg3        = (instruction & OPERAND3_MASK) >> OPERAND3_MEM_SHIFT;
                arg2        = (instruction & OPERAND2_MASK) >> OPERAND2_MEM_SHIFT;
                arg1        = (instruction & OPERAND1_MASK) >> OPERAND1_MEM_SHIFT;
                instruction = (instruction & INSTRUCTION_MASK) >> INSTRUCTION_MEM_SHIFT;

                ic.instruction = instruction;
                ic.arg1        = arg1;
                ic.arg2        = arg2;
                ic.arg3        = arg3;

                if (instruction == LOAD)
                {
                    ulong x = Convert.ToUInt32(arg2);
                    registers[arg1] = Convert.ToInt32(symbolMapList[x]);
                }
                else if (instruction == SAVE)
                {
                    ulong x = Convert.ToUInt32(arg2);
                    symbolMapList[x] = memory[arg1];
                }
                else if (instruction == CLEAR)
                {
                    memory[arg3] = 0;
                }

                // DEC, DIV, XIMUL, XOR, SHL, MOV, JMAE, JMNGE, BT, CMP, RCL
                else if (instruction == DEC)
                {
                    registers[arg1]--;
                }
                else if (instruction == DIV)
                {
                    registers[arg3] = registers[arg1] / registers[arg2];
                }
                else if (instruction == XIMUL)
                {
                    registers[arg3] = registers[arg1] * registers[arg2];
                }
                else if (instruction == XOR)
                {
                    registers[arg3] = registers[arg1] ^ registers[arg2];
                }
                else if (instruction == SHL)
                {
                    registers[arg3] = registers[arg1] << registers[arg2];
                }
                else if (instruction == MOV)
                {
                    registers[arg3] = registers[arg2];
                }
                else if (instruction == JMAE)
                {
                    if (registers[arg1] >= registers[arg2])
                    {
                        ip = ip + 1 + registers[arg3];
                    }
                }
                else if (instruction == JMNGE)
                {
                    if (registers[arg1] <= registers[arg2])
                    {
                        ip = ip + 1 + registers[arg3];
                    }
                }
                else if (instruction == BT)
                {
                    int t = registers[arg1];
                    f.CF = (registers[arg1] << registers[arg2]) ^ registers[arg2] + 1;
                }
                else if (instruction == CMP)
                {
                    int res = registers[arg1] - registers[arg2];
                    if (res > 0)
                    {
                        f.CF = 0; f.SF = 0; f.ZF = 0;
                    }
                    else if (res == 0)
                    {
                        f.CF = 0; f.SF = 0; f.ZF = 1;
                    }
                    else if (res < 0)
                    {
                        f.CF = 1; f.SF = 1; f.ZF = 0;
                    }
                }
                else if (instruction == RCL)
                {
                    //    int t1, t2;
                    //    int n = registers[arg1];
                    //    registers[arg2] = registers[arg2] % (sizeof( int ) * 8);                       // нормализуем n
                    //    t1 = registers[arg1] << registers[arg2];                      // двигаем а влево на n бит, теряя старшие биты
                    //    t2 = registers[arg1] >> (sizeof( int ) * 8 - registers[arg2] );    // перегоняем старшие биты в младшие
                    //    registers[arg3] = t1 | t2;                     // объединяем старшие и младшие биты
                    int value = registers[arg1];
                    int count = registers[arg2];
                    registers[arg3] = (value >> count) + (((value << (32 - count)) >> (32 - count)) << count);
                }

                // HALT
                else if (instruction == HALT)
                {
                    break;
                }

                states.Add(new StateClass(ip, instructionLine, memory, registers, f, ic));
                ip++;
            }
            fstr.Close();
        }
Esempio n. 6
0
 public FakeInstruction(InstructionClass iClass, Operation operation, params MachineOperand[] ops)
 {
     this.iClass = iClass;
     this.operation = operation;
     this.ops = ops;
 }
Esempio n. 7
0
 public FakeInstruction(Operation operation, params MachineOperand[] ops)
 {
     this.operation = operation;
     this.ops = ops;
     this.iClass = InstructionClass.Invalid;
 }
        public void runCode()
        {
            long  arg1 = 0, arg2 = 0, arg3 = 0, inst_ind_addr = 0;
            Int64 instruction;
            long  ip     = 0;
            long  ip_old = 0;

            states.Add(new StateClass(ip, false, "Initial Status", memory, registers, new Flags(), new InstructionClass()));

            while (true)
            {
                try
                {
                    instruction = Convert.ToInt64(memory[ip]);
                    String instructionLine = instruction.ToString();
                    // init system
                    ip++;
                    ip_old = ip;
                    InstructionClass ic = new InstructionClass();
                    // get arguments
                    // [ instruction | arg1 | arg2 | arg3 ]
                    arg3          = (instruction & OPERAND3_MASK) >> OPERAND3_MEM_SHIFT;
                    arg2          = (instruction & OPERAND2_MASK) >> OPERAND2_MEM_SHIFT;
                    arg1          = (instruction & OPERAND1_MASK) >> OPERAND1_MEM_SHIFT;
                    inst_ind_addr = (instruction & INDIRECT_ADDRESSING_MASK);
                    instruction   = (instruction & INSTRUCTION_MASK) >> INSTRUCTION_MEM_SHIFT;
                    bool ind_addr = Convert.ToBoolean(inst_ind_addr);
                    ic.instruction = instruction;
                    ic.arg1        = arg1;
                    ic.arg2        = arg2;
                    ic.arg3        = arg3;

                    if (instruction == LOAD)
                    {
                        if (ind_addr)
                        {
                            registers[arg1] = memory[memory[arg3]];
                        }
                        else
                        {
                            registers[arg1] = memory[arg3];
                        }
                    }
                    else if (instruction == SAVE)
                    {
                        if (ind_addr)
                        {
                            memory[memory[arg3]] = registers[arg1];
                        }
                        else
                        {
                            memory[arg3] = registers[arg1];
                        }
                    }
                    else if (instruction == CLEAR)
                    {
                        memory[arg3] = 0;
                    }

                    // DEC, DIV, XIMUL, XOR, SHL, MOV, JMAE, JMNGE, BT, CMP, RCL
                    else if (instruction == DEC)
                    {
                        long x = convertInt48ToInt64(registers[arg1]);
                        registers[arg1] = x - 1;
                    }
                    else if (instruction == DIV)
                    {
                        if (ind_addr)
                        {
                            long r1 = convertInt48ToInt64(registers[arg1]);
                            long r2 = convertInt48ToInt64(memory[memory[arg3]]);
                            registers[arg3] = convertInt64ToInt48(r1 / r2);
                        }
                        else
                        {
                            long r1 = convertInt48ToInt64(registers[arg1]);
                            long r2 = convertInt48ToInt64(registers[arg2]);
                            registers[arg3] = convertInt64ToInt48(r1 / r2);
                        }
                    }
                    else if (instruction == XIMUL)
                    {
                        if (ind_addr)
                        {
                            long r1 = convertInt48ToInt64(registers[arg1]);
                            long r2 = convertInt48ToInt64(memory[memory[arg3]]);
                            registers[arg3] = convertInt64ToInt48(r1 * r2);
                        }
                        else
                        {
                            long r1 = convertInt48ToInt64(registers[arg1]);
                            long r2 = convertInt48ToInt64(registers[arg2]);
                            registers[arg3] = convertInt64ToInt48(r1 * r2);
                        }
                    }
                    else if (instruction == XOR)
                    {
                        if (ind_addr)
                        {
                            long r1 = convertInt48ToInt64(registers[arg1]);
                            long r2 = convertInt48ToInt64(memory[memory[arg3]]);
                            registers[arg3] = convertInt64ToInt48(r1 ^ r2);
                        }
                        else
                        {
                            long r1 = convertInt48ToInt64(registers[arg1]);
                            long r2 = convertInt48ToInt64(registers[arg2]);
                            registers[arg3] = convertInt64ToInt48(r1 ^ r2);
                        }
                    }
                    else if (instruction == SHL)
                    {
                        if (ind_addr)
                        {
                            long r1 = convertInt48ToInt64(registers[arg1]);
                            long r2 = convertInt48ToInt64(memory[memory[arg3]]);
                            registers[arg3] = convertInt64ToInt48(r1 << Convert.ToInt32(registers[arg2]));
                        }
                        else
                        {
                            long r1 = convertInt48ToInt64(registers[arg1]);
                            long r2 = convertInt48ToInt64(registers[arg2]);
                            registers[arg3] = convertInt64ToInt48(r1 << Convert.ToInt32(registers[arg2]));
                        }
                    }
                    else if (instruction == MOV)
                    {
                        registers[arg2] = registers[arg1];
                    }
                    else if (instruction == JMAE)
                    {
                        long r1 = convertInt48ToInt64(registers[arg1]);
                        long r2 = convertInt48ToInt64(registers[arg2]);
                        if (ind_addr)
                        {
                            if (Math.Abs(r1) >= Math.Abs(r2))
                            {
                                ip = arg3;
                            }
                        }
                        else if (Math.Abs(r1) >= Math.Abs(r2))
                        {
                            ip = arg3;
                        }
                        ip_old = ip;
                    }
                    else if (instruction == JMNGE)
                    {
                        long r1 = convertInt48ToInt64(registers[arg1]);
                        long r2 = convertInt48ToInt64(registers[arg2]);
                        if (ind_addr)
                        {
                            if (r1 <= r2)
                            {
                                ip = arg3;
                            }
                        }
                        else if (r1 <= r2)
                        {
                            ip = arg3;
                        }
                        ip_old = ip;
                    }
                    else if (instruction == BEQ)
                    {
                        long r1 = convertInt48ToInt64(registers[arg1]);
                        long r2 = convertInt48ToInt64(registers[arg2]);
                        if (ind_addr)
                        {
                            if (r1 == r2)
                            {
                                ip = arg3;
                            }
                        }
                        else if (r1 == r2)
                        {
                            ip = arg3;
                        }
                        ip_old = ip;
                    }
                    else if (instruction == JALR)
                    {
                        registers[arg2] = ip;
                        ip     = registers[arg1];
                        ip_old = ip;
                    }
                    else if (instruction == BT)
                    {
                        long r1 = convertInt48ToInt64(registers[arg1]);
                        int  r2 = Convert.ToInt32(registers[arg2]);
                        f.CF = Convert.ToInt64((r1 & (1 << r2)) != 0);
                    }
                    else if (instruction == CMP)
                    {
                        long r1 = convertInt48ToInt64(registers[arg1]);
                        long r2 = convertInt48ToInt64(registers[arg2]);

                        if (r1 == r2)
                        {
                            f.CF = 0; f.SF = 0; f.ZF = 1;
                        }
                        else if (r1 > r2)
                        {
                            f.CF = 0; f.SF = 0; f.ZF = 0;
                        }
                        else if (r1 < r2)
                        {
                            f.CF = 1; f.SF = 1; f.ZF = 0;
                        }
                    }
                    else if (instruction == ADD)
                    {
                        long r1 = convertInt48ToInt64(registers[arg1]);
                        long r2 = convertInt48ToInt64(registers[arg2]);
                        registers[arg3] = convertInt64ToInt48(r1 + r2);
                    }
                    else if (instruction == NAND)
                    {
                        long r1 = convertInt48ToInt64(registers[arg1]);
                        long r2 = convertInt48ToInt64(registers[arg2]);
                        registers[arg3] = convertInt64ToInt48(~(r1 & r2));
                    }
                    else if (instruction == RCL)
                    {
                        long r1 = convertInt48ToInt64(registers[arg1]);
                        int  r2 = Convert.ToInt32(convertInt48ToInt64(registers[arg2]));
                        long t1 = r1;
                        for (int i = 0; i <= r2; i++)
                        {
                            f.CF = Convert.ToInt32(Convert.ToBoolean(t1 & 0x800000000000));
                            t1   = r1 << i;
                            t1  += f.CF;
                        }
                        if (Convert.ToBoolean(t1 & 0x800000000000))
                        {
                            registers[arg3] = convertInt64ToInt48((t1 & 0x0000FFFFFFFFFFFF) - 281474976710656);
                        }
                        else
                        {
                            registers[arg3] = convertInt64ToInt48(t1 & 0x0000FFFFFFFFFFFF);
                        }
                    }

                    // HALT
                    else if (instruction == HALT)       // checked
                    {
                        stateFlags = new Flags(f);
                        states.Add(new StateClass(ip_old, ind_addr, instructionLine, memory, registers, stateFlags, ic));
                        break;
                    }
                    // instructionLine
                    stateFlags = new Flags(f);
                    states.Add(new StateClass(ip_old, ind_addr, instructionLine, memory, registers, stateFlags, ic));
                }
                catch (Exception e)
                {
                    MessageBox.Show("Memory out of bounds", "Memory out of bounds", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    return;
                }
            }
        }