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