Search
lxdream.org :: lxdream/src/aica/armdasm.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/aica/armdasm.c
changeset 813:262efed79218
prev811:7ff871670e58
next998:1754a8c6a9cf
author nkeynes
date Mon Aug 18 12:16:55 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Display opcode as 32-bit word rather than 4 bytes... easier to match up with the manual this way
file annotate diff log raw
nkeynes@30
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@30
     3
 * 
nkeynes@4
     4
 * armdasm.c    21 Aug 2004  - ARM7tdmi (ARMv4) disassembler
nkeynes@4
     5
 *
nkeynes@30
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@30
     7
 *
nkeynes@30
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@30
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@30
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@30
    11
 * (at your option) any later version.
nkeynes@30
    12
 *
nkeynes@30
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@30
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@30
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@30
    16
 * GNU General Public License for more details.
nkeynes@4
    17
 */
nkeynes@4
    18
nkeynes@7
    19
#include "aica/armcore.h"
nkeynes@11
    20
#include "aica/armdasm.h"
nkeynes@7
    21
#include <stdlib.h>
nkeynes@4
    22
nkeynes@4
    23
#define COND(ir) (ir>>28)
nkeynes@4
    24
#define OPCODE(ir) ((ir>>20)&0x1F)
nkeynes@4
    25
#define GRP(ir) ((ir>>26)&0x03)
nkeynes@4
    26
#define IFLAG(ir) (ir&0x02000000)
nkeynes@4
    27
#define SFLAG(ir) (ir&0x00100000)
nkeynes@4
    28
#define PFLAG(ir) (ir&0x01000000)
nkeynes@4
    29
#define UFLAG(ir) (ir&0x00800000)
nkeynes@4
    30
#define BFLAG(ir) (ir&0x00400000)
nkeynes@7
    31
#define WFLAG(ir) (ir&0x00200000)
nkeynes@4
    32
#define LFLAG(ir) SFLAG(ir)
nkeynes@4
    33
#define RN(ir) ((ir>>16)&0x0F)
nkeynes@4
    34
#define RD(ir) ((ir>>12)&0x0F)
nkeynes@4
    35
#define RS(ir) ((ir>>8)&0x0F)
nkeynes@4
    36
#define RM(ir) (ir&0x0F)
nkeynes@4
    37
nkeynes@4
    38
#define IMM8(ir) (ir&0xFF)
nkeynes@4
    39
#define IMM12(ir) (ir&0xFFF)
nkeynes@811
    40
#define IMMSPLIT8(ir) (((ir&0xF00)>>4)|(ir&0x0F))
nkeynes@7
    41
#define SHIFTIMM(ir) ((ir>>7)&0x1F)
nkeynes@7
    42
#define IMMROT(ir) ((ir>>7)&0x1E)
nkeynes@4
    43
#define SHIFT(ir) ((ir>>4)&0x07)
nkeynes@4
    44
#define DISP24(ir) ((ir&0x00FFFFFF))
nkeynes@4
    45
#define FSXC(ir) msrFieldMask[RN(ir)]
nkeynes@4
    46
#define ROTIMM12(ir) ROTATE_RIGHT_LONG(IMM8(ir),IMMROT(ir))
nkeynes@431
    47
#define SIGNEXT24(n) (((n)&0x00800000) ? ((n)|0xFF000000) : ((n)&0x00FFFFFF))
nkeynes@13
    48
nkeynes@4
    49
nkeynes@11
    50
nkeynes@11
    51
const struct reg_desc_struct arm_reg_map[] = 
nkeynes@736
    52
{ {"R0", REG_INT, &armr.r[0]}, {"R1", REG_INT, &armr.r[1]},
nkeynes@736
    53
        {"R2", REG_INT, &armr.r[2]}, {"R3", REG_INT, &armr.r[3]},
nkeynes@736
    54
        {"R4", REG_INT, &armr.r[4]}, {"R5", REG_INT, &armr.r[5]},
nkeynes@736
    55
        {"R6", REG_INT, &armr.r[6]}, {"R7", REG_INT, &armr.r[7]},
nkeynes@736
    56
        {"R8", REG_INT, &armr.r[8]}, {"R9", REG_INT, &armr.r[9]},
nkeynes@736
    57
        {"R10",REG_INT, &armr.r[10]}, {"R11",REG_INT, &armr.r[11]},
nkeynes@736
    58
        {"R12",REG_INT, &armr.r[12]}, {"R13",REG_INT, &armr.r[13]},
nkeynes@736
    59
        {"R14",REG_INT, &armr.r[14]}, {"R15",REG_INT, &armr.r[15]},
nkeynes@736
    60
        {"CPSR", REG_INT, &armr.cpsr}, {"SPSR", REG_INT, &armr.spsr},
nkeynes@736
    61
        {NULL, 0, NULL} };
