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 26:ad258e3daaa5
prev14:fc481a638848
next28:81c206f59dc7
author nkeynes
date Sun Dec 25 01:28:39 2005 +0000 (18 years ago)
permissions -rw-r--r--
last change Refactor all the GUI bits out of the main directory (except for a couple
lingering temporarily in loader.c
Fix a few timeslice issues
view annotate diff log raw
     1 /**
     2  * $Id: debug_win.c,v 1.7 2005-12-24 08:02:18 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     GtkProgressBar *icounter;
    39     char icounter_text[16];
    40     char saved_regs[0];
    41 };
    43 debug_info_t init_debug_win(GtkWidget *win, struct cpu_desc_struct **cpu_list )
    44 {
    45     GnomeAppBar *appbar;
    47     debug_info_t data = g_malloc0( sizeof(struct debug_info_struct) + cpu_list[0]->regs_size );
    48     data->disasm_from = -1;
    49     data->disasm_to = -1;
    50     data->disasm_pc = -1;
    51     data->cpu = cpu_list[0];
    52     data->cpu_list = cpu_list;
    54     data->regs_list= gtk_object_get_data(GTK_OBJECT(win), "reg_list");
    55     gtk_widget_modify_font( GTK_WIDGET(data->regs_list), fixed_list_font );
    56     init_register_list( data );
    57     data->msgs_list = gtk_object_get_data(GTK_OBJECT(win), "output_list");
    58     data->disasm_list = gtk_object_get_data(GTK_OBJECT(win), "disasm_list");
    59     gtk_clist_set_column_width( data->disasm_list, 1, 16 );
    60     data->page_field = gtk_object_get_data(GTK_OBJECT(win), "page_field");
    62     appbar = gtk_object_get_data(GTK_OBJECT(win), "debug_appbar");
    63     data->icounter = gnome_appbar_get_progress( appbar );
    64     gtk_progress_bar_set_text(data->icounter, "1");
    66     gtk_object_set_data( GTK_OBJECT(win), "debug_data", data );
    67     return data;
    68 }
    70 void init_register_list( debug_info_t data ) 
    71 {
    72     int i;
    73     char buf[20];
    74     char *arr[2];
    76     gtk_clist_clear( data->regs_list );
    77     arr[1] = buf;
    78     for( i=0; data->cpu->regs_info[i].name != NULL; i++ ) {
    79         arr[0] = data->cpu->regs_info[i].name;
    80         if( data->cpu->regs_info->type == REG_INT )
    81             sprintf( buf, "%08X", *((uint32_t *)data->cpu->regs_info[i].value) );
    82         else
    83             sprintf( buf, "%f", *((float *)data->cpu->regs_info[i].value) );
    84         gtk_clist_append( data->regs_list, arr );
    85     }
    86 }
    88 /*
    89  * Check for changed registers and update the display
    90  */
    91 void update_registers( debug_info_t data )
    92 {
    93     int i;
    94     for( i=0; data->cpu->regs_info[i].name != NULL; i++ ) {
    95         if( data->cpu->regs_info[i].type == REG_INT ) {
    96             /* Yes this _is_ probably fairly evil */
    97             if( *((uint32_t *)data->cpu->regs_info[i].value) !=
    98                 *((uint32_t *)((char *)data->saved_regs + ((char *)data->cpu->regs_info[i].value - (char *)data->cpu->regs))) ) {
    99                 char buf[20];
   100                 sprintf( buf, "%08X", *((uint32_t *)data->cpu->regs_info[i].value) );
   101                 gtk_clist_set_text( data->regs_list, i, 1, buf );
   102                 gtk_clist_set_foreground( data->regs_list, i, &clrChanged );
   103             } else {
   104                 gtk_clist_set_foreground( data->regs_list, i, &clrNormal );
   105             }
   106         } else {
   107             if( *((float *)data->cpu->regs_info[i].value) !=
   108                 *((float *)((char *)data->saved_regs + ((char *)data->cpu->regs_info[i].value - (char *)data->cpu->regs))) ) {
   109                 char buf[20];
   110                 sprintf( buf, "%f", *((float *)data->cpu->regs_info[i].value) );
   111                 gtk_clist_set_text( data->regs_list, i, 1, buf );
   112                 gtk_clist_set_foreground( data->regs_list, i, &clrChanged );
   113             } else {
   114                 gtk_clist_set_foreground( data->regs_list, i, &clrNormal );
   115             }
   116         }
   117     }
   119     set_disassembly_pc( data, *data->cpu->pc, FALSE );
   120     memcpy( data->saved_regs, data->cpu->regs, data->cpu->regs_size );
   121 }
   123 void update_icount( debug_info_t data )
   124 {
   125     sprintf( data->icounter_text, "%d", *data->cpu->icount );
   126     gtk_progress_bar_set_text( data->icounter, data->icounter_text );
   127 }
   129 void set_disassembly_region( debug_info_t data, unsigned int page )
   130 {
   131     uint32_t i, posn, next;
   132     uint16_t op;
   133     char buf[80];
   134     char addr[10];
   135     char opcode[16] = "";
   136     char *arr[4] = { addr, " ", opcode, buf };
   137     unsigned int from = page & 0xFFFFF000;
   138     unsigned int to = from + 4096;
   140     gtk_clist_clear(data->disasm_list);
   142     sprintf( addr, "%08X", from );
   143     gtk_entry_set_text( data->page_field, addr );
   145     if( !data->cpu->is_valid_page_func( from ) ) {
   146         arr[3] = "This page is currently unmapped";
   147         gtk_clist_append( data->disasm_list, arr );
   148         gtk_clist_set_foreground( data->disasm_list, 0, &clrError );
   149     } else {
   150         for( i=from; i<to; i = next ) {
   151 	    next = data->cpu->disasm_func( i, buf, sizeof(buf), opcode );
   152             sprintf( addr, "%08X", i );
   153             op = sh4_read_phys_word(i);
   154             posn = gtk_clist_append( data->disasm_list, arr );
   155             if( buf[0] == '?' )
   156                 gtk_clist_set_foreground( data->disasm_list, posn, &clrWarn );
   157         }
   158         if( data->disasm_pc != -1 && data->disasm_pc >= from && data->disasm_pc < to )
   159             gtk_clist_set_foreground( data->disasm_list, address_to_row(data, data->disasm_pc),
   160                                       &clrPC );
   161     }
   163     if( page != from ) { /* not a page boundary */
   164         gtk_clist_moveto( data->disasm_list, (page-from)>>1, 0, 0.5, 0.0 );
   165     }
   166     data->disasm_from = from;
   167     data->disasm_to = to;
   168 }
   170 void jump_to_disassembly( debug_info_t data, unsigned int addr, gboolean select )
   171 {
   172     int row;
   174     if( addr < data->disasm_from || addr >= data->disasm_to )
   175         set_disassembly_region(data,addr);
   177     row = address_to_row( data, addr );
   178     if(select) {
   179         gtk_clist_select_row( data->disasm_list, row, 0 );
   180     }
   181     if( gtk_clist_row_is_visible( data->disasm_list, row ) != GTK_VISIBILITY_FULL ){
   182         gtk_clist_moveto( data->disasm_list, row, 0, 0.5, 0.0 );
   183     }
   184 }
   186 void jump_to_pc( debug_info_t data, gboolean select )
   187 {
   188     jump_to_disassembly( data, *data->cpu->pc, select );
   189 }
   191 void set_disassembly_pc( debug_info_t data, unsigned int pc, gboolean select )
   192 {
   193     int row;
   195     jump_to_disassembly( data, pc, select );
   196     if( data->disasm_pc != -1 && data->disasm_pc >= data->disasm_from && 
   197 	data->disasm_pc < data->disasm_to )
   198         gtk_clist_set_foreground( data->disasm_list, 
   199 				  (data->disasm_pc - data->disasm_from) / data->cpu->instr_size,
   200                                   &clrNormal );
   201     row = address_to_row( data, pc );
   202     gtk_clist_set_foreground( data->disasm_list, row, &clrPC );
   203     data->disasm_pc = pc;
   204 }
   206 void set_disassembly_cpu( debug_info_t data, const gchar *cpu )
   207 {
   208     int i;
   209     for( i=0; data->cpu_list[i] != NULL; i++ ) {
   210 	if( strcmp( data->cpu_list[i]->name, cpu ) == 0 ) {
   211 	    if( data->cpu != data->cpu_list[i] ) {
   212 		data->cpu = data->cpu_list[i];
   213 		set_disassembly_region( data, data->disasm_from );
   214 		init_register_list( data );
   215 	    }
   216 	    return;
   217 	}
   218     }
   219 }
   221 uint32_t row_to_address( debug_info_t data, int row ) {
   222     return data->cpu->instr_size * row + data->disasm_from;
   223 }
   225 int address_to_row( debug_info_t data, uint32_t address ) {
   226     if( data->disasm_from > address || data->disasm_to <= address )
   227 	return -1;
   228     return (address - data->disasm_from) / data->cpu->instr_size;
   229 }
   232 void emit( void *ptr, int level, int source, const char *msg, ... )
   233 {
   234     char buf[20], addr[10] = "", *p;
   235     char *arr[3] = {buf, addr};
   236     int posn;
   237     time_t tm = time(NULL);
   238     va_list ap;
   239     debug_info_t data;
   240     if( ptr == NULL )
   241 	data = main_debug;
   242     else data = (debug_info_t)ptr;
   244     va_start(ap, msg);
   245     p = g_strdup_vprintf( msg, ap );
   246     strftime( buf, sizeof(buf), "%H:%M:%S", localtime(&tm) );
   247     if( source != -1 )
   248         sprintf( addr, "%08X", *data->cpu->pc );
   249     arr[2] = p;
   250     posn = gtk_clist_append(data->msgs_list, arr);
   251     free(p);
   252     va_end(ap);
   254     gtk_clist_set_foreground( data->msgs_list, posn, msg_colors[level] );
   255     gtk_clist_moveto( data->msgs_list, posn, 0, 1.0, 0.0 );
   257     /* emit _really_ slows down the emu, to the point where the gui can be
   258      * completely unresponsive if I don't include this:
   259      */
   260     while( gtk_events_pending() )
   261         gtk_main_iteration();
   262 }
   264 debug_info_t get_debug_info( GtkWidget *widget ) {
   266     GtkWidget *win = gtk_widget_get_toplevel(widget);
   267     debug_info_t data = (debug_info_t)gtk_object_get_data( GTK_OBJECT(win), "debug_data" );
   268     return data;
   269 }
.