CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/MIPS/MIPSDis.cpp
Views: 1401
// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include <cstring>18#include "Common/StringUtils.h"19#include "Core/HLE/HLE.h"20#include "Core/MemMap.h"21#include "Core/MIPS/MIPS.h"22#include "Core/MIPS/MIPSCodeUtils.h"23#include "Core/MIPS/MIPSDis.h"24#include "Core/MIPS/MIPSTables.h"25#include "Core/MIPS/MIPSDebugInterface.h"2627#define _RS ((op>>21) & 0x1F)28#define _RT ((op>>16) & 0x1F)29#define _RD ((op>>11) & 0x1F)30#define _FS ((op>>11) & 0x1F)31#define _FT ((op>>16) & 0x1F)32#define _FD ((op>>6 ) & 0x1F)33#define _POS ((op>>6 ) & 0x1F)34#define _SIZE ((op>>11) & 0x1F)3536#define RN(i) (currentDebugMIPS->GetRegName(0, i).c_str())37#define FN(i) (currentDebugMIPS->GetRegName(1, i).c_str())38//#define VN(i) (currentDebugMIPS->GetRegName(2, i).c_str())3940namespace MIPSDis41{42std::string SignedHex(int i) {43char temp[32];44int offset = 0;45if (i < 0)46{47temp[0] = '-';48offset = 1;49i = -i;50}5152snprintf(&temp[offset], sizeof(temp) - offset, "0x%X", i);53return temp;54}5556void Dis_Generic(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {57snprintf(out, outSize, "%s\t --- unknown ---", MIPSGetName(op));58}5960void Dis_Cache(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {61int imm = SignExtend16ToS32(op & 0xFFFF);62int rs = _RS;63int func = (op >> 16) & 0x1F;64snprintf(out, outSize, "%s\tfunc=%i, %s(%s)", MIPSGetName(op), func, RN(rs), SignedHex(imm).c_str());65}6667void Dis_mxc1(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {68int fs = _FS;69int rt = _RT;70const char *name = MIPSGetName(op);71snprintf(out, outSize, "%s\t%s, %s", name, RN(rt), FN(fs));72}7374void Dis_FPU3op(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {75int ft = _FT;76int fs = _FS;77int fd = _FD;78const char *name = MIPSGetName(op);79snprintf(out, outSize, "%s\t%s, %s, %s", name, FN(fd), FN(fs), FN(ft));80}8182void Dis_FPU2op(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {83int fs = _FS;84int fd = _FD;85const char *name = MIPSGetName(op);86snprintf(out, outSize, "%s\t%s, %s", name, FN(fd), FN(fs));87}8889void Dis_FPULS(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {90int offset = SignExtend16ToS32(op & 0xFFFF);91int ft = _FT;92int rs = _RS;93const char *name = MIPSGetName(op);94snprintf(out, outSize, "%s\t%s, %s(%s)", name, FN(ft), SignedHex(offset).c_str(), RN(rs));95}9697void Dis_FPUComp(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {98int fs = _FS;99int ft = _FT;100const char *name = MIPSGetName(op);101snprintf(out, outSize, "%s\t%s, %s", name, FN(fs), FN(ft));102}103104void Dis_FPUBranch(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {105u32 off = pc;106int imm = SignExtend16ToS32(op & 0xFFFF) << 2;107off += imm + 4;108const char *name = MIPSGetName(op);109snprintf(out, outSize, "%s\t->$%08x", name, off);110}111112void Dis_RelBranch(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {113u32 off = pc;114int imm = SignExtend16ToS32(op & 0xFFFF) << 2;115int rs = _RS;116off += imm + 4;117118const char *name = MIPSGetName(op);119snprintf(out, outSize, "%s\t%s, ->$%08x", name, RN(rs), off);120}121122void Dis_Syscall(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {123u32 callno = (op>>6) & 0xFFFFF; //20 bits124int funcnum = callno & 0xFFF;125int modulenum = (callno & 0xFF000) >> 12;126snprintf(out, outSize, "syscall\t %s", GetFuncName(modulenum, funcnum));127}128129void Dis_ToHiloTransfer(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {130int rs = _RS;131const char *name = MIPSGetName(op);132snprintf(out, outSize, "%s\t%s", name, RN(rs));133}134void Dis_FromHiloTransfer(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {135int rd = _RD;136const char *name = MIPSGetName(op);137snprintf(out, outSize, "%s\t%s", name, RN(rd));138}139140void Dis_RelBranch2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {141u32 off = pc;142int imm = SignExtend16ToS32(op & 0xFFFF) << 2;143int rt = _RT;144int rs = _RS;145off += imm + 4;146147const char *name = MIPSGetName(op);148int o = op>>26;149if (o==4 && rs == rt)//beq150snprintf(out, outSize, "b\t->$%08x", off);151else if (o==20 && rs == rt)//beql152snprintf(out, outSize, "bl\t->$%08x", off);153else154snprintf(out, outSize, "%s\t%s, %s, ->$%08x", name, RN(rs), RN(rt), off);155}156157void Dis_IType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {158u32 uimm = op & 0xFFFF;159u32 suimm = SignExtend16ToU32(op);160s32 simm = SignExtend16ToS32(op);161162int rt = _RT;163int rs = _RS;164const char *name = MIPSGetName(op);165switch (op >> 26)166{167case 8: //addi168case 9: //addiu169case 10: //slti170snprintf(out, outSize, "%s\t%s, %s, %s", name, RN(rt), RN(rs), SignedHex(simm).c_str());171break;172case 11: //sltiu173snprintf(out, outSize, "%s\t%s, %s, 0x%X", name, RN(rt), RN(rs), suimm);174break;175default:176snprintf(out, outSize, "%s\t%s, %s, 0x%X", name, RN(rt), RN(rs), uimm);177break;178}179}180void Dis_ori(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {181u32 uimm = op & 0xFFFF;182int rt = _RT;183int rs = _RS;184const char *name = MIPSGetName(op);185if (rs == 0)186snprintf(out, outSize, "li\t%s, 0x%X", RN(rt), uimm);187else188snprintf(out, outSize, "%s\t%s, %s, 0x%X", name, RN(rt), RN(rs), uimm);189}190191void Dis_IType1(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {192u32 uimm = op & 0xFFFF;193int rt = _RT;194const char *name = MIPSGetName(op);195snprintf(out, outSize, "%s\t%s, 0x%X", name, RN(rt), uimm);196}197198void Dis_addi(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {199int imm = SignExtend16ToS32(op & 0xFFFF);200int rt = _RT;201int rs = _RS;202if (rs == 0)203snprintf(out, outSize, "li\t%s, %s", RN(rt), SignedHex(imm).c_str());204else205Dis_IType(op, pc, out, outSize);206}207208void Dis_ITypeMem(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {209int imm = SignExtend16ToS32(op & 0xFFFF);210int rt = _RT;211int rs = _RS;212const char *name = MIPSGetName(op);213snprintf(out, outSize, "%s\t%s, %s(%s)", name, RN(rt), SignedHex(imm).c_str(), RN(rs));214}215216void Dis_RType2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {217int rs = _RS;218int rd = _RD;219const char *name = MIPSGetName(op);220snprintf(out, outSize, "%s\t%s, %s", name, RN(rd), RN(rs));221}222223void Dis_RType3(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {224int rt = _RT;225int rs = _RS;226int rd = _RD;227const char *name = MIPSGetName(op);228snprintf(out, outSize, "%s\t%s, %s, %s", name, RN(rd), RN(rs), RN(rt));229}230231void Dis_addu(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {232int rt = _RT;233int rs = _RS;234int rd = _RD;235const char *name = MIPSGetName(op);236if (rs==0 && rt==0)237snprintf(out, outSize, "li\t%s, 0", RN(rd));238else if (rs == 0)239snprintf(out, outSize, "move\t%s, %s", RN(rd), RN(rt));240else if (rt == 0)241snprintf(out, outSize, "move\t%s, %s", RN(rd), RN(rs));242else243snprintf(out, outSize, "%s\t%s, %s, %s", name, RN(rd), RN(rs), RN(rt));244}245246void Dis_ShiftType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {247int rt = _RT;248int rs = _RS;249int rd = _RD;250int sa = (op>>6) & 0x1F;251const char *name = MIPSGetName(op);252if (((op & 0x3f) == 2) && rs == 1)253name = "rotr";254if (((op & 0x3f) == 6) && sa == 1)255name = "rotrv";256snprintf(out, outSize, "%s\t%s, %s, 0x%X", name, RN(rd), RN(rt), sa);257}258259void Dis_VarShiftType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {260int rt = _RT;261int rs = _RS;262int rd = _RD;263int sa = (op>>6) & 0x1F;264const char *name = MIPSGetName(op);265if (((op & 0x3f) == 6) && sa == 1)266name = "rotrv";267snprintf(out, outSize, "%s\t%s, %s, %s", name, RN(rd), RN(rt), RN(rs));268}269270void Dis_MulDivType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {271int rt = _RT;272int rs = _RS;273const char *name = MIPSGetName(op);274snprintf(out, outSize, "%s\t%s, %s", name, RN(rs), RN(rt));275}276277void Dis_Special3(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {278int rs = _RS;279int Rt = _RT;280int pos = _POS;281const char *name = MIPSGetName(op);282283switch (op & 0x3f)284{285case 0x0: //ext286{287int size = _SIZE + 1;288snprintf(out, outSize, "%s\t%s, %s, 0x%X, 0x%X", name, RN(Rt), RN(rs), pos, size);289}290break;291case 0x4: // ins292{293int size = (_SIZE + 1) - pos;294snprintf(out, outSize, "%s\t%s, %s, 0x%X, 0x%X", name, RN(Rt), RN(rs), pos, size);295}296break;297}298}299300void Dis_JumpType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {301u32 off = ((op & 0x03FFFFFF) << 2);302u32 addr = (pc & 0xF0000000) | off;303const char *name = MIPSGetName(op);304snprintf(out, outSize, "%s\t->$%08x", name, addr);305}306307void Dis_JumpRegType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {308int rs = _RS;309int rd = _RD;310const char *name = MIPSGetName(op);311if ((op & 0x3f) == 9 && rd != MIPS_REG_RA)312snprintf(out, outSize, "%s\t%s,->%s", name, RN(rd), RN(rs));313else314snprintf(out, outSize, "%s\t->%s", name, RN(rs));315}316317void Dis_Allegrex(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {318int rt = _RT;319int rd = _RD;320const char *name = MIPSGetName(op);321snprintf(out, outSize, "%s\t%s,%s", name, RN(rd), RN(rt));322}323324void Dis_Allegrex2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {325int rt = _RT;326int rd = _RD;327const char *name = MIPSGetName(op);328snprintf(out, outSize,"%s\t%s,%s", name, RN(rd), RN(rt));329}330331void Dis_Emuhack(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {332auto resolved = Memory::Read_Instruction(pc, true);333char disasm[256];334if (MIPS_IS_EMUHACK(resolved)) {335truncate_cpy(disasm, sizeof(disasm), "(invalid emuhack)");336} else {337MIPSDisAsm(resolved, pc, disasm, sizeof(disasm), true);338}339340switch (op.encoding >> 24) {341case 0x68:342snprintf(out, outSize, "* jitblock: %s", disasm);343break;344case 0x6a:345snprintf(out, outSize, "* replacement: %s", disasm);346break;347default:348snprintf(out, outSize, "* (invalid): %s", disasm);349break;350}351}352353354}355356357