summaryrefslogtreecommitdiff
path: root/gui/hdebug.c
blob: 996ba2ddcb350ca8fe9cc27be6808898e2f957fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/***************************************************************************
 *   Debugger-Interface for PCSX-DF                                        *
 *                                                                         *
 *   Copyright (C) 2008 Stefan Sikora                                      *
 *   hoshy['at']schrauberstube['dot']de                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA 02111-1307 USA.            *
 ***************************************************************************/

// TODO:
// - setting register values
// - step over instruction
// - Dumping/Loading of memory
// - Better gui-integration 

#include <gtk/gtk.h>
#include <string.h>
#include <pthread.h>
#include "r3000a.h"
#include "hdebug.h"

// Global variables
char buffer[1024*2];
GtkTextBuffer *hdb_listing = 0;
GtkTextBuffer *hdb_memdump = 0;
GtkTextBuffer *hdb_logging = 0;
GtkTextBuffer *hdb_registers = 0;
GtkWidget *hdb_command = 0;
GtkWidget *hdb_pausebutton = 0;

u32 hdb_memptr = 0x80000000;

int ready = 0;

// little helper for syncing with emulator
void waitforpause() {
	int actualPC;
	
	actualPC = psxRegs.pc;
	while(actualPC != psxRegs.pc) actualPC = psxRegs.pc;

	gtk_button_set_label(GTK_BUTTON(hdb_pausebutton), "RESUME");
}


// core-debugger-functions
void hdb_update_registers() {
	snprintf(buffer, 1024,
					 "v0 0x%08x   s0 0x%08x   t0 0x%08x\n" \
					 "v1 0x%08x   s1 0x%08x   t1 0x%08x\n" \
					 "                s2 0x%08x   t2 0x%08x\n" \
					 "a0 0x%08x   s3 0x%08x   t3 0x%08x\n" \
					 "a1 0x%08x   s4 0x%08x   t4 0x%08x\n" \
					 "a2 0x%08x   s5 0x%08x   t5 0x%08x\n" \
					 "a3 0x%08x   s6 0x%08x   t6 0x%08x\n" \
					 "                s7 0x%08x   t7 0x%08x\n" \
					 "k0 0x%08x   s8 0x%08x   t8 0x%08x\n" \
					 "k1 0x%08x                   t9 0x%08x\n\n" \
					 "gp 0x%08x   at 0x%08x   ra 0x%08x\n" \
					 "sp 0x%08x                   pc 0x%08x\n",
					 psxRegs.GPR.r[2], psxRegs.GPR.r[16], psxRegs.GPR.r[8],
					 psxRegs.GPR.r[3], psxRegs.GPR.r[17], psxRegs.GPR.r[9],
					 psxRegs.GPR.r[18], psxRegs.GPR.r[10],
					 psxRegs.GPR.r[4], psxRegs.GPR.r[19], psxRegs.GPR.r[11],
					 psxRegs.GPR.r[5], psxRegs.GPR.r[20], psxRegs.GPR.r[12],
					 psxRegs.GPR.r[6], psxRegs.GPR.r[21], psxRegs.GPR.r[13],
					 psxRegs.GPR.r[7], psxRegs.GPR.r[22], psxRegs.GPR.r[14],
					 psxRegs.GPR.r[23], psxRegs.GPR.r[15],
					 psxRegs.GPR.r[26], psxRegs.GPR.r[30], psxRegs.GPR.r[24],
					 psxRegs.GPR.r[27], psxRegs.GPR.r[25],
					 psxRegs.GPR.r[28], psxRegs.GPR.r[1], psxRegs.GPR.r[31],
					 psxRegs.GPR.r[29], psxRegs.pc);

	gtk_text_buffer_set_text(hdb_registers, buffer, strlen(buffer));
}


void hdb_update_listing(u32 opc) {
	int t;
	u32 *cptr;
	u32 ocod;
	char tbuf[100];
	char *bptr;
	
	buffer[0] = '\0';
	bptr = (char *)&buffer;	
	opc -= 15*4;

	for(t=0; t<=30; t++) {
		cptr = (u32 *)PSXM(opc);
		if (t == 15) strcat(bptr, ">"); else strcat(bptr, " ");
		ocod = cptr == NULL ? 0 : SWAP32(*cptr);
		sprintf(tbuf, "%s\n", disR3000AF(ocod, opc));
		strcat(bptr, tbuf);
		opc+=4;
	}
	strcat(bptr,"\0");
	gtk_text_buffer_set_text(hdb_listing, buffer, strlen(buffer));
}


void hdb_update_memdump() {
	int r, c;
	char tbuf[100];
	char *bptr;
	
	buffer[0] = '\0';
	bptr = (char*)&buffer;

	for(r=0; r<8; r++) {
		sprintf(tbuf, "0x%08x", hdb_memptr+r*16);
		strcat(bptr, tbuf);
		for(c=0; c<16; c++) {
			if(c==8) sprintf(tbuf, "-%02x", PSXMu8(hdb_memptr+r*16+c));
			else sprintf(tbuf, " %02x", PSXMu8(hdb_memptr+r*16+c));
			strcat(bptr, tbuf);
		}
		strcat(bptr, "\n");
	}
	strcat(bptr, "\0");
	gtk_text_buffer_set_text(hdb_memdump, buffer, strlen(buffer));
}


