From b504b7c8880585d2b3792abe03004399257a9cfe Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 18 Apr 2016 03:36:36 -0500 Subject: [PATCH] Include memory addresses in disassembly comments. --- src/disassembler.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/disassembler.c b/src/disassembler.c index 4dd7830..1ce791e 100644 --- a/src/disassembler.c +++ b/src/disassembler.c @@ -36,10 +36,11 @@ typedef enum { } DataType; typedef struct { - const uint8_t *data; - DataType *types; + size_t index; size_t size; int8_t slot; + const uint8_t *data; + DataType *types; } ROMBank; /* @@ -166,12 +167,13 @@ static char* size_to_string(char *output, size_t size) static void disassemble_header(Disassembly *dis, const ROM *rom) { char buf[64]; - const char *size, *product, *region; + const char *size, *product, *region, *declsize; DEBUG("Disassembling header") size = size_to_string(buf, rom->size); product = rom_product(rom); region = rom_region(rom); + declsize = size_to_string(buf, size_code_to_bytes(rom->declared_size)); WRITE_LINE(dis, ".rom_size\t\"%s\"%s\t; $%zX bytes in %zu banks", size, strlen(size) < 6 ? "\t" : "", rom->size, NUM_BANKS(rom)) @@ -186,8 +188,7 @@ static void disassemble_header(Disassembly *dis, const ROM *rom) WRITE_LINE(dis, ".rom_region\t%u\t\t; %s", rom->region_code, region ? region : "(unknown)") WRITE_LINE(dis, ".rom_declsize\t$%X\t\t; %s", - rom->declared_size, - size_to_string(buf, size_code_to_bytes(rom->declared_size))) + rom->declared_size, declsize) } /* @@ -200,14 +201,16 @@ static ROMBank* init_banks(const ROM *rom) DataType *types = cr_calloc(sizeof(DataType), rom->size); for (i = 0; i < nbanks; i++) { + banks[i].index = i; if (i == nbanks - 1 && rom->size % MMU_ROM_BANK_SIZE) banks[i].size = rom->size % MMU_ROM_BANK_SIZE; else banks[i].size = MMU_ROM_BANK_SIZE; + banks[i].slot = -1; banks[i].data = rom->data + (i * MMU_ROM_BANK_SIZE); banks[i].types = types + (i * MMU_ROM_BANK_SIZE); - banks[i].slot = -1; } + banks[nbanks].data = NULL; // Sentinel return banks; } @@ -222,6 +225,15 @@ static void free_banks(ROMBank *banks) } /* + Return the offset in bytes of the first address in the given bank. +*/ +static size_t get_bank_offset(const ROMBank *bank) +{ + return MMU_ROM_BANK_SIZE * ((bank->slot >= 0) ? bank->slot : + (bank->index > 2) ? 2 : bank->index); +} + +/* Mark the ROM's header as non-binary/non-code inside of the relevant bank. */ static void mark_header(const ROM *rom, ROMBank *banks) @@ -266,7 +278,8 @@ static void render_code(Disassembly *dis, size_t *idx, const ROMBank *bank) strcpy(padding, "\t\t\t\t\t"); } - WRITE_LINE(dis, "\t%s%s\t; %s", instr->line, padding, instr->bytestr) + WRITE_LINE(dis, "\t%s%s\t; $%04zX: %s", + instr->line, padding, get_bank_offset(bank) + *idx, instr->bytestr) (*idx) += instr->size; disas_instr_free(instr); }