ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/mon/src/mon_cmd.cpp
Revision: 1.7
Committed: 2000-09-25T12:44:34Z (24 years, 2 months ago) by cebix
Branch: MAIN
Changes since 1.6: +3 -19 lines
Log Message:
- replaced 680x0 and 80x86 disassemblers with the ones from GNU binutils
- 680x0 disassembler shows symbolic MacOS low memory globals

File Contents

# Content
1 /*
2 * mon_cmd.cpp - mon standard commands
3 *
4 * mon (C) 1997-1999 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 "sysdeps.h"
22
23 #include <stdlib.h>
24
25 #include "mon.h"
26 #include "mon_cmd.h"
27 #include "mon_disass.h"
28
29 #ifndef VERSION
30 #define VERSION "2"
31 #endif
32
33
34 /*
35 * range_args = [expression] [[COMMA] expression] END
36 *
37 * Read start address to "adr", end address to "end_adr".
38 * "adr" defaults to '.', "end_adr" defaults to '.'+def_range
39 *
40 * true: OK, false: Error
41 */
42
43 static bool range_args(uint32 *adr, uint32 *end_adr, uint32 def_range)
44 {
45 *adr = mon_dot_address;
46 *end_adr = mon_dot_address + def_range;
47
48 if (mon_token == T_END)
49 return true;
50 else {
51 if (!mon_expression(adr))
52 return false;
53 *end_adr = *adr + def_range;
54 if (mon_token == T_END)
55 return true;
56 else {
57 if (mon_token == T_COMMA) mon_get_token();
58 if (!mon_expression(end_adr))
59 return false;
60 return mon_token == T_END;
61 }
62 }
63 }
64
65
66 /*
67 * byte_string = (expression | STRING) {COMMA (expression | STRING)} END
68 */
69
70 static bool byte_string(uint8 *s, uint32 &len)
71 {
72 uint32 value;
73
74 len = 0;
75 goto start;
76
77 for (;;) {
78 if (mon_token == T_COMMA) {
79 mon_get_token();
80
81 start:
82 if (mon_token == T_STRING) {
83 uint8 *p = (uint8 *)mon_string;
84 while ((*s++ = *p++) != 0) ;
85 s--;
86 len += strlen(mon_string);
87 mon_get_token();
88 } else if (mon_expression(&value)) {
89 *s++ = value;
90 len++;
91 } else
92 return false;
93
94 } else if (mon_token == T_END)
95 return true;
96 else {
97 mon_error("',' expected");
98 return false;
99 }
100 }
101 }
102
103
104 /*
105 * Convert character to printable character
106 */
107
108 static inline uint8 char2print(uint8 c)
109 {
110 return (c >= 0x20 && c <= 0x7e) ? c : '.';
111 }
112
113
114 /*
115 * Show version
116 * ver
117 */
118
119 void version(void)
120 {
121 fprintf(monout, "mon V" VERSION "\n");
122 }
123
124
125 /*
126 * Redirect output
127 * o [file]
128 */
129
130 void redir_output(void)
131 {
132 // Close old file
133 if (monout != monerr) {
134 fclose(monout);
135 monout = monerr;
136 return;
137 }
138
139 // No argument given?
140 if (mon_token == T_END)
141 return;
142
143 // Otherwise open file
144 if (mon_token == T_STRING) {
145 mon_get_token();
146 if (mon_token != T_END) {
147 mon_error("Too many arguments");
148 return;
149 }
150 if (!(monout = fopen(mon_string, "w")))
151 mon_error("Unable to open file");
152 } else
153 mon_error("'\"' around file name expected");
154 }
155
156
157 /*
158 * Compute and display expression
159 * ? expression
160 */
161
162 void print_expr(void)
163 {
164 uint32 val;
165
166 if (!mon_expression(&val))
167 return;
168 if (mon_token != T_END) {
169 mon_error("Too many arguments");
170 return;
171 }
172
173 if (val > 0x7fffffff) {
174 fprintf(monout, "Hex unsigned: $%08x\n"
175 "Hex signed : -$%08x\n"
176 "Dec unsigned: %u\n"
177 "Dec signed : %d\n", val, -val, val, val);
178 fprintf(monout, "Char : '%c%c%c%c'\n", char2print(val >> 24), char2print(val >> 16), char2print(val >> 8), char2print(val));
179 } else {
180 fprintf(monout, "Hex : $%08x\n"
181 "Dec : %d\n", val, val);
182 fprintf(monout, "Char: '%c%c%c%c'\n", char2print(val >> 24), char2print(val >> 16), char2print(val >> 8), char2print(val));
183 }
184 }
185
186
187 /*
188 * Execute shell command
189 * \ "command"
190 */
191
192 void shell_command(void)
193 {
194 if (mon_token != T_STRING) {
195 mon_error("'\"' around command expected");
196 return;
197 }
198 mon_get_token();
199 if (mon_token != T_END) {
200 mon_error("Too many arguments");
201 return;
202 }
203 system(mon_string);
204 }
205
206
207 /*
208 * Memory dump
209 * m [start [end]]
210 */
211
212 #define MEMDUMP_BPL 16 // Bytes per line
213
214 void memory_dump(void)
215 {
216 uint32 adr, end_adr;
217 uint8 mem[MEMDUMP_BPL + 1];
218
219 mem[MEMDUMP_BPL] = 0;
220
221 if (!range_args(&adr, &end_adr, 16 * MEMDUMP_BPL - 1)) // 16 lines unless end address specified
222 return;
223
224 while (adr <= end_adr && !mon_aborted()) {
225 fprintf(monout, "%08x:", mon_use_real_mem ? adr: adr % mon_mem_size);
226 for (int i=0; i<MEMDUMP_BPL; i++, adr++) {
227 if (i % 4 == 0)
228 fprintf(monout, " %08x", mon_read_word(adr));
229 mem[i] = char2print(mon_read_byte(adr));
230 }
231 fprintf(monout, " '%s'\n", mem);
232 }
233
234 mon_dot_address = adr;
235 }
236
237
238 /*
239 * ASCII dump
240 * i [start [end]]
241 */
242
243 #define ASCIIDUMP_BPL 64 // Bytes per line
244
245 void ascii_dump(void)
246 {
247 uint32 adr, end_adr;
248 uint8 str[ASCIIDUMP_BPL + 1];
249
250 str[ASCIIDUMP_BPL] = 0;
251
252 if (!range_args(&adr, &end_adr, 16 * ASCIIDUMP_BPL - 1)) // 16 lines unless end address specified
253 return;
254
255 while (adr <= end_adr && !mon_aborted()) {
256 fprintf(monout, "%08x:", mon_use_real_mem ? adr : adr % mon_mem_size);
257 for (int i=0; i<ASCIIDUMP_BPL; i++, adr++)
258 str[i] = char2print(mon_read_byte(adr));
259 fprintf(monout, " '%s'\n", str);
260 }
261
262 mon_dot_address = adr;
263 }
264
265
266 /*
267 * Binary dump
268 * b [start [end]]
269 */
270
271 void binary_dump(void)
272 {
273 uint32 adr, end_adr;
274 uint8 str[9];
275
276 str[8] = 0;
277
278 if (!range_args(&adr, &end_adr, 7)) // 8 lines unless end address specified
279 return;
280
281 while (adr <= end_adr && !mon_aborted()) {
282 fprintf(monout, "%08x:", mon_use_real_mem ? adr : adr % mon_mem_size);
283 uint8 b = mon_read_byte(adr);
284 for (int m=0x80, i=0; i<8; m>>=1, i++)
285 str[i] = (b & m) ? '*' : '.';
286 fprintf(monout, " '%s'\n", str);
287 adr++;
288 }
289
290 mon_dot_address = adr;
291 }
292
293
294 /*
295 * Disassemble
296 * d [start [end]]
297 * d65 [start [end]]
298 * d68 [start [end]]
299 * d80 [start [end]]
300 * d86 [start [end]]
301 */
302
303 enum CPUType {
304 CPU_PPC,
305 CPU_6502,
306 CPU_680x0,
307 CPU_8080,
308 CPU_80x86
309 };
310
311 static void disassemble(CPUType type)
312 {
313 uint32 adr, end_adr;
314
315 if (!range_args(&adr, &end_adr, 16 * 4 - 1)) // 16 lines unless end address specified
316 return;
317
318 switch (type) {
319 case CPU_PPC:
320 while (adr <= end_adr && !mon_aborted()) {
321 uint32 w = mon_read_word(adr);
322 fprintf(monout, "%08x: %08x\t", mon_use_real_mem ? adr : adr % mon_mem_size, w);
323 disass_ppc(monout, mon_use_real_mem ? adr : adr % mon_mem_size, w);
324 adr += 4;
325 }
326 break;
327
328 case CPU_6502:
329 while (adr <= end_adr && !mon_aborted()) {
330 uint8 op = mon_read_byte(adr);
331 uint8 lo = mon_read_byte(adr + 1);
332 uint8 hi = mon_read_byte(adr + 2);
333 fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size);
334 adr += disass_6502(monout, mon_use_real_mem ? adr : adr % mon_mem_size, op, lo, hi);
335 }
336 break;
337
338 case CPU_680x0:
339 while (adr <= end_adr && !mon_aborted()) {
340 fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size);
341 adr += disass_68k(monout, mon_use_real_mem ? adr : adr % mon_mem_size);
342 }
343 break;
344
345 case CPU_8080:
346 while (adr <= end_adr && !mon_aborted()) {
347 uint8 op = mon_read_byte(adr);
348 uint8 lo = mon_read_byte(adr + 1);
349 uint8 hi = mon_read_byte(adr + 2);
350 fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size);
351 adr += disass_8080(monout, mon_use_real_mem ? adr : adr % mon_mem_size, op, lo, hi);
352 }
353 break;
354
355 case CPU_80x86:
356 while (adr <= end_adr && !mon_aborted()) {
357 fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size);
358 adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size);
359 }
360 break;
361 }
362
363 mon_dot_address = adr;
364 }
365
366 void disassemble_ppc(void)
367 {
368 disassemble(CPU_PPC);
369 }
370
371 void disassemble_6502(void)
372 {
373 disassemble(CPU_6502);
374 }
375
376 void disassemble_680x0(void)
377 {
378 disassemble(CPU_680x0);
379 }
380
381 void disassemble_8080(void)
382 {
383 disassemble(CPU_8080);
384 }
385
386 void disassemble_80x86(void)
387 {
388 disassemble(CPU_80x86);
389 }
390
391
392 /*
393 * Modify memory
394 * : addr bytestring
395 */
396
397 void modify(void)
398 {
399 uint32 adr, len, src_adr = 0;
400 uint8 str[256];
401
402 if (!mon_expression(&adr))
403 return;
404 if (!byte_string(str, len))
405 return;
406
407 while (src_adr < len)
408 mon_write_byte(adr++, str[src_adr++]);
409
410 mon_dot_address = adr;
411 }
412
413
414 /*
415 * Fill
416 * f start end bytestring
417 */
418
419 void fill(void)
420 {
421 uint32 adr, end_adr, len, src_adr = 0;
422 uint8 str[256];
423
424 if (!mon_expression(&adr))
425 return;
426 if (!mon_expression(&end_adr))
427 return;
428 if (!byte_string(str, len))
429 return;
430
431 while (adr <= end_adr)
432 mon_write_byte(adr++, str[src_adr++ % len]);
433 }
434
435
436 /*
437 * Transfer memory
438 * t start end dest
439 */
440
441 void transfer(void)
442 {
443 uint32 adr, end_adr, dest;
444 int num;
445
446 if (!mon_expression(&adr))
447 return;
448 if (!mon_expression(&end_adr))
449 return;
450 if (!mon_expression(&dest))
451 return;
452 if (mon_token != T_END) {
453 mon_error("Too many arguments");
454 return;
455 }
456
457 num = end_adr - adr + 1;
458
459 if (dest < adr)
460 for (int i=0; i<num; i++)
461 mon_write_byte(dest++, mon_read_byte(adr++));
462 else {
463 dest += end_adr - adr;
464 for (int i=0; i<num; i++)
465 mon_write_byte(dest--, mon_read_byte(end_adr--));
466 }
467 }
468
469
470 /*
471 * Compare
472 * c start end dest
473 */
474
475 void compare(void)
476 {
477 uint32 adr, end_adr, dest;
478 int num = 0;
479
480 if (!mon_expression(&adr))
481 return;
482 if (!mon_expression(&end_adr))
483 return;
484 if (!mon_expression(&dest))
485 return;
486 if (mon_token != T_END) {
487 mon_error("Too many arguments");
488 return;
489 }
490
491 while (adr <= end_adr && !mon_aborted()) {
492 if (mon_read_byte(adr) != mon_read_byte(dest)) {
493 fprintf(monout, "%08x ", mon_use_real_mem ? adr : adr % mon_mem_size);
494 num++;
495 if (!(num & 7))
496 fputc('\n', monout);
497 }
498 adr++; dest++;
499 }
500
501 if (num & 7)
502 fputc('\n', monout);
503 fprintf(monout, "%d byte(s) different\n", num);
504 }
505
506
507 /*
508 * Search for byte string
509 * h start end bytestring
510 */
511
512 void hunt(void)
513 {
514 uint32 adr, end_adr, len;
515 uint8 str[256];
516 int num = 0;
517
518 if (!mon_expression(&adr))
519 return;
520 if (!mon_expression(&end_adr))
521 return;
522 if (!byte_string(str, len))
523 return;
524
525 while ((adr+len-1) <= end_adr && !mon_aborted()) {
526 uint32 i;
527
528 for (i=0; i<len; i++)
529 if (mon_read_byte(adr + i) != str[i])
530 break;
531
532 if (i == len) {
533 fprintf(monout, "%08x ", mon_use_real_mem ? adr : adr % mon_mem_size);
534 num++;
535 if (num == 1)
536 mon_dot_address = adr;
537 if (!(num & 7))
538 fputc('\n', monout);
539 }
540 adr++;
541 }
542
543 if (num & 7)
544 fputc('\n', monout);
545 fprintf(monout, "Found %d occurrences\n", num);
546 }
547
548
549 /*
550 * Load data
551 * [ start "file"
552 */
553
554 void load_data(void)
555 {
556 uint32 start_adr;
557 FILE *file;
558 int fc;
559
560 if (!mon_expression(&start_adr))
561 return;
562 if (mon_token == T_END) {
563 mon_error("Missing file name");
564 return;
565 }
566 if (mon_token != T_STRING) {
567 mon_error("'\"' around file name expected");
568 return;
569 }
570 mon_get_token();
571 if (mon_token != T_END) {
572 mon_error("Too many arguments");
573 return;
574 }
575
576 if (!(file = fopen(mon_string, "rb")))
577 mon_error("Unable to open file");
578 else {
579 uint32 adr = start_adr;
580
581 while ((fc = fgetc(file)) != EOF)
582 mon_write_byte(adr++, fc);
583 fclose(file);
584
585 fprintf(monerr, "%08x bytes read from %08x to %08x\n", adr - start_adr, mon_use_real_mem ? start_adr : start_adr % mon_mem_size, mon_use_real_mem ? adr-1 : (adr-1) % mon_mem_size);
586 mon_dot_address = adr;
587 }
588 }
589
590
591 /*
592 * Save data
593 * ] start size "file"
594 */
595
596 void save_data(void)
597 {
598 uint32 start_adr, size;
599 FILE *file;
600
601 if (!mon_expression(&start_adr))
602 return;
603 if (!mon_expression(&size))
604 return;
605 if (mon_token == T_END) {
606 mon_error("Missing file name");
607 return;
608 }
609 if (mon_token != T_STRING) {
610 mon_error("'\"' around file name expected");
611 return;
612 }
613 mon_get_token();
614 if (mon_token != T_END) {
615 mon_error("Too many arguments");
616 return;
617 }
618
619 if (!(file = fopen(mon_string, "wb")))
620 mon_error("Unable to create file");
621 else {
622 uint32 adr = start_adr, end_adr = start_adr + size - 1;
623
624 while (adr <= end_adr)
625 fputc(mon_read_byte(adr++), file);
626 fclose(file);
627
628 fprintf(monerr, "%08x bytes written from %08x to %08x\n", size, mon_use_real_mem ? start_adr : start_adr % mon_mem_size, mon_use_real_mem ? end_adr : end_adr % mon_mem_size);
629 }
630 }