// Callback-functions
void on_hdb_mainwindow_destroy(GtkWidget *widget, gpointer data) {
	hdb_pause = 0;		// Run CPU
	gtk_widget_destroy((GtkWidget*)widget);
    gtk_main_quit();
    while (gtk_events_pending()) gtk_main_iteration();
	pthread_exit(NULL);
    ready = 0;
}


void on_hdb_pausebutton_clicked(GtkWidget *widget, gpointer data) {
	if(hdb_pause == 0) {
		hdb_pause = 1;
		waitforpause();

		hdb_update_registers();
		hdb_update_listing(psxRegs.pc);
		hdb_update_memdump();
	}
	else {
		hdb_pause = 0;
		gtk_button_set_label(GTK_BUTTON(widget), "PAUSE");
	}
}

void hdb_auto_pause () {
	if(hdb_pause == 0) {
		hdb_pause = 1;
		//waitforpause();

		hdb_update_registers();
		hdb_update_listing(psxRegs.pc);
		hdb_update_memdump();
	}
	else {
		hdb_pause = 0;
		//gtk_button_set_label(GTK_BUTTON(widget), "PAUSE");
	}
}

void on_hdb_tracebutton_clicked(GtkWidget *widget, gpointer data) {
	// Let emulator do one step and refresh debugger when in pause-mode!
	if(hdb_pause == 1) {
		hdb_pause = 2;

		waitforpause();
		hdb_update_registers();
		hdb_update_listing(psxRegs.pc);
	}
}


void on_hdb_dumpbutton_clicked(GtkWidget *widget, gpointer data) {
	// Save psx-memory for external analysis/modification
	// TODO
}


void on_hdb_loadbutton_clicked(GtkWidget *widget, gpointer data) {
	// Load psx-memory
	// TODO
}


void on_hdb_cmdbutton_clicked(GtkWidget *widget, gpointer data) {
	const gchar *cmdentry;
	char *actcmd;
	char *cmdsub;
	u32 tval;
	u8	tval8;
	int t;

	if ((cmdentry = gtk_entry_get_text(GTK_ENTRY(hdb_command))) != NULL) {
		actcmd = strdup(cmdentry);
		/* split and interpret debugger's commandline
			t - trace one instruction
			c - continue execution
			d addr - disassemble at addr
			m addr - show memory at addr (hex)
			s addr op1 op2 op3 ... - set memory
			b addr - break on addr
			bc - clear breakpoint
			r reg val - set register to a specific value (TODO)
		*/

		// get the command
		if ((cmdsub = strtok(actcmd, " ")) != NULL) {
			if(!strcmp(cmdsub, "t")) {
				hdb_pause = 2;
				usleep(100);			// wait 1/10th of a second
				hdb_update_registers();
				hdb_update_listing(psxRegs.pc);
			}

			if(!strcmp(cmdsub, "c")) hdb_pause = 0;	

			if(!strcmp(cmdsub, "bc")) {
				hdb_break = 0;
			}

			if(!strcmp(cmdsub, "b")) {
				cmdsub = strtok(NULL, " ");
				sscanf(cmdsub, "%x", (u32*)&tval);
				hdb_break = tval;
				hdb_pause = 3;

				waitforpause();
				hdb_update_memdump();
				hdb_update_registers();
				hdb_update_listing(psxRegs.pc);
			}

			if(!strcmp(cmdsub, "d")) {
				cmdsub = strtok(NULL, " ");
				sscanf(cmdsub, "%x", (u32*)&tval);
				hdb_update_listing(tval);
			}

			if(!strcmp(cmdsub, "m")) {
				cmdsub = strtok(NULL, " ");
				sscanf(cmdsub, "%x", (u32*)&tval);
				hdb_memptr = tval;
				hdb_update_memdump();
			}

			if(!strcmp(cmdsub, "s")) {
				cmdsub = strtok(NULL, " ");
				sscanf(cmdsub, "%x", (u32 *)&tval);
				t = 0;
				while((cmdsub = strtok(NULL, " "))) {
					u8 v;
					sscanf(cmdsub, "%x", (u8 *)&tval8);
					PSXMu8(tval+t) = tval8;
					t++;
				}
				hdb_update_memdump();
				hdb_update_listing(psxRegs.pc);
			}

			free(actcmd);
		}
		else {
			hdb_update_memdump();
			hdb_update_registers();
			hdb_update_listing(psxRegs.pc);
		}
	}
}


