Refactor memory window ASCII display code section
[emu8051.git] / src / memwin.c
index f5a7f61..ce81da9 100644 (file)
 #endif
 
 #include <stdio.h>
+#include <ctype.h> /* For isprint */
 
 #include "common.h"
+#include "memory.h"
+#include "hexfile.h"
 #include "cpu8051.h"
 #include "memwin.h"
 
+static GtkWidget *memlist;
 
-static GtkWidget *memclist;
+#define DATA_COLS 16 /* Must be a power of 2 */
+#define DATA_ROWS (INT_MEM_SIZE / DATA_COLS)
 
-
-GtkWidget *
-memwin_init( int width, int height )
+enum
 {
-  int i;
-  GtkWidget *fixed_frame;
-
-  fixed_frame = gtk_frame_new(0);
-  gtk_frame_set_shadow_type( GTK_FRAME( fixed_frame ), GTK_SHADOW_ETCHED_OUT );
-  gtk_widget_set_usize( GTK_WIDGET( fixed_frame ), width, height );
-
-  memclist = gtk_clist_new( 18 );
-  gtk_clist_set_selection_mode( GTK_CLIST( memclist ), GTK_SELECTION_SINGLE );
-  gtk_widget_set_usize( GTK_WIDGET( memclist ), 620, 250 );
-
-  for( i = 0; i < 18; i++ ) {
-    gtk_clist_set_column_justification( GTK_CLIST( memclist ), i, GTK_JUSTIFY_LEFT );
-  }
-
-  gtk_clist_set_column_width( GTK_CLIST( memclist ), 0, 5*8 );
-
-  for( i = 1; i < 17; i++ ) {
-    gtk_clist_set_column_width( GTK_CLIST( memclist ), i, 2*8 );
-  }
-
-  gtk_clist_set_column_width( GTK_CLIST( memclist ), 17, 16*8 );
-
-#if ( GTK_MAJOR_VERSION == 2)
-  PangoFontDescription *pango_font;
-  pango_font = pango_font_description_from_string( FIXED_FONT );
-  gtk_widget_modify_font( memclist, pango_font );
-#else
-  {
-    GtkStyle *style;
-    /* Setting font for the widget. */
-    style = gtk_style_new();
-    gdk_font_unref( style->font );
-    
-    /* Load a fixed font */
-    style->font = gdk_font_load( FIXED_FONT );
-    gtk_widget_set_style( GTK_WIDGET( memclist ), style );
-  }
-#endif
+       COL_ADDRESS = 0,
+       COL_ASCII = DATA_COLS + 1,
+       N_COLUMNS,
+};
+
+/* Creating a model */
+static GtkListStore *
+memwin_init_store(void)
+{
+       GtkTreeIter iter;
+       int rows;
+       int col;
+       GtkListStore *store;
+       GType col_types[N_COLUMNS];
 
-    char *memdummy[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
-  for( i = 0; i < 16; i++ ) {
-    gtk_clist_append( GTK_CLIST( memclist ), memdummy );
-  }
+       for (col = 0; col < N_COLUMNS; col++) {
+               col_types[col] = G_TYPE_STRING;
+       }
 
-  gtk_container_add( GTK_CONTAINER( fixed_frame ), memclist );
+       store = gtk_list_store_newv(N_COLUMNS, col_types);
 
-  return fixed_frame;
-}
+       /* Initialize with rows of dummy data... */
+       for (rows = 0; rows < DATA_ROWS; rows++) {
+               /* Add new row. */
+               gtk_list_store_append(store, &iter);
+       }
 
+       return store;
+}
 
-/* Dump 16 rows of 16 bytes from Address in Memory (direct addressing) */
-void
-memwin_DumpD( unsigned int Address )
+static void
+memwin_init_columns(void)
 {
-  char TextTmp[255];
-  int row, column, TextLength;
-
-  gtk_clist_freeze( GTK_CLIST( memclist ) );
-  for ( row = 0; row < 16; row++ ) {
-    sprintf( TextTmp, "%.4X", Address );
-    gtk_clist_set_text( GTK_CLIST( memclist ), row, 0, TextTmp );
-
-    for ( column = 0; column < 16; column++ ) {
-      sprintf( TextTmp, "%.2X", ( int ) cpu8051_ReadD( Address + column ) );
-      gtk_clist_set_text( GTK_CLIST( memclist ), row, column + 1, TextTmp );
-    }
-
-    TextLength = 0;
-    for ( column = 0; column < 16; column++ ) {
-      if ( ( ( int ) cpu8051_ReadD( Address + column ) >= 32 ) && ( ( int ) cpu8051_ReadD( Address + column ) <= 126 ) )
-       TextLength += sprintf( &TextTmp[ TextLength ], "%c", cpu8051_ReadD( Address + column ) );
-      else TextLength += sprintf( &TextTmp[ TextLength ], "." );
-    }
-    gtk_clist_set_text( GTK_CLIST( memclist ), row, 17, TextTmp );
-
-    Address += 16;
-  }
-
-  gtk_clist_select_row( GTK_CLIST( memclist ), 0, 0 );
-  gtk_clist_thaw( GTK_CLIST( memclist ) );
+       int i;
+       GtkCellRenderer *renderer;
+       GtkTreeViewColumn *column;
+
+       /* Columns and cell renderers */
+       renderer = gtk_cell_renderer_text_new();
+
+       /* Add address column */
+       column = gtk_tree_view_column_new_with_attributes(
+               "Address", renderer, "text", COL_ADDRESS, NULL);
+       gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
+       gtk_tree_view_append_column(GTK_TREE_VIEW(memlist), column);
+
+       for (i = 1; i < (DATA_COLS + 1); i++) {
+               column = gtk_tree_view_column_new_with_attributes(
+                       "Val", renderer, "text", i, NULL);
+               gtk_tree_view_column_set_sizing(column,
+                                               GTK_TREE_VIEW_COLUMN_AUTOSIZE);
+               gtk_tree_view_append_column(GTK_TREE_VIEW(memlist), column);
+       }
+
+       /* Add ASCII column */
+       column = gtk_tree_view_column_new_with_attributes(
+               "ASCII", renderer, "text", COL_ASCII, NULL);
+       gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
+       gtk_tree_view_append_column(GTK_TREE_VIEW(memlist), column);
 }
 
