ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/mon/src/mon_cmd.cpp
Revision: 1.19
Committed: 2007-01-21T17:32:05Z (17 years, 9 months ago) by cebix
Branch: MAIN
CVS Tags: release_3-2
Changes since 1.18: +14 -15 lines
Log Message:
fixed compiler warnings

File Contents

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