// init debugger and gui
void hdb_init() {
        if (ready) return;
	GtkWidget *hdb_mainwindow;
	GtkWidget *hdb_tracebutton;
	GtkWidget *hdb_dumpbutton;
	GtkWidget *hdb_loadbutton;
	GtkWidget *hdb_cmdbutton;
	GtkWidget *hdb_listbox, *hdb_membox, *hdb_logbox, *hdb_regbox;
	GtkWidget *hbox, *vbox, *hbox2, *hbox3;
	PangoFontDescription *font_desc;
		
	hdb_pause = 0;		// Run CPU
	
	gtk_init(NULL, NULL);
	font_desc = pango_font_description_from_string ("Fixed 8");
	
    // create and show the widgets
    hdb_mainwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title((GtkWindow*)hdb_mainwindow, "PCSX-DF Debugger");
    gtk_window_set_position((GtkWindow*)hdb_mainwindow, GTK_WIN_POS_CENTER);
    
	hbox = gtk_hbox_new(FALSE, 2);
	vbox = gtk_vbox_new(FALSE, 2);
	hbox2 = gtk_hbox_new(FALSE, 2);
	hbox3 = gtk_hbox_new(FALSE, 2);
	
	hdb_registers = gtk_text_buffer_new(NULL);
	hdb_regbox = gtk_text_view_new_with_buffer(hdb_registers);
	gtk_widget_modify_font(hdb_regbox, font_desc);

	hdb_listing = gtk_text_buffer_new(NULL);
	hdb_listbox = gtk_text_view_new_with_buffer(hdb_listing);
	gtk_widget_modify_font(hdb_listbox, font_desc);
	
	hdb_memdump = gtk_text_buffer_new(NULL);
	hdb_membox = gtk_text_view_new_with_buffer(hdb_memdump);
	gtk_widget_modify_font(hdb_membox, font_desc);

	hdb_logging = gtk_text_buffer_new(NULL);
	hdb_logbox = gtk_text_view_new_with_buffer(hdb_logging);
	gtk_widget_modify_font(hdb_logbox, font_desc);

	hdb_command = gtk_entry_new();
	hdb_cmdbutton = gtk_button_new_with_label(">");

	hdb_pausebutton = gtk_button_new_with_label("PAUSE");
	hdb_tracebutton = gtk_button_new_with_label("TRACE");
	hdb_dumpbutton = gtk_button_new_with_label("DUMP");
	hdb_loadbutton = gtk_button_new_with_label("LOAD");

	gtk_container_add(GTK_CONTAINER(hdb_mainwindow), hbox);
	gtk_container_add(GTK_CONTAINER(hbox), vbox);
	gtk_container_add(GTK_CONTAINER(hbox), hdb_listbox);

	gtk_container_add(GTK_CONTAINER(vbox), hdb_regbox);
	gtk_container_add(GTK_CONTAINER(vbox), hdb_membox);
	gtk_container_add(GTK_CONTAINER(vbox), hdb_logbox);

	gtk_container_add(GTK_CONTAINER(hbox3), hdb_command);
	gtk_container_add(GTK_CONTAINER(hbox3), hdb_cmdbutton);

	gtk_container_add(GTK_CONTAINER(vbox), hbox3);
	gtk_container_add(GTK_CONTAINER(vbox), hbox2);
	gtk_container_add(GTK_CONTAINER(hbox2), hdb_pausebutton);
	gtk_container_add(GTK_CONTAINER(hbox2), hdb_tracebutton);
	gtk_container_add(GTK_CONTAINER(hbox2), hdb_dumpbutton);
	gtk_container_add(GTK_CONTAINER(hbox2), hdb_loadbutton);

	pango_font_description_free (font_desc);
  	
    gtk_widget_show_all(hdb_mainwindow);

    // connect signals to callback functions
    g_signal_connect(G_OBJECT(hdb_mainwindow), "destroy", G_CALLBACK(on_hdb_mainwindow_destroy), NULL);
    g_signal_connect(G_OBJECT(hdb_pausebutton), "clicked", G_CALLBACK(on_hdb_pausebutton_clicked), NULL);
    g_signal_connect(G_OBJECT(hdb_tracebutton), "clicked", G_CALLBACK(on_hdb_tracebutton_clicked), NULL);
    g_signal_connect(G_OBJECT(hdb_dumpbutton), "clicked", G_CALLBACK(on_hdb_dumpbutton_clicked), NULL);
    g_signal_connect(G_OBJECT(hdb_loadbutton), "clicked", G_CALLBACK(on_hdb_loadbutton_clicked), NULL);
    g_signal_connect(G_OBJECT(hdb_cmdbutton), "clicked", G_CALLBACK(on_hdb_cmdbutton_clicked), NULL);

	hdb_update_registers();
	hdb_update_listing(psxRegs.pc);
	hdb_update_memdump();

	ready = 1;
	gtk_main();
	pthread_exit(NULL);
}


// Start debugger in own thread
void hdb_start() {
	pthread_t tid;

	pthread_create(&tid, NULL, (void *)hdb_init, NULL);
//	hdb_init();
}