/* * PSX-Tools Bundle Pack * Copyright (C) 2002-2003 Nicolas "Pixel" Noble * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* $Id: mipsdis.cpp,v 1.2 2004-01-26 15:31:55 pixel Exp $ */ #include "mipsdis.h" #include "mips.h" TDis::TDis(mipsmem * _m) : mm(_m) { reset(); } void TDis::reset() { invalid = false; } mipsmem * TDis::getmem() { return mm; } void TDis::add_branch(Uint32 target) { bheap.push(target); } void TDis::add_jump(Uint32 target) { jheap.push(target); } void TDis::add_function(Uint32 target) { fheap.push(target); } void TDis::SetTag(Uint32 target, int tag, bool v) { mm->SetTag(target, tag, v); } void TDis::Name(const String & name) { } void TDis::PushGPReg(int reg) { } void TDis::PushCPReg(int reg) { } void TDis::PushImm(Uint32 imm) { } void TDis::PushTarget(Uint32 target) { new refto_t(target, memdata::getmem(pc, getmem())); } void TDis::PushSa(Uint32 sa) { } void TDis::PushOfB(int reg, Uint32 offset, int width) { } void TDis::PushOffset(Uint32 offset) { new refto_t(offset, memdata::getmem(pc, getmem())); } void TDis::PushFull(Uint32 full) { if ((full >= 0x80000000) && (full < (0x80000000 + PSXMEM))) new refto_t(full, memdata::getmem(pc, getmem())); } void TDis::Invalid() { invalid = true; } void TDis::Suspect() { } void TDis::Comment(const String & c) { } Disassembler::Disassembler(mipsmem * _mm) : mm(_mm), dis(new TDis(mm)), started(false), infunction(false) { } Disassembler::~Disassembler() { delete dis; } void Disassembler::crawl_code(Uint32 pc) { Uint32 branched, ipc; if (pc == 0xffffffff) { pc = mm->GetPC(); } dis->bheap.push(pc); while (dis->bheap.size()) { branched = pc = dis->bheap.top(); dis->bheap.pop(); printm(M_STATUS, "Crawling to branch %8.8lX\n", pc); do { if (pc >= (0x80000000 + PSXMEM)) { dis->invalid = true; break; } if (mm->GetTag(pc, CODE) || mm->GetTag(pc, INVALID)) { pc += 4; continue; } mm->SetTag(pc, CODE, true); printm(M_STATUS, "Working at %8.8lX\n", pc); decode(dis, pc); pc += 4; dis->reset(); } while (!mm->GetTag(pc, STOP) && !dis->invalid); if (dis->invalid) { for (ipc = branched; ipc <= pc; ipc += 4) { mm->SetTag(ipc, CODE, false); mm->SetTag(ipc, INVALID, true); } } if (dis->invalid && infunction) { } } } void Disassembler::mainloop(void) { Uint32 pc; infunction = false; // Crawl the start part. printm(M_STATUS, "Starting crawl at %8.8lX\n", mm->GetPC()); if (!started) crawl_code(); started = true; // Work out all the functions. printm(M_STATUS, "Crawling all detected functions\n"); infunction = true; while (dis->fheap.size()) { pc = dis->fheap.top(); dis->fheap.pop(); printm(M_STATUS, "Crawling function %8.8lX\n", pc); if (mm->GetTag(pc, CODE)) continue; crawl_code(pc); } // Complete functions and all the detected jumps. printm(M_STATUS, "Fixing all the remaining jumps\n"); #if 0 infunction = false; while (dis->jheap.size()) { pc = dis->jheap.top(); dis->jheap.pop(); if (mm->GetTag(pc, CODE)) continue; crawl_code(pc); } #endif }