Skip to content

Commit

Permalink
mcs96 massaging
Browse files Browse the repository at this point in the history
  • Loading branch information
condret committed Aug 24, 2024
1 parent ce0f27e commit f3d2732
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 38 deletions.
72 changes: 38 additions & 34 deletions libr/arch/p/mcs96/mcs96.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ typedef struct mcs96_op_t {

#define MCS96_FE 0x2000 //0xfe extension

#define MCS96_11B_RELA 0x4000
#define MCS96_1B_RELJMP 0x8000
#define MCS96_2B_RELJMP 0x10000


static const Mcs96Op mcs96_op[] = {
{ "skip", MCS96_1B},
Expand Down Expand Up @@ -58,22 +62,22 @@ static const Mcs96Op mcs96_op[] = {
{ "invalid", MCS96_1B},
{ "invalid", MCS96_1B},
{ "invalid", MCS96_1B},
{ "sjmp", MCS96_2B}, // 0x20
{ "sjmp", MCS96_2B},
{ "sjmp", MCS96_2B},
{ "sjmp", MCS96_2B},
{ "sjmp", MCS96_2B},
{ "sjmp", MCS96_2B},
{ "sjmp", MCS96_2B},
{ "sjmp", MCS96_2B},
{ "scall", MCS96_2B}, // 0x28
{ "scall", MCS96_2B},
{ "scall", MCS96_2B},
{ "scall", MCS96_2B},
{ "scall", MCS96_2B},
{ "scall", MCS96_2B},
{ "scall", MCS96_2B},
{ "scall", MCS96_2B},
{ "sjmp", MCS96_2B | MCS96_11B_RELA}, // 0x20
{ "sjmp", MCS96_2B | MCS96_11B_RELA},
{ "sjmp", MCS96_2B | MCS96_11B_RELA},
{ "sjmp", MCS96_2B | MCS96_11B_RELA},
{ "sjmp", MCS96_2B | MCS96_11B_RELA},
{ "sjmp", MCS96_2B | MCS96_11B_RELA},
{ "sjmp", MCS96_2B | MCS96_11B_RELA},
{ "sjmp", MCS96_2B | MCS96_11B_RELA},
{ "scall", MCS96_2B | MCS96_11B_RELA}, // 0x28
{ "scall", MCS96_2B | MCS96_11B_RELA},
{ "scall", MCS96_2B | MCS96_11B_RELA},
{ "scall", MCS96_2B | MCS96_11B_RELA},
{ "scall", MCS96_2B | MCS96_11B_RELA},
{ "scall", MCS96_2B | MCS96_11B_RELA},
{ "scall", MCS96_2B | MCS96_11B_RELA},
{ "scall", MCS96_2B | MCS96_11B_RELA},
{ "jbc", MCS96_3B}, // 0x30
{ "jbc", MCS96_3B},
{ "jbc", MCS96_3B},
Expand Down Expand Up @@ -235,38 +239,38 @@ static const Mcs96Op mcs96_op[] = {
{ "invalid", MCS96_1B},
{ "pop", MCS96_2B},
{ "pop", MCS96_3B_OR_4B}, //0xcf
{ "jnst", MCS96_2B},
{ "jnh", MCS96_2B},
{ "jgt", MCS96_2B},
{ "jnc", MCS96_2B},
{ "jnvt", MCS96_2B},
{ "jnv", MCS96_2B},
{ "jge", MCS96_2B},
{ "jne", MCS96_2B},
{ "jst", MCS96_2B},
{ "jh", MCS96_2B},
{ "jle", MCS96_2B},
{ "jc", MCS96_2B},
{ "jvt", MCS96_2B},
{ "jv", MCS96_2B},
{ "jlt", MCS96_2B},
{ "je", MCS96_2B}, //0xdf
{ "jnst", MCS96_2B | MCS96_1B_RELJMP},
{ "jnh", MCS96_2B | MCS96_1B_RELJMP},
{ "jgt", MCS96_2B | MCS96_1B_RELJMP},
{ "jnc", MCS96_2B | MCS96_1B_RELJMP},
{ "jnvt", MCS96_2B | MCS96_1B_RELJMP},
{ "jnv", MCS96_2B | MCS96_1B_RELJMP},
{ "jge", MCS96_2B | MCS96_1B_RELJMP},
{ "jne", MCS96_2B | MCS96_1B_RELJMP},
{ "jst", MCS96_2B | MCS96_1B_RELJMP},
{ "jh", MCS96_2B | MCS96_1B_RELJMP},
{ "jle", MCS96_2B | MCS96_1B_RELJMP},
{ "jc", MCS96_2B | MCS96_1B_RELJMP},
{ "jvt", MCS96_2B | MCS96_1B_RELJMP},
{ "jv", MCS96_2B | MCS96_1B_RELJMP},
{ "jlt", MCS96_2B | MCS96_1B_RELJMP},
{ "je", MCS96_2B | MCS96_1B_RELJMP}, //0xdf
{ "djnz", MCS96_3B},
{ "invalid", MCS96_1B},
{ "invalid", MCS96_1B},
{ "br", MCS96_2B},
{ "invalid", MCS96_1B},
{ "invalid", MCS96_1B},
{ "invalid", MCS96_1B},
{ "ljmp", MCS96_3B},
{ "ljmp", MCS96_3B | MCS96_2B_RELJMP},
{ "invalid", MCS96_1B},
{ "invalid", MCS96_1B},
{ "invalid", MCS96_1B},
{ "invalid", MCS96_1B},
{ "invalid", MCS96_1B},
{ "invalid", MCS96_1B},
{ "invalid", MCS96_1B},
{ "lcall", MCS96_3B}, //0xef
{ "lcall", MCS96_3B | MCS96_2B_RELJMP}, //0xef
{ "ret", MCS96_1B},
{ "invalid", MCS96_1B},
{ "pushf", MCS96_1B},
Expand Down
35 changes: 31 additions & 4 deletions libr/arch/p/mcs96/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,38 @@ static int mcs96_len(const ut8 *buf, int len, RAnalOp *op) {
}
if (ret <= len) {
const char *opstr = mcs96_op[buf[0]].ins;
if ((mcs96_op[buf[0]].type & (MCS96_2OP | MCS96_REG_8)) == (MCS96_2OP | MCS96_REG_8) && buf[1] > 0x19 && buf[2] > 0x19) {
if (buf[0] == 0xf0) {
op->type = R_ANAL_OP_TYPE_RET;
op->mnemonic = strdup (opstr);
} else if ((mcs96_op[buf[0]].type & (MCS96_2OP | MCS96_REG_8)) == (MCS96_2OP | MCS96_REG_8) &&
buf[1] > 0x19 && buf[2] > 0x19) {
op->mnemonic = r_str_newf ("%s rb%02x, rb%02x", opstr, buf[1] - 0x1a, buf[2] - 0x1a);
} else if (mcs96_op[buf[0]].type & MCS96_2B) {
if (mcs96_op[buf[0]].type & MCS96_11B_RELA) {
ut16 rela = ((buf[0] & 0x7) << 8) | buf[1];
ut64 dst = op->addr + 2 - (rela & 0x400) + (rela & 0x3ff);
op->mnemonic = r_str_newf ("%s 0x%04"PFMT64x, opstr, dst);
op->type = R_ANAL_OP_TYPE_JMP;
op->jump = dst;
} else if (mcs96_op[buf[0]].type & MCS96_1B_RELJMP) {
ut64 dst = op->addr + 2 - (buf[1] & 0x80) + (buf[1] & 0x7f);
op->mnemonic = r_str_newf ("%s 0x%04"PFMT64x, opstr, dst);
op->type = R_ANAL_OP_TYPE_CJMP;
op->jump = dst;
op->fail = op->addr + 2;
} else {
op->mnemonic = strdup (opstr);
}
} else if (mcs96_op[buf[0]].type & MCS96_3B) {
if (mcs96_op[buf[0]].type & MCS96_2B_RELJMP) {
ut16 rela = (buf[2]<< 8) | buf[1];
ut64 dst = op->addr + 3 - (rela & 0x8000) + (rela & 0x7fff);
op->mnemonic = r_str_newf ("%s 0x%04"PFMT64x, opstr, dst);
op->type = (buf[0] == 0xef)? R_ANAL_OP_TYPE_CALL: R_ANAL_OP_TYPE_JMP;
op->jump = dst;
} else {
op->mnemonic = strdup (opstr);
}
} else {
op->mnemonic = strdup (opstr);
}
Expand All @@ -98,9 +128,6 @@ static int mcs96_len(const ut8 *buf, int len, RAnalOp *op) {
}

static int disassemble(RArchSession *a, RAnalOp *op, const ut8 *buf, int len) {
if (len > 1 && !memcmp (buf, "\xff\xff", 2)) {
return -1;
}
op->size = mcs96_len (buf, len, op);
return op->size;
}
Expand Down

0 comments on commit f3d2732

Please sign in to comment.