+GtkWidget *
+memwin_init(void)
+{
+       GtkWidget *scrollwin;
+       GtkListStore *store;
+
+       scrollwin = gtk_scrolled_window_new(NULL, NULL);
+       gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrollwin),
+                                           GTK_SHADOW_ETCHED_OUT);
+
+       /* Automatically add scrollbars when necessary. */
+       gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
+                                      GTK_POLICY_AUTOMATIC,
+                                      GTK_POLICY_AUTOMATIC);
 
-/* Dump 16 rows of 16 bytes from Address in Memory (indirect addressing) */
+       /* Creating a model */
+       store = memwin_init_store();
+
+
+
+       /* Creating the view component */
+       memlist = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
+       gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(memlist), TRUE);
+       gtk_container_add(GTK_CONTAINER(scrollwin), memlist);
+
+       memwin_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 scrollwin;
+}
+
+/* Dump up to 256 bytes from Address in Memory (direct addressing) */
 void
-memwin_DumpI( unsigned int Address )
+memwin_DumpD(char *MemAddress)
 {
-  char TextTmp[255];
-  int row, column, TextLength;
-  
-  gtk_clist_freeze( GTK_CLIST( memclist ) );
-  for ( row = 0; row < 16; row++ ) {
-    sprintf( TextTmp, "%.4X", Address );
-    gtk_clist_set_text( GTK_CLIST( memclist ), row, 0, TextTmp );
-    
-    for ( column = 0; column < 16; column++ ) {
-      sprintf( TextTmp, "%.2X", ( int ) cpu8051_ReadI( Address + column ) );
-      gtk_clist_set_text( GTK_CLIST( memclist ), row, column + 1, TextTmp );
-    }
-    
-    TextLength = 0;
-    for ( column = 0; column < 16; column++ ) {
-      if ( ( ( int ) cpu8051_ReadI( Address + column ) >= 32 ) && ( ( int ) cpu8051_ReadI( Address + column ) <= 126 ) )
-       TextLength += sprintf( &TextTmp[ TextLength ], "%c", cpu8051_ReadI( Address + column ) );
-      else TextLength += sprintf( &TextTmp[ TextLength ], "." );
-    }
-    gtk_clist_set_text( GTK_CLIST( memclist ), row, 17, TextTmp );
-    
-    Address += 16;
-  }
-  gtk_clist_thaw( GTK_CLIST( memclist ) );
+       int row;
+       unsigned int Address;
+       GtkListStore *store;
+
+#ifdef EMU8051_DEBUG
+       printf("memwin_DumpD, address = %s\n", MemAddress);
+#endif
+
+       if (strlen(MemAddress) != 0) {
+               if (STREQ(MemAddress, "PC"))
+                       Address = cpu8051.pc;
+               else
+                       Address = Ascii2Hex(MemAddress, strlen(MemAddress));
+       } else {
+               Address = 0;
+       }
+
+       store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(memlist)));
+
+       for (row = 0; row < DATA_ROWS; row++) {
+               int valid;
+               GtkTreeIter iter;
+               char str[4 + 2]; /* Maximum str len is for address column (4 digits) */
+               char ascii_str[DATA_COLS];
+               int col;
+
+               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);
+               }
+
+               if (!valid) {
+                       printf("Invalid iter...\n");
+                       return;
+               }
+
+               /* Display base address. */
+               sprintf(str, "%.4X", Address);
+               gtk_list_store_set(store, &iter, COL_ADDRESS, str, -1);
+
+               for (col = 0; col < DATA_COLS; col++) {
+                       u_int8_t data;
+
+                       data = cpu8051_ReadD(Address + col);
+
+                       /* Display hex data */
+                       sprintf(str, "%.2X", (u_int8_t) data);
+                       gtk_list_store_set(store, &iter, col + 1, str, -1);
+
+                       /* Append to ASCII string (if applicable). */
+                       if (!isprint(data))
+                               data = '.';
+                       sprintf(&ascii_str[col], "%c", data);
+               }
+
+               /* Display ASCII characters. */
+               gtk_list_store_set(store, &iter, COL_ASCII, ascii_str, -1);
+
+               Address += DATA_COLS;
+       }
 }