def instr_mtc0( self, fmt, rt , rd ): mne = fmt.format( rt=name(self.GPR, rt), rd=name(self.CPR, rd) ) self.CPR[rd] = self.GPR[rt] self.notify(self.addr, self.opcode, f"{mne}") self.inc_pc()
def instr_slti( self, fmt, rs , rt , immediate ): mne = fmt.format( rt=name(self.GPR, rt), rs=name(self.GPR, rs), immediate=immediate ) self.GPR[rt] = 1 if self.GPR[rs] < immediate else 0 self.notify(self.addr, self.opcode, f"{mne}") self.inc_pc()
def instr_andi( self, fmt, rs , rt , immediate ): mne = fmt.format( rt=name(self.GPR, rt), rs=name(self.GPR, rs), immediate=immediate ) self.GPR[rt] = self.GPR[rs] & immediate self.notify(self.addr, self.opcode, f"{mne}") self.inc_pc()
def instr_addu( self, fmt, rs , rt , rd ): mne = fmt.format( rt=name(self.GPR, rt), rs=name(self.GPR, rs), rd=name(self.GPR, rd) ) t = self.GPR[rs] + self.GPR[rt] #self.GPR[rt] = self.GPR[rs] & immediate self.GPR[rt] = t self.notify(self.addr, self.opcode, f"{mne}") self.inc_pc()
def instr_jalr( self, fmt, rs , rd ): mne = fmt.format( rs=name(self.GPR, rs), rd=name(self.GPR, rd) ) #self.GPR.set(R5900.PC, self.GPR.get(rs)) t = self.GPR[rs] #self.GPR[rd] = self.GPR.PC + 8 self.GPR[rd] = self.GPR.PC + 12 self.GPR.PC = t self.notify(self.addr, self.opcode, f"{mne}")
def instr_bne( self, fmt, rs , rt , offset ): mne = fmt.format( rt=name(self.GPR, rt), rs=name(self.GPR, rs), offset=offset ) self.notify(self.addr, self.opcode, f"{mne}") if self.GPR[rt] != self.GPR[rs]: offset = offset << 2 print(f"JMP +{offset}") self.GPR.PC = self.GPR.PC + 4 + offset else: self.inc_pc()
def instr_li( self, fmt, rt , val ): mne = fmt.format( rt=name(self.GPR, rt), val=val ) self.GPR[rt] = val self.notify(self.addr, self.opcode, f"{mne}") self.inc_pc()
def instr_jr( self, fmt, rs ): mne = fmt.format( rs=name(self.GPR, rs) ) self.GPR.PC = self.GPR[rs] self.notify(self.addr, self.opcode, f"{mne}")
def instr_lui( self, fmt, rt , immediate ): mne = fmt.format( rt=name(self.GPR, rt), immediate=immediate ) self.GPR[rt] = immediate & 0xFFFF0000 self.notify(self.addr, self.opcode, f"{mne}") self.inc_pc()
def instr_sd( self, fmt, base , rt , offset ): mne = fmt.format( rt=name(self.GPR, rt), base=base, offset=offset ) write_addr = self.GPR[base] + offset self.write_word(write_addr, self.GPR[rt] & 0x0000FFFF ) self.notify(self.addr, self.opcode, f"{mne}") self.inc_pc()
def instr_lw( self, fmt, base , rt , offset ): mne = fmt.format( rt=name(self.GPR, rt), base=base, offset=offset ) read_addr = self.GPR[base] + offset #t = self.read_word(read_addr) << 16 #t = t | self.read_word(write_addr + 2) # XXX TLB self.GPR[rt] = unpack("<I", self.read_qword(read_addr))[0] self.notify(self.addr, self.opcode, f"{mne}") self.inc_pc()
def cycle(self, addr): w = self.read_qword(addr) self.parse(addr, w) return print(f"{addr:08x} => {w}") c = unpack("<I", w)[0] print(f"> {addr:08x} ==> HEX: {c:08x} / {c:032b}") if c == 0x0: print("=> NOOP") self.cb(addr, c, "NOOP") elif c == 0x27bdffa0: print("=> UNKNOWN OP CODE {c:08x}, skipping") self.cb(addr, c, "UNKNOWN OP CODE") else: found = False for k, v in self.instr.items(): x = c & v['P'] if v['M'] == x: found = True if k == "lui": args = self.args(v, c) oa = v['F'].format( rt=name(self.GPR, args.rt), immediate=args.immediate ) print(f"=> {k.upper()} {oa}") #self.GPR.set(args.rt, args.immediate & 0xFFFF0000) self.GPR[args.rt] = args.immediate & 0xFFFF0000 #print(f"K0: {self.k0} ") self.cb(addr, c, f"{k.upper()} {oa}") print(f"{self.GPR}") break elif k == "tlbwi": args = self.args(v, c) oa = v['F'].format() print(f"=> {k.upper()} {oa}") print(f"Index : {self.CPR.Index:08x}") print(f"PageMask: {self.CPR.PageMask:08x}") print(f"EntryHi : {self.CPR.PageMask:08x}") print(f"EntryLo0 : {self.CPR.PageMask:08x}") print(f"EntryLo1 : {self.CPR.PageMask:08x}") #self.GPR.set(args.rt, args.immediate & 0xFFFF0000) #print(f"K0: {self.k0} ") self.cb(addr, c, f"{k.upper()} {oa}") print(f"{self.GPR}") break elif k == "li": args = self.args(v, c) oa = v['F'].format( rt=name(self.GPR, args.rt), val=args.val ) print(f"=> {k.upper()} {oa}") #self.GPR.set(args.rt, args.val) #R5900.set_reg(self.GPR, args.rt, args.val) self.GPR[args.rt] = args.val #print(f"K0: {self.k0} ") self.cb(addr, c, f"{k.upper()} {oa}") print(f"{self.GPR}") break elif k == "jr": args = self.args(v, c) oa = v['F'].format( rs=name(self.GPR, args.rs) ) print(f"=> {k.upper()} {oa}") self.GPR.PC = self.GPR[args.rs] #self.GPR.set(R5900.PC, self.GPR.get(args.rs)) #self.GPR.PC = self.GPR[args.rs] self.cb(addr, c, f"{k.upper()} {oa}") print(f"{self.GPR}") return -1 elif k == "jal": args = self.args(v, c) oa = v['F'].format( instr_index=args.instr_index ) print(f"=> {k.upper()} {oa}") #self.GPR.PC = self.GPR[args.rs] #self.GPR.RA = self.GPR.PC + 8 #self.GPR.PC = ( self.GPR.PC + 4 & 0xF0000000 ) | args.instr_index self.GPR.RA = self.GPR.PC + 12 self.GPR.PC = ( self.GPR.PC + 8 & 0xF0000000 ) | args.instr_index #self.GPR.set(R5900.PC, self.GPR.get(args.rs)) #self.GPR.PC = self.GPR[args.rs] self.cb(addr, c, f"{k.upper()} {oa}") print(f"{self.GPR}") return -1 elif k == "jalr": args = self.args(v, c) oa = v['F'].format( rs=name(self.GPR, args.rs), rd=name(self.GPR, args.rd) ) print(f"=> {k.upper()} {oa}") #self.GPR.set(R5900.PC, self.GPR.get(args.rs)) t = self.GPR[args.rs] #self.GPR[args.rd] = self.GPR.PC + 8 self.GPR[args.rd] = self.GPR.PC + 12 self.GPR.PC = t self.cb(addr, c, f"{k.upper()} {oa}") print(f"{self.GPR}") return -1 elif k == "sync": args = self.args(v, c) oa = v['F'].format(stype=args.stype) print(f"=> {k.upper()} {oa}") # TODO implement SYNC self.cb(addr, c, f"{k.upper()} {oa}") print(f"{self.GPR}") break elif k == "ori": args = self.args(v, c) oa = v['F'].format( rt=name(self.GPR, args.rt), rs=name(self.GPR, args.rs), immediate=args.immediate ) print(f"=> {k.upper()} {oa}") self.GPR[args.rt] = self.GPR[args.rs] | args.immediate #print(f"K0: {self.k0} ") self.cb(addr, c, f"{k.upper()} {oa}") print(f"{self.GPR}") break elif k == "bne": args = self.args(v, c) oa = v['F'].format( rt=name(self.GPR, args.rt), rs=name(self.GPR, args.rs), offset=args.offset ) print(f"=> {k.upper()} {oa}") print(f">> {args}") self.cb(addr, c, f"{k.upper()} {oa}") if self.GPR[args.rt] != self.GPR[args.rs]: offset = args.offset << 2 print(f"JMP +{offset}") self.GPR.PC = self.GPR.PC + 4 + offset return -1 break elif k == "sw": args = self.args(v, c) oa = v['F'].format( rt=name(self.GPR, args.rt), base=args.base, offset=args.offset ) print(f"=> {k.upper()} {oa}") print(f">> {args}") write_addr = self.GPR[args.base] + args.offset self.write_word(write_addr, self.GPR[args.rt] & 0xFFFF0000 ) self.write_word(write_addr + 2, self.GPR[args.rt] & 0x0000FFFF ) self.cb(addr, c, f"{k.upper()} {oa}") break elif k == "sd": args = self.args(v, c) oa = v['F'].format( rt=name(self.GPR, args.rt), base=args.base, offset=args.offset ) print(f"=> {k.upper()} {oa}") print(f">> {args}") write_addr = self.GPR[args.base] + args.offset self.write_word(write_addr, self.GPR[args.rt] & 0x0000FFFF ) self.cb(addr, c, f"{k.upper()} {oa}") break elif k == "mfc0": args = self.args(v, c) oa = v['F'].format( rt=name(self.GPR, args.rt), rd=name(self.CPR, args.rd) ) print(f"=> {k.upper()} {oa}") print(f">> {args}") self.GPR[args.rt] = self.CPR[args.rd] print(f"{self.GPR}") print(f"{self.CPR}") self.cb(addr, c, f"{k.upper()} {oa}") break elif k == "mtc0": args = self.args(v, c) oa = v['F'].format( rt=name(self.GPR, args.rt), rd=name(self.CPR, args.rd) ) print(f"=> {k.upper()} {oa}") print(f">> {args}") self.CPR[args.rd] = self.GPR[args.rt] print(f"{self.GPR}") print(f"{self.CPR}") self.cb(addr, c, f"{k.upper()} {oa}") break elif k == "slti": args = self.args(v, c) oa = v['F'].format( rt=name(self.GPR, args.rt), rs=name(self.GPR, args.rs), immediate=args.immediate ) print(f"=> {k.upper()} {oa}") self.GPR[args.rt] = 1 if self.GPR[args.rs] < args.immediate else 0 print(f"{self.GPR}") self.cb(addr, c, f"{k.upper()} {oa}") break if not found: self.cb(addr, c, "N/A", error=True) print(f"XXX {addr:08x} {c:08x} UNHANDLED OP CODE XXX") big = pack(">I", c) little = pack("<I", c) print(f"{big} {little}") return 4