2010-04-21 04:56:56 +00:00
|
|
|
/* MSPDebug - debugging tool for MSP430 MCUs
|
|
|
|
* Copyright (C) 2009, 2010 Daniel Beer
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "dis.h"
|
2010-08-13 03:45:08 +00:00
|
|
|
#include "output_util.h"
|
2010-04-21 04:56:56 +00:00
|
|
|
#include "stab.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
static int format_addr(msp430_amode_t amode, uint16_t addr)
|
2010-04-21 04:56:56 +00:00
|
|
|
{
|
|
|
|
char name[64];
|
|
|
|
const char *prefix = "";
|
|
|
|
|
|
|
|
switch (amode) {
|
|
|
|
case MSP430_AMODE_REGISTER:
|
|
|
|
case MSP430_AMODE_INDIRECT:
|
|
|
|
case MSP430_AMODE_INDIRECT_INC:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case MSP430_AMODE_IMMEDIATE:
|
|
|
|
prefix = "#";
|
|
|
|
case MSP430_AMODE_INDEXED:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MSP430_AMODE_ABSOLUTE:
|
|
|
|
prefix = "&";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MSP430_AMODE_SYMBOLIC:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-15 03:00:34 +00:00
|
|
|
print_address(addr, name, sizeof(name));
|
|
|
|
return printc("%s\x1b[1m%s\x1b[0m", prefix, name);
|
2010-04-21 04:56:56 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
static int format_reg(msp430_amode_t amode, msp430_reg_t reg)
|
2010-04-21 04:56:56 +00:00
|
|
|
{
|
|
|
|
const char *prefix = "";
|
|
|
|
const char *suffix = "";
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
switch (amode) {
|
|
|
|
case MSP430_AMODE_REGISTER:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MSP430_AMODE_INDEXED:
|
|
|
|
prefix = "(";
|
|
|
|
suffix = ")";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MSP430_AMODE_IMMEDIATE:
|
|
|
|
case MSP430_AMODE_SYMBOLIC:
|
|
|
|
case MSP430_AMODE_ABSOLUTE:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case MSP430_AMODE_INDIRECT_INC:
|
|
|
|
suffix = "+";
|
|
|
|
case MSP430_AMODE_INDIRECT:
|
|
|
|
prefix = "@";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = dis_reg_name(reg);
|
|
|
|
if (!name)
|
|
|
|
name = "???";
|
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
return printc("%s\x1b[33m%s\x1b[0m%s", prefix, name, suffix);
|
2010-04-21 04:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Given an operands addressing mode, value and associated register,
|
|
|
|
* print the canonical representation of it to stdout.
|
|
|
|
*
|
|
|
|
* Returns the number of characters printed.
|
|
|
|
*/
|
2010-08-13 03:45:08 +00:00
|
|
|
static int format_operand(msp430_amode_t amode, uint16_t addr,
|
2010-04-21 04:56:56 +00:00
|
|
|
msp430_reg_t reg)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
len += format_addr(amode, addr);
|
|
|
|
len += format_reg(amode, reg);
|
|
|
|
|
2010-04-21 04:56:56 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write assembly language for the instruction to this buffer */
|
2010-08-13 03:45:08 +00:00
|
|
|
static int dis_format(const struct msp430_instruction *insn)
|
2010-04-21 04:56:56 +00:00
|
|
|
{
|
2010-08-13 03:45:08 +00:00
|
|
|
int len = 0;
|
2010-04-21 04:56:56 +00:00
|
|
|
const char *opname = dis_opcode_name(insn->op);
|
2010-08-10 00:21:16 +00:00
|
|
|
const char *suffix = "";
|
2010-04-21 04:56:56 +00:00
|
|
|
|
|
|
|
if (!opname)
|
|
|
|
opname = "???";
|
|
|
|
|
2010-08-10 00:21:16 +00:00
|
|
|
if (insn->dsize == MSP430_DSIZE_BYTE)
|
|
|
|
suffix = ".B";
|
|
|
|
else if (insn->dsize == MSP430_DSIZE_AWORD)
|
|
|
|
suffix = ".A";
|
2010-08-10 01:27:35 +00:00
|
|
|
else if (insn->dsize == MSP430_DSIZE_UNKNOWN)
|
|
|
|
suffix = ".?";
|
2010-08-10 00:21:16 +00:00
|
|
|
|
2010-08-10 03:47:36 +00:00
|
|
|
/* Don't show the .A suffix for these instructions */
|
|
|
|
if (insn->op == MSP430_OP_MOVA || insn->op == MSP430_OP_CMPA ||
|
2010-08-10 04:26:45 +00:00
|
|
|
insn->op == MSP430_OP_SUBA || insn->op == MSP430_OP_ADDA ||
|
|
|
|
insn->op == MSP430_OP_BRA || insn->op == MSP430_OP_RETA)
|
2010-08-10 03:47:36 +00:00
|
|
|
suffix = "";
|
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
len += printc("\x1b[36m%s%s\x1b[0m", opname, suffix);
|
|
|
|
while (len < 8)
|
|
|
|
len += printc(" ");
|
2010-04-21 04:56:56 +00:00
|
|
|
|
|
|
|
/* Source operand */
|
|
|
|
if (insn->itype == MSP430_ITYPE_DOUBLE) {
|
2010-08-13 03:45:08 +00:00
|
|
|
len += format_operand(insn->src_mode,
|
|
|
|
insn->src_addr,
|
|
|
|
insn->src_reg);
|
|
|
|
|
|
|
|
printc(",");
|
|
|
|
while (len < 15)
|
|
|
|
len += printc(" ");
|
|
|
|
printc(" ");
|
2010-04-21 04:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Destination operand */
|
|
|
|
if (insn->itype != MSP430_ITYPE_NOARG)
|
2010-08-13 03:45:08 +00:00
|
|
|
len += format_operand(insn->dst_mode,
|
2010-04-21 04:56:56 +00:00
|
|
|
insn->dst_addr,
|
|
|
|
insn->dst_reg);
|
|
|
|
|
2010-08-10 04:04:29 +00:00
|
|
|
/* Repetition count */
|
|
|
|
if (insn->rep_register)
|
2010-08-13 03:45:08 +00:00
|
|
|
len += printc(" [repeat %s]", dis_reg_name(insn->rep_index));
|
2010-08-10 04:04:29 +00:00
|
|
|
else if (insn->rep_index)
|
2010-08-13 03:45:08 +00:00
|
|
|
len += printc(" [repeat %d]", insn->rep_index + 1);
|
2010-04-21 04:56:56 +00:00
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
return len;
|
2010-04-21 04:56:56 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
void disassemble(address_t offset, const uint8_t *data, int length)
|
2010-04-21 04:56:56 +00:00
|
|
|
{
|
|
|
|
int first_line = 1;
|
|
|
|
|
|
|
|
while (length) {
|
|
|
|
struct msp430_instruction insn = {0};
|
|
|
|
int retval;
|
|
|
|
int count;
|
|
|
|
int i;
|
2010-08-05 01:59:33 +00:00
|
|
|
address_t oboff;
|
2010-04-21 04:56:56 +00:00
|
|
|
char obname[64];
|
|
|
|
|
2011-03-15 03:00:34 +00:00
|
|
|
if (!stab_nearest(offset, obname, sizeof(obname), &oboff) &&
|
|
|
|
!oboff) {
|
|
|
|
printc("\x1b[m%s\x1b[0m:\n", obname);
|
|
|
|
} else if (first_line) {
|
|
|
|
print_address(offset, obname, sizeof(obname));
|
|
|
|
printc("\x1b[m%s\x1b[0m:\n", obname);
|
2010-04-21 04:56:56 +00:00
|
|
|
}
|
|
|
|
first_line = 0;
|
|
|
|
|
|
|
|
retval = dis_decode(data, offset, length, &insn);
|
|
|
|
count = retval > 0 ? retval : 2;
|
|
|
|
if (count > length)
|
|
|
|
count = length;
|
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
printc(" \x1b[36m%05x\x1b[0m:", offset);
|
2010-04-21 04:56:56 +00:00
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
2010-08-13 03:45:08 +00:00
|
|
|
printc(" %02x", data[i]);
|
2010-04-21 04:56:56 +00:00
|
|
|
|
2010-08-10 04:30:13 +00:00
|
|
|
while (i < 9) {
|
2010-08-13 03:45:08 +00:00
|
|
|
printc(" ");
|
2010-04-21 04:56:56 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval >= 0)
|
2010-08-13 03:45:08 +00:00
|
|
|
dis_format(&insn);
|
|
|
|
printc("\n");
|
2010-04-21 04:56:56 +00:00
|
|
|
|
|
|
|
offset += count;
|
|
|
|
length -= count;
|
|
|
|
data += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
void hexdump(address_t addr, const uint8_t *data, int data_len)
|
2010-04-21 04:56:56 +00:00
|
|
|
{
|
|
|
|
int offset = 0;
|
|
|
|
|
2010-05-11 22:11:38 +00:00
|
|
|
while (offset < data_len) {
|
2010-04-21 04:56:56 +00:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
/* Address label */
|
2010-08-13 03:45:08 +00:00
|
|
|
printc(" \x1b[36m%05x:\x1b[0m", offset + addr);
|
2010-04-21 04:56:56 +00:00
|
|
|
|
|
|
|
/* Hex portion */
|
2010-05-11 22:11:38 +00:00
|
|
|
for (i = 0; i < 16 && offset + i < data_len; i++)
|
2010-08-13 03:45:08 +00:00
|
|
|
printc(" %02x", data[offset + i]);
|
|
|
|
for (j = i; j < 16; j++)
|
|
|
|
printc(" ");
|
2010-04-21 04:56:56 +00:00
|
|
|
|
|
|
|
/* Printable characters */
|
2010-08-13 03:45:08 +00:00
|
|
|
printc(" \x1b[32m|");
|
2010-04-21 04:56:56 +00:00
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
int c = data[offset + j];
|
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
printc("%c", (c >= 32 && c <= 126) ? c : '.');
|
2010-04-21 04:56:56 +00:00
|
|
|
}
|
|
|
|
for (; j < 16; j++)
|
2010-08-13 03:45:08 +00:00
|
|
|
printc(" ");
|
|
|
|
printc("|\x1b[0m\n");
|
2010-04-21 04:56:56 +00:00
|
|
|
|
|
|
|
offset += i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
void show_regs(const address_t *regs)
|
2010-04-21 04:56:56 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
int j;
|
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
printc(" ");
|
2010-04-21 04:56:56 +00:00
|
|
|
for (j = 0; j < 4; j++) {
|
|
|
|
int k = j * 4 + i;
|
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
printc("(\x1b[1m%3s:\x1b[0m %05x) ",
|
|
|
|
dis_reg_name(k), regs[k]);
|
2010-04-21 04:56:56 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
printc("\n");
|
2010-04-21 04:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-15 03:00:34 +00:00
|
|
|
|
|
|
|
int print_address(address_t addr, char *out, int max_len)
|
|
|
|
{
|
|
|
|
char name[128];
|
|
|
|
address_t offset;
|
|
|
|
|
|
|
|
if (!stab_nearest(addr, name, sizeof(name), &offset)) {
|
|
|
|
if (offset)
|
|
|
|
snprintf(out, max_len, "%s+0x%x", name, offset);
|
|
|
|
else
|
|
|
|
snprintf(out, max_len, "%s", name);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(out, max_len, "0x%04x", addr);
|
|
|
|
return 0;
|
|
|
|
}
|