nkeynes@11
    62
nkeynes@11
    63
nkeynes@14
    64
const struct cpu_desc_struct arm_cpu_desc = 
nkeynes@736
    65
{ "ARM7", arm_disasm_instruction, arm_execute_instruction, arm_has_page,
nkeynes@736
    66
        arm_set_breakpoint, arm_clear_breakpoint, arm_get_breakpoint, 4,
nkeynes@736
    67
        (char *)&armr, sizeof(armr), arm_reg_map,
nkeynes@736
    68
        &armr.r[15] };
nkeynes@14
    69
const struct cpu_desc_struct armt_cpu_desc = 
nkeynes@736
    70
{ "ARM7T", armt_disasm_instruction, arm_execute_instruction, arm_has_page,
nkeynes@736
    71
        arm_set_breakpoint, arm_clear_breakpoint, arm_get_breakpoint, 2,
nkeynes@736
    72
        (char*)&armr, sizeof(armr), arm_reg_map,
nkeynes@736
    73
        &armr.r[15] };
nkeynes@11
    74
nkeynes@11
    75
nkeynes@11
    76
nkeynes@11
    77
nkeynes@4
    78
char *conditionNames[] = { "EQ", "NE", "CS", "CC", "MI", "PL", "VS", "VC", 
nkeynes@736
    79
        "HI", "LS", "GE", "LT", "GT", "LE", "  " /*AL*/, "NV" };
nkeynes@736
    80
nkeynes@736
    81
/* fsxc */
nkeynes@4
    82
char *msrFieldMask[] = { "", "c", "x", "xc", "s", "sc", "sx", "sxc",
nkeynes@736
    83
        "f", "fc", "fx", "fxc", "fs", "fsc", "fsx", "fsxc" };
nkeynes@7
    84
char *ldmModes[] = { "DA", "IA", "DB", "IB" };
nkeynes@4
    85
nkeynes@4
    86
#define UNIMP(ir) snprintf( buf, len, "???     " )
nkeynes@4
    87
nkeynes@7
    88
int arm_disasm_shift_operand( uint32_t ir, char *buf, int len )
nkeynes@4
    89
{
nkeynes@736
    90
    uint32_t operand, tmp;
nkeynes@736
    91
    if( IFLAG(ir) == 0 ) {
nkeynes@736
    92
        switch(SHIFT(ir)) {
nkeynes@736
    93
        case 0: /* (Rm << imm) */
nkeynes@736
    94
            tmp = SHIFTIMM(ir);
nkeynes@736
    95
            if( tmp != 0 ) {
nkeynes@736
    96
                return snprintf(buf, len, "R%d << %d", RM(ir), tmp );
nkeynes@736
    97
            } else {
nkeynes@736
    98
                return snprintf(buf, len, "R%d", RM(ir));
nkeynes@736
    99
            }
nkeynes@736
   100
        case 1: /* (Rm << Rs) */
nkeynes@736
   101
            return snprintf(buf, len, "R%d << R%d", RM(ir), RS(ir) );
nkeynes@736
   102
        case 2: /* (Rm >> imm) */
nkeynes@736
   103
            return snprintf(buf, len, "R%d >> %d", RM(ir), SHIFTIMM(ir) );
nkeynes@736
   104
        case 3: /* (Rm >> Rs) */
nkeynes@736
   105
            return snprintf(buf, len, "R%d >> R%d", RM(ir), RS(ir) );
nkeynes@736
   106
        case 4: /* (Rm >>> imm) */
nkeynes@736
   107
            return snprintf(buf, len, "R%d >>> %d", RM(ir), SHIFTIMM(ir) );
nkeynes@736
   108
        case 5: /* (Rm >>> Rs) */
nkeynes@736
   109
            return snprintf(buf, len, "R%d >>> R%d", RM(ir), RS(ir) );
nkeynes@736
   110
        case 6:
nkeynes@736
   111
            tmp = SHIFTIMM(ir);
nkeynes@736
   112
            if( tmp == 0 ) /* RRX aka rotate with carry */
nkeynes@736
   113
                return snprintf(buf, len, "R%d roc 1", RM(ir) );
nkeynes@736
   114
            else
nkeynes@736
   115
                return snprintf(buf, len, "R%d rot %d", RM(ir), SHIFTIMM(ir) );
nkeynes@736
   116
        case 7:
nkeynes@736
   117
            return snprintf(buf, len, "R%d rot R%d", RM(ir), RS(ir) );
nkeynes@736
   118
        }
nkeynes@736
   119
    } else {
nkeynes@736
   120
        operand = IMM8(ir);
nkeynes@736
   121
        tmp = IMMROT(ir);
nkeynes@736
   122
        operand = ROTATE_RIGHT_LONG(operand, tmp);
nkeynes@736
   123
        return snprintf(buf, len, "#%08Xh", operand );
nkeynes@736
   124
    }
nkeynes@736
   125
    return 0;
nkeynes@7
   126
}
nkeynes@7
   127
