ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/mon/src/mon_cmd.cpp
Revision: 1.11
Committed: 2002-07-17T10:00:54Z (21 years, 9 months ago) by gbeauche
Branch: MAIN
Changes since 1.10: +18 -18 lines
Log Message:
- Fix 64-bit support

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.10 * cxmon (C) 1997-2002 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     #define VERSION "2"
31     #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 gbeauche 1.11 static bool byte_string(uint8 *s, uintptr &len)
71 cebix 1.1 {
72 gbeauche 1.11 uintptr value;
73 cebix 1.1
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 cebix 1.5 fprintf(monout, "mon V" VERSION "\n");
122 cebix 1.1 }
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 gbeauche 1.11 uintptr val;
165 cebix 1.1
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 cebix 1.3 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 cebix 1.1 fprintf(monout, "Char : '%c%c%c%c'\n", char2print(val >> 24), char2print(val >> 16), char2print(val >> 8), char2print(val));
179     } else {
180 cebix 1.3 fprintf(monout, "Hex : $%08x\n"
181     "Dec : %d\n", val, val);
182 cebix 1.1 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 gbeauche 1.11 uintptr adr, end_adr;
217 cebix 1.1 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 gbeauche 1.11 fprintf(monout, "%0*lx:", 2 * sizeof(adr), mon_use_real_mem ? adr: adr % mon_mem_size);
226 cebix 1.1 for (int i=0; i<MEMDUMP_BPL; i++, adr++) {
227     if (i % 4 == 0)
228 cebix 1.3 fprintf(monout, " %08x", mon_read_word(adr));
229 cebix 1.1 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 gbeauche 1.11 uintptr adr, end_adr;
248 cebix 1.1 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 cebix 1.3 fprintf(monout, "%08x:", mon_use_real_mem ? adr : adr % mon_mem_size);
257 cebix 1.1 for (int i=0; i<ASCIIDUMP_BPL; i++, adr++)
258     str[i] = char2print(mon_read_byte(adr));
259     fprintf(monout, " '%s'\n", str);
260 cebix 1.5 }
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 gbeauche 1.11 uintptr adr, end_adr;
274 cebix 1.5 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 cebix 1.1 }
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 cebix 1.9 * d8086 [start [end]]
302 cebix 1.1 */
303    
304     enum CPUType {
305     CPU_PPC,
306     CPU_6502,
307     CPU_680x0,
308 cebix 1.9 CPU_Z80,
309 cebix 1.8 CPU_80x86_32,
310     CPU_80x86_16
311 cebix 1.1 };
312    
313     static void disassemble(CPUType type)
314     {
315 gbeauche 1.11 uintptr adr, end_adr;
316 cebix 1.1
317     if (!range_args(&adr, &end_adr, 16 * 4 - 1)) // 16 lines unless end address specified
318     return;
319    
320     switch (type) {
321     case CPU_PPC:
322     while (adr <= end_adr && !mon_aborted()) {
323     uint32 w = mon_read_word(adr);
324 cebix 1.3 fprintf(monout, "%08x: %08x\t", mon_use_real_mem ? adr : adr % mon_mem_size, w);
325 cebix 1.1 disass_ppc(monout, mon_use_real_mem ? adr : adr % mon_mem_size, w);
326     adr += 4;
327     }
328     break;
329    
330     case CPU_6502:
331     while (adr <= end_adr && !mon_aborted()) {
332     uint8 op = mon_read_byte(adr);
333     uint8 lo = mon_read_byte(adr + 1);
334     uint8 hi = mon_read_byte(adr + 2);
335 cebix 1.3 fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size);
336 cebix 1.1 adr += disass_6502(monout, mon_use_real_mem ? adr : adr % mon_mem_size, op, lo, hi);
337     }
338     break;
339    
340     case CPU_680x0:
341     while (adr <= end_adr && !mon_aborted()) {
342 cebix 1.3 fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size);
343 cebix 1.7 adr += disass_68k(monout, mon_use_real_mem ? adr : adr % mon_mem_size);
344 cebix 1.1 }
345     break;
346    
347 cebix 1.9 case CPU_Z80:
348 cebix 1.1 while (adr <= end_adr && !mon_aborted()) {
349 cebix 1.3 fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size);
350 cebix 1.9 adr += disass_z80(monout, mon_use_real_mem ? adr : adr % mon_mem_size);
351 cebix 1.1 }
352     break;
353    
354 cebix 1.8 case CPU_80x86_32:
355 cebix 1.1 while (adr <= end_adr && !mon_aborted()) {
356 cebix 1.3 fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size);
357 cebix 1.8 adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, false);
358     }
359     break;
360    
361     case CPU_80x86_16:
362     while (adr <= end_adr && !mon_aborted()) {
363     fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size);
364     adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, true);
365 cebix 1.1 }
366     break;
367     }
368    
369     mon_dot_address = adr;
370     }
371    
372     void disassemble_ppc(void)
373     {
374     disassemble(CPU_PPC);
375     }
376    
377     void disassemble_6502(void)
378     {
379     disassemble(CPU_6502);
380     }
381    
382     void disassemble_680x0(void)
383     {
384     disassemble(CPU_680x0);
385     }
386    
387 cebix 1.9 void disassemble_z80(void)
388 cebix 1.1 {
389 cebix 1.9 disassemble(CPU_Z80);
390 cebix 1.1 }
391    
392 cebix 1.8 void disassemble_80x86_32(void)
393     {
394     disassemble(CPU_80x86_32);
395     }
396    
397     void disassemble_80x86_16(void)
398 cebix 1.1 {
399 cebix 1.8 disassemble(CPU_80x86_16);
400 cebix 1.1 }
401    
402    
403     /*
404     * Modify memory
405     * : addr bytestring
406     */
407    
408     void modify(void)
409     {
410 gbeauche 1.11 uintptr adr, len, src_adr = 0;
411 cebix 1.1 uint8 str[256];
412    
413     if (!mon_expression(&adr))
414     return;
415     if (!byte_string(str, len))
416     return;
417    
418     while (src_adr < len)
419     mon_write_byte(adr++, str[src_adr++]);
420    
421     mon_dot_address = adr;
422     }
423    
424    
425     /*
426     * Fill
427     * f start end bytestring
428     */
429    
430     void fill(void)
431     {
432 gbeauche 1.11 uintptr adr, end_adr, len, src_adr = 0;
433 cebix 1.1 uint8 str[256];
434    
435     if (!mon_expression(&adr))
436     return;
437     if (!mon_expression(&end_adr))
438     return;
439     if (!byte_string(str, len))
440     return;
441    
442     while (adr <= end_adr)
443     mon_write_byte(adr++, str[src_adr++ % len]);
444     }
445    
446    
447     /*
448     * Transfer memory
449     * t start end dest
450     */
451    
452     void transfer(void)
453     {
454 gbeauche 1.11 uintptr adr, end_adr, dest;
455 cebix 1.1 int num;
456    
457     if (!mon_expression(&adr))
458     return;
459     if (!mon_expression(&end_adr))
460     return;
461     if (!mon_expression(&dest))
462     return;
463     if (mon_token != T_END) {
464     mon_error("Too many arguments");
465     return;
466     }
467    
468     num = end_adr - adr + 1;
469    
470     if (dest < adr)
471     for (int i=0; i<num; i++)
472     mon_write_byte(dest++, mon_read_byte(adr++));
473     else {
474     dest += end_adr - adr;
475     for (int i=0; i<num; i++)
476     mon_write_byte(dest--, mon_read_byte(end_adr--));
477     }
478     }
479    
480    
481     /*
482     * Compare
483     * c start end dest
484     */
485    
486     void compare(void)
487     {
488 gbeauche 1.11 uintptr adr, end_adr, dest;
489 cebix 1.1 int num = 0;
490    
491     if (!mon_expression(&adr))
492     return;
493     if (!mon_expression(&end_adr))
494     return;
495     if (!mon_expression(&dest))
496     return;
497     if (mon_token != T_END) {
498     mon_error("Too many arguments");
499     return;
500     }
501    
502     while (adr <= end_adr && !mon_aborted()) {
503     if (mon_read_byte(adr) != mon_read_byte(dest)) {
504 cebix 1.3 fprintf(monout, "%08x ", mon_use_real_mem ? adr : adr % mon_mem_size);
505 cebix 1.1 num++;
506     if (!(num & 7))
507     fputc('\n', monout);
508     }
509     adr++; dest++;
510     }
511    
512     if (num & 7)
513     fputc('\n', monout);
514     fprintf(monout, "%d byte(s) different\n", num);
515     }
516    
517    
518     /*
519     * Search for byte string
520     * h start end bytestring
521     */
522    
523     void hunt(void)
524     {
525 gbeauche 1.11 uintptr adr, end_adr, len;
526 cebix 1.1 uint8 str[256];
527     int num = 0;
528    
529     if (!mon_expression(&adr))
530     return;
531     if (!mon_expression(&end_adr))
532     return;
533     if (!byte_string(str, len))
534     return;
535    
536     while ((adr+len-1) <= end_adr && !mon_aborted()) {
537     uint32 i;
538    
539     for (i=0; i<len; i++)
540     if (mon_read_byte(adr + i) != str[i])
541     break;
542    
543     if (i == len) {
544 cebix 1.3 fprintf(monout, "%08x ", mon_use_real_mem ? adr : adr % mon_mem_size);
545 cebix 1.1 num++;
546     if (num == 1)
547     mon_dot_address = adr;
548     if (!(num & 7))
549     fputc('\n', monout);
550     }
551     adr++;
552     }
553    
554     if (num & 7)
555     fputc('\n', monout);
556     fprintf(monout, "Found %d occurrences\n", num);
557     }
558    
559    
560     /*
561     * Load data
562     * [ start "file"
563     */
564    
565     void load_data(void)
566     {
567 gbeauche 1.11 uintptr start_adr;
568 cebix 1.1 FILE *file;
569     int fc;
570    
571     if (!mon_expression(&start_adr))
572     return;
573     if (mon_token == T_END) {
574     mon_error("Missing file name");
575     return;
576     }
577     if (mon_token != T_STRING) {
578     mon_error("'\"' around file name expected");
579     return;
580     }
581     mon_get_token();
582     if (mon_token != T_END) {
583     mon_error("Too many arguments");
584     return;
585     }
586    
587     if (!(file = fopen(mon_string, "rb")))
588     mon_error("Unable to open file");
589     else {
590 gbeauche 1.11 uintptr adr = start_adr;
591 cebix 1.1
592     while ((fc = fgetc(file)) != EOF)
593     mon_write_byte(adr++, fc);
594     fclose(file);
595    
596 cebix 1.3 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);
597 cebix 1.1 mon_dot_address = adr;
598     }
599     }
600    
601    
602     /*
603     * Save data
604     * ] start size "file"
605     */
606    
607     void save_data(void)
608     {
609 gbeauche 1.11 uintptr start_adr, size;
610 cebix 1.1 FILE *file;
611    
612     if (!mon_expression(&start_adr))
613     return;
614     if (!mon_expression(&size))
615     return;
616     if (mon_token == T_END) {
617     mon_error("Missing file name");
618     return;
619     }
620     if (mon_token != T_STRING) {
621     mon_error("'\"' around file name expected");
622     return;
623     }
624     mon_get_token();
625     if (mon_token != T_END) {
626     mon_error("Too many arguments");
627     return;
628     }
629    
630     if (!(file = fopen(mon_string, "wb")))
631     mon_error("Unable to create file");
632     else {
633 gbeauche 1.11 uintptr adr = start_adr, end_adr = start_adr + size - 1;
634 cebix 1.1
635     while (adr <= end_adr)
636     fputc(mon_read_byte(adr++), file);
637     fclose(file);
638    
639 cebix 1.3 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);
640 cebix 1.1 }
641     }