1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus -- dbg_decoder.c *
3 * Copyright (c) 2010 Marshall B. Rogers <mbr@64.vg> *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
23 * This is a heavily modified reentrant version of the MIPS disassembler found
24 * in the NetBSD operating system. I chose to use this as a base due to the
25 * small, compact, and easily manageable code.
27 * Original copyright/license information is contained below.
30 /* $NetBSD: db_disasm.c,v 1.21 2009/12/14 00:46:06 matt Exp $ */
33 * Copyright (c) 1991, 1993
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software contributed to Berkeley by
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * from: @(#)kadb.c 8.1 (Berkeley) 6/10/93
72 #include "dbg_decoder.h"
73 #include "dbg_decoder_local.h"
74 #include "osal/preproc.h"
77 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
81 typedef uint32_t db_addr_t;
85 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
89 static const char * const r4k_str_op_name[64] =
91 /* 0 */ "spec", "bcond","j", "jal", "beq", "bne", "blez", "bgtz",
92 /* 8 */ "addi", "addiu","slti", "sltiu","andi", "ori", "xori", "lui",
93 /*16 */ "cop0", "cop1", "cop2", "cop3", "beql", "bnel", "blezl","bgtzl",
94 /*24 */ "daddi","daddiu","ldl", "ldr", "op34", "op35", "op36", "op37",
95 /*32 */ "lb", "lh", "lwl", "lw", "lbu", "lhu", "lwr", "lwu",
96 /*40 */ "sb", "sh", "swl", "sw", "sdl", "sdr", "swr", "cache",
97 /*48 */ "ll", "lwc1", "lwc2", "lwc3", "lld", "ldc1", "ldc2", "ld",
98 /*56 */ "sc", "swc1", "swc2", "swc3", "scd", "sdc1", "sdc2", "sd"
101 static const char * const r4k_str_spec_name[64] =
103 /* 0 */ "sll", "spec01","srl", "sra", "sllv", "spec05","srlv","srav",
104 /* 8 */ "jr", "jalr", "spec12","spec13","syscall","break","spec16","sync",
105 /*16 */ "mfhi", "mthi", "mflo", "mtlo", "dsllv","spec25","dsrlv","dsrav",
106 /*24 */ "mult", "multu","div", "divu", "dmult","dmultu","ddiv","ddivu",
107 /*32 */ "add", "addu", "sub", "subu", "and", "or", "xor", "nor",
108 /*40 */ "spec50","spec51","slt","sltu", "dadd","daddu","dsub","dsubu",
109 /*48 */ "tge","tgeu","tlt","tltu","teq","spec65","tne","spec67",
110 /*56 */ "dsll","spec71","dsrl","dsra","dsll32","spec75","dsrl32","dsra32"
113 static const char * const r4k_str_spec2_name[4] = /* QED RM4650, R5000, etc. */
115 /* 0 */ "mad", "madu", "mul", "spec3"
118 static const char * const r4k_str_bcond_name[32] =
120 /* 0 */ "bltz", "bgez", "bltzl", "bgezl", "?", "?", "?", "?",
121 /* 8 */ "tgei", "tgeiu", "tlti", "tltiu", "teqi", "?", "tnei", "?",
122 /*16 */ "bltzal", "bgezal", "bltzall", "bgezall", "?", "?", "?", "?",
123 /*24 */ "?", "?", "?", "?", "?", "?", "?", "?",
126 static const char * const r4k_str_cop1_name[64] =
128 /* 0 */ "add", "sub", "mul", "div", "sqrt","abs", "mov", "neg",
129 /* 8 */ "fop08", "trunc.l","fop0a","fop0b","fop0c","trunc.w","fop0e","fop0f",
130 /*16 */ "fop10", "fop11","fop12","fop13","fop14","fop15","fop16","fop17",
131 /*24 */ "fop18", "fop19","fop1a","fop1b","fop1c","fop1d","fop1e","fop1f",
132 /*32 */ "cvt.s", "cvt.d","fop22","fop23","cvt.w","cvt.l","fop26","fop27",
133 /*40 */ "fop28", "fop29","fop2a","fop2b","fop2c","fop2d","fop2e","fop2f",
134 /*48 */ "c.f", "c.un","c.eq","c.ueq","c.olt","c.ult",
136 /*56 */ "c.sf", "c.ngle","c.seq","c.ngl","c.lt","c.nge",
140 static const char * const r4k_str_fmt_name[16] =
143 "s", "d", "e", "fmt3",
144 "w", "l", "fmt6", "fmt7",
145 "fmt8", "fmt9", "fmta", "fmtb",
146 "fmtc", "fmtd", "fmte", "fmtf"
150 static const char * const r4k_str_reg_name[32] =
152 "$zero", "$at", "v0", "v1", "a0", "a1", "a2", "a3",
153 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
154 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
155 "t8", "t9", "k0", "k1", "$gp", "$sp", "s8", "$ra"
158 static const char * const r4k_str_c0_opname[64] =
160 "c0op00","tlbr", "tlbwi", "c0op03","c0op04","c0op05","tlbwr", "c0op07",
161 "tlbp", "c0op11","c0op12","c0op13","c0op14","c0op15","c0op16","c0op17",
162 "rfe", "c0op21","c0op22","c0op23","c0op24","c0op25","c0op26","c0op27",
163 "eret", "c0op31","c0op32","c0op33","c0op34","c0op35","c0op36","c0op37",
164 "c0op40","c0op41","c0op42","c0op43","c0op44","c0op45","c0op46","c0op47",
165 "c0op50","c0op51","c0op52","c0op53","c0op54","c0op55","c0op56","c0op57",
166 "c0op60","c0op61","c0op62","c0op63","c0op64","c0op65","c0op66","c0op67",
167 "c0op70","c0op71","c0op72","c0op73","c0op74","c0op75","c0op77","c0op77",
170 static const char * const r4k_str_c0_reg[32] =
172 "C0_INX", "C0_RAND", "C0_ENTRYLO0", "C0_ENTRYLO1",
173 "C0_CONTEXT", "C0_PAGEMASK", "C0_WIRED", "cp0r7",
174 "C0_BADVADDR", "C0_COUNT", "C0_ENTRYHI", "C0_COMPARE",
175 "C0_SR", "C0_CAUSE", "C0_EPC", "C0_PRID",
176 "C0_CONFIG", "C0_LLADDR", "C0_WATCHLO", "C0_WATCHHI",
177 "xcontext", "cp0r21", "cp0r22", "debug",
178 "depc", "perfcnt", "C0_ECC", "C0_CACHE_ERR",
179 "C0_TAGLO", "C0_TAGHI", "C0_ERROR_EPC", "desave"
184 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
185 Local functions - lookup
186 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
188 /* Look up a symbol */
190 lookup_sym ( struct r4k_dis_t * state,
193 if( state->lookup_sym )
194 return state->lookup_sym( address, state->lookup_sym_d );
199 /* Look up an upper 16-bits relocation */
201 lookup_rel_hi16 ( struct r4k_dis_t * state,
204 if( state->lookup_rel_hi16 )
205 return state->lookup_rel_hi16( address, state->lookup_rel_hi16_d );
210 /* Look up a lower 16-bits relocation */
212 lookup_rel_lo16 ( struct r4k_dis_t * state,
215 if( state->lookup_rel_lo16 )
216 return state->lookup_rel_lo16( address, state->lookup_rel_lo16_d );
223 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
224 Local functions - disassembler
225 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
227 /* Print text into the destination buffer */
229 db_printf ( struct r4k_dis_t * state,
238 /* Prepare user provided */
240 l = vsnprintf( buffer, sizeof(buffer), fmt, ap );
243 /* Add it to our string */
244 state->dest += sprintf(
255 /* Print an address to a string. If there's a symbol, the name will be printed */
257 print_addr ( struct r4k_dis_t * state,
264 /* Try to lookup symbol */
265 if( (sym = lookup_sym(state, address)) )
267 len = db_printf( state, "%s", sym );
271 len = db_printf( state, "0x%08X", address );
278 /* Disassemble an instruction */
280 db_disasm_insn ( struct r4k_dis_t * state,
290 switch (i.JType.op) {
293 db_printf(state, "nop");
297 * "addu" is a "move" only in 32-bit mode. What's the correct
298 * answer - never decode addu/daddu as "move"?
300 if (i.RType.func == OP_ADDU && i.RType.rt == 0) {
301 db_printf(state, "%-16s%s,%s",
303 r4k_str_reg_name[i.RType.rd],
304 r4k_str_reg_name[i.RType.rs]);
307 db_printf(state, "%-16s", r4k_str_spec_name[i.RType.func]);
308 switch (i.RType.func) {
319 db_printf(state, "%s,%s,%d",
320 r4k_str_reg_name[i.RType.rd],
321 r4k_str_reg_name[i.RType.rt],
331 db_printf(state, "%s,%s,%s",
332 r4k_str_reg_name[i.RType.rd],
333 r4k_str_reg_name[i.RType.rt],
334 r4k_str_reg_name[i.RType.rs]);
339 db_printf(state, "%s", r4k_str_reg_name[i.RType.rd]);
344 db_printf(state, "%s", r4k_str_reg_name[i.RType.rs]);
348 db_printf(state, "%s", r4k_str_reg_name[i.RType.rs]);
355 db_printf(state, "%s,%s",
356 r4k_str_reg_name[i.RType.rs],
357 r4k_str_reg_name[i.RType.rt]);
364 db_printf(state, "$zero,%s,%s",
365 r4k_str_reg_name[i.RType.rs],
366 r4k_str_reg_name[i.RType.rt]);
375 db_printf(state, "%d", (i.RType.rs << 5) | i.RType.rt);
379 db_printf(state, "%s,%s,%s",
380 r4k_str_reg_name[i.RType.rd],
381 r4k_str_reg_name[i.RType.rs],
382 r4k_str_reg_name[i.RType.rt]);
387 if (i.RType.func == OP_MUL)
388 db_printf(state, "%s\t%s,%s,%s",
389 r4k_str_spec2_name[i.RType.func & 0x3],
390 r4k_str_reg_name[i.RType.rd],
391 r4k_str_reg_name[i.RType.rs],
392 r4k_str_reg_name[i.RType.rt]);
394 db_printf(state, "%s\t%s,%s",
395 r4k_str_spec2_name[i.RType.func & 0x3],
396 r4k_str_reg_name[i.RType.rs],
397 r4k_str_reg_name[i.RType.rt]);
402 db_printf(state, "%-16s%s,", r4k_str_bcond_name[i.IType.rt],
403 r4k_str_reg_name[i.IType.rs]);
410 db_printf(state, "%-16s%s,", r4k_str_op_name[i.IType.op],
411 r4k_str_reg_name[i.IType.rs]);
416 if (i.IType.rs == 0 && i.IType.rt == 0) {
417 db_printf(state, "%-16s", "b");
423 db_printf(state, "%-16s%s,%s,", r4k_str_op_name[i.IType.op],
424 r4k_str_reg_name[i.IType.rs],
425 r4k_str_reg_name[i.IType.rt]);
427 print_addr( state, loc + 4 + ((short)i.IType.imm << 2) );
431 switch (i.RType.rs) {
435 db_printf(state, "bc0%c\t",
436 "ft"[i.RType.rt & COPz_BC_TF_MASK]);
440 db_printf(state, "%-16s%s,%s",
442 r4k_str_reg_name[i.RType.rt],
443 r4k_str_c0_reg[i.RType.rd]);
447 db_printf(state, "%-16s%s,%s",
449 r4k_str_reg_name[i.RType.rt],
450 r4k_str_c0_reg[i.RType.rd]);
454 db_printf(state, "%-16s%s,%s", "mfc0",
455 r4k_str_reg_name[i.RType.rt],
456 r4k_str_c0_reg[i.RType.rd]);
460 db_printf(state, "%-16s%s,%s","dmfc0",
461 r4k_str_reg_name[i.RType.rt],
462 r4k_str_c0_reg[i.RType.rd]);
466 db_printf(state, "%s", r4k_str_c0_opname[i.FRType.func]);
471 switch (i.RType.rs) {
474 db_printf(state, "bc1%c%s\t\t",
475 "ft"[i.RType.rt & COPz_BC_TF_MASK],
476 (insn >> 16 & 0x1F) == 2 || (insn >> 16 & 0x1F) == 3 ? "l" : "");
480 db_printf(state, "mtc1\t\t%s,$f%d",
481 r4k_str_reg_name[i.RType.rt],
486 db_printf(state, "mfc1\t\t%s,$f%d",
487 r4k_str_reg_name[i.RType.rt],
492 db_printf(state, "ctc1\t\t%s,$f%d",
493 r4k_str_reg_name[i.RType.rt],
498 db_printf(state, "cfc1\t\t%s,$f%d",
499 r4k_str_reg_name[i.RType.rt],
504 db_printf(state, "dmtc1\t\t%s,$f%d",
505 r4k_str_reg_name[i.RType.rt],
510 db_printf(state, "dmfc1\t\t%s,$f%d",
511 r4k_str_reg_name[i.RType.rt],
516 db_printf(state, "mthc1\t\t%s,$f%d",
517 r4k_str_reg_name[i.RType.rt],
522 db_printf(state, "mfhc1\t\t%s,$f%d",
523 r4k_str_reg_name[i.RType.rt],
530 if( i.FRType.func == 0x21 || i.FRType.func == 0x20 || i.FRType.func == 0x24 || i.FRType.func == 0x25 ||
531 i.FRType.func == 7 || i.FRType.func == 6 || i.FRType.func == 0xd ||
532 i.FRType.func == 4 || i.FRType.func == 5 || i.FRType.func == 9 )
535 db_printf(state, "%s.%s\t\t$f%d,$f%d",
536 r4k_str_cop1_name[i.FRType.func],
537 r4k_str_fmt_name[i.FRType.fmt],
538 i.FRType.fd, i.FRType.fs);
540 else if( i.FRType.func != 1 && i.FRType.func != 2 && (insn & 0x3F) && !(insn >> 6 & 0x1F) ) /* C */
542 db_printf(state, "%s.%s\t\t$f%d,$f%d",
543 r4k_str_cop1_name[i.FRType.func],
544 r4k_str_fmt_name[i.FRType.fmt],
545 i.FRType.fs, i.FRType.ft);
549 db_printf(state, "%s.%s\t\t$f%d,$f%d,$f%d",
550 r4k_str_cop1_name[i.FRType.func],
551 r4k_str_fmt_name[i.FRType.fmt],
552 i.FRType.fd, i.FRType.fs, i.FRType.ft);
559 db_printf(state, "%-16s", r4k_str_op_name[i.JType.op]);
560 print_addr(state, (loc & 0xF0000000) | (i.JType.target << 2));
567 db_printf(state, "%-16s$f%d,", r4k_str_op_name[i.IType.op],
586 db_printf(state, "%-16s%s,", r4k_str_op_name[i.IType.op],
587 r4k_str_reg_name[i.IType.rt]);
590 /* Part of a relocation? */
591 if( (rel = lookup_rel_lo16(state, loc)) )
597 r4k_str_reg_name[i.IType.rs]
604 db_printf(state, "%d(%s)", (short)i.IType.imm,
605 r4k_str_reg_name[i.IType.rs]);
610 if( i.IType.op == OP_ORI )
612 /* Part of a relocation? */
613 if( (rel = lookup_rel_lo16(state, loc)) )
617 "%-16s%s,%s,%%lo(%s)",
618 r4k_str_op_name[i.IType.op],
619 r4k_str_reg_name[i.IType.rt],
620 r4k_str_reg_name[i.IType.rs],
628 db_printf(state, "%-16s%s,%s,0x%x", r4k_str_op_name[i.IType.op],
629 r4k_str_reg_name[i.IType.rt],
630 r4k_str_reg_name[i.IType.rs],
637 if (i.IType.rs == 0) {
638 db_printf(state, "%-16s%s,0x%x",
640 r4k_str_reg_name[i.IType.rt],
646 db_printf(state, "%-16s%s,%s,0x%x", r4k_str_op_name[i.IType.op],
647 r4k_str_reg_name[i.IType.rt],
648 r4k_str_reg_name[i.IType.rs],
654 /* Part of a relocation? */
655 if( (rel = lookup_rel_hi16(state, loc)) )
660 r4k_str_op_name[i.IType.op],
661 r4k_str_reg_name[i.IType.rt],
667 db_printf(state, "%-16s%s,0x%x", r4k_str_op_name[i.IType.op],
668 r4k_str_reg_name[i.IType.rt],
675 db_printf(state, "%-16s0x%x,0x%x(%s)",
676 r4k_str_op_name[i.IType.op],
679 r4k_str_reg_name[i.IType.rs]);
688 /* Part of a relocation? */
689 if( (rel = lookup_rel_lo16(state, loc)) )
693 "%-16s%s,%s,%%lo(%s)",
694 r4k_str_op_name[i.IType.op],
695 r4k_str_reg_name[i.IType.rt],
696 r4k_str_reg_name[i.IType.rs],
703 if (i.IType.rs == 0) {
704 db_printf(state, "%-16s%s,%d", "li",
705 r4k_str_reg_name[i.IType.rt],
713 db_printf(state, "%-16s%s,%s,%d", r4k_str_op_name[i.IType.op],
714 r4k_str_reg_name[i.IType.rt],
715 r4k_str_reg_name[i.IType.rs],
718 /*db_printf(state, "\n");*/
725 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
727 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
729 /* Disassemble an instruction with state */
731 r4k_disassemble ( struct r4k_dis_t * state,
732 uint32_t instruction,
737 db_disasm_insn( state, instruction, location, 0 );
739 return state->length;
744 /* Disassemble an instruction but split the opcode/operands into two char *'s */
746 r4k_disassemble_split ( struct r4k_dis_t * state,
747 uint32_t instruction,
753 char buff[128], * dupd;
762 dupd = strdup( buff );
765 for( i = 0; buff[i] && buff[i] != ' '; i++ );
769 for( ; buff[i] && buff[i] == ' '; i++ );
771 *operands = &dupd[i];
778 /* Disassemble an instruction with a blank state but split op/operands */
780 r4k_disassemble_split_quick ( uint32_t instruction,
785 struct r4k_dis_t state;
788 memset( &state, 0, sizeof(state) );
791 return r4k_disassemble_split(
801 //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=[ DECODE_OP ]=-=-=-=-=-=-=-=-=-=-=-=-=-=-=[//
803 void r4300_decode_op ( uint32 instr, char * opcode, char * arguments, int counter )
810 r4k_disassemble_split_quick(
817 strcpy( opcode, _op );
818 strcpy( arguments, _args );