From: Hugo Villeneuve Date: Thu, 17 Oct 2013 01:56:32 +0000 (-0400) Subject: Refactor code for memwin and pgmwin modify X-Git-Tag: v2.0.0~130 X-Git-Url: http://gitweb.hugovil.com/?a=commitdiff_plain;h=3e67c30ca5f1f959c06d1061556fa46c11163473;p=emu8051.git Refactor code for memwin and pgmwin modify Add common functions int2asciihex and asciihex2int --- diff --git a/src/hexfile.c b/src/hexfile.c index 5ec2055..ef828b8 100644 --- a/src/hexfile.c +++ b/src/hexfile.c @@ -36,6 +36,27 @@ #include "common.h" #include "memory.h" +/* Convert integer to ASCII hex string. */ +void +int2asciihex(int val, char *str, int width) +{ + if (width == 2) + sprintf(str , "%.2X", (u_int8_t) val); + else if (width == 4) + sprintf(str , "%.4X", (u_int16_t) val); +} + +/* Convert ASCII hex string to integer. */ +int +asciihex2int(char *str) +{ + int val; + + sscanf(str, "%X", &val); + + return val; +} + /* Convert an ascii string to an hexadecimal number. */ unsigned int Ascii2Hex(char *istring, int length) diff --git a/src/hexfile.h b/src/hexfile.h index ea27f1f..0f46527 100644 --- a/src/hexfile.h +++ b/src/hexfile.h @@ -22,6 +22,12 @@ #ifndef HEXFILE_H #define HEXFILE_H 1 +void +int2asciihex(int val, char *str, int width); + +int +asciihex2int(char *str); + unsigned int Ascii2Hex(char *istring, int length); diff --git a/src/memwin.c b/src/memwin.c index f5945bf..3bd54f8 100644 --- a/src/memwin.c +++ b/src/memwin.c @@ -96,7 +96,7 @@ memwin_cell_edited(GtkCellRendererText *cell, gchar *path_string, /* Get base address. */ gtk_tree_model_get(model, &iter, COL_ADDRESS, &str, -1); - sscanf(str, "%x", &address); + address = asciihex2int(str); /* Convert column number (1, 2, 3...) to index (0, 1, 2...) */ address += (column - COL_DATA0); @@ -106,8 +106,7 @@ memwin_cell_edited(GtkCellRendererText *cell, gchar *path_string, log_info(" old value: $%02X", old); /* Convert new value (asciihex) to integer. */ - sscanf(new_str, "%x", &new); - + new = asciihex2int(new_str); if ((new < 0) || (new > 255)) { log_info(" new value: out of range"); new = old; /* Put back old value... */ @@ -119,7 +118,7 @@ memwin_cell_edited(GtkCellRendererText *cell, gchar *path_string, cpu8051_WriteD(address, new); /* Convert to text. */ - sprintf(str, "%.2X", new); + int2asciihex(new, str, 2); /* Store new value in gtk model. */ gtk_list_store_set(GTK_LIST_STORE(model), &iter, column, str, -1); @@ -260,7 +259,8 @@ memwin_DumpD(char *MemAddress) } /* Display base address. */ - sprintf(str, "%.4X", Address); + int2asciihex(Address, str, 4); + gtk_list_store_set(store, &iter, COL_ADDRESS, str, -1); for (col = 0; col < DATA_COLS; col++) { @@ -269,7 +269,8 @@ memwin_DumpD(char *MemAddress) data = cpu8051_ReadD(Address + col); /* Display hex data */ - sprintf(str, "%.2X", (u_int8_t) data); + int2asciihex(data, str, 2); + gtk_list_store_set(store, &iter, col + 1, str, -1); /* Append to ASCII string (if applicable). */ diff --git a/src/pgmwin.c b/src/pgmwin.c index e4f9b46..41c74d8 100644 --- a/src/pgmwin.c +++ b/src/pgmwin.c @@ -29,6 +29,7 @@ #include "memory.h" #include "cpu8051.h" #include "pgmwin.h" +#include "hexfile.h" static GtkWidget *pgmlist; @@ -119,7 +120,7 @@ pgmwin_sel_changed_event(GtkWidget *widget, GdkEvent *event, gpointer data) gtk_tree_model_get(model, &iter, COL_ADDR, &str_addr, -1); /* Convert hex address in ASCII to integer. */ - sscanf(str_addr, "%x", &val); + val = asciihex2int(str_addr); log_debug(" row address is: $%04X", val); @@ -228,7 +229,8 @@ pgmwin_Disasm(void) gtk_list_store_set(store, &iter, COL_BREAKPT, str, -1); /* Display base address. */ - sprintf(str, "%.4X", Address); + int2asciihex(Address, str, 4); + gtk_list_store_set(store, &iter, COL_ADDR, str, -1); OpCode = memory_read8(PGM_MEM_ID, Address); @@ -236,8 +238,8 @@ pgmwin_Disasm(void) /* Display instruction hex bytes. */ for (k = 0, col_id = COL_B0; k < InstSize; k++, col_id++) { - sprintf(str, "%.2X", memory_read8(PGM_MEM_ID, - Address + k)); + int2asciihex(memory_read8(PGM_MEM_ID, Address + k), + str, 2); gtk_list_store_set(store, &iter, col_id, str, -1); } diff --git a/src/regwin.c b/src/regwin.c index 06c643a..0578781 100644 --- a/src/regwin.c +++ b/src/regwin.c @@ -31,6 +31,7 @@ #include "regwin.h" #include "memwin.h" #include "instructions_8051.h" +#include "hexfile.h" static GtkWidget *reglist; @@ -64,7 +65,8 @@ regwin_read(int addr, int width) /* Address is low address. */ return (cpu8051_ReadD(addr + 1) << 8) + cpu8051_ReadD(addr); - } + } else + return 0xFFFFFFFF; } static void @@ -339,16 +341,14 @@ regwin_cell_edited(GtkCellRendererText *cell, gchar *path_string, /* Read current (old) value. */ gtk_tree_model_get(model, &iter, COL_VAL, &str, -1); - /* Convert old value (asciihex) to integer. */ - sscanf(str, "%x", &old); + old = asciihex2int(str); if (regwin_infos[row].w == 2) log_info(" old value: $%02X", old); else if (regwin_infos[row].w == 4) log_info(" old value: $%04X", old); - /* Convert new value (asciihex) to integer. */ - sscanf(new_str, "%x", &new); + new = asciihex2int(new_str); if (regwin_infos[row].w == 2) { if ((new < 0) || (new > 0xFF)) { @@ -367,10 +367,7 @@ regwin_cell_edited(GtkCellRendererText *cell, gchar *path_string, } /* Convert new value to text. */ - if (regwin_infos[row].w == 2) - sprintf(str, "%02X", new); - else if (regwin_infos[row].w == 4) - sprintf(str, "%04X", new); + int2asciihex(new, str, regwin_infos[row].w); /* Store new value in emulator register. */ if (regwin_infos[row].write_func == NULL) { @@ -507,14 +504,12 @@ regwin_Show(void) regwin_infos[row].w); } else { /* Read register value using custom function pointer. */ - val = regwin_infos[row].read_func(regwin_infos[row].param); + val = regwin_infos[row].read_func( + regwin_infos[row].param); } /* Convert to specified number of hex digits. */ - if (regwin_infos[row].w == 2) - sprintf(str , "%.2X", (u_int8_t) val); - else if (regwin_infos[row].w == 4) - sprintf(str , "%.4X", (u_int16_t) val); + int2asciihex(val, str, regwin_infos[row].w); gtk_list_store_set(store, &iter, COL_NAME, regwin_infos[row].name,