Remove obsolescent macro AM_PROG_CC_C_O
[emu8051.git] / src / pgmwin.c
index 80c1f08..0e8961b 100644 (file)
@@ -1,5 +1,23 @@
-/* pgmwin.c */
-
+/*
+ * pgmwin.c
+ *
+ * Copyright (C) 1999 Jonathan St-AndrĂ©
+ * Copyright (C) 1999 Hugo Villeneuve <hugo@hugovil.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
 
 #if HAVE_CONFIG_H
 #  include "config.h"
 
 #include <stdio.h>
 
+#include "common.h"
+#include "memory.h"
 #include "cpu8051.h"
 #include "pgmwin.h"
+#include "hexfile.h"
 
+static GtkWidget *pgmlist;
 
-/* private */
-GtkWidget *pgmwin;
-GtkWidget *pgmclist;
-int NbBreakpoints;
-unsigned int Breakpoints[ MAXBP ];
-unsigned int DisasmAddresses[ 24 ];
-
-
-/*
-int PgmWinNumber = 0;
-int PgmWinNumbers[ 1 ];
-*/
-
-
-/*PgmWin *PgmWinPtrs[ 1 ];*/
-
-
-/* in cpu8051.c */
-extern unsigned int PC;
+#define LIST_VIEW_NAME "Program"
+#define DATA_ROWS 100
 
+enum
+{
+       COL_BREAKPT = 0,
+       COL_ADDR,
+       COL_B0,
+       COL_B1,
+       COL_B2,
+       COL_INST,
+       COL_ARGS,
+       COL_COLOR,
+       N_COLUMNS,
+};
+
+static char *col_names[N_COLUMNS] = {
+       "BPT",
+       "Address",
+       "B0",
+       "B1",
+       "B2",
+       "Mnemonic",
+       "Arguments",
+       "COLOR", /* Not displayed, used to set foreground color of cell. */
+};
+
+/* Creating a model */
+static GtkListStore *
+pgmwin_init_store(void)
+{
+       GtkTreeIter iter;
+       int row;
+       int col;
+       GtkListStore *store;
+       GType col_types[N_COLUMNS];
+
+       /* No need for static array, all our columns are of the same type. */
+       for (col = 0; col < N_COLUMNS; col++)
+               col_types[col] = G_TYPE_STRING;
+
+       store = gtk_list_store_newv(N_COLUMNS, col_types);
+
+       /* Add rows. */
+       for (row = 0; row < DATA_ROWS; row++) {
+               gtk_list_store_append(store, &iter);
+
+               /* Color first row in red (current instruction). */
+               if (row == 0)
+                       gtk_list_store_set(store, &iter, COL_COLOR, "red", -1);
+               else
+                       gtk_list_store_set(store, &iter, COL_COLOR, "black", -1);
+       }
+
+       return store;
+}
 
-//////////////////////////////////////////////////////////////////////////////
-// PgmWin constructor
-//////////////////////////////////////////////////////////////////////////////
-void
-pgmwin_init( GtkWidget *parentwin )
+static void
+pgmwin_init_columns(void)
 {
-  int i;
-  GtkStyle *style;
-  GdkFont *fixedfont;
-  fixedfont = gdk_font_load( "-adobe-courier-medium-r-normal--12-120-75-75-m-70-iso8859-1" );
-
-  pgmclist = gtk_clist_new( 1 );
-  gtk_clist_set_selection_mode( GTK_CLIST( pgmclist ), GTK_SELECTION_SINGLE );
-  gtk_widget_set_usize( GTK_WIDGET( pgmclist ), PGM_WIN_WIDTH, PGM_WIN_HEIGHT );
-  gtk_clist_set_column_justification( GTK_CLIST( pgmclist ), 0, GTK_JUSTIFY_LEFT );
-  gtk_clist_set_column_width( GTK_CLIST( pgmclist ), 0, PGM_WIN_WIDTH-10 );
-
-  style = gtk_widget_get_style( GTK_WIDGET( pgmclist ) );
-
-#ifdef USE_GTK2
-  gtk_style_set_font( style, fixedfont );
-#else
-  style->font = fixedfont;
-#endif
+       int k;
+       GtkCellRenderer *renderer;
+
+       /* Create renderer */
+       renderer = gtk_cell_renderer_text_new();
+
+       /* Add columns, except for last one (COL_COLOR). */
+       for (k = 0; k < COL_COLOR; k++) {
+               GtkTreeViewColumn *col;
+
+               /* Create tree view column */
+               col = gtk_tree_view_column_new();
+               gtk_tree_view_column_set_title(col, col_names[k]);
+               gtk_tree_view_column_set_sizing(col,
+                                               GTK_TREE_VIEW_COLUMN_AUTOSIZE);
+               gtk_tree_view_append_column(GTK_TREE_VIEW(pgmlist), col);
+
+               /* Pack cell renderer into column */
+               gtk_tree_view_column_pack_start(col, renderer, TRUE);
+
+               /* Establish connection between cell renderer and data store. */
+               gtk_tree_view_column_set_attributes(col, renderer, "text", k,
+                                                   "foreground", COL_COLOR,
+                                                   NULL);
+       }
+}
 