nkeynes@7
   128
static int arm_disasm_address_index( uint32_t ir, char *buf, int len )
nkeynes@7
   129
{
nkeynes@736
   130
    uint32_t tmp;
nkeynes@736
   131
nkeynes@736
   132
    switch(SHIFT(ir)) {
nkeynes@736
   133
    case 0: /* (Rm << imm) */
nkeynes@736
   134
        tmp = SHIFTIMM(ir);
nkeynes@736
   135
        if( tmp != 0 ) {
nkeynes@736
   136
            return snprintf( buf, len, "R%d << %d", RM(ir), tmp );
nkeynes@736
   137
        } else {
nkeynes@736
   138
            return snprintf( buf, len, "R%d", RM(ir) );
nkeynes@736
   139
        }
nkeynes@736
   140
    case 2: /* (Rm >> imm) */
nkeynes@736
   141
        return snprintf( buf, len, "R%d >> %d", RM(ir), SHIFTIMM(ir) );
nkeynes@736
   142
    case 4: /* (Rm >>> imm) */
nkeynes@736
   143
        return snprintf( buf, len, "R%d >>> %d", RM(ir), SHIFTIMM(ir) );
nkeynes@736
   144
    case 6:
nkeynes@736
   145
        tmp = SHIFTIMM(ir);
nkeynes@736
   146
        if( tmp == 0 ) /* RRX aka rotate with carry */
nkeynes@736
   147
            return snprintf( buf, len, "R%d roc 1", RM(ir) );
nkeynes@736
   148
        else
nkeynes@736
   149
            return snprintf( buf, len, "R%d rot %d", RM(ir), tmp );
nkeynes@736
   150
    default: 
nkeynes@736
   151
        return UNIMP(ir);
nkeynes@736
   152
    }
nkeynes@7
   153
}
nkeynes@7
   154
nkeynes@13
   155
static int arm_disasm_address_operand( uint32_t ir, char *buf, int len,  int pc )
nkeynes@7
   156
{
nkeynes@7
   157
    char  shift[32];
nkeynes@7
   158
nkeynes@736
   159
    char sign = UFLAG(ir) ? '+' : '-';
nkeynes@736
   160
    /* I P U . W */
nkeynes@736
   161
    switch( (ir>>21)&0x19 ) {
nkeynes@736
   162
    case 0: /* Rn -= imm offset (post-indexed) [5.2.8 A5-28] */
nkeynes@736
   163
    case 1:
nkeynes@736
   164
        return snprintf( buf, len, "[R%d], R%d %c= #%04Xh", RN(ir), RN(ir), sign, IMM12(ir) );
nkeynes@736
   165
    case 8: /* Rn - imm offset  [5.2.2 A5-20] */
nkeynes@736
   166
        if( RN(ir) == 15 ) { /* PC relative - decode here */
nkeynes@736
   167
            uint32_t addr = pc + 8 + (UFLAG(ir) ? IMM12(ir) : -IMM12(ir));
nkeynes@736
   168
            return snprintf( buf, len, "[$%08Xh] <- #%08Xh", addr,
nkeynes@736
   169
                    arm_read_long( addr ) );
nkeynes@736
   170
        } else {
nkeynes@736
   171
            return snprintf( buf, len, "[R%d %c #%04Xh]", RN(ir), sign, IMM12(ir) );
nkeynes@736
   172
        }
nkeynes@736
   173
    case 9: /* Rn -= imm offset (pre-indexed)  [5.2.5 A5-24] */
nkeynes@736
   174
        return snprintf( buf, len, "[R%d %c= #%04Xh]", RN(ir), sign, IMM12(ir) );
nkeynes@736
   175
    case 16: /* Rn -= Rm (post-indexed)  [5.2.10 A5-32 ] */
nkeynes@736
   176
    case 17:
nkeynes@736
   177
        arm_disasm_address_index( ir, shift, sizeof(shift) );
nkeynes@736
   178
        return snprintf( buf, len, "[R%d], R%d %c= %s", RN(ir), RN(ir), sign, shift );
nkeynes@736
   179
    case 24: /* Rn - Rm  [5.2.4 A5-23] */
nkeynes@736
   180
        arm_disasm_address_index( ir, shift, sizeof(shift) );
nkeynes@736
   181
        return snprintf( buf, len, "[R%d %c %s]", RN(ir), sign, shift );
nkeynes@736
   182
    case 25: /* RN -= Rm (pre-indexed)  [5.2.7 A5-26] */
nkeynes@736
   183
        arm_disasm_address_index( ir, shift, sizeof(shift) );
nkeynes@736
   184
        return snprintf( buf, len, "[R%d %c= %s]", RN(ir), sign, shift );
nkeynes@736
   185
    default:
nkeynes@736
   186
        return UNIMP(ir); /* Unreachable */
nkeynes@736
   187
    }
nkeynes@7
   188
}
nkeynes@7
   189
