| 1 |
/* V810 Emulator |
| 2 |
* |
| 3 |
* Copyright (C) 2006 David Tucker |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
////////////////////////////////////////////////////////// |
| 21 |
// CPU Debug routines |
| 22 |
|
| 23 |
//what do we realy need? |
| 24 |
//#include <stdio.h> |
| 25 |
//#include <stdlib.h> |
| 26 |
//#include <string.h> |
| 27 |
//#include <ctype.h> |
| 28 |
|
| 29 |
#include "../../mednafen.h" |
| 30 |
#include "v810_opt.h" |
| 31 |
#include "v810_cpu.h" |
| 32 |
#include "v810_cpuD.h" |
| 33 |
|
| 34 |
|
| 35 |
//////////////////////////////////////////////////////////// |
| 36 |
// Defines |
| 37 |
|
| 38 |
//Structure to store an element in our linked list |
| 39 |
// used to dynamicaly dissasemble a rom |
| 40 |
typedef struct dasms { |
| 41 |
int offset; |
| 42 |
uint32 PC; |
| 43 |
uint32 jump; |
| 44 |
struct dasms * nextElement; |
| 45 |
} dasmS; |
| 46 |
|
| 47 |
typedef struct { |
| 48 |
int addr_mode; // Addressing mode |
| 49 |
const char * opname; // Opcode name (string) |
| 50 |
} operation; |
| 51 |
|
| 52 |
static const operation optable[80] = { |
| 53 |
{ AM_I, "mov " }, // 0x00 |
| 54 |
{ AM_I, "add " }, // 0x01 |
| 55 |
{ AM_I, "sub " }, // 0x02 |
| 56 |
{ AM_I, "cmp " }, // 0x03 |
| 57 |
{ AM_I, "shl " }, // 0x04 |
| 58 |
{ AM_I, "shr " }, // 0x05 |
| 59 |
{ AM_I, "jmp " }, // 0x06 |
| 60 |
{ AM_I, "sar " }, // 0x07 |
| 61 |
{ AM_I, "mul " }, // 0x08 |
| 62 |
{ AM_I, "div " }, // 0x09 |
| 63 |
{ AM_I, "mulu " }, // 0x0A |
| 64 |
{ AM_I, "divu " }, // 0x0B |
| 65 |
{ AM_I, "or " }, // 0x0C |
| 66 |
{ AM_I, "and " }, // 0x0D |
| 67 |
{ AM_I, "xor " }, // 0x0E |
| 68 |
{ AM_I, "not " }, // 0x0F |
| 69 |
|
| 70 |
{ AM_II, "mov " }, // 0x10 // Imediate |
| 71 |
{ AM_II, "add " }, // 0x11 |
| 72 |
{ AM_II, "setf " }, // 0x12 |
| 73 |
{ AM_II, "cmp " }, // 0x13 |
| 74 |
{ AM_II, "shl " }, // 0x14 |
| 75 |
{ AM_II, "shr " }, // 0x15 |
| 76 |
{0x8000|AM_IX,"ei " }, // 0x16 // EI(VB only) |
| 77 |
{ AM_II, "sar " }, // 0x17 |
| 78 |
{ AM_II, "trap " }, // 0x18 |
| 79 |
|
| 80 |
{ AM_IX, "reti " }, // 0x19 //BRKRETI |
| 81 |
{ AM_IX, "halt " }, // 0x1A //STBY |
| 82 |
|
| 83 |
{AM_UDEF, "??? " }, // 0x1B // Unknown |
| 84 |
{ AM_II, "ldsr " }, // 0x1C |
| 85 |
{ AM_II, "stsr " }, // 0x1D |
| 86 |
{0x8000|AM_IX,"di " }, // 0x1E // DI(VB only) |
| 87 |
{AM_BSTR, "BSTR " }, // 0x1F // Special Bit String Instructions |
| 88 |
|
| 89 |
{AM_UDEF, "??? " }, // 0x20 // Unknown // This is a fudg on our part |
| 90 |
{AM_UDEF, "??? " }, // 0x21 // Unknown // We have 6 and 7 bit instructions |
| 91 |
{AM_UDEF, "??? " }, // 0x22 // Unknown // this is filld in by the Conditional Branch Instructions |
| 92 |
{AM_UDEF, "??? " }, // 0x23 // Unknown |
| 93 |
{AM_UDEF, "??? " }, // 0x24 // Unknown |
| 94 |
{AM_UDEF, "??? " }, // 0x25 // Unknown |
| 95 |
{AM_UDEF, "??? " }, // 0x26 // Unknown |
| 96 |
{AM_UDEF, "??? " }, // 0x27 // Unknown |
| 97 |
|
| 98 |
{ AM_V, "movea" }, // 0x28 |
| 99 |
{ AM_V, "addi " }, // 0x29 |
| 100 |
{ AM_IV, "jr " }, // 0x2A |
| 101 |
{ AM_IV, "jal " }, // 0x2B |
| 102 |
{ AM_V, "ori " }, // 0x2C |
| 103 |
{ AM_V, "andi " }, // 0x2D |
| 104 |
{ AM_V, "xori " }, // 0x2E |
| 105 |
{ AM_V, "movhi" }, // 0x2F |
| 106 |
|
| 107 |
{ AM_VIa, "ld.b " }, // 0x30 |
| 108 |
{ AM_VIa, "ld.h " }, // 0x31 |
| 109 |
{AM_UDEF, "??? " }, // 0x32 // Unknown |
| 110 |
{ AM_VIa, "ld.w " }, // 0x33 |
| 111 |
{ AM_VIb, "st.b " }, // 0x34 |
| 112 |
{ AM_VIb, "st.h " }, // 0x35 |
| 113 |
{AM_UDEF, "??? " }, // 0x36 // Unknown |
| 114 |
{ AM_VIb, "st.w " }, // 0x37 |
| 115 |
{ AM_VIa, "in.b " }, // 0x38 |
| 116 |
{ AM_VIa, "in.h " }, // 0x39 |
| 117 |
{ AM_VIa, "caxi " }, // 0x3A |
| 118 |
{ AM_VIa, "in.w " }, // 0x3B |
| 119 |
{ AM_VIb, "out.b" }, // 0x3C |
| 120 |
{ AM_VIb, "out.h" }, // 0x3D |
| 121 |
{ AM_FPP, "FPP " }, // 0x3E //Floating Point Instruction, Special Case |
| 122 |
{ AM_VIb, "out.w" }, // 0x3F |
| 123 |
|
| 124 |
{ AM_III, "bv " }, // 0x40 |
| 125 |
{ AM_III, "bl " }, // 0x41 //BC 0x41 |
| 126 |
{ AM_III, "be " }, // 0x42 //BZ 0x42 |
| 127 |
{ AM_III, "bnh " }, // 0x43 |
| 128 |
{ AM_III, "bn " }, // 0x44 |
| 129 |
{ AM_III, "br " }, // 0x45 |
| 130 |
{ AM_III, "blt " }, // 0x46 |
| 131 |
{ AM_III, "ble " }, // 0x47 |
| 132 |
{ AM_III, "bnv " }, // 0x48 |
| 133 |
{ AM_III, "bnl " }, // 0x49 //BNC 0x49 |
| 134 |
{ AM_III, "bne " }, // 0x4A //BNZ 0x4A |
| 135 |
{ AM_III, "bh " }, // 0x4B |
| 136 |
{ AM_III, "bp " }, // 0x4C |
| 137 |
{ AM_III, "nop " }, // 0x4D |
| 138 |
{ AM_III, "bge " }, // 0x4E |
| 139 |
{ AM_III, "bgt " } // 0x4F |
| 140 |
}; |
| 141 |
// All instructions greater than 0x50 are undefined (this should not be posible of cource) |
| 142 |
|
| 143 |
|
| 144 |
//Structure for holding the SubOpcodes, Same as above, without the InsType. |
| 145 |
typedef struct { |
| 146 |
const char * opname; // Opcode name (string) |
| 147 |
} suboperation; |
| 148 |
|
| 149 |
|
| 150 |
// Bit String Subopcodes |
| 151 |
static const suboperation bssuboptable[16] = { |
| 152 |
{ "SCH0BSU" }, // 0x00 |
| 153 |
{ "SCH0BSD" }, // 0x01 |
| 154 |
{ "SCH1BSU" }, // 0x02 |
| 155 |
{ "SCH1BSD" }, // 0x03 |
| 156 |
{ "BError4" }, // 0x04 // Unknown |
| 157 |
{ "BError5" }, // 0x05 // Unknown |
| 158 |
{ "BError6" }, // 0x06 // Unknown |
| 159 |
{ "BError7" }, // 0x07 // Unknown |
| 160 |
{ "ORBSU " }, // 0x08 |
| 161 |
{ "ANDBSU " }, // 0x09 |
| 162 |
{ "XORBSU " }, // 0x0A |
| 163 |
{ "MOVBSU " }, // 0x0B |
| 164 |
{ "ORNBSU " }, // 0x0C |
| 165 |
{ "ANDNBSU" }, // 0x0D |
| 166 |
{ "XORNBSU" }, // 0x0E |
| 167 |
{ "NOTBSU " } // 0x0F |
| 168 |
}; |
| 169 |
|
| 170 |
// Floating Point Subopcodes |
| 171 |
static const suboperation fpsuboptable[16] = { |
| 172 |
{ "cmpf.s " }, // 0x00 |
| 173 |
{ "FError1" }, // 0x01 // Unknown |
| 174 |
{ "cvt.ws " }, // 0x02 |
| 175 |
{ "cvt.sw " }, // 0x03 |
| 176 |
{ "addf.s " }, // 0x04 |
| 177 |
{ "subf.s " }, // 0x05 |
| 178 |
{ "mulf.s " }, // 0x06 |
| 179 |
{ "divf.s " }, // 0x07 |
| 180 |
|
| 181 |
{ "FError8" }, // 0x08 // Invalid |
| 182 |
{ "FError9" }, // 0x09 // Invalid |
| 183 |
{ "FErrorA" }, // 0x0A // Invalid |
| 184 |
{ "FErrorB" }, // 0x0B // Invalid |
| 185 |
{ "FErrorC" }, // 0x0C // Invalid |
| 186 |
{ "FErrorD" }, // 0x0D // Invalid |
| 187 |
{ "FErrorE" }, // 0x0E // Invalid |
| 188 |
{ "FErrorF" } // 0x0F // Invalid |
| 189 |
}; |
| 190 |
|
| 191 |
static const suboperation fpsuboptable_vb[16] = { |
| 192 |
{ "cmpf.s " }, // 0x00 |
| 193 |
{ "FError1" }, // 0x01 // Unknown |
| 194 |
{ "cvt.ws " }, // 0x02 |
| 195 |
{ "cvt.sw " }, // 0x03 |
| 196 |
{ "addf.s " }, // 0x04 |
| 197 |
{ "subf.s " }, // 0x05 |
| 198 |
{ "mulf.s " }, // 0x06 |
| 199 |
{ "divf.s " }, // 0x07 |
| 200 |
{ "XB" }, // 0x08 // undocumented |
| 201 |
{ "XH" }, // 0x09 // undocumented //VFishing??? |
| 202 |
{ "REV" }, // 0x0A // undocumented |
| 203 |
{ "trnc.sw" }, // 0x0B |
| 204 |
{ "MPYHW" }, // 0x0C // undocumented |
| 205 |
{ "FErrorD" }, // 0x0D // Unknown |
| 206 |
{ "FErrorE" }, // 0x0E // Unknown |
| 207 |
{ "FErrorF" } // 0x0F // Unknown |
| 208 |
}; |
| 209 |
|
| 210 |
static const char *pretty_preg_names[32] = |
| 211 |
{ |
| 212 |
"r0", "r1", "hsp", "sp", "gp", "tp", "r6", "r7", |
| 213 |
"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", |
| 214 |
"r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", |
| 215 |
"r24", "r25", "r26", "r27", "r28", "r29", "r30", "lp", |
| 216 |
}; |
| 217 |
|
| 218 |
static const char *pretty_sreg_names[32] = |
| 219 |
{ |
| 220 |
"sr0(eipc)", "sr1(eipsw)", "sr2(fepc)", "sr3(fepsw)", "sr4(ecr)", "sr5(psw)", "sr6(pir)", "sr7(tkcw)", |
| 221 |
"sr8(invalid)", "sr9(invalid)", "sr10(invalid)", "sr11(invalid)", "sr12(invalid)", "sr13(invalid)", |
| 222 |
"sr14(invalid)", "sr15(invalid)", "sr16(invalid)", "sr17(invalid)", "sr18(invalid)", "sr19(invalid)", |
| 223 |
"sr20(invalid)", "sr21(invalid)", "sr22(invalid)", "sr23(invalid)", |
| 224 |
"sr24(chcw)", "sr25(adtre)", "sr26(invalid)", "sr27(invalid)", "sr28(invalid)", "sr29(invalid)", "sr30(invalid)", |
| 225 |
"sr31(invalid)" |
| 226 |
}; |
| 227 |
|
| 228 |
void v810_dis(uint32 &tPC, int num, char *buf, uint16 (*rhword)(uint32), bool vbmode) |
| 229 |
{ |
| 230 |
int lowB, highB, lowB2, highB2; // up to 4 bytes for instruction (either 16 or 32 bits) |
| 231 |
int opcode, arg1, arg2, arg3; |
| 232 |
int i = 0; |
| 233 |
|
| 234 |
buf[0] = 0; |
| 235 |
|
| 236 |
for(i = 0; i< num; i++) |
| 237 |
{ |
| 238 |
const uint16 hw0 = rhword(tPC); |
| 239 |
const uint16 hw1 = rhword(tPC + 2); |
| 240 |
|
| 241 |
lowB = hw0 & 0xFF; |
| 242 |
highB = hw0 >> 8; |
| 243 |
|
| 244 |
lowB2 = hw1 & 0xFF; |
| 245 |
highB2 = hw1 >> 8; |
| 246 |
|
| 247 |
opcode = highB >> 2; |
| 248 |
if((highB & 0xE0) == 0x80) // Special opcode format for |
| 249 |
opcode = (highB >> 1); // type III instructions. |
| 250 |
|
| 251 |
if((opcode > 0x4F) | (opcode < 0)) { |
| 252 |
//Error Invalid opcode! |
| 253 |
sprintf(&buf[strlen(buf)], "0x%04x", hw0); |
| 254 |
tPC += 2; |
| 255 |
} |
| 256 |
|
| 257 |
int am = optable[opcode].addr_mode; |
| 258 |
|
| 259 |
if((am & 0x8000) && !vbmode) |
| 260 |
am = AM_UDEF; |
| 261 |
|
| 262 |
am &= ~0x8000; |
| 263 |
|
| 264 |
switch(am) { |
| 265 |
case AM_I: // Do the same Ither way =) |
| 266 |
arg1 = (lowB >> 5) + ((highB & 0x3) << 3); |
| 267 |
arg2 = (lowB & 0x1F); |
| 268 |
if (opcode == JMP) { |
| 269 |
sprintf(&buf[strlen(buf)],"%s [%s]", optable[opcode].opname, pretty_preg_names[arg2]); |
| 270 |
} else { |
| 271 |
sprintf(&buf[strlen(buf)],"%s %s, %s", optable[opcode].opname, pretty_preg_names[arg2], pretty_preg_names[arg1]); |
| 272 |
} |
| 273 |
tPC += 2; // 16 bit instruction |
| 274 |
break; |
| 275 |
case AM_II: |
| 276 |
arg1 = (lowB >> 5) + ((highB & 0x3) << 3); |
| 277 |
arg2 = (lowB & 0x1F); |
| 278 |
if(opcode == LDSR) { |
| 279 |
sprintf(&buf[strlen(buf)],"%s %s, %s", optable[opcode].opname, pretty_preg_names[arg1], pretty_sreg_names[arg2]); |
| 280 |
} else if(opcode == STSR) { |
| 281 |
sprintf(&buf[strlen(buf)],"%s %s, %s", optable[opcode].opname, pretty_sreg_names[arg2], pretty_preg_names[arg1]); |
| 282 |
} else if(opcode == ADD_I || opcode == CMP_I) { |
| 283 |
sprintf(&buf[strlen(buf)],"%s %d, %s", optable[opcode].opname, sign_5(arg2), pretty_preg_names[arg1]); |
| 284 |
} else { |
| 285 |
sprintf(&buf[strlen(buf)],"%s %d, %s", optable[opcode].opname, arg2, pretty_preg_names[arg1]); |
| 286 |
} |
| 287 |
tPC += 2; // 16 bit instruction |
| 288 |
break; |
| 289 |
case AM_III: |
| 290 |
arg1 = ((highB & 0x1) << 8) + (lowB & 0xFE); |
| 291 |
if(opcode == NOP) |
| 292 |
sprintf(&buf[strlen(buf)],"%s", optable[opcode].opname); |
| 293 |
else |
| 294 |
sprintf(&buf[strlen(buf)],"%s %08x", optable[opcode].opname, tPC + sign_9(arg1)); |
| 295 |
tPC += 2; // 16 bit instruction |
| 296 |
break; |
| 297 |
case AM_IV: |
| 298 |
arg1 = ((highB & 0x3) << 24) + (lowB << 16) + (highB2 << 8) + lowB2; |
| 299 |
|
| 300 |
sprintf(&buf[strlen(buf)],"%s %08x", optable[opcode].opname, tPC + sign_26(arg1)); |
| 301 |
|
| 302 |
tPC += 4; // 32 bit instruction |
| 303 |
break; |
| 304 |
case AM_V: |
| 305 |
arg1 = (lowB >> 5) + ((highB & 0x3) << 3); |
| 306 |
arg2 = (lowB & 0x1F); |
| 307 |
arg3 = (highB2 << 8) + lowB2; |
| 308 |
|
| 309 |
// TODO: What would be the best way to disassemble the MOVEA instruction? |
| 310 |
//if(opcode == MOVEA) |
| 311 |
// sprintf(&buf[strlen(buf)],"%s 0x%X, %s, %s", optable[opcode].opname, (uint32)(int32)(int16)(uint16)arg3, pretty_preg_names[arg2], pretty_preg_names[arg1] ); |
| 312 |
//else |
| 313 |
sprintf(&buf[strlen(buf)],"%s 0x%X, %s, %s", optable[opcode].opname, arg3, pretty_preg_names[arg2], pretty_preg_names[arg1] ); |
| 314 |
tPC += 4; // 32 bit instruction |
| 315 |
break; |
| 316 |
case AM_VIa: // Mode6 form1 |
| 317 |
arg1 = (lowB >> 5) + ((highB & 0x3) << 3); |
| 318 |
arg2 = (lowB & 0x1F); |
| 319 |
arg3 = (highB2 << 8) + lowB2; |
| 320 |
|
| 321 |
if(!arg3) // Don't bother printing offset if it's 0 |
| 322 |
sprintf(&buf[strlen(buf)],"%s [%s], %s", optable[opcode].opname, pretty_preg_names[arg2], pretty_preg_names[arg1]); |
| 323 |
else if(sign_16(arg3) >= 0) // Make disassembly prettier if it's a positive offset |
| 324 |
sprintf(&buf[strlen(buf)],"%s 0x%04x[%s], %s", optable[opcode].opname, sign_16(arg3), pretty_preg_names[arg2], pretty_preg_names[arg1]); |
| 325 |
else |
| 326 |
sprintf(&buf[strlen(buf)],"%s %d[%s], %s", optable[opcode].opname, sign_16(arg3), pretty_preg_names[arg2], pretty_preg_names[arg1]); |
| 327 |
|
| 328 |
tPC += 4; // 32 bit instruction |
| 329 |
break; |
| 330 |
case AM_VIb: // Mode6 form2 |
| 331 |
arg1 = (lowB >> 5) + ((highB & 0x3) << 3); |
| 332 |
arg2 = (lowB & 0x1F); |
| 333 |
arg3 = (highB2 << 8) + lowB2; // whats the order??? 2,3,1 or 1,3,2 |
| 334 |
|
| 335 |
if(!arg3) // Don't bother printing offset if it's 0 |
| 336 |
sprintf(&buf[strlen(buf)],"%s %s, [%s]", optable[opcode].opname, pretty_preg_names[arg1], pretty_preg_names[arg2]); |
| 337 |
else if(sign_16(arg3) >= 0) // Make disassembly prettier if it's a positive offset |
| 338 |
sprintf(&buf[strlen(buf)],"%s %s, 0x%04x[%s]", optable[opcode].opname, pretty_preg_names[arg1], sign_16(arg3), pretty_preg_names[arg2]); |
| 339 |
else |
| 340 |
sprintf(&buf[strlen(buf)],"%s %s, %d[%s]", optable[opcode].opname, pretty_preg_names[arg1], sign_16(arg3), pretty_preg_names[arg2]); |
| 341 |
tPC += 4; // 32 bit instruction |
| 342 |
break; |
| 343 |
case AM_VII: // Unhandled |
| 344 |
sprintf(&buf[strlen(buf)],"0x%2x 0x%2x 0x%2x 0x%2x", lowB, highB, lowB2, highB2); |
| 345 |
tPC +=4; // 32 bit instruction |
| 346 |
break; |
| 347 |
case AM_VIII: // Unhandled |
| 348 |
sprintf(&buf[strlen(buf)],"0x%2x 0x%2x 0x%2x 0x%2x", lowB, highB, lowB2, highB2); |
| 349 |
tPC += 4; // 32 bit instruction |
| 350 |
break; |
| 351 |
case AM_IX: |
| 352 |
arg1 = (lowB & 0x1); // Mode ID, Ignore for now |
| 353 |
sprintf(&buf[strlen(buf)],"%s", optable[opcode].opname); |
| 354 |
tPC += 2; // 16 bit instruction |
| 355 |
break; |
| 356 |
case AM_BSTR: // Bit String Subopcodes |
| 357 |
arg1 = (lowB >> 5) + ((highB & 0x3) << 3); |
| 358 |
arg2 = (lowB & 0x1F); |
| 359 |
if(arg2 > 15) { |
| 360 |
sprintf(&buf[strlen(buf)],"BError"); |
| 361 |
} else { |
| 362 |
//sprintf(&buf[strlen(buf)],"%s, $%d", bssuboptable[arg2].opname,arg1); |
| 363 |
sprintf(&buf[strlen(buf)], "%s", bssuboptable[arg2].opname); |
| 364 |
} |
| 365 |
tPC += 2; // 16 bit instruction |
| 366 |
break; |
| 367 |
case AM_FPP: // Floating Point Subcode |
| 368 |
arg1 = (lowB >> 5) + ((highB & 0x3) << 3); |
| 369 |
arg2 = (lowB & 0x1F); |
| 370 |
arg3 = (highB2 >> 2); |
| 371 |
|
| 372 |
if(arg3 > 15) { |
| 373 |
sprintf(&buf[strlen(buf)],"(Invalid FPU: 0x%02x)", arg3); |
| 374 |
} else { |
| 375 |
sprintf(&buf[strlen(buf)],"%s %s, %s", (vbmode ? fpsuboptable_vb[arg3].opname : fpsuboptable[arg3].opname), pretty_preg_names[arg2], pretty_preg_names[arg1]); |
| 376 |
} |
| 377 |
tPC += 4; // 32 bit instruction |
| 378 |
break; |
| 379 |
case AM_UDEF: // Invalid opcode. |
| 380 |
default: // Invalid opcode. |
| 381 |
sprintf(&buf[strlen(buf)],"0x%04x", hw0); |
| 382 |
tPC += 2; |
| 383 |
} |
| 384 |
} |
| 385 |
} |