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 28:81c206f59dc7
prev26:ad258e3daaa5
next29:c29373ff63ee
author nkeynes
date Sun Dec 25 03:35:08 2005 +0000 (16 years ago)
permissions -rw-r--r--
last change Enable/disable run+stop buttons depending on state
view annotate diff log raw
     1 /**
     2  * $Id: debug_win.c,v 1.8 2005-12-25 03:35:08 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.h"
    22 #include "mem.h"
    23 #include "disasm.h"
    25 GdkColor *msg_colors[] = { &clrError, &clrError, &clrWarn, &clrNormal,
    26                            &clrDebug, &clrTrace };
    28 struct debug_info_struct {
    29     int disasm_from;
    30     int disasm_to;
    31     int disasm_pc;
    32     struct cpu_desc_struct *cpu;
    33     struct cpu_desc_struct **cpu_list;
    34     GtkCList *msgs_list;
    35     GtkCList *regs_list;
    36     GtkCList *disasm_list;
    37     GtkEntry *page_field;
    38     GtkWidget *win;
    39     GtkProgressBar *icounter;
    40     char icounter_text[16];
    41     char saved_regs[0];
    42 };
    44 debug_info_t init_debug_win(GtkWidget *win, struct cpu_desc_struct **cpu_list )
    45 {
    46     GnomeAppBar *appbar;
    48     debug_info_t data = g_malloc0( sizeof(struct debug_info_struct) + cpu_list[0]->regs_size );
    49     data->disasm_from = -1;
    50     data->disasm_to = -1;
    51     data->disasm_pc = -1;
    52     data->cpu = cpu_list[0];
    53     data->cpu_list = cpu_list;
    55     data->regs_list= gtk_object_get_data(GTK_OBJECT(win), "reg_list");
    56     data->win = win;
    57     gtk_widget_modify_font( GTK_WIDGET(data->regs_list), fixed_list_font );
    58     init_register_list( data );
    59     data->msgs_list = gtk_object_get_data(GTK_OBJECT(win), "output_list");
    60     data->disasm_list = gtk_object_get_data(GTK_OBJECT(win), "disasm_list");
    61     gtk_clist_set_column_width( data->disasm_list, 1, 16 );
    62     data->page_field = gtk_object_get_data(GTK_OBJECT(win), "page_field");
    64     appbar = gtk_object_get_data(GTK_OBJECT(win), "debug_appbar");
    65     data->icounter = gnome_appbar_get_progress( appbar );
    66     gtk_progress_bar_set_text(data->icounter, "1");
    68     gtk_object_set_data( GTK_OBJECT(win), "debug_data", data );
    69     debug_win_set_running( data, FALSE );
    70     return data;
    71 }
    73 void init_register_list( debug_info_t data ) 
    74 {
    75     int i;
    76     char buf[20];
    77     char *arr[2];
    79     gtk_clist_clear( data->regs_list );
    80     arr[1] = buf;
    81     for( i=0; data->cpu->regs_info[i].name != NULL; i++ ) {
    82         arr[0] = data->cpu->regs_info[i].name;
    83         if( data->cpu->regs_info->type == REG_INT )
    84             sprintf( buf, "%08X", *((uint32_t *)data->cpu->regs_info[i].value) );
    85         else
    86             sprintf( buf, "%f", *((float *)data->cpu->regs_info[i].value) );
    87         gtk_clist_append( data->regs_list, arr );
    88     }
    89 }
    91 /*
    92  * Check for changed registers and update the display
    93  */
    94 void update_registers( debug_info_t data )
    95 {
    96     int i;
    97     for( i=0; data->cpu->regs_info[i].name != NULL; i++ ) {
    98         if( data->cpu->regs_info[i].type == REG_INT ) {
    99             /* Yes this _is_ probably fairly evil */
   100             if( *((uint32_t *)data->cpu->regs_info[i].value) !=
   101                 *((uint32_t *)((char *)data->saved_regs + ((char *)data->cpu->regs_info[i].value - (char *)data->cpu->regs))) ) {
   102                 char buf[20];
   103                 sprintf( buf, "%08X", *((uint32_t *)data->cpu->regs_info[i].value) );
   104                 gtk_clist_set_text( data->regs_list, i, 1, buf );
   105                 gtk_clist_set_foreground( data->regs_list, i, &clrChanged );
   106             } else {
   107                 gtk_clist_set_foreground( data->regs_list, i, &clrNormal );
   108             }
   109         } else {
   110             if( *((float *)data->cpu->regs_info[i].value) !=
   111                 *((float *)((char *)data->saved_regs + ((char *)data->cpu->regs_info[i].value - (char *)data->cpu->regs))) ) {
   112                 char buf[20];
   113                 sprintf( buf, "%f", *((float *)data->cpu->regs_info[i].value) );
   114                 gtk_clist_set_text( data->regs_list, i, 1, buf );
   115                 gtk_clist_set_foreground( data->regs_list, i, &clrChanged );
   116             } else {
   117                 gtk_clist_set_foreground( data->regs_list, i, &clrNormal );
   118             }
   119         }
   120     }
   122     set_disassembly_pc( data, *data->cpu->pc, FALSE );
   123     memcpy( data->saved_regs, data->cpu->regs, data->cpu->regs_size );
   124 }
   126 void update_icount( debug_info_t data )
   127 {
   128     sprintf( data->icounter_text, "%d", *data->cpu->icount );
   129     gtk_progress_bar_set_text( data->icounter, data->icounter_text );
   130 }
   132 void set_disassembly_region( debug_info_t data, unsigned int page )
   133 {
   134     uint32_t i, posn, next;
   135     uint16_t op;
   136     char buf[80];
   137     char addr[10];
   138     char opcode[16] = "";
   139     char *arr[4] = { addr, " ", opcode, buf };
   140     unsigned int from = page & 0xFFFFF000;
   141     unsigned int to = from + 4096;
   143     gtk_clist_clear(data->disasm_list);
   145     sprintf( addr, "%08X", from );
   146     gtk_entry_set_text( data->page_field, addr );
   148     if( !data->cpu->is_valid_page_func( from ) ) {
   149         arr[3] = "This page is currently unmapped";
   150         gtk_clist_append( data->disasm_list, arr );
   151         gtk_clist_set_foreground( data->disasm_list, 0, &clrError );
   152     } else {
   153         for( i=from; i<to; i = next ) {
   154 	    next = data->cpu->disasm_func( i, buf, sizeof(buf), opcode );
   155             sprintf( addr, "%08X", i );
   156             op = sh4_read_phys_word(i);
   157             posn = gtk_clist_append( data->disasm_list, arr );
   158             if( buf[0] == '?' )
   159                 gtk_clist_set_foreground( data->disasm_list, posn, &clrWarn );
   160         }
   161         if( data->disasm_pc != -1 && data->disasm_pc >= from && data->disasm_pc < to )
   162             gtk_clist_set_foreground( data->disasm_list, address_to_row(data, data->disasm_pc),
   163                                       &clrPC );
   164     }
   166     if( page != from ) { /* not a page boundary */
   167         gtk_clist_moveto( data->disasm_list, (page-from)>>1, 0, 0.5, 0.0 );
   168     }
   169     data->disasm_from = from;
   170     data->disasm_to = to;
   171 }
   173 void jump_to_disassembly( debug_info_t data, unsigned int addr, gboolean select )
   174 {
   175     int row;
   177     if( addr < data->disasm_from || addr >= data->disasm_to )
   178         set_disassembly_region(data,addr);
   180     row = address_to_row( data, addr );
   181     if(select) {
   182         gtk_clist_select_row( data->disasm_list, row, 0 );
   183     }
   184     if( gtk_clist_row_is_visible( data->disasm_list, row ) != GTK_VISIBILITY_FULL ){
   185         gtk_clist_moveto( data->disasm_list, row, 0, 0.5, 0.0 );
   186     }
   187 }
   189 void jump_to_pc( debug_info_t data, gboolean select )
   190 {
   191     jump_to_disassembly( data, *data->cpu->pc, select );
   192 }
   194 void set_disassembly_pc( debug_info_t data, unsigned int pc, gboolean select )
   195 {
   196     int row;
   198     jump_to_disassembly( data, pc, select );
   199     if( data->disasm_pc != -1 && data->disasm_pc >= data->disasm_from && 
   200 	data->disasm_pc < data->disasm_to )
   201         gtk_clist_set_foreground( data->disasm_list, 
   202 				  (data->disasm_pc - data->disasm_from) / data->cpu->instr_size,
   203                                   &clrNormal );
   204     row = address_to_row( data, pc );
   205     gtk_clist_set_foreground( data->disasm_list, row, &clrPC );
   206     data->disasm_pc = pc;
   207 }
   209 void set_disassembly_cpu( debug_info_t data, const gchar *cpu )
   210 {
   211     int i;
   212     for( i=0; data->cpu_list[i] != NULL; i++ ) {
   213 	if( strcmp( data->cpu_list[i]->name, cpu ) == 0 ) {
   214 	    if( data->cpu != data->cpu_list[i] ) {
   215 		data->cpu = data->cpu_list[i];
   216 		set_disassembly_region( data, data->disasm_from );
   217 		init_register_list( data );
   218 	    }
   219 	    return;
   220 	}
   221     }
   222 }
   224 uint32_t row_to_address( debug_info_t data, int row ) {
   225     return data->cpu->instr_size * row + data->disasm_from;
   226 }
   228 int address_to_row( debug_info_t data, uint32_t address ) {
   229     if( data->disasm_from > address || data->disasm_to <= address )
   230 	return -1;
   231     return (address - data->disasm_from) / data->cpu->instr_size;
   232 }
   235 void emit( void *ptr, int level, int source, const char *msg, ... )
   236 {
   237     char buf[20], addr[10] = "", *p;
   238     char *arr[3] = {buf, addr};
   239     int posn;
   240     time_t tm = time(NULL);
   241     va_list ap;
   242     debug_info_t data;
   243     if( ptr == NULL )
   244 	data = main_debug;
   245     else data = (debug_info_t)ptr;
   247     va_start(ap, msg);
   248     p = g_strdup_vprintf( msg, ap );
   249     strftime( buf, sizeof(buf), "%H:%M:%S", localtime(&tm) );
   250     if( source != -1 )
   251         sprintf( addr, "%08X", *data->cpu->pc );
   252     arr[2] = p;
   253     posn = gtk_clist_append(data->msgs_list, arr);
   254     free(p);
   255     va_end(ap);
   257     gtk_clist_set_foreground( data->msgs_list, posn, msg_colors[level] );
   258     gtk_clist_moveto( data->msgs_list, posn, 0, 1.0, 0.0 );
   260     /* emit _really_ slows down the emu, to the point where the gui can be
   261      * completely unresponsive if I don't include this:
   262      */
   263     while( gtk_events_pending() )
   264         gtk_main_iteration();
   265 }
   267 debug_info_t get_debug_info( GtkWidget *widget ) {
   269     GtkWidget *win = gtk_widget_get_toplevel(widget);
   270     debug_info_t data = (debug_info_t)gtk_object_get_data( GTK_OBJECT(win), "debug_data" );
   271     return data;
   272 }
   274 void debug_win_enable_widget( debug_info_t data, const char *name, 
   275 			      gboolean enabled )
   276 {
   277     GtkWidget *widget = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(data->win), name));
   278     gtk_widget_set_sensitive( widget, enabled );
   279 }    
   281 void debug_win_set_running( debug_info_t data, gboolean isRunning ) 
   282 {
   283     debug_win_enable_widget( data, "stop_btn", isRunning );
   284     debug_win_enable_widget( data, "step_btn", !isRunning );
   285     debug_win_enable_widget( data, "run_btn", !isRunning );
   286     debug_win_enable_widget( data, "runto_btn", !isRunning );
   287 }
.