1 |
|
/* |
2 |
< |
* mon.cpp - Machine language monitor |
2 |
> |
* mon.cpp - cxmon main program |
3 |
|
* |
4 |
< |
* (C) 1997-1999 Christian Bauer |
4 |
> |
* cxmon (C) 1997-2000 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 |
8 |
> |
* the Free Software Foundation; either version 2 of the License, or |
9 |
> |
* (at your option) any later version. |
10 |
> |
* |
11 |
> |
* This program is distributed in the hope that it will be useful, |
12 |
> |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
> |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
> |
* GNU General Public License for more details. |
15 |
> |
* |
16 |
> |
* You should have received a copy of the GNU General Public License |
17 |
> |
* along with this program; if not, write to the Free Software |
18 |
> |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
*/ |
20 |
|
|
21 |
< |
#include <sys/types.h> |
22 |
< |
#include <netinet/in.h> |
21 |
> |
#include "sysdeps.h" |
22 |
> |
|
23 |
|
#include <stdio.h> |
24 |
|
#include <stdlib.h> |
11 |
– |
#include <string.h> |
25 |
|
#include <signal.h> |
26 |
|
#include <ctype.h> |
14 |
– |
#include <unistd.h> |
15 |
– |
#include <errno.h> |
27 |
|
|
28 |
+ |
#if defined(HAVE_READLINE_H) |
29 |
+ |
extern "C" { |
30 |
+ |
#include <readline.h> |
31 |
+ |
} |
32 |
+ |
#elif defined(HAVE_READLINE_READLINE_H) |
33 |
|
extern "C" { |
34 |
< |
extern char *readline(char *prompt); |
19 |
< |
extern void add_history(char *str); |
34 |
> |
#include <readline/readline.h> |
35 |
|
} |
36 |
+ |
#endif |
37 |
+ |
|
38 |
+ |
#if defined(HAVE_HISTORY_H) |
39 |
+ |
extern "C" { |
40 |
+ |
#include <history.h> |
41 |
+ |
} |
42 |
+ |
#elif defined(HAVE_READLINE_HISTORY_H) |
43 |
+ |
extern "C" { |
44 |
+ |
#include <readline/history.h> |
45 |
+ |
} |
46 |
+ |
#endif |
47 |
|
|
48 |
|
#include "mon.h" |
49 |
|
#include "mon_cmd.h" |
50 |
|
|
51 |
+ |
#ifndef VERSION |
52 |
+ |
#define VERSION "2" |
53 |
+ |
#endif |
54 |
+ |
|
55 |
|
|
56 |
|
// Buffer we're operating on |
57 |
|
bool mon_use_real_mem = false; |
207 |
|
* Access to buffer |
208 |
|
*/ |
209 |
|
|
210 |
< |
uint32 mon_read_byte(uint32 adr) |
210 |
> |
uint32 (*mon_read_byte)(uint32 adr); |
211 |
> |
|
212 |
> |
uint32 mon_read_byte_buffer(uint32 adr) |
213 |
|
{ |
214 |
< |
if (mon_use_real_mem) |
183 |
< |
return *(uint8 *)adr; |
184 |
< |
else |
185 |
< |
return mem[adr % mon_mem_size]; |
214 |
> |
return mem[adr % mon_mem_size]; |
215 |
|
} |
216 |
|
|
217 |
< |
void mon_write_byte(uint32 adr, uint32 b) |
217 |
> |
uint32 mon_read_byte_real(uint32 adr) |
218 |
|
{ |
219 |
< |
if (mon_use_real_mem) |
220 |
< |
*(uint8 *)adr = b; |
221 |
< |
else |
222 |
< |
mem[adr % mon_mem_size] = b; |
219 |
> |
return *(uint8 *)adr; |
220 |
> |
} |
221 |
> |
|
222 |
> |
void (*mon_write_byte)(uint32 adr, uint32 b); |
223 |
> |
|
224 |
> |
void mon_write_byte_buffer(uint32 adr, uint32 b) |
225 |
> |
{ |
226 |
> |
mem[adr % mon_mem_size] = b; |
227 |
> |
} |
228 |
> |
|
229 |
> |
void mon_write_byte_real(uint32 adr, uint32 b) |
230 |
> |
{ |
231 |
> |
*(uint8 *)adr = b; |
232 |
|
} |
233 |
|
|
234 |
|
uint32 mon_read_half(uint32 adr) |
235 |
|
{ |
236 |
< |
if (mon_use_real_mem) |
199 |
< |
return ntohs(*(uint16 *)adr); |
200 |
< |
else |
201 |
< |
return mem[adr % mon_mem_size] << 8 | mem[(adr+1) % mon_mem_size]; |
236 |
> |
return (mon_read_byte(adr) << 8) | mon_read_byte(adr+1); |
237 |
|
} |
238 |
|
|
239 |
|
void mon_write_half(uint32 adr, uint32 w) |
240 |
|
{ |
241 |
< |
if (mon_use_real_mem) |
242 |
< |
*(uint16 *)adr = htons(w); |
208 |
< |
else { |
209 |
< |
mem[adr % mon_mem_size] = w >> 8; |
210 |
< |
mem[(adr+1) % mon_mem_size] = w; |
211 |
< |
} |
241 |
> |
mon_write_byte(adr, w >> 8); |
242 |
> |
mon_write_byte(adr+1, w); |
243 |
|
} |
244 |
|
|
245 |
|
uint32 mon_read_word(uint32 adr) |
246 |
|
{ |
247 |
< |
if (mon_use_real_mem) |
217 |
< |
return ntohl(*(uint32 *)adr); |
218 |
< |
else |
219 |
< |
return mon_read_byte(adr) << 24 | mon_read_byte(adr+1) << 16 | mon_read_byte(adr+2) << 8 | mon_read_byte(adr+3); |
247 |
> |
return (mon_read_byte(adr) << 24) | (mon_read_byte(adr+1) << 16) | (mon_read_byte(adr+2) << 8) | mon_read_byte(adr+3); |
248 |
|
} |
249 |
|
|
250 |
|
void mon_write_word(uint32 adr, uint32 l) |
251 |
|
{ |
252 |
< |
if (mon_use_real_mem) |
253 |
< |
*(uint32 *)adr = htonl(l); |
254 |
< |
else { |
255 |
< |
mem[adr % mon_mem_size] = l >> 24; |
228 |
< |
mem[(adr+1) % mon_mem_size] = l >> 16; |
229 |
< |
mem[(adr+2) % mon_mem_size] = l >> 8; |
230 |
< |
mem[(adr+3) % mon_mem_size] = l; |
231 |
< |
} |
252 |
> |
mon_write_byte(adr, l >> 24); |
253 |
> |
mon_write_byte(adr+1, l >> 16); |
254 |
> |
mon_write_byte(adr+2, l >> 8); |
255 |
> |
mon_write_byte(adr+3, l); |
256 |
|
} |
257 |
|
|
258 |
|
|
262 |
|
|
263 |
|
static void read_line(char *prompt) |
264 |
|
{ |
265 |
+ |
#ifdef HAVE_LIBREADLINE |
266 |
|
static char *line_read = NULL; |
267 |
|
|
268 |
|
if (line_read) { |
277 |
|
|
278 |
|
strncpy(in_ptr = input, line_read, INPUT_LENGTH); |
279 |
|
input[INPUT_LENGTH-1] = 0; |
280 |
+ |
#else |
281 |
+ |
fprintf(monout, prompt); |
282 |
+ |
fflush(monout); |
283 |
+ |
fgets(in_ptr = input, INPUT_LENGTH, monin); |
284 |
+ |
char *s = strchr(input, '\n'); |
285 |
+ |
if (s != NULL) |
286 |
+ |
*s = 0; |
287 |
+ |
#endif |
288 |
|
} |
289 |
|
|
290 |
|
|
842 |
|
fprintf(monout, "No variables defined\n"); |
843 |
|
else |
844 |
|
for (Variable *v=first_var; v; v=v->next) |
845 |
< |
fprintf(monout, "%s = %08lx\n", v->name, v->value); |
845 |
> |
fprintf(monout, "%s = %08x\n", v->name, v->value); |
846 |
|
|
847 |
|
} else if (mon_token == T_NAME) { |
848 |
|
char var_name[256]; |
935 |
|
} |
936 |
|
|
937 |
|
if (mon_token == T_END) { |
938 |
< |
fprintf(monerr, "Buffer size: %08lx bytes\n", mon_mem_size); |
938 |
> |
fprintf(monerr, "Buffer size: %08x bytes\n", mon_mem_size); |
939 |
|
return; |
940 |
|
} |
941 |
|
|
947 |
|
} |
948 |
|
|
949 |
|
if ((mem = (uint8 *)realloc(mem, size)) != NULL) |
950 |
< |
fprintf(monerr, "Buffer size: %08lx bytes\n", mon_mem_size = size); |
950 |
> |
fprintf(monerr, "Buffer size: %08x bytes\n", mon_mem_size = size); |
951 |
|
else |
952 |
|
fprintf(monerr, "Unable to reallocate buffer\n"); |
953 |
|
} |
989 |
|
read_func = mon_read_word; |
990 |
|
write_func = mon_write_word; |
991 |
|
break; |
992 |
+ |
default: |
993 |
+ |
abort(); |
994 |
+ |
break; |
995 |
|
} |
996 |
|
|
997 |
|
while (adr<=end_adr) { |
1068 |
|
num_cmds = 0; |
1069 |
|
cmd_help = NULL; |
1070 |
|
|
1071 |
< |
mon_add_command("??", mon_cmd_list, "?? Show list of commands\n"); |
1072 |
< |
mon_add_command("ver", version, "ver Show version\n"); |
1073 |
< |
mon_add_command("?", print_expr, "? expression Calculate expression\n"); |
1074 |
< |
mon_add_command("@", reallocate, "@ [size] Reallocate buffer\n"); |
1075 |
< |
mon_add_command("i", ascii_dump, "i [start [end]] ASCII memory dump\n"); |
1076 |
< |
mon_add_command("m", memory_dump, "m [start [end]] Hex/ASCII memory dump\n"); |
1077 |
< |
mon_add_command("d", disassemble_ppc, "d [start [end]] Disassemble PowerPC code\n"); |
1078 |
< |
mon_add_command("d65", disassemble_6502, "d65 [start [end]] Disassemble 6502 code\n"); |
1079 |
< |
mon_add_command("d68", disassemble_680x0, "d68 [start [end]] Disassemble 680x0 code\n"); |
1080 |
< |
mon_add_command("d80", disassemble_8080, "d80 [start [end]] Disassemble 8080 code\n"); |
1081 |
< |
mon_add_command("d86", disassemble_80x86, "d86 [start [end]] Disassemble 80x86 code\n"); |
1082 |
< |
mon_add_command(":", modify, ": start string Modify memory\n"); |
1083 |
< |
mon_add_command("f", fill, "f start end string Fill memory\n"); |
1084 |
< |
mon_add_command("y", apply_byte, "y[b|h|w] start end expr Apply expression to memory\n"); |
1071 |
> |
mon_add_command("??", mon_cmd_list, "?? Show list of commands\n"); |
1072 |
> |
mon_add_command("ver", version, "ver Show version\n"); |
1073 |
> |
mon_add_command("?", print_expr, "? expression Calculate expression\n"); |
1074 |
> |
mon_add_command("@", reallocate, "@ [size] Reallocate buffer\n"); |
1075 |
> |
mon_add_command("i", ascii_dump, "i [start [end]] ASCII memory dump\n"); |
1076 |
> |
mon_add_command("m", memory_dump, "m [start [end]] Hex/ASCII memory dump\n"); |
1077 |
> |
mon_add_command("b", binary_dump, "b [start [end]] Binary memory dump\n"); |
1078 |
> |
mon_add_command("d", disassemble_ppc, "d [start [end]] Disassemble PowerPC code\n"); |
1079 |
> |
mon_add_command("d65", disassemble_6502, "d65 [start [end]] Disassemble 6502 code\n"); |
1080 |
> |
mon_add_command("d68", disassemble_680x0, "d68 [start [end]] Disassemble 680x0 code\n"); |
1081 |
> |
mon_add_command("d80", disassemble_z80, "d80 [start [end]] Disassemble Z80 code\n"); |
1082 |
> |
mon_add_command("d86", disassemble_80x86_32, "d86 [start [end]] Disassemble 80x86 (32-bit) code\n"); |
1083 |
> |
mon_add_command("d8086", disassemble_80x86_16, "d8086 [start [end]] Disassemble 80x86 (16-bit) code\n"); |
1084 |
> |
mon_add_command(":", modify, ": start string Modify memory\n"); |
1085 |
> |
mon_add_command("f", fill, "f start end string Fill memory\n"); |
1086 |
> |
mon_add_command("y", apply_byte, "y[b|h|w] start end expr Apply expression to memory\n"); |
1087 |
|
mon_add_command("yb", apply_byte, NULL); |
1088 |
|
mon_add_command("yh", apply_half, NULL); |
1089 |
|
mon_add_command("yw", apply_word, NULL); |
1090 |
< |
mon_add_command("t", transfer, "t start end dest Transfer memory\n"); |
1091 |
< |
mon_add_command("c", compare, "c start end dest Compare memory\n"); |
1092 |
< |
mon_add_command("h", help_or_hunt, "h start end string Search for byte string\n"); |
1093 |
< |
mon_add_command("\\", shell_command, "\\ \"command\" Execute shell command\n"); |
1094 |
< |
mon_add_command("ls", mon_exec, "ls [args] List directory contents\n"); |
1095 |
< |
mon_add_command("rm", mon_exec, "rm [args] Remove file(s)\n"); |
1096 |
< |
mon_add_command("cp", mon_exec, "cp [args] Copy file(s)\n"); |
1097 |
< |
mon_add_command("mv", mon_exec, "mv [args] Move file(s)\n"); |
1098 |
< |
mon_add_command("cd", mon_change_dir, "cd directory Change current directory\n"); |
1099 |
< |
mon_add_command("o", redir_output, "o [\"file\"] Redirect output\n"); |
1100 |
< |
mon_add_command("[", load_data, "[ start \"file\" Load data from file\n"); |
1101 |
< |
mon_add_command("]", save_data, "] start size \"file\" Save data to file\n"); |
1102 |
< |
mon_add_command("set", set_var, "set [var[=value]] Set/clear/show variables\n"); |
1103 |
< |
mon_add_command("cv", clear_vars, "cv Clear all variables\n"); |
1090 |
> |
mon_add_command("t", transfer, "t start end dest Transfer memory\n"); |
1091 |
> |
mon_add_command("c", compare, "c start end dest Compare memory\n"); |
1092 |
> |
mon_add_command("h", help_or_hunt, "h start end string Search for byte string\n"); |
1093 |
> |
mon_add_command("\\", shell_command, "\\ \"command\" Execute shell command\n"); |
1094 |
> |
mon_add_command("ls", mon_exec, "ls [args] List directory contents\n"); |
1095 |
> |
mon_add_command("rm", mon_exec, "rm [args] Remove file(s)\n"); |
1096 |
> |
mon_add_command("cp", mon_exec, "cp [args] Copy file(s)\n"); |
1097 |
> |
mon_add_command("mv", mon_exec, "mv [args] Move file(s)\n"); |
1098 |
> |
mon_add_command("cd", mon_change_dir, "cd directory Change current directory\n"); |
1099 |
> |
mon_add_command("o", redir_output, "o [\"file\"] Redirect output\n"); |
1100 |
> |
mon_add_command("[", load_data, "[ start \"file\" Load data from file\n"); |
1101 |
> |
mon_add_command("]", save_data, "] start size \"file\" Save data to file\n"); |
1102 |
> |
mon_add_command("set", set_var, "set [var[=value]] Set/clear/show variables\n"); |
1103 |
> |
mon_add_command("cv", clear_vars, "cv Clear all variables\n"); |
1104 |
> |
|
1105 |
> |
mon_read_byte = NULL; |
1106 |
> |
mon_write_byte = NULL; |
1107 |
|
} |
1108 |
|
|
1109 |
|
|
1134 |
|
monout = stdout; |
1135 |
|
monerr = stdout; |
1136 |
|
|
1137 |
< |
if (argc) { |
1138 |
< |
// Access real memory if mon was started as "rmon" |
1139 |
< |
char *prgname = argv[0]; |
1140 |
< |
char *lastslash; |
1141 |
< |
if ((lastslash = strrchr(prgname, '/')) != NULL) |
1142 |
< |
prgname = lastslash + 1; |
1143 |
< |
if (strcmp(prgname, "rmon") == 0) |
1137 |
> |
// Make argc/argv point to the actual arguments |
1138 |
> |
if (argc) |
1139 |
> |
argc--; argv++; |
1140 |
> |
|
1141 |
> |
// Parse arguments |
1142 |
> |
mon_macos_mode = false; |
1143 |
> |
mon_use_real_mem = false; |
1144 |
> |
while (argc > 0) { |
1145 |
> |
if (strcmp(argv[0], "-m") == 0) |
1146 |
> |
mon_macos_mode = true; |
1147 |
> |
else if (strcmp(argv[0], "-r") == 0) |
1148 |
|
mon_use_real_mem = true; |
1149 |
+ |
else |
1150 |
+ |
break; |
1151 |
+ |
argc--; argv++; |
1152 |
+ |
} |
1153 |
+ |
interactive = (argc == 0); |
1154 |
|
|
1155 |
< |
// Make argc/argv point to the actual arguments |
1156 |
< |
argc--; |
1157 |
< |
argv++; |
1158 |
< |
interactive = (argc == 0); |
1155 |
> |
// Set up memory access functions if not supplied by the user |
1156 |
> |
if (mon_read_byte == NULL) { |
1157 |
> |
if (mon_use_real_mem) |
1158 |
> |
mon_read_byte = mon_read_byte_real; |
1159 |
> |
else |
1160 |
> |
mon_read_byte = mon_read_byte_buffer; |
1161 |
> |
} |
1162 |
> |
if (mon_write_byte == NULL) { |
1163 |
> |
if (mon_use_real_mem) |
1164 |
> |
mon_write_byte = mon_write_byte_real; |
1165 |
> |
else |
1166 |
> |
mon_write_byte = mon_write_byte_buffer; |
1167 |
|
} |
1168 |
|
|
1169 |
|
// Allocate buffer |
1173 |
|
|
1174 |
|
// Print banner |
1175 |
|
if (interactive) |
1176 |
< |
fprintf(monerr, "\n *** mon V%d.%d by Christian Bauer ***\n" |
1177 |
< |
" *** Press 'h' for help ***\n\n", MON_VERSION, MON_REVISION); |
1176 |
> |
fprintf(monerr, "\n *** cxmon V" VERSION " by Christian Bauer and Marc Hellwig ***\n" |
1177 |
> |
" *** Press 'h' for help ***\n\n"); |
1178 |
|
} |
1179 |
|
|
1180 |
|
init_abort(); |
1183 |
|
while (!done) { |
1184 |
|
if (interactive) { |
1185 |
|
char prompt[16]; |
1186 |
< |
sprintf(prompt, "[%08lx]-> ", mon_dot_address); |
1186 |
> |
sprintf(prompt, "[%08x]-> ", mon_dot_address); |
1187 |
|
read_line(prompt); |
1188 |
|
} else { |
1189 |
|
if (argc == 0) { |