ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/mon/src/mon_cmd.cpp
Revision: 1.5
Committed: 2000-04-24T13:10:17Z (24 years, 1 month ago) by cebix
Branch: MAIN
Changes since 1.4: +29 -1 lines
Log Message:
- added binary dump command
- converted to automake

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_ppc.h"
28 #include "mon_68k.h"
29 #include "mon_x86.h"
30 #include "mon_6502.h"
31 #include "mon_8080.h"
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 uint16 buf[8];
341 buf[0] = mon_read_half(adr);
342 buf[1] = mon_read_half(adr + 2);
343 buf[2] = mon_read_half(adr + 4);
344 buf[3] = mon_read_half(adr + 6);
345 buf[4] = mon_read_half(adr + 8);
346 buf[5] = mon_read_half(adr + 10);
347 buf[6] = mon_read_half(adr + 12);
348 buf[7] = mon_read_half(adr + 14);
349 fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size);
350 adr += disass_68k(monout, mon_use_real_mem ? adr : adr % mon_mem_size, buf);
351 }
352 break;
353
354 case CPU_8080:
355 while (adr <= end_adr && !mon_aborted()) {
356 uint8 op = mon_read_byte(adr);
357 uint8 lo = mon_read_byte(adr + 1);
358 uint8 hi = mon_read_byte(adr + 2);
359 fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size);
360 adr += disass_8080(monout, mon_use_real_mem ? adr : adr % mon_mem_size, op, lo, hi);
361 }
362 break;
363
364 case CPU_80x86:
365 while (adr <= end_adr && !mon_aborted()) {
366 uint8 buf[16];
367 for (int i=0; i<16; i++)
368 buf[i] = mon_read_byte(adr + i);
369 fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size);
370 adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, buf);
371 }
372 break;
373 }
374
375 mon_dot_address = adr;
376 }
377
378 void disassemble_ppc(void)
379 {
380 disassemble(CPU_PPC);
381 }
382
383 void disassemble_6502(void)
384 {
385 disassemble(CPU_6502);
386 }
387
388 void disassemble_680x0(void)
389 {
390 disassemble(CPU_680x0);
391 }
392
393 void disassemble_8080(void)
394 {
395 disassemble(CPU_8080);
396 }
397
398 void disassemble_80x86(void)
399 {
400 disassemble(CPU_80x86);
401 }
402
403
404 /*
405 * Modify memory
406 * : addr bytestring
407 */
408
409 void modify(void)
410 {
411 uint32 adr, len, src_adr = 0;
412 uint8 str[256];
413
414 if (!mon_expression(&adr))
415 return;
416 if (!byte_string(str, len))
417 return;
418
419 while (src_adr < len)
420 mon_write_byte(adr++, str[src_adr++]);
421
422 mon_dot_address = adr;
423 }
424
425
426 /*
427 * Fill
428 * f start end bytestring
429 */
430
431 void fill(void)
432 {
433 uint32 adr, end_adr, len, src_adr = 0;
434 uint8 str[256];
435
436 if (!mon_expression(&adr))
437 return;
438 if (!mon_expression(&end_adr))
439 return;
440 if (!byte_string(str, len))
441 return;
442
443 while (adr <= end_adr)
444 mon_write_byte(adr++, str[src_adr++ % len]);
445 }
446
447
448 /*
449 * Transfer memory
450 * t start end dest
451 */
452
453 void transfer(void)
454 {
455 uint32 adr, end_adr, dest;
456 int num;
457
458 if (!mon_expression(&adr))
459 return;
460 if (!mon_expression(&end_adr))
461 return;
462 if (!mon_expression(&dest))
463 return;
464 if (mon_token != T_END) {
465 mon_error("Too many arguments");
466 return;
467 }
468
469 num = end_adr - adr + 1;
470
471 if (dest < adr)
472 for (int i=0; i<num; i++)
473 mon_write_byte(dest++, mon_read_byte(adr++));
474 else {
475 dest += end_adr - adr;
476 for (int i=0; i<num; i++)
477 mon_write_byte(dest--, mon_read_byte(end_adr--));
478 }
479 }
480
481
482 /*
483 * Compare
484 * c start end dest
485 */
486
487 void compare(void)
488 {
489 uint32 adr, end_adr, dest;
490 int num = 0;
491
492 if (!mon_expression(&adr))
493 return;
494 if (!mon_expression(&end_adr))
495 return;
496 if (!mon_expression(&dest))
497 return;
498 if (mon_token != T_END) {
499 mon_error("Too many arguments");
500 return;
501 }
502
503 while (adr <= end_adr && !mon_aborted()) {
504 if (mon_read_byte(adr) != mon_read_byte(dest)) {
505 fprintf(monout, "%08x ", mon_use_real_mem ? adr : adr % mon_mem_size);
506 num++;
507 if (!(num & 7))
508 fputc('\n', monout);
509 }
510 adr++; dest++;
511 }
512
513 if (num & 7)
514 fputc('\n', monout);
515 fprintf(monout, "%d byte(s) different\n", num);
516 }
517
518
519 /*
520 * Search for byte string
521 * h start end bytestring
522 */
523
524 void hunt(void)
525 {
526 uint32 adr, end_adr, len;
527 uint8 str[256];
528 int num = 0;
529
530 if (!mon_expression(&adr))
531 return;
532 if (!mon_expression(&end_adr))
533 return;
534 if (!byte_string(str, len))
535 return;
536
537 while ((adr+len-1) <= end_adr && !mon_aborted()) {
538 uint32 i;
539
540 for (i=0; i<len; i++)
541 if (mon_read_byte(adr + i) != str[i])
542 break;
543
544 if (i == len) {
545 fprintf(monout, "%08x ", mon_use_real_mem ? adr : adr % mon_mem_size);
546 num++;
547 if (num == 1)
548 mon_dot_address = adr;
549 if (!(num & 7))
550 fputc('\n', monout);
551 }
552 adr++;
553 }
554
555 if (num & 7)
556 fputc('\n', monout);
557 fprintf(monout, "Found %d occurrences\n", num);
558 }
559
560
561 /*
562 * Load data
563 * [ start "file"
564 */
565
566 void load_data(void)
567 {
568 uint32 start_adr;
569 FILE *file;
570 int fc;
571
572 if (!mon_expression(&start_adr))
573 return;
574 if (mon_token == T_END) {
575 mon_error("Missing file name");
576 return;
577 }
578 if (mon_token != T_STRING) {
579 mon_error("'\"' around file name expected");
580 return;
581 }
582 mon_get_token();
583 if (mon_token != T_END) {
584 mon_error("Too many arguments");
585 return;
586 }
587
588 if (!(file = fopen(mon_string, "rb")))
589 mon_error("Unable to open file");
590 else {
591 uint32 adr = start_adr;
592
593 while ((fc = fgetc(file)) != EOF)
594 mon_write_byte(adr++, fc);
595 fclose(file);
596
597 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);
598 mon_dot_address = adr;
599 }
600 }
601
602
603 /*
604 * Save data
605 * ] start size "file"
606 */
607
608 void save_data(void)
609 {
610 uint32 start_adr, size;
611 FILE *file;
612
613 if (!mon_expression(&start_adr))
614 return;
615 if (!mon_expression(&size))
616 return;
617 if (mon_token == T_END) {
618 mon_error("Missing file name");
619 return;
620 }
621 if (mon_token != T_STRING) {
622 mon_error("'\"' around file name expected");
623 return;
624 }
625 mon_get_token();
626 if (mon_token != T_END) {
627 mon_error("Too many arguments");
628 return;
629 }
630
631 if (!(file = fopen(mon_string, "wb")))
632 mon_error("Unable to create file");
633 else {
634 uint32 adr = start_adr, end_adr = start_adr + size - 1;
635
636 while (adr <= end_adr)
637 fputc(mon_read_byte(adr++), file);
638 fclose(file);
639
640 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);
641 }
642 }