From ab08e1c1b474faf78fae154d90f917ee0f9280ea Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Thu, 18 Jul 2024 23:39:25 +0100 Subject: [PATCH] Wrap round flash on disasm --- src/disasm.c | 28 ++++++++++++++++++++-------- src/disasm_private.h | 1 + src/disasm_tagfile.c | 8 ++++---- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/disasm.c b/src/disasm.c index 48c108a1..d621dda8 100644 --- a/src/disasm.c +++ b/src/disasm.c @@ -139,6 +139,19 @@ static int Get_Next_Opcode(const char *Bitstream) { return -1; } +int disasm_wrap(int addr) { + int flashsz = cx->dis_opts.FlashSize; + + if(flashsz > 0) { + while(addr >= flashsz) + addr -= flashsz; + while(addr < 0) + addr += flashsz; + } + + return addr; +} + int disasm(const char *Bitstream, int Read, int addr) { int Pos; int Opcode; @@ -154,7 +167,7 @@ int disasm(const char *Bitstream, int Read, int addr) { if(Opcode == -1) { Pos += 2; } else { - cx->dis_op[Opcode].Callback(Bitstream + Pos, Pos + addr, cx->dis_op[Opcode].mnemo); + cx->dis_op[Opcode].Callback(Bitstream + Pos, disasm_wrap(Pos + addr), cx->dis_op[Opcode].mnemo); Pos += Get_Bitmask_Length(cx->dis_op[Opcode].Opcode_String) / 8; } } @@ -182,14 +195,13 @@ int disasm(const char *Bitstream, int Read, int addr) { cx->dis_code[0] = 0; cx->dis_comment[0] = 0; cx->dis_after_code[0] = 0; - cx->dis_op[Opcode].Callback(Bitstream + Pos, Pos + addr, cx->dis_op[Opcode].mnemo); + cx->dis_op[Opcode].Callback(Bitstream + Pos, disasm_wrap(Pos + addr), cx->dis_op[Opcode].mnemo); - if(cx->dis_opts.Process_Labels) { - Print_JumpCalls(Pos + addr); - } + if(cx->dis_opts.Process_Labels) + Print_JumpCalls(disasm_wrap(Pos + addr)); if(cx->dis_opts.Show_Addresses) - term_out("%4x: ", Pos + addr); + term_out("%4x: ", disasm_wrap(Pos + addr)); if(cx->dis_opts.Show_Cycles) term_out("[%-3s] ", avr_opcodes[cx->dis_op[Opcode].mnemo].clock[cx->dis_opts.cycle_index]); @@ -221,8 +233,8 @@ int disasm(const char *Bitstream, int Read, int addr) { Pos += Get_Bitmask_Length(cx->dis_op[Opcode].Opcode_String) / 8; } else { - term_out(".word 0x%02x%02x ; Invalid opcode at 0x%04x\n", // @@@ show unoffical opcode what it might do - ((unsigned char *) Bitstream)[Pos + 1], ((unsigned char *) Bitstream)[Pos], Pos + addr); + term_out(".word 0x%02x%02x ; Invalid opcode at 0x%04x\n", + ((unsigned char *) Bitstream)[Pos + 1], ((unsigned char *) Bitstream)[Pos], disasm_wrap(Pos + addr)); Pos += 2; } } diff --git a/src/disasm_private.h b/src/disasm_private.h index 04bd3293..017d2eff 100644 --- a/src/disasm_private.h +++ b/src/disasm_private.h @@ -42,6 +42,7 @@ #define TYPE_STRING 4 void disasm_init_regfile(const AVRPART *p); +int disasm_wrap(int addr); int Tagfile_FindLabelAddress(int Address); char *Tagfile_GetLabel(int TagIndex); char *Tagfile_GetLabelComment(int TagIndex); diff --git a/src/disasm_tagfile.c b/src/disasm_tagfile.c index b73965cc..5e53d37e 100644 --- a/src/disasm_tagfile.c +++ b/src/disasm_tagfile.c @@ -397,7 +397,7 @@ int Tagfile_Process_Data(const char *Bitstream, int Position, int offset) { int (*ProcessingFunction)(const char *, int, int, int, const char *) = NULL; char Buffer[32]; - Index = Tagfile_FindPGMAddress(Position + offset); + Index = Tagfile_FindPGMAddress(disasm_wrap(Position + offset)); if(Index == -1) return 0; @@ -433,7 +433,7 @@ int Tagfile_Process_Data(const char *Bitstream, int Position, int offset) { } if(cx->dis_PGMLabels[Index].Count != 1) term_out("s"); - term_out(" starting at 0x%x", Position + offset); + term_out(" starting at 0x%x", disasm_wrap(Position + offset)); if(cx->dis_PGMLabels[Index].Comment != NULL) { term_out(" (%s)", cx->dis_PGMLabels[Index].Comment); @@ -442,10 +442,10 @@ int Tagfile_Process_Data(const char *Bitstream, int Position, int offset) { if((cx->dis_PGMLabels[Index].Type == TYPE_ASTRING) || (cx->dis_PGMLabels[Index].Type == TYPE_STRING)) { if(cx->dis_PGMLabels[Index].Comment != NULL) { - snprintf(Buffer, sizeof(Buffer), "%x_%s", Position + offset, cx->dis_PGMLabels[Index].Comment); + snprintf(Buffer, sizeof(Buffer), "%x_%s", disasm_wrap(Position + offset), cx->dis_PGMLabels[Index].Comment); Sanitize_String(Buffer); } else { - snprintf(Buffer, sizeof(Buffer), "%x", Position + offset); + snprintf(Buffer, sizeof(Buffer), "%x", disasm_wrap(Position + offset)); } }