1 |
|
/* |
2 |
< |
* mon.cpp - mon main program |
2 |
> |
* mon.cpp - cxmon main program |
3 |
|
* |
4 |
< |
* mon (C) 1997-1999 Christian Bauer, Marc Hellwig |
4 |
> |
* cxmon (C) 1997-2003 Christian Bauer, Marc Hellwig |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
20 |
|
|
21 |
|
#include "sysdeps.h" |
22 |
|
|
23 |
< |
#ifdef HAVE_READLINE_READLINE_H |
23 |
> |
#include <stdio.h> |
24 |
> |
#include <stdlib.h> |
25 |
> |
#include <signal.h> |
26 |
> |
#include <ctype.h> |
27 |
> |
#include <string> |
28 |
> |
#include <map> |
29 |
> |
|
30 |
> |
#if defined(HAVE_READLINE_H) |
31 |
> |
extern "C" { |
32 |
> |
#include <readline.h> |
33 |
> |
} |
34 |
> |
#elif defined(HAVE_READLINE_READLINE_H) |
35 |
|
extern "C" { |
36 |
|
#include <readline/readline.h> |
37 |
|
} |
38 |
|
#endif |
39 |
|
|
40 |
< |
#ifdef HAVE_READLINE_HISTORY_H |
40 |
> |
#if defined(HAVE_HISTORY_H) |
41 |
> |
extern "C" { |
42 |
> |
#include <history.h> |
43 |
> |
} |
44 |
> |
#elif defined(HAVE_READLINE_HISTORY_H) |
45 |
|
extern "C" { |
46 |
|
#include <readline/history.h> |
47 |
|
} |
49 |
|
|
50 |
|
#include "mon.h" |
51 |
|
#include "mon_cmd.h" |
52 |
< |
#include "version.h" |
52 |
> |
#include "mon_lowmem.h" |
53 |
> |
|
54 |
> |
#ifndef VERSION |
55 |
> |
#define VERSION "3" |
56 |
> |
#endif |
57 |
|
|
58 |
|
|
59 |
|
// Buffer we're operating on |
66 |
|
FILE *monin, *monout, *monerr; |
67 |
|
|
68 |
|
// Input line |
69 |
< |
static char input[INPUT_LENGTH]; |
69 |
> |
static char *input; |
70 |
|
static char *in_ptr; |
71 |
|
char *mon_args_ptr; |
72 |
|
|
73 |
|
// Current address, value of '.' in expressions |
74 |
< |
uint32 mon_dot_address; |
74 |
> |
uintptr mon_dot_address; |
75 |
|
|
76 |
|
// Current value of ':' in expression |
77 |
|
static uint32 colon_value; |
78 |
|
|
79 |
|
|
80 |
|
// Scanner variables |
81 |
< |
enum Token mon_token; // Last token read |
82 |
< |
uint32 mon_number; // Contains the number if mon_token==T_NUMBER |
83 |
< |
char mon_string[INPUT_LENGTH]; // Contains the string if mon_token==T_STRING |
84 |
< |
char mon_name[INPUT_LENGTH]; // Contains the variable name if mon_token==T_NAME |
81 |
> |
enum Token mon_token; // Last token read |
82 |
> |
uintptr mon_number; // Contains the number if mon_token==T_NUMBER |
83 |
> |
char *mon_string; // Contains the string if mon_token==T_STRING |
84 |
> |
char *mon_name; // Contains the variable name if mon_token==T_NAME |
85 |
|
|
86 |
|
|
87 |
|
// List of installed commands |
88 |
|
struct CmdSpec { |
89 |
< |
const char *name; // Name of command |
90 |
< |
void (*func)(void); // Function that executes this command |
89 |
> |
const char *name; // Name of command |
90 |
> |
void (*func)(); // Function that executes this command |
91 |
|
}; |
92 |
|
|
93 |
< |
static CmdSpec *cmds; // Array of CmdSpecs |
94 |
< |
static int num_cmds; // Number of installed commands |
95 |
< |
static char *cmd_help; // Help text for commands |
93 |
> |
static CmdSpec *cmds; // Array of CmdSpecs |
94 |
> |
static int num_cmds; // Number of installed commands |
95 |
> |
static char *cmd_help; // Help text for commands |
96 |
|
|
97 |
|
|
98 |
|
// List of variables |
99 |
< |
struct Variable { |
100 |
< |
Variable *next; // Pointer to next variable (must be first element of struct) |
82 |
< |
char *name; // Variable name |
83 |
< |
uint32 value; // Variable value |
84 |
< |
}; |
85 |
< |
|
86 |
< |
static Variable *first_var; // Pointer to first variable |
99 |
> |
typedef std::map<std::string, uintptr> var_map; |
100 |
> |
static var_map vars; |
101 |
|
|
102 |
|
|
103 |
|
// Prototypes |
104 |
< |
static void init_abort(void); |
105 |
< |
static void exit_abort(void); |
104 |
> |
static void init_abort(); |
105 |
> |
static void exit_abort(); |
106 |
|
|
107 |
|
static void read_line(char *prompt); // Scanner |
108 |
< |
static char get_char(void); |
108 |
> |
static char get_char(); |
109 |
|
static void put_back(char c); |
110 |
< |
static enum Token get_hex_number(uint32 &i); |
111 |
< |
static enum Token get_dec_number(uint32 &i); |
112 |
< |
static enum Token get_char_number(uint32 &i); |
113 |
< |
static enum Token get_string(char *str); |
114 |
< |
static enum Token get_hex_or_name(uint32 &i, char *name); |
115 |
< |
|
116 |
< |
static bool eor_expr(uint32 *number); // Parser |
117 |
< |
static bool and_expr(uint32 *number); |
118 |
< |
static bool shift_expr(uint32 *number); |
119 |
< |
static bool add_expr(uint32 *number); |
120 |
< |
static bool mul_expr(uint32 *number); |
121 |
< |
static bool factor(uint32 *number); |
108 |
< |
static Variable *lookup_var(const char *s); |
109 |
< |
static Variable *insert_var(const char *s); |
110 |
< |
static void remove_var(const char *s); |
110 |
> |
static enum Token get_hex_number(uintptr &i); |
111 |
> |
static enum Token get_dec_number(uintptr &i); |
112 |
> |
static enum Token get_char_number(uintptr &i); |
113 |
> |
static enum Token get_string(char *&str); |
114 |
> |
static enum Token get_hex_or_name(uintptr &i, char *&name); |
115 |
> |
|
116 |
> |
static bool eor_expr(uintptr *number); // Parser |
117 |
> |
static bool and_expr(uintptr *number); |
118 |
> |
static bool shift_expr(uintptr *number); |
119 |
> |
static bool add_expr(uintptr *number); |
120 |
> |
static bool mul_expr(uintptr *number); |
121 |
> |
static bool factor(uintptr *number); |
122 |
|
|
123 |
|
|
124 |
|
/* |
125 |
|
* Add command to mon |
126 |
|
*/ |
127 |
|
|
128 |
< |
void mon_add_command(const char *name, void (*func)(void), const char *help_text) |
128 |
> |
void mon_add_command(const char *name, void (*func)(), const char *help_text) |
129 |
|
{ |
130 |
|
num_cmds++; |
131 |
|
if (cmds) |
170 |
|
was_aborted = true; |
171 |
|
} |
172 |
|
|
173 |
< |
static void init_abort(void) |
173 |
> |
static void init_abort() |
174 |
|
{ |
175 |
|
was_aborted = false; |
176 |
|
sigemptyset(&my_sa.sa_mask); |
184 |
|
sigaction(SIGINT, &my_sa, NULL); |
185 |
|
} |
186 |
|
|
187 |
< |
static void exit_abort(void) |
187 |
> |
static void exit_abort() |
188 |
|
{ |
189 |
|
my_sa.sa_handler = SIG_DFL; |
190 |
|
sigaction(SIGINT, &my_sa, NULL); |
191 |
|
} |
192 |
|
|
193 |
< |
bool mon_aborted(void) |
193 |
> |
bool mon_aborted() |
194 |
|
{ |
195 |
|
bool ret = was_aborted; |
196 |
|
was_aborted = false; |
202 |
|
* Access to buffer |
203 |
|
*/ |
204 |
|
|
205 |
< |
uint32 mon_read_byte(uint32 adr) |
205 |
> |
uint32 (*mon_read_byte)(uintptr adr); |
206 |
> |
|
207 |
> |
uint32 mon_read_byte_buffer(uintptr adr) |
208 |
|
{ |
209 |
< |
if (mon_use_real_mem) |
197 |
< |
return *(uint8 *)adr; |
198 |
< |
else |
199 |
< |
return mem[adr % mon_mem_size]; |
209 |
> |
return mem[adr % mon_mem_size]; |
210 |
|
} |
211 |
|
|
212 |
< |
void mon_write_byte(uint32 adr, uint32 b) |
212 |
> |
uint32 mon_read_byte_real(uintptr adr) |
213 |
|
{ |
214 |
< |
if (mon_use_real_mem) |
205 |
< |
*(uint8 *)adr = b; |
206 |
< |
else |
207 |
< |
mem[adr % mon_mem_size] = b; |
214 |
> |
return *(uint8 *)adr; |
215 |
|
} |
216 |
|
|
217 |
< |
uint32 mon_read_half(uint32 adr) |
217 |
> |
void (*mon_write_byte)(uintptr adr, uint32 b); |
218 |
> |
|
219 |
> |
void mon_write_byte_buffer(uintptr adr, uint32 b) |
220 |
|
{ |
221 |
< |
if (mon_use_real_mem) |
213 |
< |
return ntohs(*(uint16 *)adr); |
214 |
< |
else |
215 |
< |
return mem[adr % mon_mem_size] << 8 | mem[(adr+1) % mon_mem_size]; |
221 |
> |
mem[adr % mon_mem_size] = b; |
222 |
|
} |
223 |
|
|
224 |
< |
void mon_write_half(uint32 adr, uint32 w) |
224 |
> |
void mon_write_byte_real(uintptr adr, uint32 b) |
225 |
|
{ |
226 |
< |
if (mon_use_real_mem) |
221 |
< |
*(uint16 *)adr = htons(w); |
222 |
< |
else { |
223 |
< |
mem[adr % mon_mem_size] = w >> 8; |
224 |
< |
mem[(adr+1) % mon_mem_size] = w; |
225 |
< |
} |
226 |
> |
*(uint8 *)adr = b; |
227 |
|
} |
228 |
|
|
229 |
< |
uint32 mon_read_word(uint32 adr) |
229 |
> |
uint32 mon_read_half(uintptr adr) |
230 |
|
{ |
231 |
< |
if (mon_use_real_mem) |
231 |
< |
return ntohl(*(uint32 *)adr); |
232 |
< |
else |
233 |
< |
return mon_read_byte(adr) << 24 | mon_read_byte(adr+1) << 16 | mon_read_byte(adr+2) << 8 | mon_read_byte(adr+3); |
231 |
> |
return (mon_read_byte(adr) << 8) | mon_read_byte(adr+1); |
232 |
|
} |
233 |
|
|
234 |
< |
void mon_write_word(uint32 adr, uint32 l) |
234 |
> |
void mon_write_half(uintptr adr, uint32 w) |
235 |
|
{ |
236 |
< |
if (mon_use_real_mem) |
237 |
< |
*(uint32 *)adr = htonl(l); |
238 |
< |
else { |
239 |
< |
mem[adr % mon_mem_size] = l >> 24; |
240 |
< |
mem[(adr+1) % mon_mem_size] = l >> 16; |
241 |
< |
mem[(adr+2) % mon_mem_size] = l >> 8; |
242 |
< |
mem[(adr+3) % mon_mem_size] = l; |
243 |
< |
} |
236 |
> |
mon_write_byte(adr, w >> 8); |
237 |
> |
mon_write_byte(adr+1, w); |
238 |
> |
} |
239 |
> |
|
240 |
> |
uint32 mon_read_word(uintptr adr) |
241 |
> |
{ |
242 |
> |
return (mon_read_byte(adr) << 24) | (mon_read_byte(adr+1) << 16) | (mon_read_byte(adr+2) << 8) | mon_read_byte(adr+3); |
243 |
> |
} |
244 |
> |
|
245 |
> |
void mon_write_word(uintptr adr, uint32 l) |
246 |
> |
{ |
247 |
> |
mon_write_byte(adr, l >> 24); |
248 |
> |
mon_write_byte(adr+1, l >> 16); |
249 |
> |
mon_write_byte(adr+2, l >> 8); |
250 |
> |
mon_write_byte(adr+3, l); |
251 |
|
} |
252 |
|
|
253 |
|
|
257 |
|
|
258 |
|
static void read_line(char *prompt) |
259 |
|
{ |
260 |
+ |
#ifdef HAVE_LIBREADLINE |
261 |
|
static char *line_read = NULL; |
262 |
|
|
263 |
< |
if (line_read) { |
264 |
< |
free(line_read); |
265 |
< |
line_read = NULL; |
263 |
> |
if (input) |
264 |
> |
free(input); |
265 |
> |
input = readline(prompt); |
266 |
> |
|
267 |
> |
if (input) { |
268 |
> |
if (*input) |
269 |
> |
add_history(input); |
270 |
> |
} else { |
271 |
> |
// EOF, quit cxmon |
272 |
> |
input = (char *)malloc(2); |
273 |
> |
input[0] = 'x'; |
274 |
> |
input[1] = 0; |
275 |
> |
fprintf(monout, "x\n"); |
276 |
|
} |
277 |
|
|
278 |
< |
line_read = readline(prompt); |
279 |
< |
|
280 |
< |
if (line_read && *line_read) |
281 |
< |
add_history(line_read); |
282 |
< |
|
283 |
< |
strncpy(in_ptr = input, line_read, INPUT_LENGTH); |
284 |
< |
input[INPUT_LENGTH-1] = 0; |
278 |
> |
in_ptr = input; |
279 |
> |
#else |
280 |
> |
#define INPUT_LENGTH 256 |
281 |
> |
if (!input) |
282 |
> |
input = (char *)malloc(INPUT_LENGTH); |
283 |
> |
fprintf(monout, prompt); |
284 |
> |
fflush(monout); |
285 |
> |
fgets(in_ptr = input, INPUT_LENGTH, monin); |
286 |
> |
char *s = strchr(input, '\n'); |
287 |
> |
if (s != NULL) |
288 |
> |
*s = 0; |
289 |
> |
#endif |
290 |
|
} |
291 |
|
|
292 |
|
|
294 |
|
* Read a character from the input line |
295 |
|
*/ |
296 |
|
|
297 |
< |
static char get_char(void) |
297 |
> |
static char get_char() |
298 |
|
{ |
299 |
|
return *in_ptr++; |
300 |
|
} |
314 |
|
* Scanner: Get a token from the input line |
315 |
|
*/ |
316 |
|
|
317 |
< |
enum Token mon_get_token(void) |
317 |
> |
enum Token mon_get_token() |
318 |
|
{ |
319 |
< |
char c; |
319 |
> |
char c = get_char(); |
320 |
|
|
321 |
|
// Skip spaces |
322 |
< |
while ((c = get_char()) == ' ') ; |
322 |
> |
while (isspace(c)) |
323 |
> |
c = get_char(); |
324 |
|
|
325 |
|
switch (c) { |
326 |
|
case 0: |
393 |
|
} |
394 |
|
} |
395 |
|
|
396 |
< |
static enum Token get_hex_number(uint32 &i) |
396 |
> |
static enum Token get_hex_number(uintptr &i) |
397 |
|
{ |
398 |
|
char c = get_char(); |
399 |
|
|
402 |
|
return T_NULL; |
403 |
|
|
404 |
|
do { |
405 |
+ |
c = tolower(c); |
406 |
|
if (c < 'a') |
407 |
|
i = (i << 4) + (c - '0'); |
408 |
|
else |
418 |
|
} |
419 |
|
} |
420 |
|
|
421 |
< |
static enum Token get_dec_number(uint32 &i) |
421 |
> |
static enum Token get_dec_number(uintptr &i) |
422 |
|
{ |
423 |
|
char c = get_char(); |
424 |
|
|
439 |
|
} |
440 |
|
} |
441 |
|
|
442 |
< |
static enum Token get_char_number(uint32 &i) |
442 |
> |
static enum Token get_char_number(uintptr &i) |
443 |
|
{ |
444 |
|
char c; |
445 |
|
|
454 |
|
return T_NULL; |
455 |
|
} |
456 |
|
|
457 |
< |
static enum Token get_string(char *str) |
457 |
> |
static enum Token get_string(char *&str) |
458 |
|
{ |
459 |
< |
char c; |
459 |
> |
// Remember start of string |
460 |
> |
char *old_in_ptr = in_ptr; |
461 |
|
|
462 |
+ |
// Determine string length |
463 |
+ |
char c; |
464 |
+ |
unsigned n = 0; |
465 |
|
while ((c = get_char()) != 0) { |
466 |
< |
if (c == '"') { |
467 |
< |
*str = 0; |
468 |
< |
return T_STRING; |
469 |
< |
} |
470 |
< |
*str++ = c; |
466 |
> |
n++; |
467 |
> |
if (c == '"') |
468 |
> |
break; |
469 |
> |
} |
470 |
> |
if (c == 0) { |
471 |
> |
mon_error("Unterminated string"); |
472 |
> |
return T_NULL; |
473 |
|
} |
474 |
|
|
475 |
< |
mon_error("Unterminated string"); |
476 |
< |
return T_NULL; |
475 |
> |
// Allocate new buffer (n: size needed including terminating 0) |
476 |
> |
str = (char *)realloc(str, n); |
477 |
> |
|
478 |
> |
// Copy string to buffer |
479 |
> |
char *p = str; |
480 |
> |
in_ptr = old_in_ptr; |
481 |
> |
while (--n) |
482 |
> |
*p++ = get_char(); |
483 |
> |
*p++ = 0; |
484 |
> |
get_char(); // skip closing '"' |
485 |
> |
return T_STRING; |
486 |
|
} |
487 |
|
|
488 |
< |
static enum Token get_hex_or_name(uint32 &i, char *name) |
488 |
> |
static enum Token get_hex_or_name(uintptr &i, char *&name) |
489 |
|
{ |
490 |
+ |
// Remember start of token |
491 |
|
char *old_in_ptr = in_ptr; |
453 |
– |
char c; |
492 |
|
|
493 |
|
// Try hex number first |
494 |
|
if (get_hex_number(i) == T_NUMBER) |
495 |
|
return T_NUMBER; |
496 |
|
|
497 |
< |
// Not a hex number, must be a variable name |
497 |
> |
// Not a hex number, must be a variable name; determine its length |
498 |
|
in_ptr = old_in_ptr; |
499 |
< |
c = get_char(); |
499 |
> |
char c = get_char(); |
500 |
> |
unsigned n = 1; |
501 |
|
do { |
502 |
< |
*name++ = c; |
502 |
> |
n++; |
503 |
|
c = get_char(); |
504 |
|
} while (isalnum(c)); |
505 |
|
|
506 |
< |
*name = 0; |
507 |
< |
put_back(c); |
506 |
> |
// Allocate new buffer (n: size needed including terminating 0) |
507 |
> |
name = (char *)realloc(name, n); |
508 |
> |
|
509 |
> |
// Copy name to buffer |
510 |
> |
in_ptr = old_in_ptr; |
511 |
> |
char *p = name; |
512 |
> |
while (--n) |
513 |
> |
*p++ = get_char(); |
514 |
> |
*p = 0; |
515 |
|
return T_NAME; |
516 |
|
} |
517 |
|
|
521 |
|
* true: OK, false: Error |
522 |
|
*/ |
523 |
|
|
524 |
< |
bool mon_expression(uint32 *number) |
524 |
> |
bool mon_expression(uintptr *number) |
525 |
|
{ |
526 |
< |
uint32 accu, expr; |
526 |
> |
uintptr accu, expr; |
527 |
|
|
528 |
|
if (!eor_expr(&accu)) |
529 |
|
return false; |
549 |
|
* true: OK, false: Error |
550 |
|
*/ |
551 |
|
|
552 |
< |
static bool eor_expr(uint32 *number) |
552 |
> |
static bool eor_expr(uintptr *number) |
553 |
|
{ |
554 |
< |
uint32 accu, expr; |
554 |
> |
uintptr accu, expr; |
555 |
|
|
556 |
|
if (!and_expr(&accu)) |
557 |
|
return false; |
577 |
|
* true: OK, false: Error |
578 |
|
*/ |
579 |
|
|
580 |
< |
static bool and_expr(uint32 *number) |
580 |
> |
static bool and_expr(uintptr *number) |
581 |
|
{ |
582 |
< |
uint32 accu, expr; |
582 |
> |
uintptr accu, expr; |
583 |
|
|
584 |
|
if (!shift_expr(&accu)) |
585 |
|
return false; |
605 |
|
* true: OK, false: Error |
606 |
|
*/ |
607 |
|
|
608 |
< |
static bool shift_expr(uint32 *number) |
608 |
> |
static bool shift_expr(uintptr *number) |
609 |
|
{ |
610 |
< |
uint32 accu, expr; |
610 |
> |
uintptr accu, expr; |
611 |
|
|
612 |
|
if (!add_expr(&accu)) |
613 |
|
return false; |
640 |
|
* true: OK, false: Error |
641 |
|
*/ |
642 |
|
|
643 |
< |
static bool add_expr(uint32 *number) |
643 |
> |
static bool add_expr(uintptr *number) |
644 |
|
{ |
645 |
< |
uint32 accu, expr; |
645 |
> |
uintptr accu, expr; |
646 |
|
|
647 |
|
if (!mul_expr(&accu)) |
648 |
|
return false; |
675 |
|
* true: OK, false: Error |
676 |
|
*/ |
677 |
|
|
678 |
< |
static bool mul_expr(uint32 *number) |
678 |
> |
static bool mul_expr(uintptr *number) |
679 |
|
{ |
680 |
< |
uint32 accu, fact; |
680 |
> |
uintptr accu, fact; |
681 |
|
|
682 |
|
if (!factor(&accu)) |
683 |
|
return false; |
725 |
|
* true: OK, false: Error |
726 |
|
*/ |
727 |
|
|
728 |
< |
static bool factor(uint32 *number) |
728 |
> |
static bool factor(uintptr *number) |
729 |
|
{ |
730 |
|
switch (mon_token) { |
731 |
|
case T_NUMBER: |
734 |
|
return true; |
735 |
|
|
736 |
|
case T_NAME:{ |
737 |
< |
Variable *var; |
738 |
< |
if ((var = lookup_var(mon_name)) != NULL) { |
739 |
< |
*number = var->value; |
737 |
> |
var_map::const_iterator v = vars.find(mon_name); |
738 |
> |
if (v == vars.end()) |
739 |
> |
return false; |
740 |
> |
else { |
741 |
> |
*number = v->second; |
742 |
|
mon_get_token(); |
743 |
|
return true; |
744 |
< |
} else |
697 |
< |
return false; |
744 |
> |
} |
745 |
|
} |
746 |
|
|
747 |
|
case T_DOT: |
801 |
|
|
802 |
|
|
803 |
|
/* |
757 |
– |
* Lookup the value of a variable |
758 |
– |
*/ |
759 |
– |
|
760 |
– |
static Variable *lookup_var(const char *s) |
761 |
– |
{ |
762 |
– |
// Lookup variable |
763 |
– |
for (Variable *var=first_var; var; var=var->next) |
764 |
– |
if (!strcmp(s, var->name)) |
765 |
– |
return var; |
766 |
– |
|
767 |
– |
// Not found, error |
768 |
– |
mon_error("Undefined variable"); |
769 |
– |
return NULL; |
770 |
– |
} |
771 |
– |
|
772 |
– |
|
773 |
– |
/* |
774 |
– |
* Insert new variable (or redefine old) |
775 |
– |
*/ |
776 |
– |
|
777 |
– |
static Variable *insert_var(const char *s) |
778 |
– |
{ |
779 |
– |
// Lookup variable |
780 |
– |
for (Variable *var=first_var; var; var=var->next) |
781 |
– |
if (!strcmp(s, var->name)) |
782 |
– |
return var; |
783 |
– |
|
784 |
– |
// Insert new variable |
785 |
– |
Variable *var = new Variable; |
786 |
– |
var->name = strdup(s); |
787 |
– |
var->next = first_var; |
788 |
– |
first_var = var; |
789 |
– |
return var; |
790 |
– |
} |
791 |
– |
|
792 |
– |
|
793 |
– |
/* |
794 |
– |
* Remove variable |
795 |
– |
*/ |
796 |
– |
|
797 |
– |
static void remove_var(const char *s) |
798 |
– |
{ |
799 |
– |
Variable *var, *prev = (Variable *)&first_var; |
800 |
– |
|
801 |
– |
// Lookup variable and remove it |
802 |
– |
for (var=prev->next; var; prev=var, var=var->next) |
803 |
– |
if (!strcmp(s, var->name)) { |
804 |
– |
prev->next = var->next; |
805 |
– |
free(var->name); |
806 |
– |
free(var); |
807 |
– |
return; |
808 |
– |
} |
809 |
– |
} |
810 |
– |
|
811 |
– |
|
812 |
– |
/* |
804 |
|
* Set/clear/show variables |
805 |
|
* set [var[=value]] |
806 |
|
*/ |
807 |
|
|
808 |
< |
static void set_var(void) |
808 |
> |
static void set_var() |
809 |
|
{ |
810 |
|
if (mon_token == T_END) { |
811 |
|
|
812 |
|
// Show all variables |
813 |
< |
if (first_var == NULL) |
813 |
> |
if (vars.empty()) |
814 |
|
fprintf(monout, "No variables defined\n"); |
815 |
< |
else |
816 |
< |
for (Variable *v=first_var; v; v=v->next) |
817 |
< |
fprintf(monout, "%s = %08lx\n", v->name, v->value); |
815 |
> |
else { |
816 |
> |
var_map::const_iterator v = vars.begin(), end = vars.end(); |
817 |
> |
for (v=vars.begin(); v!=end; ++v) |
818 |
> |
fprintf(monout, "%s = %08x\n", v->first.c_str(), v->second); |
819 |
> |
} |
820 |
|
|
821 |
|
} else if (mon_token == T_NAME) { |
822 |
< |
char var_name[256]; |
830 |
< |
strcpy(var_name, mon_name); |
822 |
> |
std::string var_name = mon_name; |
823 |
|
mon_get_token(); |
824 |
|
if (mon_token == T_ASSIGN) { |
825 |
|
|
826 |
|
// Set variable |
827 |
< |
uint32 value; |
827 |
> |
uintptr value; |
828 |
|
mon_get_token(); |
829 |
|
if (!mon_expression(&value)) |
830 |
|
return; |
832 |
|
mon_error("Too many arguments"); |
833 |
|
return; |
834 |
|
} |
835 |
< |
insert_var(var_name)->value = value; |
835 |
> |
vars[var_name] = value; |
836 |
|
|
837 |
|
} else if (mon_token == T_END) { |
838 |
|
|
839 |
|
// Clear variable |
840 |
< |
remove_var(var_name); |
840 |
> |
vars.erase(var_name); |
841 |
|
|
842 |
|
} else |
843 |
|
mon_error("'=' expected"); |
851 |
|
* cv |
852 |
|
*/ |
853 |
|
|
854 |
< |
static void clear_vars(void) |
854 |
> |
static void clear_vars() |
855 |
|
{ |
856 |
< |
Variable *var, *next; |
865 |
< |
for (var=first_var; var; var=next) { |
866 |
< |
free(var->name); |
867 |
< |
next = var->next; |
868 |
< |
free(var); |
869 |
< |
} |
870 |
< |
first_var = NULL; |
856 |
> |
vars.clear(); |
857 |
|
} |
858 |
|
|
859 |
|
|
862 |
|
* h |
863 |
|
*/ |
864 |
|
|
865 |
< |
static void help_or_hunt(void) |
865 |
> |
static void help_or_hunt() |
866 |
|
{ |
867 |
|
if (mon_token != T_END) { |
868 |
|
hunt(); |
879 |
|
* ?? |
880 |
|
*/ |
881 |
|
|
882 |
< |
static void mon_cmd_list(void) |
882 |
> |
static void mon_cmd_list() |
883 |
|
{ |
884 |
|
for (int i=0; i<num_cmds; i++) |
885 |
|
fprintf(monout, "%s ", cmds[i].name); |
892 |
|
* @ [size] |
893 |
|
*/ |
894 |
|
|
895 |
< |
static void reallocate(void) |
895 |
> |
static void reallocate() |
896 |
|
{ |
897 |
< |
uint32 size; |
897 |
> |
uintptr size; |
898 |
|
|
899 |
|
if (mon_use_real_mem) { |
900 |
|
fprintf(monerr, "Cannot reallocate buffer in real mode\n"); |
902 |
|
} |
903 |
|
|
904 |
|
if (mon_token == T_END) { |
905 |
< |
fprintf(monerr, "Buffer size: %08lx bytes\n", mon_mem_size); |
905 |
> |
fprintf(monerr, "Buffer size: %08x bytes\n", mon_mem_size); |
906 |
|
return; |
907 |
|
} |
908 |
|
|
914 |
|
} |
915 |
|
|
916 |
|
if ((mem = (uint8 *)realloc(mem, size)) != NULL) |
917 |
< |
fprintf(monerr, "Buffer size: %08lx bytes\n", mon_mem_size = size); |
917 |
> |
fprintf(monerr, "Buffer size: %08x bytes\n", mon_mem_size = size); |
918 |
|
else |
919 |
|
fprintf(monerr, "Unable to reallocate buffer\n"); |
920 |
|
} |
927 |
|
|
928 |
|
static void apply(int size) |
929 |
|
{ |
930 |
< |
uint32 adr, end_adr, value; |
930 |
> |
uintptr adr, end_adr, value; |
931 |
|
char c; |
932 |
|
|
933 |
|
if (!mon_expression(&adr)) |
941 |
|
return; |
942 |
|
} |
943 |
|
|
944 |
< |
uint32 (*read_func)(uint32 adr); |
945 |
< |
void (*write_func)(uint32 adr, uint32 val); |
944 |
> |
uint32 (*read_func)(uintptr adr); |
945 |
> |
void (*write_func)(uintptr adr, uint32 val); |
946 |
|
switch (size) { |
947 |
|
case 1: |
948 |
|
read_func = mon_read_byte; |
956 |
|
read_func = mon_read_word; |
957 |
|
write_func = mon_write_word; |
958 |
|
break; |
959 |
+ |
default: |
960 |
+ |
abort(); |
961 |
+ |
break; |
962 |
|
} |
963 |
|
|
964 |
|
while (adr<=end_adr) { |
982 |
|
mon_dot_address = adr; |
983 |
|
} |
984 |
|
|
985 |
< |
static void apply_byte(void) |
985 |
> |
static void apply_byte() |
986 |
|
{ |
987 |
|
apply(1); |
988 |
|
} |
989 |
|
|
990 |
< |
static void apply_half(void) |
990 |
> |
static void apply_half() |
991 |
|
{ |
992 |
|
apply(2); |
993 |
|
} |
994 |
|
|
995 |
< |
static void apply_word(void) |
995 |
> |
static void apply_word() |
996 |
|
{ |
997 |
|
apply(4); |
998 |
|
} |
1002 |
|
* Execute command via system() (for ls, rm, etc.) |
1003 |
|
*/ |
1004 |
|
|
1005 |
< |
static void mon_exec(void) |
1005 |
> |
static void mon_exec() |
1006 |
|
{ |
1007 |
|
system(input); |
1008 |
|
} |
1012 |
|
* Change current directory |
1013 |
|
*/ |
1014 |
|
|
1015 |
< |
static void mon_change_dir(void) |
1015 |
> |
void mon_change_dir() |
1016 |
|
{ |
1028 |
– |
char c; |
1017 |
|
in_ptr = input; |
1018 |
< |
while ((c = get_char()) == ' ') ; |
1019 |
< |
while ((c = get_char()) != ' ') ; |
1020 |
< |
while ((c = get_char()) == ' ') ; |
1018 |
> |
char c = get_char(); |
1019 |
> |
while (isspace(c)) |
1020 |
> |
c = get_char(); |
1021 |
> |
while (isgraph(c)) |
1022 |
> |
c = get_char(); |
1023 |
> |
while (isspace(c)) |
1024 |
> |
c = get_char(); |
1025 |
|
put_back(c); |
1026 |
|
if (chdir(in_ptr) != 0) |
1027 |
|
mon_error("Cannot change directory"); |
1032 |
|
* Initialize mon |
1033 |
|
*/ |
1034 |
|
|
1035 |
< |
void mon_init(void) |
1035 |
> |
void mon_init() |
1036 |
|
{ |
1037 |
|
cmds = NULL; |
1038 |
|
num_cmds = 0; |
1039 |
|
cmd_help = NULL; |
1040 |
|
|
1041 |
< |
mon_add_command("??", mon_cmd_list, "?? Show list of commands\n"); |
1042 |
< |
mon_add_command("ver", version, "ver Show version\n"); |
1043 |
< |
mon_add_command("?", print_expr, "? expression Calculate expression\n"); |
1044 |
< |
mon_add_command("@", reallocate, "@ [size] Reallocate buffer\n"); |
1045 |
< |
mon_add_command("i", ascii_dump, "i [start [end]] ASCII memory dump\n"); |
1046 |
< |
mon_add_command("m", memory_dump, "m [start [end]] Hex/ASCII memory dump\n"); |
1047 |
< |
mon_add_command("d", disassemble_ppc, "d [start [end]] Disassemble PowerPC code\n"); |
1048 |
< |
mon_add_command("d65", disassemble_6502, "d65 [start [end]] Disassemble 6502 code\n"); |
1049 |
< |
mon_add_command("d68", disassemble_680x0, "d68 [start [end]] Disassemble 680x0 code\n"); |
1050 |
< |
mon_add_command("d80", disassemble_8080, "d80 [start [end]] Disassemble 8080 code\n"); |
1051 |
< |
mon_add_command("d86", disassemble_80x86, "d86 [start [end]] Disassemble 80x86 code\n"); |
1052 |
< |
mon_add_command(":", modify, ": start string Modify memory\n"); |
1053 |
< |
mon_add_command("f", fill, "f start end string Fill memory\n"); |
1054 |
< |
mon_add_command("y", apply_byte, "y[b|h|w] start end expr Apply expression to memory\n"); |
1041 |
> |
mon_add_command("??", mon_cmd_list, "?? Show list of commands\n"); |
1042 |
> |
mon_add_command("ver", version, "ver Show version\n"); |
1043 |
> |
mon_add_command("?", print_expr, "? expression Calculate expression\n"); |
1044 |
> |
mon_add_command("@", reallocate, "@ [size] Reallocate buffer\n"); |
1045 |
> |
mon_add_command("i", ascii_dump, "i [start [end]] ASCII memory dump\n"); |
1046 |
> |
mon_add_command("m", memory_dump, "m [start [end]] Hex/ASCII memory dump\n"); |
1047 |
> |
mon_add_command("b", binary_dump, "b [start [end]] Binary memory dump\n"); |
1048 |
> |
mon_add_command("d", disassemble_ppc, "d [start [end]] Disassemble PowerPC code\n"); |
1049 |
> |
mon_add_command("d65", disassemble_6502, "d65 [start [end]] Disassemble 6502 code\n"); |
1050 |
> |
mon_add_command("d68", disassemble_680x0, "d68 [start [end]] Disassemble 680x0 code\n"); |
1051 |
> |
mon_add_command("d80", disassemble_z80, "d80 [start [end]] Disassemble Z80 code\n"); |
1052 |
> |
mon_add_command("d86", disassemble_80x86_32, "d86 [start [end]] Disassemble 80x86 (32-bit) code\n"); |
1053 |
> |
mon_add_command("d8086", disassemble_80x86_16, "d8086 [start [end]] Disassemble 80x86 (16-bit) code\n"); |
1054 |
> |
mon_add_command("d8664", disassemble_x86_64, "d8664 [start [end]] Disassemble x86-64 code\n"); |
1055 |
> |
mon_add_command(":", modify, ": start string Modify memory\n"); |
1056 |
> |
mon_add_command("f", fill, "f start end string Fill memory\n"); |
1057 |
> |
mon_add_command("y", apply_byte, "y[b|h|w] start end expr Apply expression to memory\n"); |
1058 |
|
mon_add_command("yb", apply_byte, NULL); |
1059 |
|
mon_add_command("yh", apply_half, NULL); |
1060 |
|
mon_add_command("yw", apply_word, NULL); |
1061 |
< |
mon_add_command("t", transfer, "t start end dest Transfer memory\n"); |
1062 |
< |
mon_add_command("c", compare, "c start end dest Compare memory\n"); |
1063 |
< |
mon_add_command("h", help_or_hunt, "h start end string Search for byte string\n"); |
1064 |
< |
mon_add_command("\\", shell_command, "\\ \"command\" Execute shell command\n"); |
1065 |
< |
mon_add_command("ls", mon_exec, "ls [args] List directory contents\n"); |
1066 |
< |
mon_add_command("rm", mon_exec, "rm [args] Remove file(s)\n"); |
1067 |
< |
mon_add_command("cp", mon_exec, "cp [args] Copy file(s)\n"); |
1068 |
< |
mon_add_command("mv", mon_exec, "mv [args] Move file(s)\n"); |
1069 |
< |
mon_add_command("cd", mon_change_dir, "cd directory Change current directory\n"); |
1070 |
< |
mon_add_command("o", redir_output, "o [\"file\"] Redirect output\n"); |
1071 |
< |
mon_add_command("[", load_data, "[ start \"file\" Load data from file\n"); |
1072 |
< |
mon_add_command("]", save_data, "] start size \"file\" Save data to file\n"); |
1073 |
< |
mon_add_command("set", set_var, "set [var[=value]] Set/clear/show variables\n"); |
1074 |
< |
mon_add_command("cv", clear_vars, "cv Clear all variables\n"); |
1061 |
> |
mon_add_command("t", transfer, "t start end dest Transfer memory\n"); |
1062 |
> |
mon_add_command("c", compare, "c start end dest Compare memory\n"); |
1063 |
> |
mon_add_command("h", help_or_hunt, "h start end string Search for byte string\n"); |
1064 |
> |
mon_add_command("\\", shell_command, "\\ \"command\" Execute shell command\n"); |
1065 |
> |
mon_add_command("ls", mon_exec, "ls [args] List directory contents\n"); |
1066 |
> |
mon_add_command("rm", mon_exec, "rm [args] Remove file(s)\n"); |
1067 |
> |
mon_add_command("cp", mon_exec, "cp [args] Copy file(s)\n"); |
1068 |
> |
mon_add_command("mv", mon_exec, "mv [args] Move file(s)\n"); |
1069 |
> |
mon_add_command("cd", mon_change_dir, "cd directory Change current directory\n"); |
1070 |
> |
mon_add_command("o", redir_output, "o [\"file\"] Redirect output\n"); |
1071 |
> |
mon_add_command("[", load_data, "[ start \"file\" Load data from file\n"); |
1072 |
> |
mon_add_command("]", save_data, "] start size \"file\" Save data to file\n"); |
1073 |
> |
mon_add_command("set", set_var, "set [var[=value]] Set/clear/show variables\n"); |
1074 |
> |
mon_add_command("cv", clear_vars, "cv Clear all variables\n"); |
1075 |
> |
|
1076 |
> |
mon_read_byte = NULL; |
1077 |
> |
mon_write_byte = NULL; |
1078 |
> |
|
1079 |
> |
input = NULL; |
1080 |
> |
mon_string = NULL; |
1081 |
> |
mon_name = NULL; |
1082 |
|
} |
1083 |
|
|
1084 |
|
|
1086 |
|
* Deinitialize mon |
1087 |
|
*/ |
1088 |
|
|
1089 |
< |
void mon_exit(void) |
1089 |
> |
void mon_exit() |
1090 |
|
{ |
1091 |
< |
free(cmds); |
1092 |
< |
cmds = NULL; |
1091 |
> |
if (cmds) { |
1092 |
> |
free(cmds); |
1093 |
> |
cmds = NULL; |
1094 |
> |
} |
1095 |
|
num_cmds = 0; |
1096 |
|
cmd_help = NULL; |
1097 |
+ |
|
1098 |
+ |
if (input) { |
1099 |
+ |
free(input); |
1100 |
+ |
input = NULL; |
1101 |
+ |
} |
1102 |
+ |
if (mon_string) { |
1103 |
+ |
free(mon_string); |
1104 |
+ |
mon_string = NULL; |
1105 |
+ |
} |
1106 |
+ |
if (mon_name) { |
1107 |
+ |
free(mon_name); |
1108 |
+ |
mon_name = NULL; |
1109 |
+ |
} |
1110 |
|
} |
1111 |
|
|
1112 |
|
|
1117 |
|
void mon(int argc, char **argv) |
1118 |
|
{ |
1119 |
|
bool done = false, interactive = true; |
1103 |
– |
char c, cmd[INPUT_LENGTH]; |
1120 |
|
|
1121 |
|
// Setup input/output streams |
1122 |
|
monin = stdin; |
1123 |
|
monout = stdout; |
1124 |
|
monerr = stdout; |
1125 |
|
|
1126 |
< |
if (argc) { |
1127 |
< |
// Access real memory if mon was started as "rmon" |
1128 |
< |
char *prgname = argv[0]; |
1129 |
< |
char *lastslash; |
1130 |
< |
if ((lastslash = strrchr(prgname, '/')) != NULL) |
1131 |
< |
prgname = lastslash + 1; |
1132 |
< |
if (strcmp(prgname, "rmon") == 0) |
1126 |
> |
// Make argc/argv point to the actual arguments |
1127 |
> |
const char *prg_name = argv[0]; |
1128 |
> |
if (argc) |
1129 |
> |
argc--; argv++; |
1130 |
> |
|
1131 |
> |
// Parse arguments |
1132 |
> |
mon_macos_mode = false; |
1133 |
> |
mon_use_real_mem = false; |
1134 |
> |
while (argc > 0) { |
1135 |
> |
if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0) { |
1136 |
> |
printf("Usage: %s [-m] [-r] [command...]\n", prg_name); |
1137 |
> |
exit(0); |
1138 |
> |
} else if (strcmp(argv[0], "-m") == 0) |
1139 |
> |
mon_macos_mode = true; |
1140 |
> |
else if (strcmp(argv[0], "-r") == 0) |
1141 |
|
mon_use_real_mem = true; |
1142 |
+ |
else |
1143 |
+ |
break; |
1144 |
+ |
argc--; argv++; |
1145 |
+ |
} |
1146 |
+ |
interactive = (argc == 0); |
1147 |
|
|
1148 |
< |
// Make argc/argv point to the actual arguments |
1149 |
< |
argc--; |
1150 |
< |
argv++; |
1151 |
< |
interactive = (argc == 0); |
1148 |
> |
// Set up memory access functions if not supplied by the user |
1149 |
> |
if (mon_read_byte == NULL) { |
1150 |
> |
if (mon_use_real_mem) |
1151 |
> |
mon_read_byte = mon_read_byte_real; |
1152 |
> |
else |
1153 |
> |
mon_read_byte = mon_read_byte_buffer; |
1154 |
> |
} |
1155 |
> |
if (mon_write_byte == NULL) { |
1156 |
> |
if (mon_use_real_mem) |
1157 |
> |
mon_write_byte = mon_write_byte_real; |
1158 |
> |
else |
1159 |
> |
mon_write_byte = mon_write_byte_buffer; |
1160 |
|
} |
1161 |
|
|
1162 |
|
// Allocate buffer |
1166 |
|
|
1167 |
|
// Print banner |
1168 |
|
if (interactive) |
1169 |
< |
fprintf(monerr, "\n *** mon V%d.%d by Christian Bauer and Marc Hellwig ***\n" |
1170 |
< |
" *** Press 'h' for help ***\n\n", VERSION_MAJOR, VERSION_MINOR); |
1169 |
> |
fprintf(monerr, "\n *** cxmon V" VERSION " by Christian Bauer and Marc Hellwig ***\n" |
1170 |
> |
" *** Press 'h' for help ***\n\n"); |
1171 |
> |
} |
1172 |
> |
|
1173 |
> |
// Clear variables |
1174 |
> |
vars.clear(); |
1175 |
> |
|
1176 |
> |
// In MacOS mode, pull in the lowmem globals as variables |
1177 |
> |
if (mon_macos_mode) { |
1178 |
> |
const lowmem_info *l = lowmem; |
1179 |
> |
while (l->name) { |
1180 |
> |
vars[l->name] = l->addr; |
1181 |
> |
l++; |
1182 |
> |
} |
1183 |
|
} |
1184 |
|
|
1185 |
|
init_abort(); |
1186 |
|
|
1187 |
|
// Read and parse command line |
1188 |
+ |
char *cmd = NULL; |
1189 |
|
while (!done) { |
1190 |
|
if (interactive) { |
1191 |
|
char prompt[16]; |
1192 |
< |
sprintf(prompt, "[%08lx]-> ", mon_dot_address); |
1192 |
> |
sprintf(prompt, "[%0*lx]-> ", 2 * sizeof(mon_dot_address), mon_dot_address); |
1193 |
|
read_line(prompt); |
1194 |
+ |
if (!input) { |
1195 |
+ |
done = true; |
1196 |
+ |
continue; |
1197 |
+ |
} |
1198 |
|
} else { |
1199 |
|
if (argc == 0) { |
1200 |
|
done = true; |
1201 |
|
break; |
1202 |
|
} else { |
1203 |
< |
strncpy(in_ptr = input, argv[0], INPUT_LENGTH); |
1203 |
> |
unsigned n = strlen(argv[0]) + 1; |
1204 |
> |
input = (char *)realloc(input, n); |
1205 |
> |
strcpy(in_ptr = input, argv[0]); |
1206 |
|
argc--; |
1207 |
|
argv++; |
1208 |
|
} |
1209 |
|
} |
1210 |
|
|
1211 |
|
// Skip leading spaces |
1212 |
< |
while ((c = get_char()) == ' ') ; |
1212 |
> |
char c = get_char(); |
1213 |
> |
while (isspace(c)) |
1214 |
> |
c = get_char(); |
1215 |
> |
put_back(c); |
1216 |
> |
if (!c) |
1217 |
> |
continue; // blank line |
1218 |
|
|
1219 |
|
// Read command word |
1220 |
< |
char *p = cmd; |
1221 |
< |
do { |
1161 |
< |
*p++ = c; |
1220 |
> |
char *p = in_ptr; |
1221 |
> |
while (isgraph(c)) |
1222 |
|
c = get_char(); |
1163 |
– |
} while (isgraph(c)); |
1164 |
– |
*p = 0; |
1223 |
|
put_back(c); |
1224 |
+ |
unsigned n = in_ptr - p; |
1225 |
+ |
cmd = (char *)realloc(cmd, n + 1); |
1226 |
+ |
memcpy(cmd, p, n); |
1227 |
+ |
cmd[n] = 0; |
1228 |
|
|
1229 |
|
// Execute command |
1168 |
– |
if (cmd[0] == 0) // Blank line |
1169 |
– |
continue; |
1230 |
|
if (strcmp(cmd, "x") == 0) { // Exit |
1231 |
|
done = true; |
1232 |
|
continue; |
1242 |
|
cmd_done: ; |
1243 |
|
} |
1244 |
|
|
1245 |
+ |
if (cmd) |
1246 |
+ |
free(cmd); |
1247 |
+ |
|
1248 |
|
exit_abort(); |
1249 |
|
|
1250 |
|
// Free buffer |