ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/mon/src/mon_cmd.cpp
Revision: 1.8
Committed: 2000-10-06T00:04:22Z (24 years, 1 month ago) by cebix
Branch: MAIN
Changes since 1.7: +18 -5 lines
Log Message:
new command "d8086" for disassembling 16-bit x86 code

File Contents

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