Search
lxdream.org :: lxdream/src/gui/debug_win.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gui/debug_win.c
changeset 45:f99236f0632e
prev43:0cf3e339cc59
next69:2e8634272a98
author nkeynes
date Tue Dec 27 08:41:22 2005 +0000 (14 years ago)
permissions -rw-r--r--
last change Fix output list click
Fix mmr column widths
Fix run-to for ARM
view annotate diff log raw
     1 /**
     2  * $Id: debug_win.c,v 1.14 2005-12-27 08:41:22 nkeynes Exp $
     3  * This file is responsible for the main debugger gui frame.
     4  *
     5  * Copyright (c) 2005 Nathan Keynes.
     6  *
     7  * This program is free software; you can redistribute it and/or modify
     8  * it under the terms of the GNU General Public License as published by
     9  * the Free Software Foundation; either version 2 of the License, or
    10  * (at your option) any later version.
    11  *
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  */
    17 #include <stdlib.h>
    18 #include <stdarg.h>
    19 #include <gnome.h>
    20 #include <math.h>
    21 #include "gui/gui.h"
    22 #include "mem.h"
    23 #include "cpu.h"
    25 GdkColor *msg_colors[] = { &clrError, &clrError, &clrWarn, &clrNormal,
    26                            &clrDebug, &clrTrace };
    28 void init_register_list( debug_info_t data );
    30 struct debug_info_struct {
    31     int disasm_from;
    32     int disasm_to;
    33     int disasm_pc;
    34     struct cpu_desc_struct *cpu;
    35     struct cpu_desc_struct **cpu_list;
    36     GtkCList *msgs_list;
    37     GtkCList *regs_list;
    38     GtkCList *disasm_list;
    39     GtkEntry *page_field;
    40     GtkWidget *win;
    41     GtkProgressBar *icounter;
    42     char icounter_text[16];
    43     char saved_regs[0];
    44 };
    46 debug_info_t init_debug_win(GtkWidget *win, const cpu_desc_t *cpu_list )
    47 {
    48     GnomeAppBar *appbar;
    50     debug_info_t data = g_malloc0( sizeof(struct debug_info_struct) + cpu_list[0]->regs_size );
    51     data->disasm_from = -1;
    52     data->disasm_to = -1;
    53     data->disasm_pc = -1;
    54     data->cpu = cpu_list[0];
    55     data->cpu_list = cpu_list;
    57     data->regs_list= gtk_object_get_data(GTK_OBJECT(win), "reg_list");
    58     data->win = win;
    59     gtk_widget_modify_font( GTK_WIDGET(data->regs_list), fixed_list_font );
    60     init_register_list( data );
    61     data->msgs_list = gtk_object_get_data(GTK_OBJECT(win), "output_list");
    62     data->disasm_list = gtk_object_get_data(GTK_OBJECT(win), "disasm_list");
    63     gtk_clist_set_column_width( data->disasm_list, 1, 16 );
    64     data->page_field = gtk_object_get_data(GTK_OBJECT(win), "page_field");
    66     appbar = gtk_object_get_data(GTK_OBJECT(win), "debug_appbar");
    67     data->icounter = gnome_appbar_get_progress( appbar );
    68     gtk_progress_bar_set_text(data->icounter, "1");
    70     gtk_object_set_data( GTK_OBJECT(win), "debug_data", data );
    71     set_disassembly_pc( data, *data->cpu->pc, FALSE );
    72     debug_win_set_running( data, FALSE );
    73     return data;
    74 }
    76 void init_register_list( debug_info_t data ) 
    77 {
    78     int i;
    79     char buf[20];
    80     char *arr[2];
    82     gtk_clist_clear( data->regs_list );
    83     arr[1] = buf;
    84     for( i=0; data->cpu->regs_info[i].name != NULL; i++ ) {
    85         arr[0] = data->cpu->regs_info[i].name;
    86         if( data->cpu->regs_info->type == REG_INT )
    87             sprintf( buf, "%08X", *((uint32_t *)data->cpu->regs_info[i].value) );
    88         else
    89             sprintf( buf, "%f", *((float *)data->cpu->regs_info[i].value) );
    90         gtk_clist_append( data->regs_list, arr );
    91     }
    92 }
    94 /*
    95  * Check for changed registers and update the display
    96  */
    97 void update_registers( debug_info_t data )
    98 {
    99     int i;
   100     for( i=0; data->cpu->regs_info[i].name != NULL; i++ ) {
   101         if( data->cpu->regs_info[i].type == REG_INT ) {
   102             /* Yes this _is_ probably fairly evil */
   103             if( *((uint32_t *)data->cpu->regs_info[i].value) !=
   104                 *((uint32_t *)((char *)data->saved_regs + ((char *)data->cpu->regs_info[i].value - (char *)data->cpu->regs))) ) {
   105                 char buf[20];
   106                 sprintf( buf, "%08X", *((uint32_t *)data->cpu->regs_info[i].value) );
   107                 gtk_clist_set_text( data->regs_list, i, 1, buf );
   108                 gtk_clist_set_foreground( data->regs_list, i, &clrChanged );
   109             } else {
   110                 gtk_clist_set_foreground( data->regs_list, i, &clrNormal );
   111             }
   112         } else {
   113             if( *((float *)data->cpu->regs_info[i].value) !=
   114                 *((float *)((char *)data->saved_regs + ((char *)data->cpu->regs_info[i].value - (char *)data->cpu->regs))) ) {
   115                 char buf[20];
   116                 sprintf( buf, "%f", *((float *)data->cpu->regs_info[i].value) );
   117                 gtk_clist_set_text( data->regs_list, i, 1, buf );
   118                 gtk_clist_set_foreground( data->regs_list, i, &clrChanged );
   119             } else {
   120                 gtk_clist_set_foreground( data->regs_list, i, &clrNormal );
   121             }
   122         }
   123     }
   125     set_disassembly_pc( data, *data->cpu->pc, FALSE );
   126     memcpy( data->saved_regs, data->cpu->regs, data->cpu->regs_size );
   127 }
   129 void update_icount( debug_info_t data )
   130 {
   131     sprintf( data->icounter_text, "%d", *data->cpu->icount );
   132     gtk_progress_bar_set_text( data->icounter, data->icounter_text );
   133 }
   135 void set_disassembly_region( debug_info_t data, unsigned int page )
   136 {
   137     uint32_t i, posn, next;
   138     uint16_t op;
   139     char buf[80];
   140     char addr[10];
   141     char opcode[16] = "";
   142     char *arr[4] = { addr, " ", opcode, buf };
   143     unsigned int from = page & 0xFFFFF000;
   144     unsigned int to = from + 4096;
   146     gtk_clist_clear(data->disasm_list);
   148     sprintf( addr, "%08X", from );
   149     gtk_entry_set_text( data->page_field, addr );
   151     if( !data->cpu->is_valid_page_func( from ) ) {
   152         arr[3] = "This page is currently unmapped";
   153         gtk_clist_append( data->disasm_list, arr );
   154         gtk_clist_set_foreground( data->disasm_list, 0, &clrError );
   155     } else {
   156         for( i=from; i<to; i = next ) {
   157 	    next = data->cpu->disasm_func( i, buf, sizeof(buf), opcode );
   158             sprintf( addr, "%08X", i );
   159             op = sh4_read_phys_word(i);
   160             posn = gtk_clist_append( data->disasm_list, arr );
   161             if( buf[0] == '?' )
   162                 gtk_clist_set_foreground( data->disasm_list, posn, &clrWarn );
   163 	    if( data->cpu->get_breakpoint != NULL ) {
   164 		int type = data->cpu->get_breakpoint( i );
   165 		switch(type) {
   166 		case BREAK_ONESHOT:
   167 		    gtk_clist_set_background( data->disasm_list, posn, &clrTempBreak );
   168 		    break;
   169 		case BREAK_KEEP:
   170 		    gtk_clist_set_background( data->disasm_list, posn, &clrBreak );
   171 		    break;
   172 		}
   173 	    }
   174         }
   175         if( data->disasm_pc != -1 && data->disasm_pc >= from && data->disasm_pc < to )
   176             gtk_clist_set_foreground( data->disasm_list, address_to_row(data, data->disasm_pc),
   177                                       &clrPC );
   178     }
   180     if( page != from ) { /* not a page boundary */
   181         gtk_clist_moveto( data->disasm_list, (page-from)>>1, 0, 0.5, 0.0 );
   182     }
   183     data->disasm_from = from;
   184     data->disasm_to = to;
   185 }
   187 void jump_to_disassembly( debug_info_t data, unsigned int addr, gboolean select )
   188 {
   189     int row;
   191     if( addr < data->disasm_from || addr >= data->disasm_to )
   192         set_disassembly_region(data,addr);
   194     row = address_to_row( data, addr );
   195     if(select) {
   196         gtk_clist_select_row( data->disasm_list, row, 0 );
   197     }
   198     if( gtk_clist_row_is_visible( data->disasm_list, row ) != GTK_VISIBILITY_FULL ){
   199         gtk_clist_moveto( data->disasm_list, row, 0, 0.5, 0.0 );
   200     }
   201 }
   203 void jump_to_pc( debug_info_t data, gboolean select )
   204 {
   205     jump_to_disassembly( data, *data->cpu->pc, select );
   206 }
   208 void set_disassembly_pc( debug_info_t data, unsigned int pc, gboolean select )
   209 {
   210     int row;
   212     jump_to_disassembly( data, pc, select );
   213     if( data->disasm_pc != -1 && data->disasm_pc >= data->disasm_from && 
   214 	data->disasm_pc < data->disasm_to )
   215         gtk_clist_set_foreground( data->disasm_list, 
   216 				  (data->disasm_pc - data->disasm_from) / data->cpu->instr_size,
   217                                   &clrNormal );
   218     row = address_to_row( data, pc );
   219     gtk_clist_set_foreground( data->disasm_list, row, &clrPC );
   220     data->disasm_pc = pc;
   221 }
   223 void set_disassembly_cpu( debug_info_t data, const gchar *cpu )
   224 {
   225     int i;
   226     for( i=0; data->cpu_list[i] != NULL; i++ ) {
   227 	if( strcmp( data->cpu_list[i]->name, cpu ) == 0 ) {
   228 	    if( data->cpu != data->cpu_list[i] ) {
   229 		data->cpu = data->cpu_list[i];
   230 		set_disassembly_pc( data, *data->cpu->pc, FALSE );
   231 		init_register_list( data );
   232 		update_icount( data );
   233 	    }
   234 	    return;
   235 	}
   236     }
   237 }
   239 void debug_win_toggle_breakpoint( debug_info_t data, int row )
   240 {
   241     uint32_t pc = row_to_address( data, row );
   242     int oldType = data->cpu->get_breakpoint( pc );
   243     if( oldType != BREAK_NONE ) {
   244 	data->cpu->clear_breakpoint( pc, oldType );
   245 	gtk_clist_set_background( data->disasm_list, row, &clrWhite );
   246     } else {
   247 	data->cpu->set_breakpoint( pc, BREAK_KEEP );
   248 	gtk_clist_set_background( data->disasm_list, row, &clrBreak );
   249     }
   250 }
   252 void debug_win_set_oneshot_breakpoint( debug_info_t data, int row )
   253 {
   254     uint32_t pc = row_to_address( data, row );
   255     data->cpu->clear_breakpoint( pc, BREAK_ONESHOT );
   256     data->cpu->set_breakpoint( pc, BREAK_ONESHOT );
   257     gtk_clist_set_background( data->disasm_list, row, &clrTempBreak );
   258 }
   260 /**
   261  * Execute a single instruction using the current CPU mode.
   262  */
   263 void debug_win_single_step( debug_info_t data )
   264 {
   265     data->cpu->step_func();
   266     gtk_gui_update();
   267 }
   269 uint32_t row_to_address( debug_info_t data, int row ) {
   270     return data->cpu->instr_size * row + data->disasm_from;
   271 }
   273 int address_to_row( debug_info_t data, uint32_t address ) {
   274     if( data->disasm_from > address || data->disasm_to <= address )
   275 	return -1;
   276     return (address - data->disasm_from) / data->cpu->instr_size;
   277 }
   280 void emit( void *ptr, int level, const gchar *source, const char *msg, ... )
   281 {
   282     char buf[20], addr[10] = "", *p;
   283     const char *arr[4] = {buf, source, addr};
   284     int posn;
   285     time_t tm = time(NULL);
   286     va_list ap;
   287     debug_info_t data;
   288     if( ptr == NULL )
   289 	data = main_debug;
   290     else data = (debug_info_t)ptr;
   292     va_start(ap, msg);
   293     p = g_strdup_vprintf( msg, ap );
   294     strftime( buf, sizeof(buf), "%H:%M:%S", localtime(&tm) );
   295     sprintf( addr, "%08X", *data->cpu->pc );
   296     arr[3] = p;
   297     posn = gtk_clist_append(data->msgs_list, arr);
   298     free(p);
   299     va_end(ap);
   301     gtk_clist_set_foreground( data->msgs_list, posn, msg_colors[level] );
   302     gtk_clist_moveto( data->msgs_list, posn, 0, 1.0, 0.0 );
   304     /* emit _really_ slows down the emu, to the point where the gui can be
   305      * completely unresponsive if I don't include this:
   306      */
   307     while( gtk_events_pending() )
   308         gtk_main_iteration();
   309 }
   311 debug_info_t get_debug_info( GtkWidget *widget ) {
   313     GtkWidget *win = gtk_widget_get_toplevel(widget);
   314     debug_info_t data = (debug_info_t)gtk_object_get_data( GTK_OBJECT(win), "debug_data" );
   315     return data;
   316 }
   318 void debug_win_enable_widget( debug_info_t data, const char *name, 
   319 			      gboolean enabled )
   320 {
   321     GtkWidget *widget = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(data->win), name));
   322     gtk_widget_set_sensitive( widget, enabled );
   323 }    
   325 void debug_win_set_running( debug_info_t data, gboolean isRunning ) 
   326 {
   327     debug_win_enable_widget( data, "stop_btn", isRunning );
   328     debug_win_enable_widget( data, "step_btn", !isRunning );
   329     debug_win_enable_widget( data, "run_btn", !isRunning );
   330     debug_win_enable_widget( data, "runto_btn", !isRunning );
   331 }
.