Replace deprecated gtk_clist (pgmwin)
authorHugo Villeneuve <hugo@hugovil.com>
Wed, 9 Oct 2013 02:34:03 +0000 (22:34 -0400)
committerHugo Villeneuve <hugo@hugovil.com>
Thu, 10 Oct 2013 01:08:17 +0000 (21:08 -0400)
TODO
src/cpu8051.c
src/cpu8051.h
src/pgmwin.c

diff --git a/TODO b/TODO
index 4896be3..133ca0c 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,11 +1,11 @@
        THINGS TO DO
        ------------
 
+-Gtk tree view: Switch from G_TYPE_STRING to G_TYPE_UINT for numbers, and
+ create custom renderer to display as hex numbers.
+
 -Compile only one program for both graphical and console modes, or
  support only the graphical mode.
-- Some fine tuning around the classes
-- Make MemWin, RegWin be able to make a standalone
-  window if parentwin=0.
 - Connect File->Open, View->... and Help->License menus.
 - Make the RegWin and MemWin react when mouse clicked. (modify register or
   memory)
index d8e69cb..22ecf1e 100644 (file)
@@ -472,6 +472,123 @@ cpu8051_IntMemBitInfo(uint8_t bit_address, char *text)
        sprintf(&text[len], ".%X", bit_address);
 }
 
+/* Get instruction size from opcode */
+int
+cpu8051_get_instruction_size(unsigned char opcode)
+{
+       return InstSizesTbl[opcode];
+}
+
+/* Display instruction menmonic. */
+void
+cpu8051_disasm_mnemonic(unsigned char OpCode, char *buf)
+{
+       sprintf(buf, "%s", InstTextTbl[InstTypesTbl[OpCode]]);
+}
+
+/* Disasm instruction arguments starting at address into a text string */
+void
+cpu8051_disasm_args(unsigned int address, char *buf)
+{
+       int len = 0;
+       char TextTmp[20];
+       unsigned char OpCode;
+       int ArgTblOfs;
+       int i;
+
+       OpCode = memory_read8(PGM_MEM_ID, address);
+       ArgTblOfs = OpCode << 2;
+       address++;
+
+       /*
+        * MOV direct, direct (OpCode 85h) is peculiar, the operands
+        * are inverted
+        */
+       if (OpCode == 0x85) {
+               cpu8051_SFRMemInfo(memory_read8(PGM_MEM_ID, address + 1),
+                                  TextTmp);
+               len += sprintf(&buf[len], "%s,", TextTmp);
+               cpu8051_SFRMemInfo(memory_read8(PGM_MEM_ID, address),
+                                  TextTmp);
+               len += sprintf(&buf[len], "%s", TextTmp);
+               address += 2;
+               return;
+       }
+
+       for (i = 1; i <= InstArgTbl[ArgTblOfs]; i++) {
+               switch (InstArgTbl[ArgTblOfs + i]) {
+               case ADDR11: {
+                       len += sprintf(&buf[len],
+                                      "%.4XH", ((OpCode << 3) & 0xF00) +
+                                      (memory_read8(PGM_MEM_ID, address)));
+                       address++;
+                       break;
+               }
+               case ADDR16: {
+                       len += sprintf(
+                               &buf[len], "%.4XH",
+                               ((memory_read8(PGM_MEM_ID, address) << 8) +
+                                memory_read8(PGM_MEM_ID, address + 1)));
+                       address += 2;
+                       break;
+               }
+               case DIRECT: {
+                       cpu8051_SFRMemInfo(memory_read8(PGM_MEM_ID, address),
+                                          TextTmp);
+                       len += sprintf(&buf[len], "%s", TextTmp);
+                       address++;
+                       break;
+               }
+               case BITADDR: {
+                       cpu8051_IntMemBitInfo(
+                               (memory_read8(PGM_MEM_ID, address) & 0xF8),
+                               TextTmp);
+                       len += sprintf(&buf[len], "%s.%X" , TextTmp,
+                                      (memory_read8(PGM_MEM_ID, address) & 7));
+                       address++;
+                       break;
+               }
+               case RELADDR: {
+                       address++;
+                       len += sprintf(&buf[len], "%.4XH", (address & 0xFF00) +
+                                      (((address & 0xFF) +
+                                        memory_read8(PGM_MEM_ID,
+                                                     address - 1)) & 0xFF));
+                       break;
+               }
+               case DATAIMM: {
+                       len += sprintf(&buf[len], "#%.2XH",
+                                      memory_read8(PGM_MEM_ID, address));
+                       address++;
+                       break;
+               }
+               case DATA16: {
+                       len += sprintf(&buf[len], "#%.4XH",
+                                      ((memory_read8(PGM_MEM_ID,
+                                                     address) << 8) +
+                                       memory_read8(PGM_MEM_ID, address+1)));
+                       address += 2;
+                       break;
+               }
+               case CBITADDR: {
+                       cpu8051_IntMemBitInfo((memory_read8(PGM_MEM_ID,
+                                                           address) & 0xF8),
+                                             TextTmp);
+                       len += sprintf(&buf[len], "/%s.%X", TextTmp,
+                                      (memory_read8(PGM_MEM_ID, address) & 7));
+                       address++;
+                       break;
+               }
+               default: {
+                       len += sprintf(&buf[len], "%s",
+                                      ArgsTextTbl[InstArgTbl[ArgTblOfs + i]]);
+               }
+               }
+               if (i < InstArgTbl[ArgTblOfs])
+                       len += sprintf(&buf[len], ",");
+       }
+}
+
 /* Disasm one instruction at Address into a Text string */
 int
 cpu8051_Disasm(unsigned int Address, char *Text)
