Esempio n. 1
0
    def debug(self):
        while True:
            cmd = input("Press Enter to continue...\n> ").strip()
            if not cmd:
                return
            cmd = cmd.upper().split()
            if cmd[0] == 'A': # input and run the code (simple code)
                asm_code = input("Run code > ")
                ins = [s for s in re.split(" |,", asm_code.strip().upper()) if s]
                self.EU.print(ins)
                self.EU.opcode = ins[0]
                if len(ins) > 1:
                    self.EU.opd = ins[1:]
                self.EU.get_opbyte()
                self.EU.control_circuit()
                return

            if cmd[0] == 'D': # show memory:  d 0x20000
                if len(cmd) == 2:
                    adr1 = self.EU.get_int(cmd[1])
                    self.show_memory(adr1, adr1 + 50)
                elif len(cmd) == 3:
                    adr1 = to_decimal(cmd[1])
                    adr2 = to_decimal(cmd[2])
                    self.show_memory(adr1, adr2)

            elif cmd[0] == 'R': # show registers
                self.show_regs()
Esempio n. 2
0
    def miscellaneous_ins(self):
        if self.opcode == 'NOP':
            pass
        elif self.opcode == 'INT':
            # print('中断开始')
            if not self.opd:
                self.print("\n断点中断\n")
                self.interrupt = True
            else:
                int_type = to_decimal(self.opd[0])
                if int_type == 3:  # 断点
                    self.print("\n断点中断\n")
                    self.interrupt = True
                elif int_type == to_decimal('10H'):
                    self.bios_isr_10h()
                elif int_type == to_decimal('21H'):
                    self.dos_isr_21h()
                elif int_type in [to_decimal(i) for i in ['7ch']]:
                    self.interrupt_handler(int_type)
                else:
                    sys.exit("Interrupt Type Error")

        elif self.opcode == 'IRET':
            if self.int_msg:
                self.print("中断例程结束,恢复现场中...\n")
            self.opcode = 'POP'
            self.opd = ['IP']
            self.control_circuit()

            self.opcode = 'POP'
            self.opd = ['CS']
            self.control_circuit()

            self.opcode = 'POPF'
            self.control_circuit()
            if self.int_msg:
                self.print("恢复现场成功\n")

        elif self.opcode == 'XLAT':
            pass
        elif self.opcode == 'HLT':
            self.shutdown = True
        elif self.opcode == 'ESC':
            pass
        elif self.opcode == 'INTO':
            if self.FR.overflow:
                self.interrupt_handler(4)  # Overflow Interrupt
        elif self.opcode == 'LOCK':
            pass
        elif self.opcode == 'WAIT':
            pass
        else:
            sys.exit("operation code not support")
Esempio n. 3
0
    def get_int(self, opd):
        # 自动获取操作数值
        # 若opd为int也作为地址访问
        if isinstance(opd, int):
            opd = '[' + str(opd) + ']'
        # 寄存器
        if self.is_reg(opd):
            res = self.read_reg(opd)
        # 内存
        elif '[' in opd:
            if self.opbyte == 1:
                res_list = self.__get_byte(opd)
            elif self.opbyte == 2:
                res_list = self.__get_word(opd)
            elif self.opbyte == 4:
                res_list = self.__get_dword(opd)
            else:
                sys.exit("Opbyte Error")
            res = 0
            assert res_list, "Empty memory space"
            # print("res_list", res_list)

            for num in res_list:
                res = (res << 8) + (int(num, 16) & 0xff)
        # 立即数
        else:
            res = to_decimal(opd)
        # self.print("get_int " + hex(res) + " from " + opd)
        return res
Esempio n. 4
0
 def input_output_ins(self):
     if self.opcode == 'IN':
         # Input from port into AL or AX. IN AX, 4; IN AL, 7;
         # And we are not restricted to AL and AX, you can input to all regs.
         port = to_decimal(self.opd[1])
         val = to_decimal(input(f"Input to Port {port}: "))
         self.write_reg(self.opd[0], val)
     elif self.opcode == 'OUT':
         # Output from AL or AX to port. OUT 4, AX; OUT DX, AX
         # And we are not restricted to AL and AX, you can output from all regs.
         # If port > 255, use DX.
         port = self.get_int(self.opd[0])
         val = self.read_reg(self.opd[1])
         self.print("> " * 16 +
                    "@Port {}: 0x{:<4x} => {}\n".format(port, val, val))
     else:
         sys.exit("operation code not support")
Esempio n. 5
0
 def get_offset(self, opd):
     # 解析所有内存寻址:直接、寄存器间接、基址、变址、基址变址、相对基址变址
     # lea 0x3412:0x34; lea [si+bx]; lea 0x12; lea ss:offset; lea [bx][di]
     adr_reg = ['BX', 'SI', 'DI', 'BP']
     seg_reg = ['DS', 'CS', 'SS', 'ES']
     opd = opd.split(':')[-1]  # 去掉段前缀
     par_list = [s for s in re.split('\W', opd) if s]
     offset = 0
     for par in par_list:
         if par in adr_reg:
             offset += self.read_reg(par)
         elif par in seg_reg:
             pass
         else:
             offset += to_decimal(par)
     return offset
Esempio n. 6
0
 def get_address(self, opd):
     # 解析所有内存寻址:直接、寄存器间接、基址、变址、基址变址、相对基址变址
     adr_reg = ['BX', 'SI', 'DI', 'BP']
     seg_reg = ['DS', 'CS', 'SS', 'ES']
     par_list = [s for s in re.split('\W', opd) if s]
     address = 0
     has_seg = False
     for par in par_list:
         if par in adr_reg:
             address += self.read_reg(par)
         elif par in seg_reg:
             address += self.read_reg(par) << 4
             has_seg = True
         else:
             address += to_decimal(par)
     if not has_seg:
         if 'BP' in par_list:  # 存在BP时默认段寄存器为SS
             address += self.read_reg('SS') << 4
         else:
             address += self.read_reg('DS') << 4
     # self.print("Get Address " + hex(address) + " from operand: " + opd)
     return address
Esempio n. 7
0
 def __get_char(self, address):
     # 获取内存address处的ASCII字符
     return chr(to_decimal(self.bus.read_byte(address)[0]))