add alternative LGPL z80-disassembler

This commit is contained in:
condret 2015-01-27 18:31:09 +01:00
parent fb970bebb9
commit 2828ca508c
7 changed files with 1484 additions and 1 deletions

View file

@ -0,0 +1,249 @@
/* radare - LGPL - Copyright 2014-2015 - condret */
#include <r_asm.h>
#include <r_types.h>
#include <stdio.h>
#include "z80_tab.h"
ut8 z80_fddd_branch_index_res (ut8 hex)
{
switch (hex) {
case 0x9:
return 0x0;
case 0x19:
return 0x1;
case 0x21:
case 0x22:
case 0x23:
case 0x24:
case 0x25:
case 0x26:
return hex-0x1f;
case 0x29:
case 0x2a:
case 0x2b:
case 0x2c:
case 0x2d:
case 0x2e:
return hex-0x21;
case 0x34:
case 0x35:
case 0x36:
return hex-0x27;
case 0x39:
return 0x11;
case 0x44:
case 0x45:
case 0x46:
return hex-0x32;
case 0x4c:
case 0x4d:
case 0x4e:
return hex-0x37;
case 0x54: //0x18
case 0x55:
case 0x56:
return hex-0x3c;
case 0x5c:
case 0x5d:
case 0x5e: //0x1d
return hex-0x41;
case 0x60:
case 0x61:
case 0x62:
case 0x63:
case 0x64:
case 0x65:
case 0x66:
case 0x67:
case 0x68:
case 0x69:
case 0x6a:
case 0x6b:
case 0x6c:
case 0x6d:
case 0x6e:
case 0x6f:
case 0x70:
case 0x71:
case 0x72:
case 0x73:
case 0x74:
case 0x75:
return hex-0x42;
case 0x77:
return 0x34;
case 0x7c:
case 0x7d:
case 0x7e:
return hex-0x47;
case 0x84:
case 0x85:
case 0x86:
return hex-0x4c;
case 0x8c:
case 0x8d:
case 0x8e:
return hex-0x51;
case 0x94:
case 0x95:
case 0x96:
return hex-0x56;
case 0x9c:
case 0x9d:
case 0x9e:
return hex-0x5b;
case 0xa4:
case 0xa5:
case 0xa6:
return hex-0x60;
case 0xac:
case 0xad:
case 0xae:
return hex-0x65;
case 0xb4:
case 0xb5:
case 0xb6:
return hex-0x6a;
case 0xbc:
case 0xbd:
case 0xbe:
return hex-0x6f;
case 0xcb:
return 0x50;
case 0xe1:
return 0x51;
case 0xe3:
return 0x52;
case 0xe5:
return 0x53;
case 0xe9:
return 0x54;
case 0xf9:
return 0x55;
}
return 0x56;
}
ut8 z80_ed_branch_index_res (ut8 hex) {
if (hex > 0x39 && 0x4c > hex)
return hex-0x40;
if (hex == 0x4d)
return 0xc;
if (hex > 0x4d && 0x54 > hex)
return hex-0x42;
if (hex > 0x55 && 0x5c > hex)
return hex-0x44;
if (hex > 0x5d && 0x63 > hex)
return hex-0x46;
if (hex > 0x66 && 0x6b > hex)
return hex-0x4a;
if (hex > 0x6e && 0x74 > hex)
return hex-0x4e;
if (hex > 0x77 && 0x7c > hex)
return hex-0x51;
if (hex > 0x9f && 0xa4 > hex)
return hex-0x75;
if (hex > 0xa7 && 0xac > hex)
return hex-0x79;
if (hex > 0xaf && 0xb4 > hex)
return hex-0x7d;
if (hex > 0xb7 && 0xbc > hex)
return hex-0x81;
return 0x3b;
}
static int z80OpLength (const ut8 *buf, int len) {
z80_opcode *op;
int type, ret = 0;
if (len < 1)
return 0;
op = z80_op;
if (op[buf[0]].type & Z80_OP_UNK) {
if (len < 2)
return 0;
if (op[buf[0]].type & Z80_ENC0) {
op = (z80_opcode *)op[buf[0]].op_moar;
type = op[z80_fddd_branch_index_res(buf[1])].type;
}
if (op[buf[0]].type & Z80_ENC1) {
op = (z80_opcode *)op[buf[0]].op_moar;
type = op[z80_ed_branch_index_res(buf[1])].type;
}
} else {
type = op[buf[0]].type;
}
if (type & Z80_OP8)
ret++;
if ((type & Z80_ARG8) && !(type & Z80_ARG16)) //XXX
ret++;
if (type & Z80_OP16)
ret += 2;
if (type & Z80_ARG16)
ret += 2;
if (type & Z80_OP24)
ret += 3;
if (ret > len)
return 0;
return ret;
}
static int z80Disass (RAsmOp *op, const ut8 *buf, int len) {
int ret = z80OpLength (buf, len);
z80_opcode *z_op;
char **cb_tab;
ut8 res;
if (!ret)
return ret;
z_op = z80_op;
switch (z_op[buf[0]].type) {
case Z80_OP8:
sprintf (op->buf_asm, "%s", z_op[buf[0]].name);
return ret;
case Z80_OP8^Z80_ARG8:
sprintf (op->buf_asm, z_op[buf[0]].name, buf[1]);
return ret;
case Z80_OP8^Z80_ARG16:
sprintf (op->buf_asm, z_op[buf[0]].name, buf[1]+(buf[2]<<8));
return ret;
case Z80_OP16:
cb_tab = (char **) z_op[buf[0]].op_moar;
sprintf (op->buf_asm, "%s", cb_tab[buf[1]]);
return ret;
case Z80_OP_UNK ^ Z80_ENC1:
z_op = (z80_opcode *)z_op[buf[0]].op_moar;
res = z80_ed_branch_index_res (buf[1]);
if (z_op[res].type == Z80_OP16)
sprintf (op->buf_asm, "%s", z_op[res].name);
if (z_op[res].type == Z80_OP16^Z80_ARG16)
sprintf (op->buf_asm, z_op[res].name, buf[2]+(buf[3]<<8));
return ret;
case Z80_OP_UNK ^ Z80_ENC0:
z_op = (z80_opcode *)z_op[buf[0]].op_moar;
res = z80_fddd_branch_index_res (buf[1]);
if (z_op[res].type == Z80_OP16)
sprintf (op->buf_asm, "%s", z_op[res].name);
if (z_op[res].type == Z80_OP16^Z80_ARG16)
sprintf (op->buf_asm, z_op[res].name, buf[2]+(buf[3]<<8));
if (z_op[res].type == Z80_OP16^Z80_ARG8, Z80_ARG16)
sprintf (op->buf_asm, z_op[res].name, buf[2], buf[3]);
if (z_op[res].type == Z80_OP24^Z80_ARG8) {
cb_tab = (char **) z_op[res].op_moar;
sprintf (op->buf_asm, cb_tab[buf[3]], buf[2]);
}
}
return ret;
}
/*
int main () {
RAsmOp rop;
ut8 buf[5];
buf[0] = 0xfd;
buf[1] = 0xcb;
z80Disass (&rop, buf, 5);
printf ("%s\n", rop.buf_asm);
return 1;
}
*/