@@ -483,27 +600,34 @@ cpu8051_Disasm(unsigned int Address, char *Text)
        int InstSize;
        int i;
 
+       /* Display address. */
+       len += sprintf(Text, " %.4X ", Address);
+
        OpCode = memory_read8(PGM_MEM_ID, Address);
        InstSize = InstSizesTbl[OpCode];
 
-       len += sprintf(Text, " %.4X ", Address);
-
+       /* Display hex bytes. */
        for (i = 0; i < InstSize; i++)
                len += sprintf(&Text[len], " %.2X",
                                      memory_read8(PGM_MEM_ID, Address + i));
 
        Address++;
 
+       /* Padd remaining area with spaces. */
        for (; len < 17;)
                len += sprintf(&Text[len], " ");
 
+       /* Display instruction menmonic. */
        len += sprintf(&Text[len], "%s ",
                              InstTextTbl[InstTypesTbl[OpCode]]);
        ArgTblOfs = OpCode << 2;
 
+       /* Padd remaining area with spaces. */
        for (; len < 25;)
                len += sprintf(&Text[len], " ");
 
+       /* Display instruction arguments. */
+
        /*
         * MOV direct, direct (OpCode 85h) is peculiar, the operands
         * are inverted
index f11122a..52e710d 100644 (file)
@@ -87,6 +87,15 @@ cpu8051_ReadI(unsigned int Address);
 unsigned char
 cpu8051_ReadB(uint8_t bit_address);
 
+int
+cpu8051_get_instruction_size(unsigned char opcode);
+
+void
+cpu8051_disasm_mnemonic(unsigned char OpCode, char *buf);
+
+void
+cpu8051_disasm_args(unsigned int address, char *buf);
+
 int
 cpu8051_Disasm(unsigned int Address, char *Text);
 
index 7bf0eb9..3a54091 100644 (file)
 #include <stdio.h>
 
 #include "common.h"
+#include "memory.h"
 #include "cpu8051.h"
 #include "pgmwin.h"
 
-static GtkWidget *pgmclist;
-static unsigned int DisasmAddresses[24];
+static GtkWidget *pgmlist;
 
-/* Disasm 24 lines from CPU */
-void
-pgmwin_Disasm(void)
+#define DATA_ROWS 24
+
+enum
 {
-       char TextTmp[255];
-       int row;
-       int InstSize;
-       unsigned int Address;
+       COL_BREAKPT = 0,
+       COL_ADDR,
+       COL_B0,
+       COL_B1,
+       COL_B2,
+       COL_INST,
+       COL_ARGS,
+       N_COLUMNS,
+};
 