nkeynes@811
   190
static int arm_disasm_address3_operand( uint32_t ir, char *buf, int len, int pc )
nkeynes@811
   191
{
nkeynes@811
   192
	char sign = UFLAG(ir) ? '+' : '-';
nkeynes@811
   193
	
nkeynes@811
   194
	switch( (ir>>21) & 0x0B) {
nkeynes@811
   195
    case 0: /* Rn -= Rm (post-indexed) [5.3.7 A5-48] */
nkeynes@811
   196
    case 1: /* UNPREDICTABLE */
nkeynes@811
   197
    	return snprintf( buf, len, "[R%d], R%d %c= R%d", RN(ir), RN(ir), sign, RM(ir) ) ;
nkeynes@811
   198
    case 2: /* Rn -= imm (post-indexed) [5.3.6 A5-46] */
nkeynes@811
   199
    case 3: /* UNPREDICTABLE */
nkeynes@811
   200
    	return snprintf( buf, len, "[R%d], R%d %c= #%04Xh", RN(ir), RN(ir), sign, IMMSPLIT8(ir) );
nkeynes@811
   201
    case 8: /* Rn - Rm [5.3.3 A5-38] */
nkeynes@811
   202
    	return snprintf( buf, len, "[R%d %c R%d]", RN(ir), sign, RM(ir) );
nkeynes@811
   203
    case 9: /* Rn -= Rm (pre-indexed) [5.3.5 A5-42] */
nkeynes@811
   204
    	return snprintf( buf, len, "[R%d %c= R%d]", RN(ir), sign, RM(ir) );
nkeynes@811
   205
    case 10: /* Rn - imm offset [5.3.2 A5-36] */
nkeynes@811
   206
    	return snprintf( buf, len, "[R%d %c #%04Xh]", RN(ir), sign, IMMSPLIT8(ir) );
nkeynes@811
   207
    case 11: /* Rn -= imm offset (pre-indexed) [5.3.4 A5-40] */
nkeynes@811
   208
    	return snprintf( buf, len, "[R%d %c= #%04Xh]", RN(ir), sign, IMMSPLIT8(ir) );
nkeynes@811
   209
    default:
nkeynes@811
   210
    	return UNIMP(ir); /* Unreachable */
nkeynes@811
   211
    }
nkeynes@811
   212
}
nkeynes@811
   213
nkeynes@11
   214
