File: command.c

package info (click to toggle)
fuse-emulator 1.1.1%2Bdfsg1-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,120 kB
  • ctags: 8,968
  • sloc: ansic: 78,960; sh: 11,228; perl: 3,742; makefile: 1,104; yacc: 236; lex: 140
file content (299 lines) | stat: -rw-r--r-- 7,615 bytes parent folder | download
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
/* command.c: Parse a debugger command
   Copyright (c) 2002-2008 Philip Kendall

   $Id: command.c 4730 2012-09-03 12:50:19Z 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 <ctype.h>
#include <stdio.h>
#include <string.h>

#include "debugger.h"
#include "debugger_internals.h"
#include "mempool.h"
#include "ui/ui.h"
#include "utils.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;

int yyparse( void );
int yywrap( void );

/* Evaluate the debugger command given in 'command' */
void
debugger_command_evaluate( const char *command )
{
  if( !command ) return;

  if( command_buffer ) free( command_buffer );

  command_buffer = utils_safe_strdup( command );

  /* Start parsing at the start of the given command */
  command_ptr = command_buffer;
    
  /* Parse the command */
  yyparse();

  /* And free any memory we allocated while parsing */
  mempool_free( debugger_memory_pool );

  ui_debugger_update();
}

/* Utility functions called from the flex scanner */

int
yywrap( void )
{
  return 1;
}

/* 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 < (size_t)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 register name to a useful index value */
int
debugger_register_hash( const char *name )
{
  int hash = 0x0000, primed = 0;
  size_t length;
  const char *ptr;

  length = strlen( name );

  if( name[ length - 1 ] == '\'' ) { primed = 1; length--; }

  for( ptr = name; ptr < name + length; ptr++ ) {
    hash <<= 8; hash |= tolower( *ptr );
  }

  if( primed ) hash |= 0x8000;

  switch( hash ) {
    case 0x0061: case 0x8061:	/* A, A' */
    case 0x0066: case 0x8066:	/* F, F' */
    case 0x0062: case 0x8062:	/* B, B' */
    case 0x0063: case 0x8063:	/* C, C' */
    case 0x0064: case 0x8064:	/* D, D' */
    case 0x0065: case 0x8065:	/* E, E' */
    case 0x0068: case 0x8068:	/* H, H' */
    case 0x006c: case 0x806c:	/* L, L' */
    case 0x6166: case 0xe166:	/* AF, AF' */
    case 0x6263: case 0xe263:	/* BC, BC' */
    case 0x6465: case 0xe465:	/* DE, DE' */
    case 0x686c: case 0xe86c:	/* HL, HL' */
    case 0x7370:		/* SP */
    case 0x7063:		/* PC */
    case 0x6978:		/* IX */
    case 0x6979:		/* IY */
    case 0x696d:		/* IM */
    case 0x69666631:		/* IFF1 */
    case 0x69666632:		/* IFF2 */
      return hash;

    default: return -1;
  }
}

/* Utility functions called by the bison parser */

/* The error callback if yyparse finds an error */
void
yyerror( const char *s )
{
  ui_error( UI_ERROR_ERROR, "Invalid debugger command: %s", s );
}

/* Get the value of a register */
libspectrum_word
debugger_register_get( int which )
{
  switch( which ) {

    /* 8-bit registers */
  case 0x0061: return A;
  case 0x8061: return A_;
  case 0x0066: return F;
  case 0x8066: return F_;
  case 0x0062: return B;
  case 0x8062: return B_;
  case 0x0063: return C;
  case 0x8063: return C_;
  case 0x0064: return D;
  case 0x8064: return D_;
  case 0x0065: return E;
  case 0x8065: return E_;
  case 0x0068: return H;
  case 0x8068: return H_;
  case 0x006c: return L;
  case 0x806c: return L_;
    
    /* 16-bit registers */
  case 0x6166: return AF;
  case 0xe166: return AF_;
  case 0x6263: return BC;
  case 0xe263: return BC_;
  case 0x6465: return DE;
  case 0xe465: return DE_;
  case 0x686c: return HL;
  case 0xe86c: return HL_;

  case 0x7370: return SP;
  case 0x7063: return PC;
  case 0x6978: return IX;
  case 0x6979: return IY;

   /* interrupt flags */
  case 0x696d: return IM;
  case 0x69666631: return IFF1;
  case 0x69666632: return IFF2;

  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, libspectrum_word value )
{
  switch( which ) {

    /* 8-bit registers */
    case 0x0061: A = value; break;
    case 0x8061: A_ = value; break;
    case 0x0066: F = value; break;
    case 0x8066: F_ = value; break;
    case 0x0062: B = value; break;
    case 0x8062: B_ = value; break;
    case 0x0063: C = value; break;
    case 0x8063: C_ = value; break;
    case 0x0064: D = value; break;
    case 0x8064: D_ = value; break;
    case 0x0065: E = value; break;
    case 0x8065: E_ = value; break;
    case 0x0068: H = value; break;
    case 0x8068: H_ = value; break;
    case 0x006c: L = value; break;
    case 0x806c: L_ = value; break;

    /* 16-bit registers */
    case 0x6166: AF = value; break;
    case 0xe166: AF_ = value; break;
    case 0x6263: BC = value; break;
    case 0xe263: BC_ = value; break;
    case 0x6465: DE = value; break;
    case 0xe465: DE_ = value; break;
    case 0x686c: HL = value; break;
    case 0xe86c: HL_ = value; break;

    case 0x7370: SP = value; break;
    case 0x7063: PC = value; break;
    case 0x6978: IX = value; break;
    case 0x6979: IY = value; break;

     /* interrupt flags */
    case 0x696d: if( value >= 0 && value <=2 ) IM = value; break;
    case 0x69666631: IFF1 = !!value; break;
    case 0x69666632: IFF2 = !!value; break;

  default:
    ui_error( UI_ERROR_ERROR, "attempt to set unknown register '%d'", which );
    break;
  }
}

/* Get the textual representation of a register */
const char *
debugger_register_text( int which )
{
  switch( which ) {

    /* 8-bit registers */
  case 0x0061: return "A";
  case 0x8061: return "A'";
  case 0x0066: return "F";
  case 0x8066: return "F'";
  case 0x0062: return "B";
  case 0x8062: return "B'";
  case 0x0063: return "C";
  case 0x8063: return "C'";
  case 0x0064: return "D";
  case 0x8064: return "D'";
  case 0x0065: return "E";
  case 0x8065: return "E'";
  case 0x0068: return "H";
  case 0x8068: return "H'";
  case 0x006c: return "L";
  case 0x806c: return "L'";
    
    /* 16-bit registers */
  case 0x6166: return "AF";
  case 0xe166: return "AF'";
  case 0x6263: return "BC";
  case 0xe263: return "BC'";
  case 0x6465: return "DE";
  case 0xe465: return "DE'";
  case 0x686c: return "HL";
  case 0xe86c: return "HL'";

  case 0x7370: return "SP";
  case 0x7063: return "PC";
  case 0x6978: return "IX";
  case 0x6979: return "IY";

   /* interrupt flags */
  case 0x696d: return "IM";
  case 0x69666631: return "IFF1";
  case 0x69666632: return "IFF2";

  default:
    ui_error( UI_ERROR_ERROR, "attempt to get unknown register '%d'", which );
    return "(invalid)";
  }
}