1 |
|
/* |
2 |
< |
* mon.cpp - mon main program |
2 |
> |
* mon.cpp - cxmon main program |
3 |
|
* |
4 |
< |
* mon (C) 1997-2000 Christian Bauer, Marc Hellwig |
4 |
> |
* cxmon (C) 1997-2002 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 |
24 |
|
#include <stdlib.h> |
25 |
|
#include <signal.h> |
26 |
|
#include <ctype.h> |
27 |
+ |
#include <string> |
28 |
+ |
#include <map> |
29 |
|
|
30 |
< |
#ifdef HAVE_READLINE_READLINE_H |
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 "mon_lowmem.h" |
53 |
+ |
|
54 |
+ |
#ifndef VERSION |
55 |
+ |
#define VERSION "2" |
56 |
+ |
#endif |
57 |
|
|
58 |
|
|
59 |
|
// Buffer we're operating on |
96 |
|
|
97 |
|
|
98 |
|
// List of variables |
99 |
< |
struct Variable { |
100 |
< |
Variable *next; // Pointer to next variable (must be first element of struct) |
86 |
< |
char *name; // Variable name |
87 |
< |
uint32 value; // Variable value |
88 |
< |
}; |
89 |
< |
|
90 |
< |
static Variable *first_var; // Pointer to first variable |
99 |
> |
typedef map<string, uint32> var_map; |
100 |
> |
static var_map vars; |
101 |
|
|
102 |
|
|
103 |
|
// Prototypes |
119 |
|
static bool add_expr(uint32 *number); |
120 |
|
static bool mul_expr(uint32 *number); |
121 |
|
static bool factor(uint32 *number); |
112 |
– |
static Variable *lookup_var(const char *s); |
113 |
– |
static Variable *insert_var(const char *s); |
114 |
– |
static void remove_var(const char *s); |
122 |
|
|
123 |
|
|
124 |
|
/* |
394 |
|
return T_NULL; |
395 |
|
|
396 |
|
do { |
397 |
+ |
c = tolower(c); |
398 |
|
if (c < 'a') |
399 |
|
i = (i << 4) + (c - '0'); |
400 |
|
else |
703 |
|
return true; |
704 |
|
|
705 |
|
case T_NAME:{ |
706 |
< |
Variable *var; |
707 |
< |
if ((var = lookup_var(mon_name)) != NULL) { |
708 |
< |
*number = var->value; |
706 |
> |
var_map::const_iterator v = vars.find(mon_name); |
707 |
> |
if (v == vars.end()) |
708 |
> |
return false; |
709 |
> |
else { |
710 |
> |
*number = v->second; |
711 |
|
mon_get_token(); |
712 |
|
return true; |
713 |
< |
} else |
704 |
< |
return false; |
713 |
> |
} |
714 |
|
} |
715 |
|
|
716 |
|
case T_DOT: |
770 |
|
|
771 |
|
|
772 |
|
/* |
764 |
– |
* Lookup the value of a variable |
765 |
– |
*/ |
766 |
– |
|
767 |
– |
static Variable *lookup_var(const char *s) |
768 |
– |
{ |
769 |
– |
// Lookup variable |
770 |
– |
for (Variable *var=first_var; var; var=var->next) |
771 |
– |
if (!strcmp(s, var->name)) |
772 |
– |
return var; |
773 |
– |
|
774 |
– |
// Not found, error |
775 |
– |
mon_error("Undefined variable"); |
776 |
– |
return NULL; |
777 |
– |
} |
778 |
– |
|
779 |
– |
|
780 |
– |
/* |
781 |
– |
* Insert new variable (or redefine old) |
782 |
– |
*/ |
783 |
– |
|
784 |
– |
static Variable *insert_var(const char *s) |
785 |
– |
{ |
786 |
– |
// Lookup variable |
787 |
– |
for (Variable *var=first_var; var; var=var->next) |
788 |
– |
if (!strcmp(s, var->name)) |
789 |
– |
return var; |
790 |
– |
|
791 |
– |
// Insert new variable |
792 |
– |
Variable *var = new Variable; |
793 |
– |
var->name = strdup(s); |
794 |
– |
var->next = first_var; |
795 |
– |
first_var = var; |
796 |
– |
return var; |
797 |
– |
} |
798 |
– |
|
799 |
– |
|
800 |
– |
/* |
801 |
– |
* Remove variable |
802 |
– |
*/ |
803 |
– |
|
804 |
– |
static void remove_var(const char *s) |
805 |
– |
{ |
806 |
– |
Variable *var, *prev = (Variable *)&first_var; |
807 |
– |
|
808 |
– |
// Lookup variable and remove it |
809 |
– |
for (var=prev->next; var; prev=var, var=var->next) |
810 |
– |
if (!strcmp(s, var->name)) { |
811 |
– |
prev->next = var->next; |
812 |
– |
free(var->name); |
813 |
– |
free(var); |
814 |
– |
return; |
815 |
– |
} |
816 |
– |
} |
817 |
– |
|
818 |
– |
|
819 |
– |
/* |
773 |
|
* Set/clear/show variables |
774 |
|
* set [var[=value]] |
775 |
|
*/ |
779 |
|
if (mon_token == T_END) { |
780 |
|
|
781 |
|
// Show all variables |
782 |
< |
if (first_var == NULL) |
782 |
> |
if (vars.empty()) |
783 |
|
fprintf(monout, "No variables defined\n"); |
784 |
< |
else |
785 |
< |
for (Variable *v=first_var; v; v=v->next) |
786 |
< |
fprintf(monout, "%s = %08x\n", v->name, v->value); |
784 |
> |
else { |
785 |
> |
var_map::const_iterator v = vars.begin(), end = vars.end(); |
786 |
> |
for (v=vars.begin(); v!=end; ++v) |
787 |
> |
fprintf(monout, "%s = %08x\n", v->first.c_str(), v->second); |
788 |
> |
} |
789 |
|
|
790 |
|
} else if (mon_token == T_NAME) { |
791 |
< |
char var_name[256]; |
837 |
< |
strcpy(var_name, mon_name); |
791 |
> |
string var_name = mon_name; |
792 |
|
mon_get_token(); |
793 |
|
if (mon_token == T_ASSIGN) { |
794 |
|
|
801 |
|
mon_error("Too many arguments"); |
802 |
|
return; |
803 |
|
} |
804 |
< |
insert_var(var_name)->value = value; |
804 |
> |
vars[var_name] = value; |
805 |
|
|
806 |
|
} else if (mon_token == T_END) { |
807 |
|
|
808 |
|
// Clear variable |
809 |
< |
remove_var(var_name); |
809 |
> |
vars.erase(var_name); |
810 |
|
|
811 |
|
} else |
812 |
|
mon_error("'=' expected"); |
822 |
|
|
823 |
|
static void clear_vars(void) |
824 |
|
{ |
825 |
< |
Variable *var, *next; |
872 |
< |
for (var=first_var; var; var=next) { |
873 |
< |
free(var->name); |
874 |
< |
next = var->next; |
875 |
< |
free(var); |
876 |
< |
} |
877 |
< |
first_var = NULL; |
825 |
> |
vars.clear(); |
826 |
|
} |
827 |
|
|
828 |
|
|
1004 |
|
num_cmds = 0; |
1005 |
|
cmd_help = NULL; |
1006 |
|
|
1007 |
< |
mon_add_command("??", mon_cmd_list, "?? Show list of commands\n"); |
1008 |
< |
mon_add_command("ver", version, "ver Show version\n"); |
1009 |
< |
mon_add_command("?", print_expr, "? expression Calculate expression\n"); |
1010 |
< |
mon_add_command("@", reallocate, "@ [size] Reallocate buffer\n"); |
1011 |
< |
mon_add_command("i", ascii_dump, "i [start [end]] ASCII memory dump\n"); |
1012 |
< |
mon_add_command("m", memory_dump, "m [start [end]] Hex/ASCII memory dump\n"); |
1013 |
< |
mon_add_command("b", binary_dump, "b [start [end]] Binary memory dump\n"); |
1014 |
< |
mon_add_command("d", disassemble_ppc, "d [start [end]] Disassemble PowerPC code\n"); |
1015 |
< |
mon_add_command("d65", disassemble_6502, "d65 [start [end]] Disassemble 6502 code\n"); |
1016 |
< |
mon_add_command("d68", disassemble_680x0, "d68 [start [end]] Disassemble 680x0 code\n"); |
1017 |
< |
mon_add_command("d80", disassemble_8080, "d80 [start [end]] Disassemble 8080 code\n"); |
1018 |
< |
mon_add_command("d86", disassemble_80x86, "d86 [start [end]] Disassemble 80x86 code\n"); |
1019 |
< |
mon_add_command(":", modify, ": start string Modify memory\n"); |
1020 |
< |
mon_add_command("f", fill, "f start end string Fill memory\n"); |
1021 |
< |
mon_add_command("y", apply_byte, "y[b|h|w] start end expr Apply expression to memory\n"); |
1007 |
> |
mon_add_command("??", mon_cmd_list, "?? Show list of commands\n"); |
1008 |
> |
mon_add_command("ver", version, "ver Show version\n"); |
1009 |
> |
mon_add_command("?", print_expr, "? expression Calculate expression\n"); |
1010 |
> |
mon_add_command("@", reallocate, "@ [size] Reallocate buffer\n"); |
1011 |
> |
mon_add_command("i", ascii_dump, "i [start [end]] ASCII memory dump\n"); |
1012 |
> |
mon_add_command("m", memory_dump, "m [start [end]] Hex/ASCII memory dump\n"); |
1013 |
> |
mon_add_command("b", binary_dump, "b [start [end]] Binary memory dump\n"); |
1014 |
> |
mon_add_command("d", disassemble_ppc, "d [start [end]] Disassemble PowerPC code\n"); |
1015 |
> |
mon_add_command("d65", disassemble_6502, "d65 [start [end]] Disassemble 6502 code\n"); |
1016 |
> |
mon_add_command("d68", disassemble_680x0, "d68 [start [end]] Disassemble 680x0 code\n"); |
1017 |
> |
mon_add_command("d80", disassemble_z80, "d80 [start [end]] Disassemble Z80 code\n"); |
1018 |
> |
mon_add_command("d86", disassemble_80x86_32, "d86 [start [end]] Disassemble 80x86 (32-bit) code\n"); |
1019 |
> |
mon_add_command("d8086", disassemble_80x86_16, "d8086 [start [end]] Disassemble 80x86 (16-bit) code\n"); |
1020 |
> |
mon_add_command(":", modify, ": start string Modify memory\n"); |
1021 |
> |
mon_add_command("f", fill, "f start end string Fill memory\n"); |
1022 |
> |
mon_add_command("y", apply_byte, "y[b|h|w] start end expr Apply expression to memory\n"); |
1023 |
|
mon_add_command("yb", apply_byte, NULL); |
1024 |
|
mon_add_command("yh", apply_half, NULL); |
1025 |
|
mon_add_command("yw", apply_word, NULL); |
1026 |
< |
mon_add_command("t", transfer, "t start end dest Transfer memory\n"); |
1027 |
< |
mon_add_command("c", compare, "c start end dest Compare memory\n"); |
1028 |
< |
mon_add_command("h", help_or_hunt, "h start end string Search for byte string\n"); |
1029 |
< |
mon_add_command("\\", shell_command, "\\ \"command\" Execute shell command\n"); |
1030 |
< |
mon_add_command("ls", mon_exec, "ls [args] List directory contents\n"); |
1031 |
< |
mon_add_command("rm", mon_exec, "rm [args] Remove file(s)\n"); |
1032 |
< |
mon_add_command("cp", mon_exec, "cp [args] Copy file(s)\n"); |
1033 |
< |
mon_add_command("mv", mon_exec, "mv [args] Move file(s)\n"); |
1034 |
< |
mon_add_command("cd", mon_change_dir, "cd directory Change current directory\n"); |
1035 |
< |
mon_add_command("o", redir_output, "o [\"file\"] Redirect output\n"); |
1036 |
< |
mon_add_command("[", load_data, "[ start \"file\" Load data from file\n"); |
1037 |
< |
mon_add_command("]", save_data, "] start size \"file\" Save data to file\n"); |
1038 |
< |
mon_add_command("set", set_var, "set [var[=value]] Set/clear/show variables\n"); |
1039 |
< |
mon_add_command("cv", clear_vars, "cv Clear all variables\n"); |
1026 |
> |
mon_add_command("t", transfer, "t start end dest Transfer memory\n"); |
1027 |
> |
mon_add_command("c", compare, "c start end dest Compare memory\n"); |
1028 |
> |
mon_add_command("h", help_or_hunt, "h start end string Search for byte string\n"); |
1029 |
> |
mon_add_command("\\", shell_command, "\\ \"command\" Execute shell command\n"); |
1030 |
> |
mon_add_command("ls", mon_exec, "ls [args] List directory contents\n"); |
1031 |
> |
mon_add_command("rm", mon_exec, "rm [args] Remove file(s)\n"); |
1032 |
> |
mon_add_command("cp", mon_exec, "cp [args] Copy file(s)\n"); |
1033 |
> |
mon_add_command("mv", mon_exec, "mv [args] Move file(s)\n"); |
1034 |
> |
mon_add_command("cd", mon_change_dir, "cd directory Change current directory\n"); |
1035 |
> |
mon_add_command("o", redir_output, "o [\"file\"] Redirect output\n"); |
1036 |
> |
mon_add_command("[", load_data, "[ start \"file\" Load data from file\n"); |
1037 |
> |
mon_add_command("]", save_data, "] start size \"file\" Save data to file\n"); |
1038 |
> |
mon_add_command("set", set_var, "set [var[=value]] Set/clear/show variables\n"); |
1039 |
> |
mon_add_command("cv", clear_vars, "cv Clear all variables\n"); |
1040 |
|
|
1041 |
|
mon_read_byte = NULL; |
1042 |
|
mon_write_byte = NULL; |
1070 |
|
monout = stdout; |
1071 |
|
monerr = stdout; |
1072 |
|
|
1073 |
< |
if (argc) { |
1074 |
< |
// Access real memory if mon was started as "rmon" |
1075 |
< |
char *prgname = argv[0]; |
1076 |
< |
char *lastslash; |
1077 |
< |
if ((lastslash = strrchr(prgname, '/')) != NULL) |
1078 |
< |
prgname = lastslash + 1; |
1079 |
< |
if (strcmp(prgname, "rmon") == 0) |
1073 |
> |
// Make argc/argv point to the actual arguments |
1074 |
> |
const char *prg_name = argv[0]; |
1075 |
> |
if (argc) |
1076 |
> |
argc--; argv++; |
1077 |
> |
|
1078 |
> |
// Parse arguments |
1079 |
> |
mon_macos_mode = false; |
1080 |
> |
mon_use_real_mem = false; |
1081 |
> |
while (argc > 0) { |
1082 |
> |
if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0) { |
1083 |
> |
printf("Usage: %s [-m] [-r] [command...]\n", prg_name); |
1084 |
> |
exit(0); |
1085 |
> |
} else if (strcmp(argv[0], "-m") == 0) |
1086 |
> |
mon_macos_mode = true; |
1087 |
> |
else if (strcmp(argv[0], "-r") == 0) |
1088 |
|
mon_use_real_mem = true; |
1089 |
< |
|
1090 |
< |
// Make argc/argv point to the actual arguments |
1091 |
< |
argc--; |
1135 |
< |
argv++; |
1136 |
< |
interactive = (argc == 0); |
1089 |
> |
else |
1090 |
> |
break; |
1091 |
> |
argc--; argv++; |
1092 |
|
} |
1093 |
+ |
interactive = (argc == 0); |
1094 |
|
|
1095 |
|
// Set up memory access functions if not supplied by the user |
1096 |
|
if (mon_read_byte == NULL) { |
1113 |
|
|
1114 |
|
// Print banner |
1115 |
|
if (interactive) |
1116 |
< |
fprintf(monerr, "\n *** mon V" VERSION " by Christian Bauer and Marc Hellwig ***\n" |
1116 |
> |
fprintf(monerr, "\n *** cxmon V" VERSION " by Christian Bauer and Marc Hellwig ***\n" |
1117 |
|
" *** Press 'h' for help ***\n\n"); |
1118 |
|
} |
1119 |
|
|
1120 |
+ |
// Clear variables |
1121 |
+ |
vars.clear(); |
1122 |
+ |
|
1123 |
+ |
// In MacOS mode, pull in the lowmem globals as variables |
1124 |
+ |
if (mon_macos_mode) { |
1125 |
+ |
const lowmem_info *l = lowmem; |
1126 |
+ |
while (l->name) { |
1127 |
+ |
vars[l->name] = l->addr; |
1128 |
+ |
l++; |
1129 |
+ |
} |
1130 |
+ |
} |
1131 |
+ |
|
1132 |
|
init_abort(); |
1133 |
|
|
1134 |
|
// Read and parse command line |