File diff suppressed because it is too large Load diff

View file

@ -17,7 +17,7 @@ ARCHS=mips_gnu.mk x86_cs.mk sparc_cs.mk sparc_gnu.mk java.mk bf.mk arm_gnu.mk da
ARCHS+=x86_as.mk x86_nz.mk cris_gnu.mk
ARCHS+=ppc_gnu.mk ppc_cs.mk x86_olly.mk x86_udis.mk csr.mk x86_nasm.mk avr.mk
ARCHS+=msil.mk sh.mk arm_winedbg.mk tms320.mk gb.mk snes.mk ebc.mk malbolge.mk ws.mk
ARCHS+=6502.mk h8300.mk cr16.mk v850.mk spc700.mk propeller.mk msp430.mk i4004.mk
ARCHS+=6502.mk h8300.mk cr16.mk v850.mk spc700.mk propeller.mk msp430.mk i4004.mk z80_cr.mk
include $(ARCHS)
all: ${ALL_TARGETS}

33
libr/asm/p/asm_z80-cr.c Normal file
View file

@ -0,0 +1,33 @@
/* radare - LGPL - Copyright 2012-2013 - pancake */
#include <r_types.h>
#include <r_util.h>
#include <r_lib.h>
#include <r_asm.h>
#include "../arch/z80-cr/z80-cr.c"
static int do_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
return op->size = z80Disass (op, buf, len);
}
RAsmPlugin r_asm_plugin_z80_cr = {
.name = "z80-cr",
.desc = "Zilog Z80",
.license = "LGPL",
.arch = "z80",
.bits = 8,
.init = NULL,
.fini = NULL,
.disassemble = do_disassemble,
.assemble = NULL,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_z80_cr
};
#endif

9
libr/asm/p/z80_cr.mk Normal file
View file

@ -0,0 +1,9 @@
OBJ_Z80_CR=asm_z80-cr.o
STATIC_OBJ+=${OBJ_Z80_CR}
TARGET_Z80_CR=asm_z80-cr.${EXT_SO}
ALL_TARGETS+=${TARGET_Z80_CR}
${TARGET_Z80_CR}: ${OBJ_Z80_CR}
${CC} $(call libname,asm_z80-cr) ${LDFLAGS} ${CFLAGS} -o ${TARGET_Z80_CR} ${OBJ_Z80_CR}

View file

@ -206,6 +206,7 @@ extern RAsmPlugin r_asm_plugin_propeller;
extern RAsmPlugin r_asm_plugin_msp430;
extern RAsmPlugin r_asm_plugin_i4004;
extern RAsmPlugin r_asm_plugin_cris_gnu;
extern RAsmPlugin r_asm_plugin_z80_cr;
#endif
#ifdef __cplusplus

View file

@ -38,6 +38,7 @@ asm.x86_nz
asm.x86_cs
asm.xcore_cs
asm.z80
asm.z80_cr
asm.i8080
asm.i4004
asm.8051