diff src/gba/armdis.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/gba/armdis.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,774 @@
     1.4 +/************************************************************************/
     1.5 +/* Arm/Thumb command set disassembler                                   */
     1.6 +/************************************************************************/
     1.7 +#include <cstdio>
     1.8 +
     1.9 +#include "GBAGlobals.h"
    1.10 +#include "armdis.h"
    1.11 +#include "elf.h"
    1.12 +
    1.13 +struct Opcodes
    1.14 +{
    1.15 +	u32 mask;
    1.16 +	u32 cval;
    1.17 +	const char *mnemonic;
    1.18 +};
    1.19 +
    1.20 +const char hdig[] = "0123456789abcdef";
    1.21 +
    1.22 +const char *decVals[16] = {
    1.23 +	"0", "1",  "2",	 "3",  "4",	 "5",  "6", "7", "8",
    1.24 +	"9", "10", "11", "12", "13", "14", "15"
    1.25 +};
    1.26 +
    1.27 +const char *regs[16] = {
    1.28 +	"r0", "r1", "r2",  "r3",  "r4",	 "r5", "r6", "r7",
    1.29 +	"r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc"
    1.30 +};
    1.31 +
    1.32 +const char *conditions[16] = {
    1.33 +	"eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
    1.34 +	"hi", "ls", "ge", "lt", "gt", "le", "",	  "nv"
    1.35 +};
    1.36 +
    1.37 +const char *shifts[5] = {
    1.38 +	"lsl", "lsr", "asr", "ror", "rrx"
    1.39 +};
    1.40 +
    1.41 +const char *armMultLoadStore[12] = {
    1.42 +	// non-stack
    1.43 +	"da", "ia", "db", "ib",
    1.44 +	// stack store
    1.45 +	"ed", "ea", "fd", "fa",
    1.46 +	// stack load
    1.47 +	"fa", "fd", "ea", "ed"
    1.48 +};
    1.49 +
    1.50 +const Opcodes thumbOpcodes[] = {
    1.51 +	// Format 1
    1.52 +	{ 0xf800, 0x0000, "lsl %r0, %r3, %o"	  },
    1.53 +	{ 0xf800, 0x0800, "lsr %r0, %r3, %o"	  },
    1.54 +	{ 0xf800, 0x1000, "asr %r0, %r3, %o"	  },
    1.55 +	// Format 2
    1.56 +	{ 0xfe00, 0x1800, "add %r0, %r3, %r6"	  },
    1.57 +	{ 0xfe00, 0x1a00, "sub %r0, %r3, %r6"	  },
    1.58 +	{ 0xfe00, 0x1c00, "add %r0, %r3, %i"	  },
    1.59 +	{ 0xfe00, 0x1e00, "sub %r0, %r3, %i"	  },
    1.60 +	// Format 3
    1.61 +	{ 0xf800, 0x2000, "mov %r8, %O"			  },
    1.62 +	{ 0xf800, 0x2800, "cmp %r8, %O"			  },
    1.63 +	{ 0xf800, 0x3000, "add %r8, %O"			  },
    1.64 +	{ 0xf800, 0x3800, "sub %r8, %O"			  },
    1.65 +	// Format 4
    1.66 +	{ 0xffc0, 0x4000, "and %r0, %r3"		  },
    1.67 +	{ 0xffc0, 0x4040, "eor %r0, %r3"		  },
    1.68 +	{ 0xffc0, 0x4080, "lsl %r0, %r3"		  },
    1.69 +	{ 0xffc0, 0x40c0, "lsr %r0, %r3"		  },
    1.70 +	{ 0xffc0, 0x4100, "asr %r0, %r3"		  },
    1.71 +	{ 0xffc0, 0x4140, "adc %r0, %r3"		  },
    1.72 +	{ 0xffc0, 0x4180, "sbc %r0, %r3"		  },
    1.73 +	{ 0xffc0, 0x41c0, "ror %r0, %r3"		  },
    1.74 +	{ 0xffc0, 0x4200, "tst %r0, %r3"		  },
    1.75 +	{ 0xffc0, 0x4240, "neg %r0, %r3"		  },
    1.76 +	{ 0xffc0, 0x4280, "cmp %r0, %r3"		  },
    1.77 +	{ 0xffc0, 0x42c0, "cmn %r0, %r3"		  },
    1.78 +	{ 0xffc0, 0x4300, "orr %r0, %r3"		  },
    1.79 +	{ 0xffc0, 0x4340, "mul %r0, %r3"		  },
    1.80 +	{ 0xffc0, 0x4380, "bic %r0, %r3"		  },
    1.81 +	{ 0xffc0, 0x43c0, "mvn %r0, %r3"		  },
    1.82 +	// Format 5
    1.83 +	{ 0xff80, 0x4700, "bx %h36"				  },
    1.84 +	{ 0xfcc0, 0x4400, "[ ??? ]"				  },
    1.85 +	{ 0xff00, 0x4400, "add %h07, %h36"		  },
    1.86 +	{ 0xff00, 0x4500, "cmp %h07, %h36"		  },
    1.87 +	{ 0xff00, 0x4600, "mov %h07, %h36"		  },
    1.88 +	// Format 6
    1.89 +	{ 0xf800, 0x4800, "ldr %r8, [%I] (=%J)"	  },
    1.90 +	// Format 7
    1.91 +	{ 0xfa00, 0x5000, "str%b %r0, [%r3, %r6]" },
    1.92 +	{ 0xfa00, 0x5800, "ldr%b %r0, [%r3, %r6]" },
    1.93 +	// Format 8
    1.94 +	{ 0xfe00, 0x5200, "strh %r0, [%r3, %r6]"  },
    1.95 +	{ 0xfe00, 0x5600, "ldsb %r0, [%r3, %r6]"  },
    1.96 +	{ 0xfe00, 0x5a00, "ldrh %r0, [%r3, %r6]"  },
    1.97 +	{ 0xfe00, 0x5e00, "ldsh %r0, [%r3, %r6]"  },
    1.98 +	// Format 9
    1.99 +	{ 0xe800, 0x6000, "str%B %r0, [%r3, %p]"  },
   1.100 +	{ 0xe800, 0x6800, "ldr%B %r0, [%r3, %p]"  },
   1.101 +	// Format 10
   1.102 +	{ 0xf800, 0x8000, "strh %r0, [%r3, %e]"	  },
   1.103 +	{ 0xf800, 0x8800, "ldrh %r0, [%r3, %e]"	  },
   1.104 +	// Format 11
   1.105 +	{ 0xf800, 0x9000, "str %r8, [sp, %w]"	  },
   1.106 +	{ 0xf800, 0x9800, "ldr %r8, [sp, %w]"	  },
   1.107 +	// Format 12
   1.108 +	{ 0xf800, 0xa000, "add %r8, pc, %w (=%K)" },
   1.109 +	{ 0xf800, 0xa800, "add %r8, sp, %w"		  },
   1.110 +	// Format 13
   1.111 +	{ 0xff00, 0xb000, "add sp, %s"			  },
   1.112 +	// Format 14
   1.113 +	{ 0xffff, 0xb500, "push {lr}"			  },
   1.114 +	{ 0xff00, 0xb400, "push {%l}"			  },
   1.115 +	{ 0xff00, 0xb500, "push {%l,lr}"		  },
   1.116 +	{ 0xffff, 0xbd00, "pop {pc}"			  },
   1.117 +	{ 0xff00, 0xbd00, "pop {%l,pc}"			  },
   1.118 +	{ 0xff00, 0xbc00, "pop {%l}"			  },
   1.119 +	// Format 15
   1.120 +	{ 0xf800, 0xc000, "stmia %r8!, {%l}"	  },
   1.121 +	{ 0xf800, 0xc800, "ldmia %r8!, {%l}"	  },
   1.122 +	// Format 17
   1.123 +	{ 0xff00, 0xdf00, "swi %m"				  },
   1.124 +	// Format 16
   1.125 +	{ 0xf000, 0xd000, "b%c %W"				  },
   1.126 +	// Format 18
   1.127 +	{ 0xf800, 0xe000, "b %a"				  },
   1.128 +	// Format 19
   1.129 +	{ 0xf800, 0xf000, "bl %A"				  },
   1.130 +	{ 0xf800, 0xf800, "blh %Z"				  },
   1.131 +	{ 0xff00, 0xbe00, "bkpt %O"				  },
   1.132 +	// Unknown
   1.133 +	{ 0x0000, 0x0000, "[ ??? ]"				  }
   1.134 +};
   1.135 +
   1.136 +const Opcodes armOpcodes[] = {
   1.137 +	// Undefined
   1.138 +	{ 0x0e000010, 0x06000010, "[ undefined ]"				  },
   1.139 +	// Branch instructions
   1.140 +	{ 0x0ff000f0, 0x01200010, "bx%c %r0"					  },
   1.141 +	{ 0x0f000000, 0x0a000000, "b%c %o"						  },
   1.142 +	{ 0x0f000000, 0x0b000000, "bl%c %o"						  },
   1.143 +	{ 0x0f000000, 0x0f000000, "swi%c %q"					  },
   1.144 +	// PSR transfer
   1.145 +	{ 0x0fbf0fff, 0x010f0000, "mrs%c %r3, %p"				  },
   1.146 +	{ 0x0db0f000, 0x0120f000, "msr%c %p, %i"				  },
   1.147 +	// Multiply instructions
   1.148 +	{ 0x0fe000f0, 0x00000090, "mul%c%s %r4, %r0, %r2"		  },
   1.149 +	{ 0x0fe000f0, 0x00200090, "mla%c%s %r4, %r0, %r2, %r3"	  },
   1.150 +	{ 0x0fa000f0, 0x00800090, "%umull%c%s %r3, %r4, %r0, %r2" },
   1.151 +	{ 0x0fa000f0, 0x00a00090, "%umlal%c%s %r3, %r4, %r0, %r2" },
   1.152 +	// Load/Store instructions
   1.153 +	{ 0x0fb00ff0, 0x01000090, "swp%c%b %r3, %r0, [%r4]"		  },
   1.154 +	{ 0x0fb000f0, 0x01000090, "[ ??? ]"						  },
   1.155 +	{ 0x0c100000, 0x04000000, "str%c%b%t %r3, %a"			  },
   1.156 +	{ 0x0c100000, 0x04100000, "ldr%c%b%t %r3, %a"			  },
   1.157 +	{ 0x0e100090, 0x00000090, "str%c%h %r3, %a"				  },
   1.158 +	{ 0x0e100090, 0x00100090, "ldr%c%h %r3, %a"				  },
   1.159 +	{ 0x0e100000, 0x08000000, "stm%c%m %r4%l"				  },
   1.160 +	{ 0x0e100000, 0x08100000, "ldm%c%m %r4%l"				  },
   1.161 +	// Data processing
   1.162 +	{ 0x0de00000, 0x00000000, "and%c%s %r3, %r4, %i"		  },
   1.163 +	{ 0x0de00000, 0x00200000, "eor%c%s %r3, %r4, %i"		  },
   1.164 +	{ 0x0de00000, 0x00400000, "sub%c%s %r3, %r4, %i"		  },
   1.165 +	{ 0x0de00000, 0x00600000, "rsb%c%s %r3, %r4, %i"		  },
   1.166 +	{ 0x0de00000, 0x00800000, "add%c%s %r3, %r4, %i"		  },
   1.167 +	{ 0x0de00000, 0x00a00000, "adc%c%s %r3, %r4, %i"		  },
   1.168 +	{ 0x0de00000, 0x00c00000, "sbc%c%s %r3, %r4, %i"		  },
   1.169 +	{ 0x0de00000, 0x00e00000, "rsc%c%s %r3, %r4, %i"		  },
   1.170 +	{ 0x0de00000, 0x01000000, "tst%c%s %r4, %i"				  },
   1.171 +	{ 0x0de00000, 0x01200000, "teq%c%s %r4, %i"				  },
   1.172 +	{ 0x0de00000, 0x01400000, "cmp%c%s %r4, %i"				  },
   1.173 +	{ 0x0de00000, 0x01600000, "cmn%c%s %r4, %i"				  },
   1.174 +	{ 0x0de00000, 0x01800000, "orr%c%s %r3, %r4, %i"		  },
   1.175 +	{ 0x0de00000, 0x01a00000, "mov%c%s %r3, %i"				  },
   1.176 +	{ 0x0de00000, 0x01c00000, "bic%c%s %r3, %r4, %i"		  },
   1.177 +	{ 0x0de00000, 0x01e00000, "mvn%c%s %r3, %i"				  },
   1.178 +	// Coprocessor operations
   1.179 +	{ 0x0f000010, 0x0e000000, "cdp%c %P, %N, %r3, %R4, %R0%V" },
   1.180 +	{ 0x0e100000, 0x0c000000, "stc%c%L %P, %r3, %A"			  },
   1.181 +	{ 0x0f100010, 0x0e000010, "mcr%c %P, %N, %r3, %R4, %R0%V" },
   1.182 +	{ 0x0f100010, 0x0e100010, "mrc%c %P, %N, %r3, %R4, %R0%V" },
   1.183 +	// Unknown
   1.184 +	{ 0x00000000, 0x00000000, "[ ??? ]"						  }
   1.185 +};
   1.186 +
   1.187 +char *addStr(char *dest, const char *src)
   1.188 +{
   1.189 +	while (*src)
   1.190 +	{
   1.191 +		*dest++ = *src++;
   1.192 +	}
   1.193 +	return dest;
   1.194 +}
   1.195 +
   1.196 +char *addHex(char *dest, int siz, u32 val)
   1.197 +{
   1.198 +	if (siz == 0)
   1.199 +	{
   1.200 +		siz = 28;
   1.201 +		while ((((val >> siz) & 15) == 0) && (siz >= 4))
   1.202 +			siz -= 4;
   1.203 +		siz += 4;
   1.204 +	}
   1.205 +	while (siz > 0)
   1.206 +	{
   1.207 +		siz	   -= 4;
   1.208 +		*dest++ = hdig[(val >> siz) & 15];
   1.209 +	}
   1.210 +	return dest;
   1.211 +}
   1.212 +
   1.213 +int disArm(u32 offset, char *dest, int flags)
   1.214 +{
   1.215 +	u32 opcode = debuggerReadMemory(offset);
   1.216 +
   1.217 +	const Opcodes *sp = armOpcodes;
   1.218 +	while (sp->cval != (opcode & sp->mask))
   1.219 +		sp++;
   1.220 +
   1.221 +	if (flags & DIS_VIEW_ADDRESS)
   1.222 +	{
   1.223 +		dest	= addHex(dest, 32, offset);
   1.224 +		*dest++ = ' ';
   1.225 +	}
   1.226 +	if (flags & DIS_VIEW_CODE)
   1.227 +	{
   1.228 +		dest	= addHex(dest, 32, opcode);
   1.229 +		*dest++ = ' ';
   1.230 +	}
   1.231 +
   1.232 +	const char *src = sp->mnemonic;
   1.233 +	while (*src)
   1.234 +	{
   1.235 +		if (*src != '%')
   1.236 +			*dest++ = *src++;
   1.237 +		else
   1.238 +		{
   1.239 +			src++;
   1.240 +			switch (*src)
   1.241 +			{
   1.242 +			case 'c':
   1.243 +				dest = addStr(dest, conditions[opcode >> 28]);
   1.244 +				break;
   1.245 +			case 'r':
   1.246 +				dest = addStr(dest, regs[(opcode >> ((*(++src) - '0') * 4)) & 15]);
   1.247 +				break;
   1.248 +			case 'o':
   1.249 +			{
   1.250 +				*dest++ = '$';
   1.251 +				int off = opcode & 0xffffff;
   1.252 +				if (off & 0x800000)
   1.253 +					off |= 0xff000000;
   1.254 +				off <<= 2;
   1.255 +				dest  = addHex(dest, 32, offset + 8 + off);
   1.256 +			}
   1.257 +			break;
   1.258 +			case 'i':
   1.259 +				if (opcode & (1 << 25))
   1.260 +				{
   1.261 +					dest = addStr(dest, "#0x");
   1.262 +					int imm = opcode & 0xff;
   1.263 +					int rot = (opcode & 0xf00) >> 7;
   1.264 +					int val = (imm << (32 - rot)) | (imm >> rot);
   1.265 +					dest = addHex(dest, 0, val);
   1.266 +				}
   1.267 +				else
   1.268 +				{
   1.269 +					dest = addStr(dest, regs[opcode & 0x0f]);
   1.270 +					int shi = (opcode >> 5) & 3;
   1.271 +					int sdw = (opcode >> 7) & 0x1f;
   1.272 +					if ((sdw == 0) && (shi == 3))
   1.273 +						shi = 4;
   1.274 +					if ((sdw) || (opcode & 0x10) || (shi))
   1.275 +					{
   1.276 +						dest = addStr(dest, ", ");
   1.277 +						dest = addStr(dest, shifts[shi]);
   1.278 +						if (opcode & 0x10)
   1.279 +						{
   1.280 +							*dest++ = ' ';
   1.281 +							dest	= addStr(dest, regs[(opcode >> 8) & 15]);
   1.282 +						}
   1.283 +						else
   1.284 +						{
   1.285 +							if (sdw == 0 && ((shi == 1) || (shi == 2)))
   1.286 +								sdw = 32;
   1.287 +							if (shi != 4)
   1.288 +							{
   1.289 +								dest = addStr(dest, " #0x");
   1.290 +								dest = addHex(dest, 8, sdw);
   1.291 +							}
   1.292 +						}
   1.293 +					}
   1.294 +				}
   1.295 +				break;
   1.296 +			case 'p':
   1.297 +				if (opcode & (1 << 22))
   1.298 +					dest = addStr(dest, "spsr");
   1.299 +				else
   1.300 +					dest = addStr(dest, "cpsr");
   1.301 +				if (opcode & 0x00F00000)
   1.302 +				{
   1.303 +					*dest++ = '_';
   1.304 +					if (opcode & 0x00080000)
   1.305 +						*dest++ = 'f';
   1.306 +					if (opcode & 0x00040000)
   1.307 +						*dest++ = 's';
   1.308 +					if (opcode & 0x00020000)
   1.309 +						*dest++ = 'x';
   1.310 +					if (opcode & 0x00010000)
   1.311 +						*dest++ = 'c';
   1.312 +				}
   1.313 +				break;
   1.314 +			case 's':
   1.315 +				if (opcode & (1 << 20))
   1.316 +					*dest++ = 's';
   1.317 +				break;
   1.318 +			case 'S':
   1.319 +				if (opcode & (1 << 22))
   1.320 +					*dest++ = 's';
   1.321 +				break;
   1.322 +			case 'u':
   1.323 +				if (opcode & (1 << 22))
   1.324 +					*dest++ = 's';
   1.325 +				else
   1.326 +					*dest++ = 'u';
   1.327 +				break;
   1.328 +			case 'b':
   1.329 +				if (opcode & (1 << 22))
   1.330 +					*dest++ = 'b';
   1.331 +				break;
   1.332 +			case 'a':
   1.333 +				if ((opcode & 0x076f0000) == 0x004f0000)
   1.334 +				{
   1.335 +					*dest++ = '[';
   1.336 +					*dest++ = '$';
   1.337 +					int adr = offset + 8;
   1.338 +					int add = (opcode & 15) | ((opcode >> 8) & 0xf0);
   1.339 +					if (opcode & (1 << 23))
   1.340 +						adr += add;
   1.341 +					else
   1.342 +						adr -= add;
   1.343 +					dest	= addHex(dest, 32, adr);
   1.344 +					*dest++ = ']';
   1.345 +					dest	= addStr(dest, " (=");
   1.346 +					*dest++ = '$';
   1.347 +					dest	= addHex(dest, 32, debuggerReadMemory(adr));
   1.348 +					*dest++ = ')';
   1.349 +				}
   1.350 +				if ((opcode & 0x072f0000) == 0x050f0000)
   1.351 +				{
   1.352 +					*dest++ = '[';
   1.353 +					*dest++ = '$';
   1.354 +					int adr = offset + 8;
   1.355 +					if (opcode & (1 << 23))
   1.356 +						adr += opcode & 0xfff;
   1.357 +					else
   1.358 +						adr -= opcode & 0xfff;
   1.359 +					dest	= addHex(dest, 32, adr);
   1.360 +					*dest++ = ']';
   1.361 +					dest	= addStr(dest, " (=");
   1.362 +					*dest++ = '$';
   1.363 +					dest	= addHex(dest, 32, debuggerReadMemory(adr));
   1.364 +					*dest++ = ')';
   1.365 +				}
   1.366 +				else
   1.367 +				{
   1.368 +					int reg = (opcode >> 16) & 15;
   1.369 +					*dest++ = '[';
   1.370 +					dest	= addStr(dest, regs[reg]);
   1.371 +					if (!(opcode & (1 << 24)))
   1.372 +						*dest++ = ']';
   1.373 +					if (((opcode & (1 << 25)) && (opcode & (1 << 26))) || (!(opcode & (1 << 22)) && !(opcode & (1 << 26))))
   1.374 +					{
   1.375 +						dest = addStr(dest, ", ");
   1.376 +						if (!(opcode & (1 << 23)))
   1.377 +							*dest++ = '-';
   1.378 +						dest = addStr(dest, regs[opcode & 0x0f]);
   1.379 +						int shi = (opcode >> 5) & 3;
   1.380 +						if (opcode & (1 << 26))
   1.381 +						{
   1.382 +							if (((opcode >> 7) & 0x1f) || (opcode & 0x10) || (shi == 1) || (shi == 2))
   1.383 +							{
   1.384 +								dest = addStr(dest, ", ");
   1.385 +								dest = addStr(dest, shifts[shi]);
   1.386 +								if (opcode & 0x10)
   1.387 +								{
   1.388 +									*dest++ = ' ';
   1.389 +									dest	= addStr(dest, regs[(opcode >> 8) & 15]);
   1.390 +								}
   1.391 +								else
   1.392 +								{
   1.393 +									int sdw = (opcode >> 7) & 0x1f;
   1.394 +									if (sdw == 0 && ((shi == 1) || (shi == 2)))
   1.395 +										sdw = 32;
   1.396 +									dest = addStr(dest, " #0x");
   1.397 +									dest = addHex(dest, 8, sdw);
   1.398 +								}
   1.399 +							}
   1.400 +						}
   1.401 +					}
   1.402 +					else
   1.403 +					{
   1.404 +						int off;
   1.405 +						if (opcode & (1 << 26))
   1.406 +							off = opcode & 0xfff;
   1.407 +						else
   1.408 +							off = (opcode & 15) | ((opcode >> 4) & 0xf0);
   1.409 +						if (off)
   1.410 +						{
   1.411 +							dest = addStr(dest, ", ");
   1.412 +							if (!(opcode & (1 << 23)))
   1.413 +								*dest++ = '-';
   1.414 +							dest = addStr(dest, "#0x");
   1.415 +							dest = addHex(dest, 0, off);
   1.416 +						}
   1.417 +					}
   1.418 +					if (opcode & (1 << 24))
   1.419 +					{
   1.420 +						*dest++ = ']';
   1.421 +						if (opcode & (1 << 21))
   1.422 +							*dest++ = '!';
   1.423 +					}
   1.424 +				}
   1.425 +				break;
   1.426 +			case 't':
   1.427 +				if ((opcode & 0x01200000) == 0x01200000)
   1.428 +					*dest++ = 't';
   1.429 +				break;
   1.430 +			case 'h':
   1.431 +				if (opcode & (1 << 6))
   1.432 +					*dest++ = 's';
   1.433 +				if (opcode & (1 << 5))
   1.434 +					*dest++ = 'h';
   1.435 +				else
   1.436 +					*dest++ = 'b';
   1.437 +				break;
   1.438 +			case 'm':
   1.439 +				if (((opcode >> 16) & 15) == 13)
   1.440 +				{
   1.441 +					if (opcode & 0x00100000)
   1.442 +						dest = addStr(dest, armMultLoadStore[8 + ((opcode >> 23) & 3)]);
   1.443 +					else
   1.444 +						dest = addStr(dest, armMultLoadStore[4 + ((opcode >> 23) & 3)]);
   1.445 +				}
   1.446 +				else
   1.447 +					dest = addStr(dest, armMultLoadStore[(opcode >> 23) & 3]);
   1.448 +				break;
   1.449 +			case 'l':
   1.450 +				if (opcode & (1 << 21))
   1.451 +					*dest++ = '!';
   1.452 +				dest = addStr(dest, ", {");
   1.453 +				{
   1.454 +					int rlst	  = opcode & 0xffff;
   1.455 +					int msk		  = 0;
   1.456 +					int not_first = 0;
   1.457 +					while (msk < 16)
   1.458 +					{
   1.459 +						if (rlst & (1 << msk))
   1.460 +						{
   1.461 +							int fr = msk;
   1.462 +							while (rlst & (1 << msk))
   1.463 +								msk++;
   1.464 +							int to = msk - 1;
   1.465 +							if (not_first)
   1.466 +								//dest = addStr(dest, ", ");
   1.467 +								*dest++ = ',';
   1.468 +							dest = addStr(dest, regs[fr]);
   1.469 +							if (fr != to)
   1.470 +							{
   1.471 +								if (fr == to - 1)
   1.472 +									//dest = addStr(", ");
   1.473 +									*dest++ = ',';
   1.474 +								else
   1.475 +									*dest++ = '-';
   1.476 +								dest = addStr(dest, regs[to]);
   1.477 +							}
   1.478 +							not_first = 1;
   1.479 +						}
   1.480 +						else
   1.481 +							msk++;
   1.482 +					}
   1.483 +					*dest++ = '}';
   1.484 +					if (opcode & (1 << 22))
   1.485 +						*dest++ = '^';
   1.486 +				}
   1.487 +				break;
   1.488 +			case 'q':
   1.489 +				*dest++ = '$';
   1.490 +				dest	= addHex(dest, 24, opcode & 0xffffff);
   1.491 +				break;
   1.492 +			case 'P':
   1.493 +				*dest++ = 'p';
   1.494 +				dest	= addStr(dest, decVals[(opcode >> 8) & 15]);
   1.495 +				break;
   1.496 +			case 'N':
   1.497 +				if (opcode & 0x10)
   1.498 +					dest = addStr(dest, decVals[(opcode >> 21) & 7]);
   1.499 +				else
   1.500 +					dest = addStr(dest, decVals[(opcode >> 20) & 15]);
   1.501 +				break;
   1.502 +			case 'R':
   1.503 +			{
   1.504 +				src++;
   1.505 +				int reg = 4 * (*src - '0');
   1.506 +				*dest++ = 'c';
   1.507 +				dest	= addStr(dest, decVals[(opcode >> reg) & 15]);
   1.508 +			}
   1.509 +			break;
   1.510 +			case 'V':
   1.511 +			{
   1.512 +				int val = (opcode >> 5) & 7;
   1.513 +				if (val)
   1.514 +				{
   1.515 +					dest = addStr(dest, ", ");
   1.516 +					dest = addStr(dest, decVals[val]);
   1.517 +				}
   1.518 +			}
   1.519 +			break;
   1.520 +			case 'L':
   1.521 +				if (opcode & (1 << 22))
   1.522 +					*dest++ = 'l';
   1.523 +				break;
   1.524 +			case 'A':
   1.525 +				if ((opcode & 0x012f0000) == 0x010f0000)
   1.526 +				{
   1.527 +					int adr = offset + 8;
   1.528 +					int add = (opcode & 0xff) << 2;
   1.529 +					if (opcode & (1 << 23))
   1.530 +						adr += add;
   1.531 +					else
   1.532 +						adr -= add;
   1.533 +					*dest++ = '$';
   1.534 +					addHex(dest, 32, adr);
   1.535 +				}
   1.536 +				else
   1.537 +				{
   1.538 +					*dest++ = '[';
   1.539 +					dest	= addStr(dest, regs[(opcode >> 16) & 15]);
   1.540 +					if (!(opcode & (1 << 24)))
   1.541 +						*dest++ = ']';
   1.542 +					int off = (opcode & 0xff) << 2;
   1.543 +					if (off)
   1.544 +					{
   1.545 +						dest = addStr(dest, ", ");
   1.546 +						if (!(opcode & (1 << 23)))
   1.547 +							*dest++ = '-';
   1.548 +						dest = addStr(dest, "#0x");
   1.549 +						dest = addHex(dest, 0, off);
   1.550 +					}
   1.551 +					if (opcode & (1 << 24))
   1.552 +					{
   1.553 +						*dest++ = ']';
   1.554 +						if (opcode & (1 << 21))
   1.555 +							*dest++ = '!';
   1.556 +					}
   1.557 +				}
   1.558 +				break;
   1.559 +			}
   1.560 +			src++;
   1.561 +		}
   1.562 +	}
   1.563 +	*dest++ = 0;
   1.564 +
   1.565 +	return 4;
   1.566 +}
   1.567 +
   1.568 +int disThumb(u32 offset, char *dest, int flags)
   1.569 +{
   1.570 +	u32 opcode = debuggerReadHalfWord(offset);
   1.571 +
   1.572 +	const Opcodes *sp = thumbOpcodes;
   1.573 +	int ret = 2;
   1.574 +	while (sp->cval != (opcode & sp->mask))
   1.575 +		sp++;
   1.576 +
   1.577 +	if (flags & DIS_VIEW_ADDRESS)
   1.578 +	{
   1.579 +		dest	= addHex(dest, 32, offset);
   1.580 +		*dest++ = ' ';
   1.581 +	}
   1.582 +	if (flags & DIS_VIEW_CODE)
   1.583 +	{
   1.584 +		dest	= addHex(dest, 16, opcode);
   1.585 +		*dest++ = ' ';
   1.586 +	}
   1.587 +
   1.588 +	const char *src = sp->mnemonic;
   1.589 +	while (*src)
   1.590 +	{
   1.591 +		if (*src != '%')
   1.592 +			*dest++ = *src++;
   1.593 +		else
   1.594 +		{
   1.595 +			src++;
   1.596 +			switch (*src)
   1.597 +			{
   1.598 +			case 'r':
   1.599 +				src++;
   1.600 +				dest = addStr(dest, regs[(opcode >> (*src - '0')) & 7]);
   1.601 +				break;
   1.602 +			case 'o':
   1.603 +				dest = addStr(dest, "#0x");
   1.604 +				{
   1.605 +					int val = (opcode >> 6) & 0x1f;
   1.606 +					dest = addHex(dest, 8, val);
   1.607 +				}
   1.608 +				break;
   1.609 +			case 'p':
   1.610 +				dest = addStr(dest, "#0x");
   1.611 +				{
   1.612 +					int val = (opcode >> 6) & 0x1f;
   1.613 +					if (!(opcode & (1 << 12)))
   1.614 +						val <<= 2;
   1.615 +					dest = addHex(dest, 0, val);
   1.616 +				}
   1.617 +				break;
   1.618 +			case 'e':
   1.619 +				dest = addStr(dest, "#0x");
   1.620 +				dest = addHex(dest, 0, ((opcode >> 6) & 0x1f) << 1);
   1.621 +				break;
   1.622 +			case 'i':
   1.623 +				dest = addStr(dest, "#0x");
   1.624 +				dest = addHex(dest, 0, (opcode >> 6) & 7);
   1.625 +				break;
   1.626 +			case 'h':
   1.627 +			{
   1.628 +				src++;
   1.629 +				int reg = (opcode >> (*src - '0')) & 7;
   1.630 +				src++;
   1.631 +				if (opcode & (1 << (*src - '0')))
   1.632 +					reg += 8;
   1.633 +				dest = addStr(dest, regs[reg]);
   1.634 +			}
   1.635 +			break;
   1.636 +			case 'O':
   1.637 +				dest = addStr(dest, "#0x");
   1.638 +				dest = addHex(dest, 0, (opcode & 0xff));
   1.639 +				break;
   1.640 +			case 'I':
   1.641 +				*dest++ = '$';
   1.642 +				dest	= addHex(dest, 32, (offset & 0xfffffffc) + 4 + ((opcode & 0xff) << 2));
   1.643 +				break;
   1.644 +			case 'J':
   1.645 +			{
   1.646 +				u32 value = debuggerReadMemory((offset & 0xfffffffc) + 4 +
   1.647 +				                               ((opcode & 0xff) << 2));
   1.648 +				*dest++ = '$';
   1.649 +				dest	= addHex(dest, 32, value);
   1.650 +				const char *s = elfGetAddressSymbol(value);
   1.651 +				if (*s)
   1.652 +				{
   1.653 +					*dest++ = ' ';
   1.654 +					dest	= addStr(dest, s);
   1.655 +				}
   1.656 +			}
   1.657 +			break;
   1.658 +			case 'K':
   1.659 +			{
   1.660 +				u32 value = (offset & 0xfffffffc) + 4 + ((opcode & 0xff) << 2);
   1.661 +				*dest++ = '$';
   1.662 +				dest	= addHex(dest, 32, value);
   1.663 +				const char *s = elfGetAddressSymbol(value);
   1.664 +				if (*s)
   1.665 +				{
   1.666 +					*dest++ = ' ';
   1.667 +					dest	= addStr(dest, s);
   1.668 +				}
   1.669 +			}
   1.670 +			break;
   1.671 +			case 'b':
   1.672 +				if (opcode & (1 << 10))
   1.673 +					*dest++ = 'b';
   1.674 +				break;
   1.675 +			case 'B':
   1.676 +				if (opcode & (1 << 12))
   1.677 +					*dest++ = 'b';
   1.678 +				break;
   1.679 +			case 'w':
   1.680 +				dest = addStr(dest, "#0x");
   1.681 +				dest = addHex(dest, 0, (opcode & 0xff) << 2);
   1.682 +				break;
   1.683 +			case 'W':
   1.684 +				*dest++ = '$';
   1.685 +				{
   1.686 +					int add = opcode & 0xff;
   1.687 +					if (add & 0x80)
   1.688 +						add |= 0xffffff00;
   1.689 +					dest = addHex(dest, 32, (offset & 0xfffffffe) + 4 + (add << 1));
   1.690 +				}
   1.691 +				break;
   1.692 +			case 'c':
   1.693 +				dest = addStr(dest, conditions[(opcode >> 8) & 15]);
   1.694 +				break;
   1.695 +			case 's':
   1.696 +				if (opcode & (1 << 7))
   1.697 +					*dest++ = '-';
   1.698 +				dest = addStr(dest, "#0x");
   1.699 +				dest = addHex(dest, 0, (opcode & 0x7f) << 2);
   1.700 +				break;
   1.701 +			case 'l':
   1.702 +			{
   1.703 +				int rlst	  = opcode & 0xff;
   1.704 +				int msk		  = 0;
   1.705 +				int not_first = 0;
   1.706 +				while (msk < 8)
   1.707 +				{
   1.708 +					if (rlst & (1 << msk))
   1.709 +					{
   1.710 +						int fr = msk;
   1.711 +						while (rlst & (1 << msk))
   1.712 +							msk++;
   1.713 +						int to = msk - 1;
   1.714 +						if (not_first)
   1.715 +							*dest++ = ',';
   1.716 +						dest = addStr(dest, regs[fr]);
   1.717 +						if (fr != to)
   1.718 +						{
   1.719 +							if (fr == to - 1)
   1.720 +								*dest++ = ',';
   1.721 +							else
   1.722 +								*dest++ = '-';
   1.723 +							dest = addStr(dest, regs[to]);
   1.724 +						}
   1.725 +						not_first = 1;
   1.726 +					}
   1.727 +					else
   1.728 +						msk++;
   1.729 +				}
   1.730 +			}
   1.731 +			break;
   1.732 +			case 'm':
   1.733 +				*dest++ = '$';
   1.734 +				dest	= addHex(dest, 8, opcode & 0xff);
   1.735 +				break;
   1.736 +			case 'Z':
   1.737 +				*dest++ = '$';
   1.738 +				dest	= addHex(dest, 16, (opcode & 0x7ff) << 1);
   1.739 +				break;
   1.740 +			case 'a':
   1.741 +				*dest++ = '$';
   1.742 +				{
   1.743 +					int add = opcode & 0x07ff;
   1.744 +					if (add & 0x400)
   1.745 +						add |= 0xfffff800;
   1.746 +					add <<= 1;
   1.747 +					dest  = addHex(dest, 32, offset + 4 + add);
   1.748 +				}
   1.749 +				break;
   1.750 +			case 'A':
   1.751 +			{
   1.752 +				int nopcode = debuggerReadHalfWord(offset + 2);
   1.753 +				int add		= opcode & 0x7ff;
   1.754 +				if (add & 0x400)
   1.755 +					add |= 0xfff800;
   1.756 +				add		= (add << 12) | ((nopcode & 0x7ff) << 1);
   1.757 +				*dest++ = '$';
   1.758 +				dest	= addHex(dest, 32, offset + 4 + add);
   1.759 +				const char *s = elfGetAddressSymbol(offset + 4 + add);
   1.760 +				if (*s)
   1.761 +				{
   1.762 +					*dest++ = ' ';
   1.763 +					*dest++ = '(';
   1.764 +					dest	= addStr(dest, s);
   1.765 +					*dest++ = ')';
   1.766 +				}
   1.767 +				ret = 4;
   1.768 +			}
   1.769 +			break;
   1.770 +			}
   1.771 +			src++;
   1.772 +		}
   1.773 +	}
   1.774 +	*dest++ = 0;
   1.775 +	return ret;
   1.776 +}
   1.777 +