-       Address = cpu8051.pc;
+char *col_names[N_COLUMNS] = {
+       "BPT",
+       "Address",
+       "B0",
+       "B1",
+       "B2",
+       "Mnemonic",
+       "Arguments",
+};
 
-       gtk_clist_freeze(GTK_CLIST(pgmclist));
-       for (row = 0; row < 24; row++) {
-               InstSize = cpu8051_Disasm(Address, TextTmp);
-               if (IsBreakpoint(Address))
-                       TextTmp[0] = '*';
-               gtk_clist_set_text(GTK_CLIST(pgmclist), row, 0, TextTmp);
-               DisasmAddresses[row] = Address;
-               Address += InstSize;
+/* Creating a model */
+static GtkListStore *
+pgmwin_init_store(void)
+{
+       GtkTreeIter iter;
+       int rows;
+       int col;
+       GtkListStore *store;
+       GType col_types[N_COLUMNS];
+
+       for (col = 0; col < N_COLUMNS; col++)
+               col_types[col] = G_TYPE_STRING;
+
+       store = gtk_list_store_newv(N_COLUMNS, col_types);
+
+       /* Initialize with rows of dummy data... */
+       for (rows = 0; rows < DATA_ROWS; rows++)
+               gtk_list_store_append(store, &iter);
+
+       return store;
+}
+
+static void
+pgmwin_init_columns(void)
+{
+       int k;
+       GtkCellRenderer *renderer;
+
+       /* Columns and cell renderers */
+       renderer = gtk_cell_renderer_text_new();
+
+       /* Add columns */
+       for (k = 0; k < N_COLUMNS; k++) {
+               GtkTreeViewColumn *col;
+
+               col = gtk_tree_view_column_new_with_attributes(
+                       col_names[k], renderer, "text", k, NULL);
+               gtk_tree_view_column_set_sizing(col,
+                                               GTK_TREE_VIEW_COLUMN_AUTOSIZE);
+               gtk_tree_view_append_column(GTK_TREE_VIEW(pgmlist), col);
        }
-       gtk_clist_select_row(GTK_CLIST(pgmclist), 0, 0);
-       gtk_clist_thaw(GTK_CLIST(pgmclist));
 }
 
 /* Mouse button pressed in the window. */
 static gint
-pgmwin_ButtonPressEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
+pgmwin_sel_changed_event(GtkWidget *widget, GdkEvent *event, gpointer data)
 {
-       gint row, column;
-       char TextTmp[255];
-
-       gtk_clist_get_selection_info(GTK_CLIST(pgmclist),
-                                    (int) event->button.x,
-                                    (int) event->button.y, &row, &column);
-       if (row >= 24 || row < 0)
-               return TRUE;
-       if (column >= 1 || column < 0)
-               return TRUE;
-       sprintf(TextTmp, "pgmwin_ButtonPressEvent() at %d,%d\n", column, row);
-       g_print(TextTmp);
-       ToggleBreakpoint(DisasmAddresses[row]);
-       pgmwin_Disasm();
+       GtkTreeSelection *selection;
+       GtkTreeModel     *model;
+       GtkTreeIter       iter;
+
+#ifdef EMU8051_DEBUG
+       printf("pgmwin_sel_changed_event()\n");
+#endif
+
+       /* This will only work in single or browse selection mode! */
+       selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pgmlist));
+
+       if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
+               char *str_addr;
+               int val;
+
+               gtk_tree_model_get(model, &iter, COL_ADDR, &str_addr, -1);
+
+               /* Convert hex address in ASCII to integer. */
+               sscanf(str_addr, "%x", &val);
+
+#ifdef EMU8051_DEBUG
+               printf("  row address is: $%04X\n", val);
+#endif
+
+               ToggleBreakpoint(val);
+               pgmwin_Disasm();
+
+               g_free(str_addr);
+       } else {
+#ifdef EMU8051_DEBUG
+               printf("  no row selected.\n");
+#endif
+       }
 
        return FALSE;
 }
 
-
 GtkWidget *
 pgmwin_init(void)
 {
-       int i;
+       GtkWidget *frame;
        GtkWidget *scrollwin;
+       GtkListStore *store;
+       GtkTreeSelection *selection;
+
+       frame = gtk_frame_new("Program");
 
        scrollwin = gtk_scrolled_window_new(NULL, NULL);
        gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrollwin),