-  gtk_widget_set_style( GTK_WIDGET( pgmclist ), style );
+/* Mouse button pressed in the window. */
+static gint
+pgmwin_sel_changed_event(GtkWidget *widget, GdkEvent *event, gpointer data)
+{
+       GtkTreeSelection *selection;
+       GtkTreeModel     *model;
+       GtkTreeIter       iter;
 
-  char *pgmdummy[] = { 0 };
-  for ( i = 0; i < 24; i++ ) gtk_clist_append( GTK_CLIST( pgmclist ), pgmdummy );
+       log_debug("pgmwin_sel_changed_event()");
 
-  gtk_container_add( GTK_CONTAINER( parentwin ), pgmclist );
+       /* This will only work in single or browse selection mode! */
+       selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pgmlist));
 
-  gtk_widget_show( pgmclist );
+       if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
+               char *str_addr;
+               int val;
 
-  NbBreakpoints = 0;
+               gtk_tree_model_get(model, &iter, COL_ADDR, &str_addr, -1);
 
-  /*
-  PgmWinPtrs[ PgmWinNumber ] = this;
-  PgmWinNumbers[ PgmWinNumber ] = PgmWinNumber;
-  gtk_signal_connect( GTK_OBJECT( pgmclist ), "button-press-event", GTK_SIGNAL_FUNC( PgmWinButtonPress ), &PgmWinNumbers[ PgmWinNumber++ ] );
-  */
+               /* Convert hex address in ASCII to integer. */
+               val = asciihex2int(str_addr);
 
-}
+               log_debug("  row address is: $%04X", val);
 
+               ToggleBreakpoint(val);
+               pgmwin_refresh();
 
-//////////////////////////////////////////////////////////////////////////////
-// void pgmwin_Disasm( )
-// Disasm 24 lines from CPU
-//////////////////////////////////////////////////////////////////////////////
-void
-pgmwin_Disasm( )
-{
-char TextTmp[255];
-int row;
-//int TextLength;
-int InstSize;
-unsigned int Address;
-Address = PC;
-
-gtk_clist_freeze( GTK_CLIST( pgmclist ) );
- for ( row = 0; row < 24; row++ ) {
-   InstSize = cpu8051_Disasm( Address, TextTmp );
-   if ( pgmwin_IsBreakpoint( Address ) ) TextTmp[0] = '*';
-   gtk_clist_set_text( GTK_CLIST( pgmclist ), row, 0, TextTmp );
-   DisasmAddresses[ row ] = Address;
-   Address += InstSize;
- }
-gtk_clist_select_row( GTK_CLIST( pgmclist ), 0, 0 );
-gtk_clist_thaw( GTK_CLIST( pgmclist ) );
-}
+               g_free(str_addr);
+       } else {
+               log_debug("  no row selected");
+       }
 
-//////////////////////////////////////////////////////////////////////////////
-// gint pgmwin_ButtonPressEvent( GtkWidget *widget, GdkEvent *event, gpointer data )
-// Mouse button pressed in the window
-//////////////////////////////////////////////////////////////////////////////
-gint pgmwin_ButtonPressEvent( GtkWidget *widget, GdkEvent *event, gpointer data )
-{
-  gint row, column;
-  char TextTmp[ 255 ];
-  //g_print( "pgmwin_ButtonPressEvent(...)\n" );
-  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 );
-  pgmwin_ToggleBreakpoint( DisasmAddresses[ row ] );
-  pgmwin_Disasm( );
-  return FALSE;
+       return FALSE;
 }
 
-//////////////////////////////////////////////////////////////////////////////
-// gint PgmWinButtonPress( GtkWidget *widget, GdkEvent *event, gpointer data )
-// Signal Stub with 3 parameters
-//////////////////////////////////////////////////////////////////////////////
-void
-PgmWinButtonPress( GtkWidget *widget, GdkEvent *event, gpointer data )
+GtkWidget *
+pgmwin_init(void)
 {
-  /*int PWNumber = *( (int *) data );*/
-  
-  pgmwin_ButtonPressEvent( widget, event, 0 );
-}
+       GtkWidget *frame;
+       GtkWidget *scrollwin;
+       GtkListStore *store;
+       GtkTreeSelection *selection;
 
