From 87924aebd0c390fe15703d215baeb2c8095281b5 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Mon, 26 Feb 2024 06:56:24 +0100 Subject: [PATCH] Fixed 16 bit values --- src/archs.c | 17 ++++++++++++++++- src/archs.h | 5 +++++ src/uldas.c | 11 +++++------ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/archs.c b/src/archs.c index a8d4aa4..75c77d6 100644 --- a/src/archs.c +++ b/src/archs.c @@ -326,10 +326,25 @@ const char *ULAS_SM83_REGS[] = { void ulas_arch_set(enum ulas_archs arch) { switch (arch) { case ULAS_ARCH_SM83: - ulas.arch = (struct ulas_arch){ULAS_SM83_REGS, ULAS_SM83_REGS_LEN, + ulas.arch = (struct ulas_arch){arch, ULAS_SM83_REGS, ULAS_SM83_REGS_LEN, ULASINSTRS_SM83, ULAS_LE}; break; default: ULASPANIC("Unknown architecture\n"); } } + +unsigned int ulas_arch_opcode_len(const char *buf, unsigned long read) { + if (read == 0) { + return 0; + } + switch (ulas.arch.type) { + case ULAS_ARCH_SM83: + if ((unsigned char)buf[0] == 0xCB) { + return 2; + } + return 1; + default: + return 0; + } +} diff --git a/src/archs.h b/src/archs.h index 26e885a..822c0ae 100644 --- a/src/archs.h +++ b/src/archs.h @@ -69,6 +69,7 @@ enum ulas_asmspetok { enum ulas_archs { ULAS_ARCH_SM83 }; struct ulas_arch { + enum ulas_archs type; const char **regs_names; unsigned long regs_len; @@ -78,4 +79,8 @@ struct ulas_arch { void ulas_arch_set(enum ulas_archs arch); +// returns how many bytes of an instruction are occupied +// by the opcode based on its data +unsigned int ulas_arch_opcode_len(const char *buf, unsigned long read); + #endif diff --git a/src/uldas.c b/src/uldas.c index fdb512f..cecb44b 100644 --- a/src/uldas.c +++ b/src/uldas.c @@ -17,7 +17,7 @@ void ulas_dasm_instr_fout(FILE *src, FILE *dst, const struct ulas_instr *instr, ulas_dasm_print_addr(dst); fprintf(dst, " %s ", instr->name); - int bi = 0; + unsigned int bi = ulas_arch_opcode_len(buf, read); for (int i = 0; instr->tokens[i]; i++) { int dat = instr->tokens[i]; switch (dat) { @@ -25,19 +25,18 @@ void ulas_dasm_instr_fout(FILE *src, FILE *dst, const struct ulas_instr *instr, case ULAS_A8: fprintf(dst, "0x%x", buf[bi++]); break; + case ULAS_A16: case ULAS_E16: { unsigned short val = 0; if (ulas.arch.endianess == ULAS_BE) { - + val = (short)buf[bi + 1] | (short)(buf[bi] << 8); } else { - val = (char)buf[bi+1] | (char)(buf[bi] << 8); + val = (short)buf[bi] | (short)(buf[bi + 1] << 8); } - bi+=2; + bi += 2; fprintf(dst, "0x%x", val); break; } - case ULAS_A16: - break; default: { const char *reg = ulas_asmregstr(dat); if (reg) { -- 2.30.2