2 * $Id: debug_win.c,v 1.21 2007-10-03 09:32:09 nkeynes Exp $
3 * This file is responsible for the main debugger gui frame.
5 * Copyright (c) 2005 Nathan Keynes.
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.
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.
22 #include "sh4/sh4dasm.h"
28 GdkColor *msg_colors[] = { &clrError, &clrError, &clrWarn, &clrNormal,
29 &clrDebug, &clrTrace };
30 char *msg_levels[] = { "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" };
31 int global_msg_level = EMIT_WARN;
33 void init_register_list( debug_info_t data );
35 struct debug_info_struct {
39 struct cpu_desc_struct *cpu;
40 const cpu_desc_t *cpu_list;
43 GtkCList *disasm_list;
46 GtkProgressBar *icounter;
47 char icounter_text[16];
51 debug_info_t init_debug_win(GtkWidget *win, const cpu_desc_t *cpu_list )
55 debug_info_t data = g_malloc0( sizeof(struct debug_info_struct) + cpu_list[0]->regs_size );
56 data->disasm_from = -1;
59 data->cpu = cpu_list[0];
60 data->cpu_list = cpu_list;
62 data->regs_list= gtk_object_get_data(GTK_OBJECT(win), "reg_list");
64 gtk_widget_modify_font( GTK_WIDGET(data->regs_list), fixed_list_font );
65 init_register_list( data );
66 data->msgs_list = gtk_object_get_data(GTK_OBJECT(win), "output_list");
67 data->disasm_list = gtk_object_get_data(GTK_OBJECT(win), "disasm_list");
68 gtk_clist_set_column_width( data->disasm_list, 1, 16 );
69 data->page_field = gtk_object_get_data(GTK_OBJECT(win), "page_field");
71 appbar = gtk_object_get_data(GTK_OBJECT(win), "debug_appbar");
72 data->icounter = gnome_appbar_get_progress( appbar );
73 gtk_progress_bar_set_text(data->icounter, "1");
75 gtk_object_set_data( GTK_OBJECT(win), "debug_data", data );
76 set_disassembly_pc( data, *data->cpu->pc, FALSE );
77 debug_win_set_running( data, FALSE );
81 void init_register_list( debug_info_t data )
87 gtk_clist_clear( data->regs_list );
89 for( i=0; data->cpu->regs_info[i].name != NULL; i++ ) {
90 arr[0] = data->cpu->regs_info[i].name;
91 if( data->cpu->regs_info->type == REG_INT )
92 sprintf( buf, "%08X", *((uint32_t *)data->cpu->regs_info[i].value) );
94 sprintf( buf, "%f", *((float *)data->cpu->regs_info[i].value) );
95 gtk_clist_append( data->regs_list, arr );
100 * Check for changed registers and update the display
102 void update_registers( debug_info_t data )
105 for( i=0; data->cpu->regs_info[i].name != NULL; i++ ) {
106 if( data->cpu->regs_info[i].type == REG_INT ) {
107 /* Yes this _is_ probably fairly evil */
108 if( *((uint32_t *)data->cpu->regs_info[i].value) !=
109 *((uint32_t *)((char *)data->saved_regs + ((char *)data->cpu->regs_info[i].value - (char *)data->cpu->regs))) ) {
111 sprintf( buf, "%08X", *((uint32_t *)data->cpu->regs_info[i].value) );
112 gtk_clist_set_text( data->regs_list, i, 1, buf );
113 gtk_clist_set_foreground( data->regs_list, i, &clrChanged );
115 gtk_clist_set_foreground( data->regs_list, i, &clrNormal );
118 if( *((float *)data->cpu->regs_info[i].value) !=
119 *((float *)((char *)data->saved_regs + ((char *)data->cpu->regs_info[i].value - (char *)data->cpu->regs))) ) {
121 sprintf( buf, "%f", *((float *)data->cpu->regs_info[i].value) );
122 gtk_clist_set_text( data->regs_list, i, 1, buf );
123 gtk_clist_set_foreground( data->regs_list, i, &clrChanged );
125 gtk_clist_set_foreground( data->regs_list, i, &clrNormal );
130 set_disassembly_pc( data, *data->cpu->pc, FALSE );
131 memcpy( data->saved_regs, data->cpu->regs, data->cpu->regs_size );
134 void update_icount( debug_info_t data )
137 // sprintf( data->icounter_text, "%d", *data->cpu->icount );
138 sprintf( data->icounter_text, "%d", pvr2_get_frame_count() );
139 gtk_progress_bar_set_text( data->icounter, data->icounter_text );
143 void set_disassembly_region( debug_info_t data, unsigned int page )
145 uint32_t i, posn, next;
148 char opcode[16] = "";
149 char *arr[4] = { addr, " ", opcode, buf };
150 unsigned int from = page & 0xFFFFF000;
151 unsigned int to = from + 4096;
153 gtk_clist_clear(data->disasm_list);
155 sprintf( addr, "%08X", from );
156 gtk_entry_set_text( data->page_field, addr );
158 if( !data->cpu->is_valid_page_func( from ) ) {
159 arr[3] = "This page is currently unmapped";
160 gtk_clist_append( data->disasm_list, arr );
161 gtk_clist_set_foreground( data->disasm_list, 0, &clrError );
163 for( i=from; i<to; i = next ) {
164 next = data->cpu->disasm_func( i, buf, sizeof(buf), opcode );
165 sprintf( addr, "%08X", i );
166 posn = gtk_clist_append( data->disasm_list, arr );
168 gtk_clist_set_foreground( data->disasm_list, posn, &clrWarn );
169 if( data->cpu->get_breakpoint != NULL ) {
170 int type = data->cpu->get_breakpoint( i );
173 gtk_clist_set_background( data->disasm_list, posn, &clrTempBreak );
176 gtk_clist_set_background( data->disasm_list, posn, &clrBreak );
181 if( data->disasm_pc != -1 && data->disasm_pc >= from && data->disasm_pc < to )
182 gtk_clist_set_foreground( data->disasm_list, address_to_row(data, data->disasm_pc),
186 if( page != from ) { /* not a page boundary */
187 gtk_clist_moveto( data->disasm_list, (page-from)>>1, 0, 0.5, 0.0 );
189 data->disasm_from = from;
190 data->disasm_to = to;
193 void jump_to_disassembly( debug_info_t data, unsigned int addr, gboolean select )
197 if( addr < data->disasm_from || addr >= data->disasm_to )
198 set_disassembly_region(data,addr);
200 row = address_to_row( data, addr );
202 gtk_clist_select_row( data->disasm_list, row, 0 );
204 if( gtk_clist_row_is_visible( data->disasm_list, row ) != GTK_VISIBILITY_FULL ){
205 gtk_clist_moveto( data->disasm_list, row, 0, 0.5, 0.0 );
209 void jump_to_pc( debug_info_t data, gboolean select )
211 jump_to_disassembly( data, *data->cpu->pc, select );
214 void set_disassembly_pc( debug_info_t data, unsigned int pc, gboolean select )
218 jump_to_disassembly( data, pc, select );
219 if( data->disasm_pc != -1 && data->disasm_pc >= data->disasm_from &&
220 data->disasm_pc < data->disasm_to )
221 gtk_clist_set_foreground( data->disasm_list,
222 (data->disasm_pc - data->disasm_from) / data->cpu->instr_size,
224 row = address_to_row( data, pc );
225 gtk_clist_set_foreground( data->disasm_list, row, &clrPC );
226 data->disasm_pc = pc;
229 void set_disassembly_cpu( debug_info_t data, const gchar *cpu )
232 for( i=0; data->cpu_list[i] != NULL; i++ ) {
233 if( strcmp( data->cpu_list[i]->name, cpu ) == 0 ) {
234 if( data->cpu != data->cpu_list[i] ) {
235 data->cpu = data->cpu_list[i];
236 data->disasm_from = data->disasm_to = -1; /* Force reload */
237 set_disassembly_pc( data, *data->cpu->pc, FALSE );
238 init_register_list( data );
239 update_icount( data );
246 void debug_win_toggle_breakpoint( debug_info_t data, int row )
248 uint32_t pc = row_to_address( data, row );
249 int oldType = data->cpu->get_breakpoint( pc );
250 if( oldType != BREAK_NONE ) {
251 data->cpu->clear_breakpoint( pc, oldType );
252 gtk_clist_set_background( data->disasm_list, row, &clrWhite );
254 data->cpu->set_breakpoint( pc, BREAK_KEEP );
255 gtk_clist_set_background( data->disasm_list, row, &clrBreak );
259 void debug_win_set_oneshot_breakpoint( debug_info_t data, int row )
261 uint32_t pc = row_to_address( data, row );
262 data->cpu->clear_breakpoint( pc, BREAK_ONESHOT );
263 data->cpu->set_breakpoint( pc, BREAK_ONESHOT );
264 gtk_clist_set_background( data->disasm_list, row, &clrTempBreak );
268 * Execute a single instruction using the current CPU mode.
270 void debug_win_single_step( debug_info_t data )
272 data->cpu->step_func();
276 uint32_t row_to_address( debug_info_t data, int row ) {
277 return data->cpu->instr_size * row + data->disasm_from;
280 int address_to_row( debug_info_t data, uint32_t address ) {
281 if( data->disasm_from > address || data->disasm_to <= address )
283 return (address - data->disasm_from) / data->cpu->instr_size;
287 void emit( void *ptr, int level, const gchar *source, const char *msg, ... )
289 char buf[20], addr[10] = "", *p;
290 const char *arr[4] = {buf, source, addr};
292 time_t tm = time(NULL);
297 else data = (debug_info_t)ptr;
299 if( level > global_msg_level ) {
304 strftime( buf, sizeof(buf), "%H:%M:%S", localtime(&tm) );
307 fprintf( stderr, "%s %08X %-5s ", buf, *sh4_cpu_desc.pc, msg_levels[level] );
308 vfprintf( stderr, msg, ap );
309 fprintf( stderr, "\n" );
314 p = g_strdup_vprintf( msg, ap );
315 sprintf( addr, "%08X", *data->cpu->pc );
317 posn = gtk_clist_append(data->msgs_list, arr);
321 gtk_clist_set_foreground( data->msgs_list, posn, msg_colors[level] );
322 gtk_clist_moveto( data->msgs_list, posn, 0, 1.0, 0.0 );
324 /* emit _really_ slows down the emu, to the point where the gui can be
325 * completely unresponsive if I don't include this:
327 while( gtk_events_pending() )
328 gtk_main_iteration();
331 debug_info_t get_debug_info( GtkWidget *widget ) {
333 GtkWidget *win = gtk_widget_get_toplevel(widget);
334 debug_info_t data = (debug_info_t)gtk_object_get_data( GTK_OBJECT(win), "debug_data" );
338 void debug_win_enable_widget( debug_info_t data, const char *name,
341 GtkWidget *widget = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(data->win), name));
342 gtk_widget_set_sensitive( widget, enabled );
345 void debug_win_set_running( debug_info_t data, gboolean isRunning )
348 debug_win_enable_widget( data, "stop_btn", isRunning );
349 debug_win_enable_widget( data, "step_btn", !isRunning );
350 debug_win_enable_widget( data, "run_btn", !isRunning );
351 debug_win_enable_widget( data, "runto_btn", !isRunning );
.