1
0
mirror of https://git.code.sf.net/p/fuse-emulator/fuse synced 2026-01-27 01:41:34 +03:00
Files
fuse/debugger/command.c
2003-07-10 13:31:53 +00:00

185 lines
4.5 KiB
C

/* command.c: Parse a debugger command
Copyright (c) 2002 Philip Kendall
$Id$
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author contact information:
E-mail: pak21-fuse@srcf.ucam.org
Postal address: 15 Crescent Road, Wokingham, Berks, RG40 2DB, England
*/
#include <config.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "debugger.h"
#include "debugger_internals.h"
#include "ui/ui.h"
#include "z80/z80.h"
#include "z80/z80_macros.h"
/* The last debugger command we were given to execute */
static char *command_buffer = NULL;
/* And a pointer as to how much we've parsed */
static char *command_ptr;
/* Necessary declaration */
int yyparse( void );
/* Evaluate the debugger command given in 'command' */
int
debugger_command_evaluate( const char *command )
{
if( command_buffer ) free( command_buffer );
command_buffer = strdup( command );
if( !command_buffer ) {
ui_error( UI_ERROR_ERROR, "Out of memory at %s:%d", __FILE__, __LINE__ );
return 1;
}
/* Start parsing at the start of the given command */
command_ptr = command_buffer;
/* Parse the command. If there was a problem, skip the rest of this line */
if( yyparse() ) command_ptr = "\0";
ui_debugger_update();
return 0;
}
/* Utility functions called from the flex scanner */
/* Called to get up to 'max_size' bytes of the command to be parsed */
int
debugger_command_input( char *buf, int *result, int max_size )
{
size_t length = strlen( command_ptr );
if( !length ) {
return 0;
} else if( length < max_size ) {
memcpy( buf, command_ptr, length );
*result = length; command_ptr += length;
return 1;
} else {
memcpy( buf, command_ptr, max_size );
*result = max_size; command_ptr += max_size;
return 1;
}
}
/* Convert a 16-bit register name to a useful index value */
int
debugger_reg16_index( char *name )
{
int hash = 0x100 * tolower( name[0] ) + tolower( name[1] );
switch( hash ) {
case 0x6166: /* AF */
case 0x6263: /* BC */
case 0x6465: /* DE */
case 0x686c: /* HL */
case 0x7370: /* SP */
case 0x7063: /* PC */
case 0x6978: /* IX */
case 0x6979: /* IY */
return hash;
default: return -1;
}
}
/* Utility functions called by the bison parser */
/* The error callback if yyparse finds an error */
void
yyerror( char *s )
{
ui_error( UI_ERROR_ERROR, "Invalid debugger command: %s", s );
}
/* Get the value of a register */
int
debugger_register_get( int which )
{
switch( which ) {
/* 8-bit registers */
case 'a': return A;
case 'b': return B;
case 'c': return C;
case 'd': return D;
case 'e': return E;
case 'f': return F;
case 'h': return H;
case 'l': return L;
/* 16-bit registers */
case 0x6166: return AF;
case 0x6263: return BC;
case 0x6465: return DE;
case 0x686c: return HL;
case 0x7370: return SP;
case 0x7063: return PC;
case 0x6978: return IX;
case 0x6979: return IY;
default:
ui_error( UI_ERROR_ERROR, "attempt to get unknown register '%d'", which );
return 0;
}
}
/* Set the value of a register */
void
debugger_register_set( int which, int value )
{
switch( which ) {
/* 8-bit registers */
case 'a': A = value; break;
case 'b': B = value; break;
case 'c': C = value; break;
case 'd': D = value; break;
case 'e': E = value; break;
case 'f': F = value; break;
case 'h': H = value; break;
case 'l': L = value; break;
/* 16-bit registers */
case 0x6166: AF = value; break;
case 0x6263: BC = value; break;
case 0x6465: DE = value; break;
case 0x686c: HL = value; break;
case 0x7370: SP = value; break;
case 0x7063: PC = value; break;
case 0x6978: IX = value; break;
case 0x6979: IY = value; break;
default:
ui_error( UI_ERROR_ERROR, "attempt to set unknown register '%d'", which );
break;
}
}