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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
|
/* debugger.c: The debugger widget
Copyright (c) 2002-2004 Philip Kendall, Darren Salt
$Id: debugger.c 4103 2009-11-21 10:16:36Z fredm $
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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <libspectrum.h>
#include "debugger/debugger.h"
#include "display.h"
#include "ide/zxcf.h"
#include "keyboard.h"
#include "machine.h"
#include "scld.h"
#include "ui/uidisplay.h"
#include "ula.h"
#include "widget.h"
#include "widget_internals.h"
#include "z80/z80.h"
#include "z80/z80_macros.h"
static enum {
DB_REGISTERS, DB_BYTES, DB_TEXT, DB_DISASM, DB_BREAKPT
} display = DB_REGISTERS;
struct {
libspectrum_word value;
size_t page;
char x, width, column;
} db_editv = {
0, 0, 255, 0
};
static libspectrum_word debugger_memaddr;
static int breakpt_no = 0, breakpt_show = 0;
/* Various data displays */
static void display_registers( void );
static void display_bytes( void );
static void display_text( void );
static void display_disasm( void );
static void display_breakpts( void );
/* Scrolling for the data displays */
static void scroll( int step );
#define LC(X) ( (X)*8 - DISPLAY_BORDER_ASPECT_WIDTH )
#define LR(Y) ( (Y)*8 - DISPLAY_BORDER_HEIGHT )
static inline const char *format_8_bit( void )
{
return debugger_output_base == 10 ? "%-3d" : "%02X";
}
static inline const char *format_16_bit( void )
{
return debugger_output_base == 10 ? "%-5d" : "%04X";
}
int ui_debugger_activate( void )
{
return widget_do( WIDGET_TYPE_DEBUGGER, NULL );
}
int ui_debugger_deactivate( int interruptible GCC_UNUSED )
{
/* Refresh the Spectrum's display, including the border */
display_refresh_all();
return widget_end_all( WIDGET_FINISHED_OK );
}
int ui_debugger_update( void )
{
return widget_debugger_draw( NULL );
}
int ui_debugger_disassemble( libspectrum_word addr )
{
debugger_memaddr = addr;
return 0;
}
int widget_debugger_draw( void *data )
{
static const char state[][8] = {
"Running", "Halted", "Stepped", "Breakpt"
};
int x;
char pbuf[8];
widget_rectangle( LC(0), LR(0), 40 * 8, 11 * 8 + 4, 1 );
widget_rectangle( LC(0), LR(11) + 2, 320, 1, 7 );
switch ( display ) {
case DB_REGISTERS: display_registers(); break;
case DB_BYTES: display_bytes(); break;
case DB_TEXT: display_text(); break;
case DB_DISASM: display_disasm(); break;
case DB_BREAKPT: display_breakpts(); break;
}
widget_printstring( LC(0), LR(9) - 4, 6, state[debugger_mode] );
widget_printstring( LC(10), LR(9) - 4, 6,
"\022S\021ingle step \022C\021ontinue Co\022m\021mand" );
x = LC(-1);
if( display != DB_REGISTERS )
x = widget_printstring( x + 8, LR(10), 7, "\022R\021egs" );
if( display != DB_BYTES )
x = widget_printstring( x + 8, LR(10), 7, "\022B\021ytes" );
if( display != DB_TEXT )
x = widget_printstring( x + 8, LR(10), 7, "\022T\021ext" );
if( display != DB_DISASM )
x = widget_printstring( x + 8, LR(10), 7, "\022D\021isasm" );
if( display != DB_BREAKPT )
x = widget_printstring( x + 8, LR(10), 7, "Brea\022k\021pts" );
widget_printstring_right( LC(25) + 4, LR(10), 5, "PC" );
sprintf( pbuf, "%04X", PC );
widget_printstring_fixed( LC(26) / 8, LR(10) / 8, 7, pbuf );
widget_printstring_right( LR(35) + 4, LR(10), 5, "Bas\022e\021" );
sprintf( pbuf, "%d", debugger_output_base );
widget_printstring( LR(36), LR(10), 7, pbuf );
widget_display_lines( LR(0) / 8, 12 );
return 0;
}
void widget_debugger_keyhandler( input_key key )
{
/* Display mode */
switch ( key ) {
case INPUT_KEY_Escape: /* Close widget */
widget_end_widget( WIDGET_FINISHED_CANCEL );
debugger_run();
break;
case INPUT_KEY_c:
case INPUT_KEY_Return: /* Close widget */
case INPUT_KEY_KP_Enter:
widget_end_all( WIDGET_FINISHED_OK );
debugger_run();
break;
case INPUT_KEY_s: /* Single step & reopen widget */
debugger_mode = DEBUGGER_MODE_HALTED;
widget_end_all( WIDGET_FINISHED_OK );
break;
case INPUT_KEY_r: /* Display the registers */
display = DB_REGISTERS;
widget_debugger_draw( NULL );
break;
case INPUT_KEY_b: /* Display a memory dump (bytes) */
display = DB_BYTES;
widget_debugger_draw( NULL );
break;
case INPUT_KEY_t: /* Display a memory dump (text) */
display = DB_TEXT;
widget_debugger_draw( NULL );
break;
case INPUT_KEY_d: /* Display a disassembly */
display = DB_DISASM;
widget_debugger_draw( NULL );
break;
case INPUT_KEY_k: /* Display the breakpoints */
display = DB_BREAKPT;
widget_debugger_draw( NULL );
break;
case INPUT_KEY_e: /* Switch base */
debugger_output_base = 26 - debugger_output_base; /* 10 or 16 */
widget_debugger_draw( NULL );
break;
case INPUT_KEY_m: /* Enter a command */
{
widget_text_t text_data;
text_data.title = "Debugger command";
text_data.allow = WIDGET_INPUT_ASCII;
text_data.text[0] = 0;
if( !widget_do( WIDGET_TYPE_TEXT, &text_data ) )
debugger_command_evaluate( widget_text_text );
}
break;
case INPUT_KEY_Up: /* Back one line */
scroll( -1 );
break;
case INPUT_KEY_Down: /* Back one instruction or four lines */
scroll( 1 );
break;
case INPUT_KEY_Page_Up: /* Back eight lines */
scroll( -8 );
break;
case INPUT_KEY_Page_Down: /* Forward eight lines */
scroll( 8 );
break;
case INPUT_KEY_Home: /* To start of memory */
debugger_memaddr = 0;
scroll( 0 );
break;
case INPUT_KEY_End: /* To end of RAM */
debugger_memaddr = 0;
scroll( -8 );
break;
default:;
}
}
static void show_register0( int x, int y, const char *label, int value )
{
char pbuf[8];
sprintf( pbuf, "%d", value );
widget_printstring_right( x - 4, y, 5, label );
widget_printstring_fixed( x / 8, y / 8, 7, pbuf );
}
static void show_register1( int x, int y, const char *label, int value )
{
char pbuf[8];
sprintf( pbuf, format_8_bit(), value );
widget_printstring_right( x - 4, y, 5, label );
widget_printstring_fixed( x / 8, y / 8, 7, pbuf );
}
static void show_register2( int x, int y, const char *label, int value )
{
char pbuf[8];
sprintf( pbuf, format_16_bit(), value );
widget_printstring_right( x - 4, y, 5, label );
widget_printstring_fixed( x / 8, y / 8, 7, pbuf );
}
static void display_registers( void )
{
char pbuf[16];
int i, capabilities;
show_register2( LC(3), LR(0), "AF", AF );
show_register2( LC(12), LR(0), "AF'", AF_ );
show_register2( LC(20), LR(0), "SP", SP );
show_register2( LC(29), LR(0), "PC", PC );
show_register1( LC(36), LR(0), "R", ( R & 0x7F ) | ( R7 & 0x80 ) );
show_register2( LC(3), LR(1), "BC", BC );
show_register2( LC(12), LR(1), "BC'", BC_ );
show_register2( LC(20), LR(1), "IX", IX );
show_register2( LC(29), LR(1), "IY", IY );
show_register1( LC(36), LR(1), "I", I );
show_register2( LC(3), LR(2), "DE", DE );
show_register2( LC(12), LR(2), "DE'", DE_ );
show_register0( LC(20), LR(2), "IM", IM );
show_register0( LC(29), LR(2), "IFF1", IFF1 );
show_register0( LC(36), LR(2), "IFF2", IFF2 );
show_register2( LC(3), LR(3), "HL", HL );
show_register2( LC(12), LR(3), "HL'", HL_ );
widget_printstring_fixed( LC(20) / 8, LR(3) / 8, 5, "SZ5H3PNC" );
show_register1( LC(36), LR(3), "ULA", ula_last_byte() );
sprintf( pbuf, "%d", tstates );
widget_printstring_right( LC(12) - 4, LR(4), 5, "Tstates" );
widget_printstring_fixed( LC(12) / 8, LR(4) / 8, 7, pbuf );
for( i = 0; i < 8; ++i )
pbuf[i] = '0' + ( F >> i & 1 );
pbuf[8] = 0;
widget_printstring_fixed( LC(20) / 8, LR(4) / 8, 7, pbuf );
capabilities = libspectrum_machine_capabilities( machine_current->machine );
if( capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_AY )
show_register1( LC(37), LR(4), "AY",
machine_current->ay.current_register );
if( capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_128_MEMORY )
show_register1( LC(6), LR(5), "128Mem",
machine_current->ram.last_byte );
if( capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_128_MEMORY )
show_register1( LC(15), LR(5), "+3Mem",
machine_current->ram.last_byte2 );
if( capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_TIMEX_VIDEO ||
capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_TIMEX_MEMORY ||
capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_SE_MEMORY )
show_register1( LC(24), LR(5), "TmxDec", scld_last_dec.byte );
if( capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_TIMEX_MEMORY ||
capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_SE_MEMORY )
show_register1( LC(33), LR(5), "TmxHSR", scld_last_hsr );
if( settings_current.zxcf_active )
show_register1( LC(6), LR(5), "ZXCF", zxcf_last_memctl() );
for( i = 0; i < 8; ++i ) {
int x = LC(2 + 10 * ( i & 3) ), y = LR(i < 4 ? 6 : 7);
sprintf( pbuf, "P%X", i );
widget_printstring_right( x, y, 5, pbuf );
snprintf( pbuf, sizeof( pbuf ), "%s %d",
memory_bank_name( &memory_map_read[i] ),
memory_map_read[i].page_num );
x = widget_printstring( x + 4, y, 7, pbuf ) + 4;
if( memory_map_read[i].writable )
x = widget_printstring( x, y, 4, "w" );
if( memory_map_read[i].contended )
x = widget_printstring( x, y, 4, "c" );
}
}
static void display_bytes( void )
{
int x, y;
char pbuf[36];
for( y = 0; y < 8; ++y ) {
libspectrum_word addr = debugger_memaddr + y * 8;
sprintf( pbuf, format_16_bit(), addr );
widget_printstring_fixed( LC(1) / 8, LR(y) / 8, 7, pbuf );
widget_printstring( LC(6), LR(y), 5, ":" );
for( x = 0; x < 8; ++x ) {
sprintf( pbuf + x * 4, format_8_bit(),
readbyte_internal( addr + x ) );
if( x < 7 )
strcat( pbuf, " " );
}
widget_printstring_fixed( LC(7) / 8, LR(y) / 8, 7, pbuf );
}
}
static void display_text( void )
{
int x, y;
char pbuf[8];
for( y = 0; y < 8; ++y ) {
libspectrum_word addr = debugger_memaddr + y * 32;
sprintf( pbuf, format_16_bit(), addr );
widget_printstring_fixed( LC(1) / 8, LR(y) / 8, 7, pbuf );
widget_printstring( LC(6), LR(y), 5, ":" );
for( x = 0; x < 32; ++x )
widget_printchar_fixed( LC(x + 8) / 8, LR(y) / 8, 7,
readbyte_internal( addr + x ) );
}
}
static void display_disasm( void )
{
int y;
char pbuf[40];
libspectrum_word addr = debugger_memaddr;
for( y = 0; y < 8; ++y ) {
size_t length;
char *spc;
sprintf( pbuf, format_16_bit(), addr );
widget_printstring_fixed( LC(1) / 8, LR(y) / 8, 7, pbuf );
widget_printstring( LC(6), LR(y), 5, ":" );
debugger_disassemble( pbuf, sizeof( pbuf ), &length, addr );
addr += length;
spc = strchr( pbuf, ' ' );
if( spc )
*spc = 0;
widget_printstring( LC(8), LR(y), 7, pbuf );
if( spc ) {
spc += 1 + strspn( spc + 1, " " );
widget_printstring( LC(12) + 4, LR(y), 7, spc );
}
}
}
static void display_breakpts( void )
{
GSList *ptr;
int i = -breakpt_show;
char pbuf[80], fmt[20];
if( i )
widget_up_arrow( LC(0), LR(0), 7 );
for( ptr = debugger_breakpoints; i < 8 && ptr; ptr = ptr->next, ++i ) {
const debugger_breakpoint *bp = ptr->data;
if( i < 0 )
continue;
sprintf( pbuf, "%lu", ( unsigned long )bp->id );
widget_printstring( LC(1), LR(i), 5, pbuf );
widget_printstring( LC(6), LR(i), 7,
debugger_breakpoint_type_abbr[bp->type] );
switch ( bp->type ) {
case DEBUGGER_BREAKPOINT_TYPE_EXECUTE:
case DEBUGGER_BREAKPOINT_TYPE_READ:
case DEBUGGER_BREAKPOINT_TYPE_WRITE:
if( bp->value.address.page == -1 )
sprintf( pbuf, format_16_bit(), bp->value.address.offset );
else {
debugger_breakpoint_decode_page( pbuf, sizeof( pbuf ) - 12,
bp->value.address.page );
if( pbuf[0] == '[' )
sprintf( pbuf, "?%d", bp->value.address.page );
strcat( pbuf, ":" );
sprintf( pbuf + strlen( pbuf ), format_16_bit(),
bp->value.address.offset );
}
break;
case DEBUGGER_BREAKPOINT_TYPE_PORT_READ:
case DEBUGGER_BREAKPOINT_TYPE_PORT_WRITE:
sprintf( fmt, "%s:%s", format_16_bit(), format_16_bit() );
sprintf( pbuf, fmt, bp->value.port.mask, bp->value.port.port );
break;
case DEBUGGER_BREAKPOINT_TYPE_TIME:
sprintf( pbuf, "%5d", bp->value.time.tstates );
break;
case DEBUGGER_BREAKPOINT_TYPE_EVENT:
sprintf( pbuf, "%s:%s", bp->value.event.type, bp->value.event.detail );
break;
}
widget_printstring( LC(10), LR(i), 6, pbuf );
sprintf( pbuf, "%lu", ( unsigned long )bp->ignore );
widget_printstring( LC(18) + 4, LR(i), 7, pbuf );
sprintf( pbuf, "%s", debugger_breakpoint_life_abbr[bp->life] );
widget_printstring( LC(23), LR(i), 7, pbuf );
if( bp->condition ) {
debugger_expression_deparse( pbuf, sizeof( pbuf ), bp->condition );
widget_printstring( LC(28) + 4, LR(i), 6, pbuf );
}
}
if( !i )
widget_printstring( LC(1), LR(0), 5, "(No breakpoints)" );
else if( ptr )
widget_down_arrow( LC(0), LC(7), 7 );
}
static void scroll( int step )
{
switch ( display ) {
case DB_BYTES:
debugger_memaddr += 8 * step;
break;
case DB_TEXT:
debugger_memaddr += 32 * step;
break;
case DB_DISASM:
if( step > 0 )
for( ; step; --step ) {
size_t length;
debugger_disassemble( NULL, 0, &length, debugger_memaddr );
debugger_memaddr += length;
} else
for( ; step; ++step ) {
/* For details, see ui/gtk/debugger.c:move_disassembly() */
size_t i, longest = 1;
for( i = 1; i <= 8; ++i ) {
size_t length;
debugger_disassemble( NULL, 0, &length, debugger_memaddr );
if( length == i )
longest = i;
}
debugger_memaddr -= longest;
}
break;
case DB_BREAKPT:
{
int length = g_slist_length( debugger_breakpoints );
breakpt_no += step;
if( breakpt_no >= length )
breakpt_no = length - 1;
if( breakpt_no < 0 )
breakpt_no = 0;
if( breakpt_no < breakpt_show )
breakpt_show = breakpt_no;
else if( breakpt_no > breakpt_show + 7 )
breakpt_show = breakpt_no - 7;
}
break;
default:
return;
}
widget_debugger_draw( NULL );
}
|