1 |
|
/* |
2 |
< |
* mon_cmd.cpp - mon standard commands |
2 |
> |
* mon_cmd.cpp - cxmon standard commands |
3 |
|
* |
4 |
< |
* mon (C) 1997-1999 Christian Bauer, Marc Hellwig |
4 |
> |
* cxmon (C) 1997-2003 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 |
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" |
27 |
> |
#include "mon_disass.h" |
28 |
> |
|
29 |
> |
#ifndef VERSION |
30 |
> |
#define VERSION "3" |
31 |
> |
#endif |
32 |
|
|
33 |
|
|
34 |
|
/* |
40 |
|
* true: OK, false: Error |
41 |
|
*/ |
42 |
|
|
43 |
< |
static bool range_args(uint32 *adr, uint32 *end_adr, uint32 def_range) |
43 |
> |
static bool range_args(uintptr *adr, uintptr *end_adr, uint32 def_range) |
44 |
|
{ |
45 |
|
*adr = mon_dot_address; |
46 |
|
*end_adr = mon_dot_address + def_range; |
67 |
|
* byte_string = (expression | STRING) {COMMA (expression | STRING)} END |
68 |
|
*/ |
69 |
|
|
70 |
< |
static bool byte_string(uint8 *s, uint32 &len) |
70 |
> |
static bool byte_string(uint8 *&str, uintptr &len) |
71 |
|
{ |
72 |
< |
uint32 value; |
72 |
> |
uintptr value; |
73 |
|
|
74 |
+ |
static const int GRANULARITY = 16; // must be a power of 2 |
75 |
+ |
str = NULL; |
76 |
|
len = 0; |
77 |
|
goto start; |
78 |
|
|
83 |
|
start: |
84 |
|
if (mon_token == T_STRING) { |
85 |
|
uint8 *p = (uint8 *)mon_string; |
86 |
< |
while ((*s++ = *p++) != 0) ; |
87 |
< |
s--; |
88 |
< |
len += strlen(mon_string); |
86 |
> |
unsigned n = strlen(mon_string); |
87 |
> |
str = (uint8 *)realloc(str, (len + n - 1 + GRANULARITY) & ~(GRANULARITY - 1)); |
88 |
> |
memcpy(str + len, mon_string, n); |
89 |
> |
len += n; |
90 |
|
mon_get_token(); |
91 |
|
} else if (mon_expression(&value)) { |
92 |
< |
*s++ = value; |
92 |
> |
str = (uint8 *)realloc(str, (len + GRANULARITY) & ~(GRANULARITY - 1)); |
93 |
> |
str[len] = value; |
94 |
|
len++; |
95 |
< |
} else |
95 |
> |
} else { |
96 |
> |
if (str) |
97 |
> |
free(str); |
98 |
|
return false; |
99 |
+ |
} |
100 |
|
|
101 |
< |
} else if (mon_token == T_END) |
101 |
> |
} else if (mon_token == T_END) { |
102 |
|
return true; |
103 |
< |
else { |
103 |
> |
} else { |
104 |
|
mon_error("',' expected"); |
105 |
+ |
if (str) |
106 |
+ |
free(str); |
107 |
|
return false; |
108 |
|
} |
109 |
|
} |
170 |
|
|
171 |
|
void print_expr(void) |
172 |
|
{ |
173 |
< |
uint32 val; |
173 |
> |
uintptr val; |
174 |
|
|
175 |
|
if (!mon_expression(&val)) |
176 |
|
return; |
222 |
|
|
223 |
|
void memory_dump(void) |
224 |
|
{ |
225 |
< |
uint32 adr, end_adr; |
225 |
> |
uintptr adr, end_adr; |
226 |
|
uint8 mem[MEMDUMP_BPL + 1]; |
227 |
|
|
228 |
|
mem[MEMDUMP_BPL] = 0; |
231 |
|
return; |
232 |
|
|
233 |
|
while (adr <= end_adr && !mon_aborted()) { |
234 |
< |
fprintf(monout, "%08x:", mon_use_real_mem ? adr: adr % mon_mem_size); |
234 |
> |
fprintf(monout, "%0*lx:", 2 * sizeof(adr), mon_use_real_mem ? adr: adr % mon_mem_size); |
235 |
|
for (int i=0; i<MEMDUMP_BPL; i++, adr++) { |
236 |
|
if (i % 4 == 0) |
237 |
|
fprintf(monout, " %08x", mon_read_word(adr)); |
253 |
|
|
254 |
|
void ascii_dump(void) |
255 |
|
{ |
256 |
< |
uint32 adr, end_adr; |
256 |
> |
uintptr adr, end_adr; |
257 |
|
uint8 str[ASCIIDUMP_BPL + 1]; |
258 |
|
|
259 |
|
str[ASCIIDUMP_BPL] = 0; |
262 |
|
return; |
263 |
|
|
264 |
|
while (adr <= end_adr && !mon_aborted()) { |
265 |
< |
fprintf(monout, "%08x:", mon_use_real_mem ? adr : adr % mon_mem_size); |
265 |
> |
fprintf(monout, "%0*lx:", 2 * sizeof(adr), mon_use_real_mem ? adr : adr % mon_mem_size); |
266 |
|
for (int i=0; i<ASCIIDUMP_BPL; i++, adr++) |
267 |
|
str[i] = char2print(mon_read_byte(adr)); |
268 |
|
fprintf(monout, " '%s'\n", str); |
279 |
|
|
280 |
|
void binary_dump(void) |
281 |
|
{ |
282 |
< |
uint32 adr, end_adr; |
282 |
> |
uintptr adr, end_adr; |
283 |
|
uint8 str[9]; |
284 |
|
|
285 |
|
str[8] = 0; |
288 |
|
return; |
289 |
|
|
290 |
|
while (adr <= end_adr && !mon_aborted()) { |
291 |
< |
fprintf(monout, "%08x:", mon_use_real_mem ? adr : adr % mon_mem_size); |
291 |
> |
fprintf(monout, "%0*lx:", 2 * sizeof(adr), mon_use_real_mem ? adr : adr % mon_mem_size); |
292 |
|
uint8 b = mon_read_byte(adr); |
293 |
|
for (int m=0x80, i=0; i<8; m>>=1, i++) |
294 |
|
str[i] = (b & m) ? '*' : '.'; |
307 |
|
* d68 [start [end]] |
308 |
|
* d80 [start [end]] |
309 |
|
* d86 [start [end]] |
310 |
+ |
* d8086 [start [end]] |
311 |
|
*/ |
312 |
|
|
313 |
|
enum CPUType { |
314 |
|
CPU_PPC, |
315 |
|
CPU_6502, |
316 |
|
CPU_680x0, |
317 |
< |
CPU_8080, |
318 |
< |
CPU_80x86 |
317 |
> |
CPU_Z80, |
318 |
> |
CPU_80x86_32, |
319 |
> |
CPU_80x86_16, |
320 |
> |
CPU_x86_64 |
321 |
|
}; |
322 |
|
|
323 |
|
static void disassemble(CPUType type) |
324 |
|
{ |
325 |
< |
uint32 adr, end_adr; |
325 |
> |
uintptr adr, end_adr; |
326 |
|
|
327 |
|
if (!range_args(&adr, &end_adr, 16 * 4 - 1)) // 16 lines unless end address specified |
328 |
|
return; |
331 |
|
case CPU_PPC: |
332 |
|
while (adr <= end_adr && !mon_aborted()) { |
333 |
|
uint32 w = mon_read_word(adr); |
334 |
< |
fprintf(monout, "%08x: %08x\t", mon_use_real_mem ? adr : adr % mon_mem_size, w); |
334 |
> |
fprintf(monout, "%0*lx: %08x\t", 2 * sizeof(adr), mon_use_real_mem ? adr : adr % mon_mem_size, w); |
335 |
|
disass_ppc(monout, mon_use_real_mem ? adr : adr % mon_mem_size, w); |
336 |
|
adr += 4; |
337 |
|
} |
342 |
|
uint8 op = mon_read_byte(adr); |
343 |
|
uint8 lo = mon_read_byte(adr + 1); |
344 |
|
uint8 hi = mon_read_byte(adr + 2); |
345 |
< |
fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size); |
345 |
> |
fprintf(monout, "%0*lx: ", 2 * sizeof(adr), mon_use_real_mem ? adr : adr % mon_mem_size); |
346 |
|
adr += disass_6502(monout, mon_use_real_mem ? adr : adr % mon_mem_size, op, lo, hi); |
347 |
|
} |
348 |
|
break; |
349 |
|
|
350 |
|
case CPU_680x0: |
351 |
|
while (adr <= end_adr && !mon_aborted()) { |
352 |
< |
uint16 buf[8]; |
353 |
< |
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); |
352 |
> |
fprintf(monout, "%0*lx: ", 2 * sizeof(adr), mon_use_real_mem ? adr : adr % mon_mem_size); |
353 |
> |
adr += disass_68k(monout, mon_use_real_mem ? adr : adr % mon_mem_size); |
354 |
|
} |
355 |
|
break; |
356 |
|
|
357 |
< |
case CPU_8080: |
357 |
> |
case CPU_Z80: |
358 |
|
while (adr <= end_adr && !mon_aborted()) { |
359 |
< |
uint8 op = mon_read_byte(adr); |
360 |
< |
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); |
359 |
> |
fprintf(monout, "%0*lx: ", 2 * sizeof(adr), mon_use_real_mem ? adr : adr % mon_mem_size); |
360 |
> |
adr += disass_z80(monout, mon_use_real_mem ? adr : adr % mon_mem_size); |
361 |
|
} |
362 |
|
break; |
363 |
|
|
364 |
< |
case CPU_80x86: |
364 |
> |
case CPU_x86_64: |
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); |
366 |
> |
fprintf(monout, "%0*lx: ", 2 * sizeof(adr), mon_use_real_mem ? adr : adr % mon_mem_size); |
367 |
> |
adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, 64); |
368 |
|
} |
369 |
|
break; |
370 |
+ |
|
371 |
+ |
case CPU_80x86_32: |
372 |
+ |
while (adr <= end_adr && !mon_aborted()) { |
373 |
+ |
fprintf(monout, "%0*lx: ", 2 * sizeof(adr), mon_use_real_mem ? adr : adr % mon_mem_size); |
374 |
+ |
adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, 32); |
375 |
+ |
} |
376 |
+ |
break; |
377 |
+ |
|
378 |
+ |
case CPU_80x86_16: |
379 |
+ |
while (adr <= end_adr && !mon_aborted()) { |
380 |
+ |
fprintf(monout, "%0*lx: ", 2 * sizeof(adr), mon_use_real_mem ? adr : adr % mon_mem_size); |
381 |
+ |
adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, 16); |
382 |
+ |
} |
383 |
|
} |
384 |
|
|
385 |
|
mon_dot_address = adr; |
400 |
|
disassemble(CPU_680x0); |
401 |
|
} |
402 |
|
|
403 |
< |
void disassemble_8080(void) |
403 |
> |
void disassemble_z80(void) |
404 |
> |
{ |
405 |
> |
disassemble(CPU_Z80); |
406 |
> |
} |
407 |
> |
|
408 |
> |
void disassemble_80x86_32(void) |
409 |
|
{ |
410 |
< |
disassemble(CPU_8080); |
410 |
> |
disassemble(CPU_80x86_32); |
411 |
|
} |
412 |
|
|
413 |
< |
void disassemble_80x86(void) |
413 |
> |
void disassemble_80x86_16(void) |
414 |
|
{ |
415 |
< |
disassemble(CPU_80x86); |
415 |
> |
disassemble(CPU_80x86_16); |
416 |
> |
} |
417 |
> |
|
418 |
> |
void disassemble_x86_64(void) |
419 |
> |
{ |
420 |
> |
disassemble(CPU_x86_64); |
421 |
|
} |
422 |
|
|
423 |
|
|
428 |
|
|
429 |
|
void modify(void) |
430 |
|
{ |
431 |
< |
uint32 adr, len, src_adr = 0; |
432 |
< |
uint8 str[256]; |
431 |
> |
uintptr adr, len, src_adr = 0; |
432 |
> |
uint8 *str; |
433 |
|
|
434 |
|
if (!mon_expression(&adr)) |
435 |
|
return; |
438 |
|
|
439 |
|
while (src_adr < len) |
440 |
|
mon_write_byte(adr++, str[src_adr++]); |
421 |
– |
|
441 |
|
mon_dot_address = adr; |
442 |
+ |
|
443 |
+ |
free(str); |
444 |
|
} |
445 |
|
|
446 |
|
|
451 |
|
|
452 |
|
void fill(void) |
453 |
|
{ |
454 |
< |
uint32 adr, end_adr, len, src_adr = 0; |
455 |
< |
uint8 str[256]; |
454 |
> |
uintptr adr, end_adr, len, src_adr = 0; |
455 |
> |
uint8 *str; |
456 |
|
|
457 |
|
if (!mon_expression(&adr)) |
458 |
|
return; |
463 |
|
|
464 |
|
while (adr <= end_adr) |
465 |
|
mon_write_byte(adr++, str[src_adr++ % len]); |
466 |
+ |
|
467 |
+ |
free(str); |
468 |
|
} |
469 |
|
|
470 |
|
|
475 |
|
|
476 |
|
void transfer(void) |
477 |
|
{ |
478 |
< |
uint32 adr, end_adr, dest; |
478 |
> |
uintptr adr, end_adr, dest; |
479 |
|
int num; |
480 |
|
|
481 |
|
if (!mon_expression(&adr)) |
509 |
|
|
510 |
|
void compare(void) |
511 |
|
{ |
512 |
< |
uint32 adr, end_adr, dest; |
512 |
> |
uintptr adr, end_adr, dest; |
513 |
|
int num = 0; |
514 |
|
|
515 |
|
if (!mon_expression(&adr)) |
525 |
|
|
526 |
|
while (adr <= end_adr && !mon_aborted()) { |
527 |
|
if (mon_read_byte(adr) != mon_read_byte(dest)) { |
528 |
< |
fprintf(monout, "%08x ", mon_use_real_mem ? adr : adr % mon_mem_size); |
528 |
> |
fprintf(monout, "%0*lx ", 2 * sizeof(adr), mon_use_real_mem ? adr : adr % mon_mem_size); |
529 |
|
num++; |
530 |
|
if (!(num & 7)) |
531 |
|
fputc('\n', monout); |
546 |
|
|
547 |
|
void hunt(void) |
548 |
|
{ |
549 |
< |
uint32 adr, end_adr, len; |
550 |
< |
uint8 str[256]; |
549 |
> |
uintptr adr, end_adr, len; |
550 |
> |
uint8 *str; |
551 |
|
int num = 0; |
552 |
|
|
553 |
|
if (!mon_expression(&adr)) |
565 |
|
break; |
566 |
|
|
567 |
|
if (i == len) { |
568 |
< |
fprintf(monout, "%08x ", mon_use_real_mem ? adr : adr % mon_mem_size); |
568 |
> |
fprintf(monout, "%0*lx ", 2 * sizeof(adr), mon_use_real_mem ? adr : adr % mon_mem_size); |
569 |
|
num++; |
570 |
|
if (num == 1) |
571 |
|
mon_dot_address = adr; |
575 |
|
adr++; |
576 |
|
} |
577 |
|
|
578 |
+ |
free(str); |
579 |
+ |
|
580 |
|
if (num & 7) |
581 |
|
fputc('\n', monout); |
582 |
|
fprintf(monout, "Found %d occurrences\n", num); |
590 |
|
|
591 |
|
void load_data(void) |
592 |
|
{ |
593 |
< |
uint32 start_adr; |
593 |
> |
uintptr start_adr; |
594 |
|
FILE *file; |
595 |
|
int fc; |
596 |
|
|
613 |
|
if (!(file = fopen(mon_string, "rb"))) |
614 |
|
mon_error("Unable to open file"); |
615 |
|
else { |
616 |
< |
uint32 adr = start_adr; |
616 |
> |
uintptr adr = start_adr; |
617 |
|
|
618 |
|
while ((fc = fgetc(file)) != EOF) |
619 |
|
mon_write_byte(adr++, fc); |
620 |
|
fclose(file); |
621 |
|
|
622 |
< |
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); |
622 |
> |
fprintf(monerr, "%08x bytes read from %0*lx to %0*lx\n", adr - start_adr, 2 * sizeof(adr), mon_use_real_mem ? start_adr : start_adr % mon_mem_size, 2 * sizeof(adr), mon_use_real_mem ? adr-1 : (adr-1) % mon_mem_size); |
623 |
|
mon_dot_address = adr; |
624 |
|
} |
625 |
|
} |
632 |
|
|
633 |
|
void save_data(void) |
634 |
|
{ |
635 |
< |
uint32 start_adr, size; |
635 |
> |
uintptr start_adr, size; |
636 |
|
FILE *file; |
637 |
|
|
638 |
|
if (!mon_expression(&start_adr)) |
656 |
|
if (!(file = fopen(mon_string, "wb"))) |
657 |
|
mon_error("Unable to create file"); |
658 |
|
else { |
659 |
< |
uint32 adr = start_adr, end_adr = start_adr + size - 1; |
659 |
> |
uintptr adr = start_adr, end_adr = start_adr + size - 1; |
660 |
|
|
661 |
|
while (adr <= end_adr) |
662 |
|
fputc(mon_read_byte(adr++), file); |
663 |
|
fclose(file); |
664 |
|
|
665 |
< |
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); |
665 |
> |
fprintf(monerr, "%08x bytes written from %0*lx to %0*lx\n", size, 2 * sizeof(adr), mon_use_real_mem ? start_adr : start_adr % mon_mem_size, 2 * sizeof(adr), mon_use_real_mem ? end_adr : end_adr % mon_mem_size); |
666 |
|
} |
667 |
|
} |