From 610f9cf549677a5caf560c6ef3e00ec206ba71e6 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 17 Apr 2016 02:10:41 -0500 Subject: [PATCH] Start filling out argument decoder table; make things const. --- src/disassembler/arguments.c | 437 ++++++++++++++++++++++++++++++++++++------- src/disassembler/mnemonics.c | 8 +- src/disassembler/sizes.c | 6 +- 3 files changed, 379 insertions(+), 72 deletions(-) diff --git a/src/disassembler/arguments.c b/src/disassembler/arguments.c index d19989f..0dcb14b 100644 --- a/src/disassembler/arguments.c +++ b/src/disassembler/arguments.c @@ -17,12 +17,11 @@ typedef enum { AT_REG_HL, AT_REG_IX, AT_REG_IY, AT_REG_SP, AT_REG_AF_, AT_REG_IXH, AT_REG_IXL, AT_REG_IYH, AT_REG_IYL, /* Immediate */ - AT_IMM_U16, AT_IMM_U8, AT_IMM_S8, AT_IMM_REL, AT_IMM_BIT, AT_IMM_RST, - AT_IMM_IM, + AT_IMM_U16, AT_IMM_U8, AT_IMM_REL, AT_IMM_BIT, AT_IMM_RST, AT_IMM_IM, /* Indirect */ AT_IDR_HL, AT_IDR_BC, AT_IDR_DE, AT_IDR_SP, AT_IDR_IMM, /* Indexed */ - AT_IDX_IX, AT_IDX_IY, + AT_IX_IY, /* Condition */ AT_COND_NZ, AT_COND_Z, AT_COND_NC, AT_COND_C, AT_COND_PO, AT_COND_PE, AT_COND_P, AT_COND_M, @@ -54,7 +53,6 @@ typedef enum { #define YL AT_REG_IYL #define M2 AT_IMM_U16 #define M1 AT_IMM_U8 -#define MS AT_IMM_S8 #define ML AT_IMM_REL #define MB AT_IMM_BIT #define MR AT_IMM_RST @@ -64,8 +62,7 @@ typedef enum { #define ND AT_IDR_DE #define NS AT_IDR_SP #define NM AT_IDR_IMM -#define DX AT_IDX_IX -#define DY AT_IDX_IY +#define XY AT_IX_IY #define NZ AT_COND_NZ #define Z_ AT_COND_Z #define NC AT_COND_NC @@ -79,10 +76,238 @@ typedef enum { static ArgType instr_args[3][256] = { { + __, BC, NB, BC, B_, __, B_, __, __, __, __, BC, C_, C_, C_, __, + __, DE, ND, DE, D_, __, __, __, __, __, __, DE, E_, E_, E_, __, + __, HL, __, HL, H_, __, __, __, __, __, __, HL, L_, L_, L_, __, + __, SP, __, SP, __, __, __, __, __, __, __, SP, A_, A_, A_, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ + }, + { + __, M2, __, __, __, __, M1, __, __, __, __, __, __, __, M1, __, + __, M2, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, M2, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, M2, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ + }, + { + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ + } +}; + +static ArgType instr_args_extended[3][256] = { + { + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ + }, + { + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ + }, + { + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ + } +}; + +static ArgType instr_args_bits[3][256] = { + { + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ + }, + { + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ + }, + { + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ + } +}; + +static ArgType instr_args_index[3][256] = { + { + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, XY, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ + }, + { + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ + }, + { + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ + } +}; + +static ArgType instr_args_index_bits[3][256] = { + { + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, - __, __, __, __, __, __, __, __, __, __, __, __, A_, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, @@ -158,7 +383,6 @@ static ArgType instr_args[3][256] = { #undef YL #undef M2 #undef M1 -#undef MS #undef ML #undef MB #undef MR @@ -168,8 +392,7 @@ static ArgType instr_args[3][256] = { #undef ND #undef NS #undef NM -#undef DX -#undef DY +#undef XY #undef NZ #undef Z_ #undef NC @@ -182,60 +405,139 @@ static ArgType instr_args[3][256] = { #undef PM /* + Decode an immediate argument. +*/ +static void decode_immediate(char *arg, ArgType type, const uint8_t *bytes) +{ + char *format; + + switch (type) { + case AT_IMM_U16: + sprintf(arg, "$%04X", bytes[1] + (bytes[2] << 8)); + break; + case AT_IMM_U8: + // TODO: one byte? + strcpy(arg, "???"); + break; + case AT_IMM_REL: + // TODO: one byte, signed, plus 2? + strcpy(arg, "???"); + break; + case AT_IMM_BIT: + // TODO: bit ... + strcpy(arg, "???"); + break; + case AT_IMM_RST: + // TODO: reset ... + strcpy(arg, "???"); + break; + case AT_IMM_IM: + // TODO: interrupt mode ... + strcpy(arg, "???"); + break; + case AT_IDR_IMM: + // TODO: two bytes? + strcpy(arg, "???"); + break; + case AT_IX_IY: + format = bytes[0] == 0xDD ? "(ix%+hhd)" : "(iy%+hhd)"; + sprintf(arg, format, (int8_t) bytes[1]); + break; + case AT_PORT_IM: + sprintf(arg, "(%u)", bytes[1]); + break; + default: + FATAL("invalid call: decode_immediate(arg, %d, ...)", type) + return; + } +} + +/* Decode a single argument, given its type. */ static void decode_argument(char *arg, ArgType type, const uint8_t *bytes) { + const char *value; + switch (type) { - case AT_NONE: arg[0] = '\0'; break; - case AT_REG_A: strcpy(arg, "a"); break; - case AT_REG_B: strcpy(arg, "b"); break; - case AT_REG_C: strcpy(arg, "c"); break; - case AT_REG_D: strcpy(arg, "d"); break; - case AT_REG_E: strcpy(arg, "e"); break; - case AT_REG_H: strcpy(arg, "h"); break; - case AT_REG_L: strcpy(arg, "l"); break; - case AT_REG_I: strcpy(arg, "i"); break; - case AT_REG_R: strcpy(arg, "r"); break; - case AT_REG_AF: strcpy(arg, "af"); break; - case AT_REG_BC: strcpy(arg, "bc"); break; - case AT_REG_DE: strcpy(arg, "de"); break; - case AT_REG_HL: strcpy(arg, "hl"); break; - case AT_REG_IX: strcpy(arg, "ix"); break; - case AT_REG_IY: strcpy(arg, "iy"); break; - case AT_REG_SP: strcpy(arg, "sp"); break; - case AT_REG_AF_: strcpy(arg, "af'"); break; - case AT_REG_IXH: strcpy(arg, "ixh"); break; - case AT_REG_IXL: strcpy(arg, "ixl"); break; - case AT_REG_IYH: strcpy(arg, "iyh"); break; - case AT_REG_IYL: strcpy(arg, "iyl"); break; - case AT_IMM_U16: strcpy(arg, "???"); break; // TODO - case AT_IMM_U8: strcpy(arg, "???"); break; // TODO - case AT_IMM_S8: strcpy(arg, "???"); break; // TODO - case AT_IMM_REL: strcpy(arg, "???"); break; // TODO - case AT_IMM_BIT: strcpy(arg, "???"); break; // TODO - case AT_IMM_RST: strcpy(arg, "???"); break; // TODO - case AT_IMM_IM: strcpy(arg, "???"); break; // TODO - case AT_IDR_HL: strcpy(arg, "(hl)"); break; - case AT_IDR_BC: strcpy(arg, "(bc)"); break; - case AT_IDR_DE: strcpy(arg, "(de)"); break; - case AT_IDR_SP: strcpy(arg, "(sp)"); break; - case AT_IDR_IMM: strcpy(arg, "???"); break; // TODO - case AT_IDX_IX: strcpy(arg, "???"); break; // TODO - case AT_IDX_IY: strcpy(arg, "???"); break; // TODO - case AT_COND_NZ: strcpy(arg, "nz"); break; - case AT_COND_Z: strcpy(arg, "z"); break; - case AT_COND_NC: strcpy(arg, "nc"); break; - case AT_COND_C: strcpy(arg, "c"); break; - case AT_COND_PO: strcpy(arg, "po"); break; - case AT_COND_PE: strcpy(arg, "pe"); break; - case AT_COND_P: strcpy(arg, "p"); break; - case AT_COND_M: strcpy(arg, "m"); break; - case AT_PORT_C: strcpy(arg, "(c)"); break; - case AT_PORT_IM: strcpy(arg, "???"); break; // TODO + case AT_NONE: + arg[0] = '\0'; + return; + case AT_REG_A: value = "a"; break; + case AT_REG_B: value = "b"; break; + case AT_REG_C: value = "c"; break; + case AT_REG_D: value = "d"; break; + case AT_REG_E: value = "e"; break; + case AT_REG_H: value = "h"; break; + case AT_REG_L: value = "l"; break; + case AT_REG_I: value = "i"; break; + case AT_REG_R: value = "r"; break; + case AT_REG_AF: value = "af"; break; + case AT_REG_BC: value = "bc"; break; + case AT_REG_DE: value = "de"; break; + case AT_REG_HL: value = "hl"; break; + case AT_REG_IX: value = "ix"; break; + case AT_REG_IY: value = "iy"; break; + case AT_REG_SP: value = "sp"; break; + case AT_REG_AF_: value = "af'"; break; + case AT_REG_IXH: value = "ixh"; break; + case AT_REG_IXL: value = "ixl"; break; + case AT_REG_IYH: value = "iyh"; break; + case AT_REG_IYL: value = "iyl"; break; + case AT_IDR_HL: value = "(hl)"; break; + case AT_IDR_BC: value = "(bc)"; break; + case AT_IDR_DE: value = "(de)"; break; + case AT_IDR_SP: value = "(sp)"; break; + case AT_COND_NZ: value = "nz"; break; + case AT_COND_Z: value = "z"; break; + case AT_COND_NC: value = "nc"; break; + case AT_COND_C: value = "c"; break; + case AT_COND_PO: value = "po"; break; + case AT_COND_PE: value = "pe"; break; + case AT_COND_P: value = "p"; break; + case AT_COND_M: value = "m"; break; + case AT_PORT_C: value = "(c)"; break; + case AT_IMM_U16: + case AT_IMM_U8: + case AT_IMM_REL: + case AT_IMM_BIT: + case AT_IMM_RST: + case AT_IMM_IM: + case AT_IDR_IMM: + case AT_IX_IY: + case AT_PORT_IM: + decode_immediate(arg, type, bytes); + return; default: - FATAL("invalid call: decode_argument(%u, ...)", type) + FATAL("invalid call: decode_argument(arg, %d, ...)", type) + return; + } + strcpy(arg, value); +} + +/* + Return the appropriate argument table for the given instruction. + + This function also increments the instruction bytes array to skip any + prefix bits, so bytes[0] will always be the instruction. + + This function return type is ridiculous, but it returns a pointer to an + array of 3 arrays of 256 ArgTypes. +*/ +static inline ArgType (*get_table_and_adjust(const uint8_t **bytes))[3][256] +{ + uint8_t b = (*bytes)[0]; + + if (b == 0xED) + return (*bytes)++, &instr_args_extended; + if (b == 0xCB) + return (*bytes)++, &instr_args_bits; + if (b == 0xDD || b == 0xFD) { + if ((*bytes)[1] == 0xCB) + return (*bytes) += 2, &instr_args_index_bits; + return (*bytes)++, &instr_args_index; } + return &instr_args; } /* @@ -246,11 +548,12 @@ static void decode_argument(char *arg, ArgType type, const uint8_t *bytes) char* decode_arguments(const uint8_t *bytes) { char args[3][MAX_ARG_SIZE], *result; - ArgType type; - size_t i; + ArgType (*table)[3][256], type; + size_t i, len; + table = get_table_and_adjust(&bytes); for (i = 0; i < 3; i++) { - type = instr_args[i][bytes[0]]; + type = (*table)[i][bytes[0]]; decode_argument(args[i], type, bytes); } @@ -258,9 +561,13 @@ char* decode_arguments(const uint8_t *bytes) return NULL; if (!*args[1]) return cr_strdup(args[0]); - if (!*args[2]) - // TODO - return NULL; - // TODO - return NULL; + + // Two or three arguments; need to add commas: + len = strlen(args[0]) + strlen(args[1]) + strlen(args[2]); + result = malloc(sizeof(char) * (len + 2 * (*args[2] ? 3 : 2) + 1)); + if (*args[2]) + sprintf(result, "%s, %s, %s", args[0], args[1], args[2]); + else + sprintf(result, "%s, %s", args[0], args[1]); + return result; } diff --git a/src/disassembler/mnemonics.c b/src/disassembler/mnemonics.c index 603a98e..abeb9e7 100644 --- a/src/disassembler/mnemonics.c +++ b/src/disassembler/mnemonics.c @@ -3,7 +3,7 @@ #include "mnemonics.h" -static char* instr_mnemonics[256] = { +static char* const instr_mnemonics[256] = { /* 00 */ "nop", "ld", "ld", "inc", "inc", "dec", "ld", "rlca", /* 08 */ "ex", "add", "ld", "dec", "inc", "dec", "ld", "rrca", /* 10 */ "djnz", "ld", "ld", "inc", "inc", "dec", "ld", "rla", @@ -38,7 +38,7 @@ static char* instr_mnemonics[256] = { /* F8 */ "ret", "ld", "jp", "ei", "call", "", "cp", "rst" }; -static char* instr_mnemonics_extended[256] = { +static char* const instr_mnemonics_extended[256] = { /* 00 */ "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", /* 08 */ "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", /* 10 */ "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", @@ -73,7 +73,7 @@ static char* instr_mnemonics_extended[256] = { /* F8 */ "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop" }; -static char* instr_mnemonics_bits[256] = { +static char* const instr_mnemonics_bits[256] = { /* 00 */ "rlc", "rlc", "rlc", "rlc", "rlc", "rlc", "rlc", "rlc", /* 08 */ "rrc", "rrc", "rrc", "rrc", "rrc", "rrc", "rrc", "rrc", /* 10 */ "rl", "rl", "rl", "rl", "rl", "rl", "rl", "rl", @@ -108,7 +108,7 @@ static char* instr_mnemonics_bits[256] = { /* F8 */ "set", "set", "set", "set", "set", "set", "set", "set" }; -static char* instr_mnemonics_index[256] = { +static char* const instr_mnemonics_index[256] = { /* 00 */ "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", /* 08 */ "nop", "add", "nop", "nop", "nop", "nop", "nop", "nop", /* 10 */ "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", diff --git a/src/disassembler/sizes.c b/src/disassembler/sizes.c index 449d1b6..7ae396d 100644 --- a/src/disassembler/sizes.c +++ b/src/disassembler/sizes.c @@ -3,7 +3,7 @@ #include "sizes.h" -static size_t instr_sizes[256] = { +static const size_t instr_sizes[256] = { 1, 3, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 3, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 3, 3, 1, 1, 1, 2, 1, 2, 1, 3, 1, 1, 1, 2, 1, @@ -22,7 +22,7 @@ static size_t instr_sizes[256] = { 1, 1, 3, 1, 3, 1, 2, 1, 1, 1, 3, 1, 3, 0, 2, 1 }; -static size_t instr_sizes_extended[256] = { +static const size_t instr_sizes_extended[256] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, @@ -41,7 +41,7 @@ static size_t instr_sizes_extended[256] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }; -static size_t instr_sizes_index[256] = { +static const size_t instr_sizes_index[256] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2,