@@ -94,23 +159,103 @@ pgmwin_init(void)
                                       GTK_POLICY_AUTOMATIC,
                                       GTK_POLICY_AUTOMATIC);
 
-       pgmclist = gtk_clist_new(1);
-       gtk_clist_set_selection_mode(GTK_CLIST(pgmclist), GTK_SELECTION_SINGLE);
-       gtk_clist_set_column_justification(GTK_CLIST(pgmclist), 0,
-                                          GTK_JUSTIFY_LEFT);
+       gtk_container_add(GTK_CONTAINER(frame), scrollwin);
+
+       /* Creating a model */
+       store = pgmwin_init_store();
+
+       /* Creating the view component */
+       pgmlist = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
+       gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(pgmlist), TRUE);
+       gtk_container_add(GTK_CONTAINER(scrollwin), pgmlist);
+
+       selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pgmlist));
+
+       /* Only one row can be selected at a time. */
+       gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
+
+       g_signal_connect(selection, "changed",
+               G_CALLBACK(pgmwin_sel_changed_event), NULL);
+
+       pgmwin_init_columns();
+
+       /*
+        * The tree view has acquired its own reference to the model, so we can
+        * drop ours. That way the model will be freed automatically when the
+        * tree view is destroyed.
+        */
+       g_object_unref(store);
+
+       return frame;
+}
+
+/* Show disassembled program. */
+void
+pgmwin_Disasm(void)
+{
+       int row;
+       GtkListStore *store;
+       unsigned int Address;
+
+       Address = cpu8051.pc;
+
+       store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(pgmlist)));
 
-       PangoFontDescription *pango_font;
-       pango_font = pango_font_description_from_string(FIXED_FONT);
-       gtk_widget_modify_font(pgmclist, pango_font);
+       for (row = 0; row < DATA_ROWS; row++) {
+               int valid;
+               GtkTreeIter iter;
+               char str[128];
+               int k;
+               int col_id;
+               int InstSize;
+               unsigned char OpCode;
 
-       char *pgmdummy[] = { 0 };
-       for (i = 0; i < 24; i++)
-               gtk_clist_append(GTK_CLIST(pgmclist), pgmdummy);
+               if (row == 0) {
+                       /* Get first row in list store */
+                       valid = gtk_tree_model_get_iter_first(
+                               GTK_TREE_MODEL(store), &iter);
+               } else {
+                       /* Get next row in list store */
+                       valid = gtk_tree_model_iter_next(
+                               GTK_TREE_MODEL(store), &iter);
+               }
 
-       gtk_container_add(GTK_CONTAINER(scrollwin), pgmclist);
+               if (!valid) {
+                       printf("Invalid iter...\n");
+                       return;
+               }
+
+               /* Display breakpoints. */
+               if (IsBreakpoint(Address))
+                       sprintf(str, "*");
+               else
+                       str[0] = '\0';
 
-       gtk_signal_connect(GTK_OBJECT(pgmclist), "button-press-event",
-                          GTK_SIGNAL_FUNC(pgmwin_ButtonPressEvent), NULL);
+               gtk_list_store_set(store, &iter, COL_BREAKPT, str, -1);
 
-       return scrollwin;
+               /* Display base address. */
+               sprintf(str, "%.4X", Address);
+               gtk_list_store_set(store, &iter, COL_ADDR, str, -1);
+
+               OpCode = memory_read8(PGM_MEM_ID, Address);
+               InstSize = cpu8051_get_instruction_size(OpCode);
+
+               /* 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));
+                       gtk_list_store_set(store, &iter, col_id, str, -1);
+               }
+
+               /* Display instruction menmonic. */
+               cpu8051_disasm_mnemonic(OpCode, str);
+               gtk_list_store_set(store, &iter, COL_INST, str, -1);
+
+               /* Display instruction arguments (if applicable). */
+               str[0] = '\0';
+               cpu8051_disasm_args(Address, str);
+               gtk_list_store_set(store, &iter, COL_ARGS, str, -1);
+
+               Address += InstSize;
+       }
 }