uint32_t arm_disasm_instruction( uint32_t pc, char *buf, int len, char *opcode )
nkeynes@7
   215
{
nkeynes@48
   216
    char operand[64];
nkeynes@11
   217
    uint32_t ir = arm_read_long(pc);
nkeynes@11
   218
    int i,j;
nkeynes@736
   219
nkeynes@813
   220
    sprintf( opcode, "%08X", ir );
nkeynes@11
   221
nkeynes@4
   222
    if( COND(ir) == 0x0F ) {
nkeynes@736
   223
        UNIMP(ir);
nkeynes@736
   224
        return pc+4;
nkeynes@4
   225
    }
nkeynes@4
   226
    char *cond = conditionNames[COND(ir)];
nkeynes@4
   227
nkeynes@736
   228
    switch( GRP(ir) ) {
nkeynes@736
   229
    case 0:
nkeynes@736
   230
        if( (ir & 0x0D900000) == 0x01000000 ) {
nkeynes@736
   231
            /* Instructions that aren't actual data processing */
nkeynes@736
   232
            switch( ir & 0x0FF000F0 ) {
nkeynes@736
   233
            case 0x01200010: /* BXcc */
nkeynes@736
   234
                snprintf(buf, len, "BX%s     R%d", cond, RM(ir));
nkeynes@736
   235
                break;
nkeynes@736
   236
            case 0x01000000: /* MRS Rd, CPSR */
nkeynes@736
   237
                snprintf(buf, len, "MRS%s    R%d, CPSR", cond, RD(ir));
nkeynes@736
   238
                break;
nkeynes@736
   239
            case 0x01400000: /* MRS Rd, SPSR */
nkeynes@736
   240
                snprintf(buf, len, "MRS%s    R%d, SPSR", cond, RD(ir));
nkeynes@736
   241
                break;
nkeynes@736
   242
            case 0x01200000: /* MSR CPSR, Rm */
nkeynes@736
   243
                snprintf(buf, len, "MSR%s    CPSR_%s, R%d", cond, FSXC(ir), RM(ir));
nkeynes@736
   244
                break;
nkeynes@736
   245
            case 0x01600000: /* MSR SPSR, Rm */
nkeynes@736
   246
                snprintf(buf, len, "MSR%s    SPSR_%s, R%d", cond, FSXC(ir), RM(ir));
nkeynes@736
   247
                break;
nkeynes@736
   248
            case 0x03200000: /* MSR CPSR, imm */
nkeynes@736
   249
                snprintf(buf, len, "MSR%s    CPSR_%s, #%08X", cond, FSXC(ir), ROTIMM12(ir));
nkeynes@736
   250
                break;
nkeynes@736
   251
            case 0x03600000: /* MSR SPSR, imm */
nkeynes@736
   252
                snprintf(buf, len, "MSR%s    SPSR_%s, #%08X", cond, FSXC(ir), ROTIMM12(ir));
nkeynes@736
   253
                break;
nkeynes@736
   254
            default:
nkeynes@736
   255
                UNIMP();
nkeynes@736
   256
            }
nkeynes@736
   257
        } else if( (ir & 0x0E000090) == 0x00000090 ) {
nkeynes@736
   258
            /* Neither are these */
nkeynes@736
   259
            switch( (ir>>5)&0x03 ) {
nkeynes@736
   260
            case 0:
nkeynes@736
   261
                /* Arithmetic extension area */
nkeynes@736
   262
                switch(OPCODE(ir)) {
nkeynes@736
   263
                case 0: /* MUL */
nkeynes@736
   264
                    snprintf(buf,len, "MUL%s    R%d, R%d, R%d", cond, RN(ir), RM(ir), RS(ir) );
nkeynes@736
   265
                    break;
nkeynes@736
   266
                case 1: /* MULS */
nkeynes@736
   267
                    break;
nkeynes@736
   268
                case 2: /* MLA */
nkeynes@736
   269
                    snprintf(buf,len, "MLA%s    R%d, R%d, R%d, R%d", cond, RN(ir), RM(ir), RS(ir), RD(ir) );
nkeynes@736
   270
                    break;
nkeynes@736
   271
                case 3: /* MLAS */
nkeynes@736
   272
                    break;
nkeynes@736
   273
                case 8: /* UMULL */
nkeynes@736
   274
                    snprintf(buf,len, "UMULL%s  R%d, R%d, R%d, R%d", cond, RD(ir), RN(ir), RM(ir), RS(ir) );
nkeynes@736
   275
                    break;
nkeynes@736
   276
                case 9: /* UMULLS */
nkeynes@736
   277
                    break;
nkeynes@736
   278
                case 10: /* UMLAL */
nkeynes@736
   279
                    snprintf(buf,len, "UMLAL%s  R%d, R%d, R%d, R%d", cond, RD(ir), RN(ir), RM(ir), RS(ir) );
nkeynes@736
   280
                    break;
nkeynes@736
   281
                case 11: /* UMLALS */
nkeynes@736
   282
                    break;
nkeynes@736
   283
                case 12: /* SMULL */
nkeynes@736
   284
                    snprintf(buf,len, "SMULL%s  R%d, R%d, R%d, R%d", cond, RD(ir), RN(ir), RM(ir), RS(ir) );
nkeynes@736
   285
                    break;
nkeynes@736
   286
                case 13: /* SMULLS */
nkeynes@736
   287
                    break;
nkeynes@736
   288
                case 14: /* SMLAL */
nkeynes@736
   289
                    snprintf(buf,len, "SMLAL%s  R%d, R%d, R%d, R%d", cond, RD(ir), RN(ir), RM(ir), RS(ir) );
nkeynes@736
   290
                    break;
nkeynes@736
   291
                case 15: /* SMLALS */
nkeynes@4
   292
nkeynes@736
   293
                    break;
nkeynes@736
   294
                case 16: /* SWP */
nkeynes@736
   295
                    snprintf(buf,len, "SWP%s    R%d, R%d, [R%d]", cond, RD(ir), RN(ir), RM(ir) );
nkeynes@736
   296
                    break;
nkeynes@736
   297
                case 20: /* SWPB */
nkeynes@736
   298
                    snprintf(buf,len, "SWPB%s   R%d, R%d, [R%d]", cond, RD(ir), RN(ir), RM(ir) );
nkeynes@736
   299
                    break;
nkeynes@736
   300
                default:
nkeynes@736
   301
                    UNIMP(ir);
nkeynes@736
   302
                }
nkeynes@736
   303
                break;
nkeynes@736
   304
                case 1:
nkeynes@811
   305
                    arm_disasm_address3_operand( ir, operand, sizeof(operand), pc );
nkeynes@811
   306
                    if( LFLAG(ir) ) { /* LDRH */
nkeynes@811
   307
                        snprintf(buf, len, "LDR%sH    R%d, %s", cond, RD(ir), operand );
nkeynes@811
   308
                    } else { /* STRH */
nkeynes@811
   309
                        snprintf(buf, len, "STR%sH    R%d, %s", cond, RD(ir), operand );
nkeynes@736
   310
                    }
nkeynes@736
   311
                    break;
nkeynes@736
   312
                case 2:
nkeynes@811
   313
                    if( LFLAG(ir) ) {  /* LDRSB */
nkeynes@811
   314
                        arm_disasm_address3_operand( ir, operand, sizeof(operand), pc );
nkeynes@811
   315
                        snprintf(buf, len, "LDR%sSB    R%d, %s", cond, RD(ir), operand );                       
nkeynes@736
   316
                    } else {
nkeynes@811
   317
                        UNIMP(ir);
nkeynes@736
   318
                    }
nkeynes@736
   319
                    break;
nkeynes@736
   320
                case 3:
nkeynes@811
   321
                    if( LFLAG(ir) ) { /* LDRSH */
nkeynes@811
   322
                        arm_disasm_address3_operand( ir, operand, sizeof(operand), pc );
nkeynes@811
   323
                        snprintf(buf, len, "LDR%sSH    R%d, %s", cond, RD(ir), operand );                       
nkeynes@736
   324
                    } else {
nkeynes@811
   325
                        UNIMP(ir);
nkeynes@736
   326
                    }
nkeynes@736
   327
                    break;
nkeynes@736
   328
            }
nkeynes@736
   329
        } else {
nkeynes@736
   330
            /* Data processing */
nkeynes@4
   331
nkeynes@736
   332
            switch(OPCODE(ir)) {
nkeynes@736
   333
            case 0: /* AND Rd, Rn, operand */
nkeynes@736
   334
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   335
                snprintf(buf, len, "AND%s    R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   336
                break;
nkeynes@736
   337
            case 1: /* ANDS Rd, Rn, operand */
nkeynes@736
   338
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   339
                snprintf(buf, len, "ANDS%s   R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   340
                break;
nkeynes@736
   341
            case 2: /* EOR Rd, Rn, operand */
nkeynes@736
   342
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   343
                snprintf(buf, len, "EOR%s    R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   344
                break;
nkeynes@736
   345
            case 3: /* EORS Rd, Rn, operand */
nkeynes@736
   346
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   347
                snprintf(buf, len, "EORS%s   R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   348
                break;
nkeynes@736
   349
            case 4: /* SUB Rd, Rn, operand */
nkeynes@736
   350
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   351
                snprintf(buf, len, "SUB%s    R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   352
                break;
nkeynes@736
   353
            case 5: /* SUBS Rd, Rn, operand */
nkeynes@736
   354
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   355
                snprintf(buf, len, "SUBS%s   R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   356
                break;
nkeynes@736
   357
            case 6: /* RSB Rd, Rn, operand */
nkeynes@736
   358
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   359
                snprintf(buf, len, "RSB%s    R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   360
                break;
nkeynes@736
   361
            case 7: /* RSBS Rd, Rn, operand */
nkeynes@736
   362
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   363
                snprintf(buf, len, "RSBS%s   R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   364
                break;
nkeynes@736
   365
            case 8: /* ADD Rd, Rn, operand */
nkeynes@736
   366
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   367
                snprintf(buf, len, "ADD%s    R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   368
                break;
nkeynes@736
   369
            case 9: /* ADDS Rd, Rn, operand */
nkeynes@736
   370
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   371
                snprintf(buf, len, "ADDS%s   R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   372
                break;
nkeynes@736
   373
            case 10: /* ADC Rd, Rn, operand */
nkeynes@736
   374
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   375
                snprintf(buf, len, "ADC%s    R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   376
                break;
nkeynes@736
   377
            case 11: /* ADCS Rd, Rn, operand */
nkeynes@736
   378
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   379
                snprintf(buf, len, "ADCS%s   R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   380
                break;
nkeynes@736
   381
            case 12: /* SBC Rd, Rn, operand */
nkeynes@736
   382
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   383
                snprintf(buf, len, "SBC%s    R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   384
                break;
nkeynes@736
   385
            case 13: /* SBCS Rd, Rn, operand */
nkeynes@736
   386
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   387
                snprintf(buf, len, "SBCS%s   R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   388
                break;
nkeynes@736
   389
            case 14: /* RSC Rd, Rn, operand */
nkeynes@736
   390
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   391
                snprintf(buf, len, "RSC%s    R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   392
                break;
nkeynes@736
   393
            case 15: /* RSCS Rd, Rn, operand */
nkeynes@736
   394
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   395
                snprintf(buf, len, "RSCS%s   R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   396
                break;
nkeynes@736
   397
            case 17: /* TST Rd, Rn, operand */
nkeynes@736
   398
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   399
                snprintf(buf, len, "TST%s    R%d, %s", cond, RN(ir), operand);
nkeynes@736
   400
                break;
nkeynes@736
   401
            case 19: /* TEQ Rd, Rn, operand */
nkeynes@736
   402
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   403
                snprintf(buf, len, "TEQ%s    R%d, %s", cond, RN(ir), operand);
nkeynes@736
   404
                break;
nkeynes@736
   405
            case 21: /* CMP Rd, Rn, operand */
nkeynes@736
   406
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   407
                snprintf(buf, len, "CMP%s    R%d, %s", cond, RN(ir), operand);
nkeynes@736
   408
                break;
nkeynes@736
   409
            case 23: /* CMN Rd, Rn, operand */
nkeynes@736
   410
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   411
                snprintf(buf, len, "CMN%s    R%d, %s", cond, RN(ir), operand);
nkeynes@736
   412
                break;
nkeynes@736
   413
            case 24: /* ORR Rd, Rn, operand */
nkeynes@736
   414
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   415
                snprintf(buf, len, "ORR%s    R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   416
                break;
nkeynes@736
   417
            case 25: /* ORRS Rd, Rn, operand */
nkeynes@736
   418
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   419
                snprintf(buf, len, "ORRS%s   R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   420
                break;
nkeynes@736
   421
            case 26: /* MOV Rd, operand */
nkeynes@736
   422
                if( ir == 0xE1A00000 ) {
nkeynes@736
   423
                    /* Not technically a different instruction,
nkeynes@736
   424
                     * but this one is commonly used as a NOP,
nkeynes@736
   425
                     * so... 
nkeynes@736
   426
                     */
nkeynes@736
   427
                    snprintf(buf, len, "NOP");
nkeynes@736
   428
                } else {
nkeynes@736
   429
                    arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   430
                    snprintf(buf, len, "MOV%s    R%d, %s", cond, RD(ir), operand);
nkeynes@736
   431
                }
nkeynes@736
   432
                break;
nkeynes@736
   433
            case 27: /* MOVS Rd, operand */
nkeynes@736
   434
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   435
                snprintf(buf, len, "MOVS%s   R%d, %s", cond, RD(ir), operand);
nkeynes@736
   436
                break;
nkeynes@736
   437
            case 28: /* BIC Rd, Rn, operand */
nkeynes@736
   438
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   439
                snprintf(buf, len, "BIC%s    R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   440
                break;
nkeynes@736
   441
            case 29: /* BICS Rd, Rn, operand */
nkeynes@736
   442
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   443
                snprintf(buf, len, "BICS%s   R%d, R%d, %s", cond, RD(ir), RN(ir), operand);
nkeynes@736
   444
                break;
nkeynes@736
   445
            case 30: /* MVN Rd, Rn, operand */
nkeynes@736
   446
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   447
                snprintf(buf, len, "MVN%s    R%d, %s", cond, RD(ir), operand);
nkeynes@736
   448
                break;
nkeynes@736
   449
            case 31: /* MVNS Rd, Rn, operand */
nkeynes@736
   450
                arm_disasm_shift_operand(ir, operand, sizeof(operand));
nkeynes@736
   451
                snprintf(buf, len, "MVNS%s   R%d, %s", cond, RD(ir), operand);
nkeynes@736
   452
                break;
nkeynes@736
   453
            default:
nkeynes@736
   454
                UNIMP(ir);
nkeynes@736
   455
            }
nkeynes@736
   456
        }
nkeynes@736
   457
        break;
nkeynes@736
   458
    case 1: /* Load/store */
nkeynes@736
   459
        arm_disasm_address_operand( ir, operand, sizeof(operand), pc );
nkeynes@736
   460
        switch( (ir>>20)&0x17 ) {
nkeynes@736
   461
        case 0:
nkeynes@736
   462
        case 16:
nkeynes@736
   463
        case 18:
nkeynes@736
   464
            snprintf(buf, len, "STR%s    R%d, %s", cond, RD(ir), operand );
nkeynes@736
   465
            break;
nkeynes@736
   466
        case 1:
nkeynes@736
   467
        case 17:
nkeynes@736
   468
        case 19:
nkeynes@736
   469
            snprintf(buf, len, "LDR%s    R%d, %s", cond, RD(ir), operand );
nkeynes@736
   470
            break;
nkeynes@736
   471
        case 2:
nkeynes@736
   472
            snprintf(buf, len, "STRT%s   R%d, %s", cond, RD(ir), operand );
nkeynes@736
   473
            break;
nkeynes@736
   474
        case 3:
nkeynes@736
   475
            snprintf(buf, len, "LDRT%s   R%d, %s", cond, RD(ir), operand );
nkeynes@736
   476
            break;
nkeynes@736
   477
        case 4:
nkeynes@736
   478
        case 20:
nkeynes@736
   479
        case 22:
nkeynes@736
   480
            snprintf(buf, len, "STRB%s   R%d, %s", cond, RD(ir), operand );
nkeynes@736
   481
            break;
nkeynes@736
   482
        case 5:
nkeynes@736
   483
        case 21:
nkeynes@736
   484
        case 23:
nkeynes@736
   485
            snprintf(buf, len, "LDRB%s   R%d, %s", cond, RD(ir), operand );
nkeynes@736
   486
            break;
nkeynes@736
   487
        case 6:
nkeynes@736
   488
            snprintf(buf, len, "STRBT%s  R%d, %s", cond, RD(ir), operand );
nkeynes@736
   489
            break;
nkeynes@736
   490
        case 7: 
nkeynes@736
   491
            snprintf(buf, len, "LDRBT%s  R%d, %s", cond, RD(ir), operand );
nkeynes@736
   492
            break;
nkeynes@736
   493
        }
nkeynes@736
   494
        break;
nkeynes@736
   495
        case 2: 
nkeynes@736
   496
            if( (ir & 0x02000000) == 0x02000000 ) {
nkeynes@736
   497
                int32_t offset = SIGNEXT24(ir&0x00FFFFFF) << 2;
nkeynes@736
   498
                if( (ir & 0x01000000) == 0x01000000 ) { 
nkeynes@736
   499
                    snprintf( buf, len, "BL%s     $%08Xh", cond, pc + offset + 8 );
nkeynes@736
   500
                } else {
nkeynes@736
   501
                    snprintf( buf, len, "B%s      $%08Xh", cond, pc + offset + 8 );
nkeynes@736
   502
                }
nkeynes@736
   503
            } else {
nkeynes@736
   504
                /* Load/store multiple */
nkeynes@736
   505
                j = snprintf( buf, len, LFLAG(ir) ? "LDM%s%s  R%d%c,":"STM%s%s  R%d%c,", 
nkeynes@736
   506
                        ldmModes[(ir>>23)&0x03], cond, RN(ir), WFLAG(ir)?'!':' ' );
nkeynes@736
   507
                buf += j;
nkeynes@736
   508
                len -= j;
nkeynes@736
   509
                for( i = 0; i<16 && len > 2; i++ ) {
nkeynes@736
   510
                    if( ir & (1<<i) ) {
nkeynes@736
   511
                        j = snprintf( buf, len, "R%d", i );
nkeynes@736
   512
                        buf+=j;
nkeynes@736
   513
                        len-=j;
nkeynes@736
   514
                    }
nkeynes@736
   515
                }
nkeynes@736
   516
                if( BFLAG(ir) && len > 0 ) {
nkeynes@736
   517
                    buf[0] = '^';
nkeynes@736
   518
                    buf[1] = '\0';
nkeynes@736
   519
                }
nkeynes@736
   520
            }
nkeynes@736
   521
            break;
nkeynes@736
   522
        case 3: /* Copro */
nkeynes@736
   523
            if( (ir & 0x0F000000) == 0x0F000000 ) {
nkeynes@736
   524
                snprintf( buf, len, "SWI%s    #%08Xh", cond, SIGNEXT24(ir) );
nkeynes@736
   525
            } else {
nkeynes@736
   526
                UNIMP(ir);
nkeynes@736
   527
            }
nkeynes@736
   528
            break;
nkeynes@736
   529
    }
nkeynes@736
   530
nkeynes@736
   531
nkeynes@736
   532
nkeynes@736
   533
    return pc+4;
nkeynes@4
   534
}
nkeynes@11
   535
nkeynes@11
   536
nkeynes@11
   537
uint32_t armt_disasm_instruction( uint32_t pc, char *buf, int len, char *opcode )
nkeynes@11
   538
{
nkeynes@11
   539
    uint32_t ir = arm_read_word(pc);
nkeynes@11
   540
    sprintf( opcode, "%02X %02X", ir&0xFF, (ir>>8) );
nkeynes@11
   541
    UNIMP(ir);
nkeynes@11
   542
    return pc+2;
nkeynes@11
   543
}
.