+       frame = gtk_frame_new(LIST_VIEW_NAME);
 
-//////////////////////////////////////////////////////////////////////////////
-// void pgmwin_ShowBreakpoints( )
-// Show Breakpoints list
-//////////////////////////////////////////////////////////////////////////////
-void pgmwin_ShowBreakpoints( )
-{
-  int Index;
+       scrollwin = gtk_scrolled_window_new(NULL, NULL);
+       gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrollwin),
+                                           GTK_SHADOW_ETCHED_OUT);
 
-  for ( Index = 0; Index < NbBreakpoints ; Index++ )
-    printf( "Breakpoint at Address = %.4X\n", Breakpoints[ Index ] );
-}
+       /* Automatically add scrollbars when necessary. */
+       gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
+                                      GTK_POLICY_AUTOMATIC,
+                                      GTK_POLICY_AUTOMATIC);
 
-//////////////////////////////////////////////////////////////////////////////
-// void pgmwin_ClearBreakpoint( unsigned int Address )
-// Clear Breakpoint at Address from list
-//////////////////////////////////////////////////////////////////////////////
-void pgmwin_ClearBreakpoint( unsigned int Address )
-{
-  int Index = 0;
-  while ( Index < NbBreakpoints && Breakpoints[ Index ] != Address ) Index++;
-  if ( Breakpoints[ Index ] != Address ) return;
-  Breakpoints[ Index ] = Breakpoints[ NbBreakpoints - 1 ];
-  NbBreakpoints--;
-}
+       gtk_container_add(GTK_CONTAINER(frame), scrollwin);
 
-//////////////////////////////////////////////////////////////////////////////
-// void pgmwin_SetBreakpoint( unsigned int Address )
-// Set Breakpoint at Address from list
-//////////////////////////////////////////////////////////////////////////////
-void pgmwin_SetBreakpoint( unsigned int Address )
-{
-  if ( pgmwin_IsBreakpoint( Address ) ) return;
-  if ( NbBreakpoints < MAXBP ) Breakpoints[ NbBreakpoints++ ] = Address;
-}
+       /* Creating a model */
+       store = pgmwin_init_store();
 
-//////////////////////////////////////////////////////////////////////////////
-// int pgmwin_IsBreakpoint( unsigned int Address )
-// Is the a breakpoint at Address
-//////////////////////////////////////////////////////////////////////////////
-int pgmwin_IsBreakpoint( unsigned int Address )
-{
-  int Index = 0;
-  while ( Index < NbBreakpoints && Breakpoints[ Index ] != Address ) Index++;
-  return ( Breakpoints[ Index ] == Address && Index < NbBreakpoints );
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// void pgmwin_ToggleBreakpoint( unsigned int Address )
-// Toggle the breakpoint at Address
-//////////////////////////////////////////////////////////////////////////////
-void pgmwin_ToggleBreakpoint( unsigned int Address )
-{
-  if ( pgmwin_IsBreakpoint( Address ) ) pgmwin_ClearBreakpoint( Address );
-  else pgmwin_SetBreakpoint( Address );
-}
+       /* 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_refresh(void)
+{
+       int row;
+       GtkListStore *store;
+       unsigned int Address;
+
+       Address = cpu8051.pc;
+
+       store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(pgmlist)));
+
+       for (row = 0; row < DATA_ROWS; row++) {
+               int valid;
+               GtkTreeIter iter;
+               char str[128];
+               int k;
+               int col_id;
+               int InstSize;
+               unsigned char OpCode;
+
+               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;
+               }
+
+               if (Address > 0xFFFF) {
+                       /*
+                        * Not the most elegant solution, but it works to not
+                        * display instructions past last address, 0xFFFF.
+                        */
+                       gtk_list_store_set(store, &iter,
+                                          COL_BREAKPT, NULL,
+                                          COL_ADDR, NULL,
+                                          COL_B0, NULL,
+                                          COL_B1, NULL,
+                                          COL_B2, NULL,
+                                          COL_INST, NULL,
+                                          COL_ARGS, NULL,
+                                          COL_COLOR, NULL,
+                                          -1);
+               } else {
+                       /* Display breakpoints. */
+                       if (IsBreakpoint(Address))
+                               sprintf(str, "*");
+                       else
+                               str[0] = '\0';
+
+                       gtk_list_store_set(store, &iter, COL_BREAKPT, str, -1);
+
+                       /* Display base address. */
+                       int2asciihex(Address, str, 4);
+
+                       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 < 3; k++, col_id++) {
+                               if (k < InstSize)
+                                       int2asciihex(memory_read8(PGM_MEM_ID,
+                                                                 Address + k),
+                                                    str, 2);
+                               else
+                                       str[0] = '\0';
+
+                               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;
+               }
+       }
+}