summaryrefslogtreecommitdiff
path: root/lib/mipsdump.cpp
blob: ac77b33603fab1b2e5e69b086b1355671b2465e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 *  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: mipsdump.cpp,v 1.1 2004-01-03 15:04:47 pixel Exp $ */

#include "mipsdump.h"
#include "mips.h"

TDump::TDump(mipsmem * mm) : TDis(mm) {
    reset();
}

void TDump::reset() {
    invalid = false;
    hasbr = false;
    hastg = false;
    hasfc = false;
    name = "";
    comments = "";
    args.clear();
}

void TDump::add_branch(Uint32 _tg) {
    tg = _tg;
    hasbr = true;
}

void TDump::add_jump(Uint32 _tg) {
    tg = _tg;
    hastg = true;
}

void TDump::add_function(Uint32 _tg) {
    tg = _tg;
    hasfc = true;
}

void TDump::SetTag(Uint32 a, int t, bool v) {
}

void TDump::Name(const String & _name) {
    name = _name;
}

void TDump::PushGPReg(int r) {
    pairarg p;
    
    p.left = T_GPREGISTER;
    p.right.v = r;
    
    args.push_back(p);
}

void TDump::PushCPReg(int r) {
    pairarg p;
    
    p.left = T_CPREGISTER;
    p.right.v = r;
    
    args.push_back(p);
}

void TDump::PushImm(Uint32 imm) {
    pairarg p;
    
    p.left = T_IMM16;
    p.right.v = imm;
    
    args.push_back(p);
}

void TDump::PushTarget(Uint32 target) {
    pairarg p;
    
    p.left = T_IMM32;
    p.right.v = target;
    
    args.push_back(p);
}

void TDump::PushSa(Uint32 sa) {
    pairarg p;
    
    p.left = T_IMM8;
    p.right.v = sa;
    
    args.push_back(p);
}

void TDump::PushOfB(int reg, Uint32 offset, int width) {
    pairarg p;
    
    p.left = T_OFB;
    p.right.OfB.o = offset;
    p.right.OfB.r = reg;
    p.right.OfB.w = width;
    
    args.push_back(p);
}

void TDump::PushOffset(Uint32 offset) {
    pairarg p;
    
    p.left = T_IMM32;
    p.right.v = offset;
    
    args.push_back(p);
}

void TDump::PushFull(Uint32 full) {
    pairarg p;
    
    p.left = T_IMM32;
    p.right.v = full;
    
    args.push_back(p);
}

void TDump::Invalid() {
    invalid = true;
}

void TDump::Suspect() {
}

void TDump::Comment(const String & c) {
    comments = c;
}

Dumper::Dumper(mipsmem * _mm) : dump(new TDump(_mm)), mm(_mm) {
}

void Dumper::process() {
    Uint32 pc, code;
    memdata * mem;
    
    for (pc = 0x80000000; pc < (0x80000000 + PSXMEM); pc++) {
	if (mm->GetTag(pc, CODE)) {
	    decode(dump, pc);
	    code = mm->Read32(pc);
	    printm(M_STATUS, "%8.8lX %8.8lX: " + dump->name + "\t", pc, code);
	    for (std::vector<pairarg>::iterator i = dump->args.begin(); i != dump->args.end(); i++) {
		switch(i->left) {
		case T_GPREGISTER:
		    printm(M_BARE, "$%s", registers[i->right.v]);
		    break;
		case T_CPREGISTER:
		    printm(M_BARE, "$%s", CP0registers[i->right.v]);
		    break;
		case T_IMM8:
		    printm(M_BARE, "0x%2.2lX", i->right.v);
		    break;
		case T_IMM16:
		    printm(M_BARE, "0x%4.4lX", i->right.v);
		    break;
		case T_IMM32:
		    printm(M_BARE, "0x%8.8lX", i->right.v);
		    break;
		case T_OFB:
		    printm(M_BARE, "0x%4.4lX($%s)", i->right.OfB.o, registers[i->right.OfB.r]);
		    break;
		}
		if ((i + 1) != dump->args.end()) {
		    printm(M_BARE, ", ");
		}
	    }
	    if (dump->comments != "") {
		printm(M_BARE, "\t; " + dump->comments);
	    }
	    printm(M_BARE, "\n");
	    mem = mm->GetDatas(pc);
	    if (mem) {
		reffrom_t * from;
		refto_t * to;
		
		from = mem->getreffrom();
		to = mem->getrefto();
		
		for (from = mem->getreffrom(); from; from = from->getnext()) {
		    printm(M_STATUS, "     Reference from 0x%8.8lX\n", from->getref()->getaddress());
		}
		
		if (to) {
		    printm(M_STATUS, "     Reference to 0x%8.8lX\n", to->getref()->getaddress());
		}
	    }
	    pc += 3;
	    dump->reset();
	}
    }
}