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-2004 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 |
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" |
30 |
< |
#include "version.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 |
|
|
82 |
|
|
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); |
85 |
> |
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 |
|
mon_get_token(); |
90 |
|
} else if (mon_expression(&value)) { |
91 |
< |
*s++ = value; |
91 |
> |
str = (uint8 *)realloc(str, (len + GRANULARITY) & ~(GRANULARITY - 1)); |
92 |
> |
str[len] = value; |
93 |
|
len++; |
94 |
< |
} else |
94 |
> |
} else { |
95 |
> |
if (str) |
96 |
> |
free(str); |
97 |
|
return false; |
98 |
+ |
} |
99 |
|
|
100 |
< |
} else if (mon_token == T_END) |
100 |
> |
} else if (mon_token == T_END) { |
101 |
|
return true; |
102 |
< |
else { |
102 |
> |
} else { |
103 |
|
mon_error("',' expected"); |
104 |
+ |
if (str) |
105 |
+ |
free(str); |
106 |
|
return false; |
107 |
|
} |
108 |
|
} |
126 |
|
|
127 |
|
void version(void) |
128 |
|
{ |
129 |
< |
fprintf(monout, "mon V%d.%d\n", VERSION_MAJOR, VERSION_MINOR); |
129 |
> |
fprintf(monout, "cxmon V" VERSION "\n"); |
130 |
|
} |
131 |
|
|
132 |
|
|
169 |
|
|
170 |
|
void print_expr(void) |
171 |
|
{ |
172 |
< |
uint32 val; |
172 |
> |
uintptr val; |
173 |
|
|
174 |
|
if (!mon_expression(&val)) |
175 |
|
return; |
179 |
|
} |
180 |
|
|
181 |
|
if (val > 0x7fffffff) { |
182 |
< |
fprintf(monout, "Hex unsigned: $%08lx\n" |
183 |
< |
"Hex signed : -$%08lx\n" |
184 |
< |
"Dec unsigned: %lu\n" |
185 |
< |
"Dec signed : %ld\n", val, -val, val, val); |
182 |
> |
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 |
|
fprintf(monout, "Char : '%c%c%c%c'\n", char2print(val >> 24), char2print(val >> 16), char2print(val >> 8), char2print(val)); |
187 |
|
} else { |
188 |
< |
fprintf(monout, "Hex : $%08lx\n" |
189 |
< |
"Dec : %ld\n", val, val); |
188 |
> |
fprintf(monout, "Hex : $%08x\n" |
189 |
> |
"Dec : %d\n", val, val); |
190 |
|
fprintf(monout, "Char: '%c%c%c%c'\n", char2print(val >> 24), char2print(val >> 16), char2print(val >> 8), char2print(val)); |
191 |
|
} |
192 |
|
} |
221 |
|
|
222 |
|
void memory_dump(void) |
223 |
|
{ |
224 |
< |
uint32 adr, end_adr; |
224 |
> |
uintptr adr, end_adr; |
225 |
|
uint8 mem[MEMDUMP_BPL + 1]; |
226 |
|
|
227 |
|
mem[MEMDUMP_BPL] = 0; |
230 |
|
return; |
231 |
|
|
232 |
|
while (adr <= end_adr && !mon_aborted()) { |
233 |
< |
fprintf(monout, "%08lx:", mon_use_real_mem ? adr: adr % mon_mem_size); |
233 |
> |
fprintf(monout, "%0*lx:", int(2 * sizeof(adr)), mon_use_real_mem ? adr: adr % mon_mem_size); |
234 |
|
for (int i=0; i<MEMDUMP_BPL; i++, adr++) { |
235 |
|
if (i % 4 == 0) |
236 |
< |
fprintf(monout, " %08lx", mon_read_word(adr)); |
236 |
> |
fprintf(monout, " %08x", mon_read_word(adr)); |
237 |
|
mem[i] = char2print(mon_read_byte(adr)); |
238 |
|
} |
239 |
|
fprintf(monout, " '%s'\n", mem); |
252 |
|
|
253 |
|
void ascii_dump(void) |
254 |
|
{ |
255 |
< |
uint32 adr, end_adr; |
255 |
> |
uintptr adr, end_adr; |
256 |
|
uint8 str[ASCIIDUMP_BPL + 1]; |
257 |
|
|
258 |
|
str[ASCIIDUMP_BPL] = 0; |
261 |
|
return; |
262 |
|
|
263 |
|
while (adr <= end_adr && !mon_aborted()) { |
264 |
< |
fprintf(monout, "%08lx:", mon_use_real_mem ? adr : adr % mon_mem_size); |
264 |
> |
fprintf(monout, "%0*lx:", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
265 |
|
for (int i=0; i<ASCIIDUMP_BPL; i++, adr++) |
266 |
|
str[i] = char2print(mon_read_byte(adr)); |
267 |
|
fprintf(monout, " '%s'\n", str); |
272 |
|
|
273 |
|
|
274 |
|
/* |
275 |
+ |
* Binary dump |
276 |
+ |
* b [start [end]] |
277 |
+ |
*/ |
278 |
+ |
|
279 |
+ |
void binary_dump(void) |
280 |
+ |
{ |
281 |
+ |
uintptr adr, end_adr; |
282 |
+ |
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 |
+ |
fprintf(monout, "%0*lx:", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
291 |
+ |
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 |
+ |
} |
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 |
+ |
* d8086 [start [end]] |
310 |
|
*/ |
311 |
|
|
312 |
|
enum CPUType { |
313 |
|
CPU_PPC, |
314 |
|
CPU_6502, |
315 |
|
CPU_680x0, |
316 |
< |
CPU_8080, |
317 |
< |
CPU_80x86 |
316 |
> |
CPU_Z80, |
317 |
> |
CPU_80x86_32, |
318 |
> |
CPU_80x86_16, |
319 |
> |
CPU_x86_64, |
320 |
> |
CPU_MIPS, |
321 |
> |
CPU_MIPS_LITTLE, |
322 |
|
}; |
323 |
|
|
324 |
|
static void disassemble(CPUType type) |
325 |
|
{ |
326 |
< |
uint32 adr, end_adr; |
326 |
> |
uintptr adr, end_adr; |
327 |
|
|
328 |
|
if (!range_args(&adr, &end_adr, 16 * 4 - 1)) // 16 lines unless end address specified |
329 |
|
return; |
332 |
|
case CPU_PPC: |
333 |
|
while (adr <= end_adr && !mon_aborted()) { |
334 |
|
uint32 w = mon_read_word(adr); |
335 |
< |
fprintf(monout, "%08lx: %08lx\t", mon_use_real_mem ? adr : adr % mon_mem_size, w); |
335 |
> |
fprintf(monout, "%0*lx: %08x\t", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size, w); |
336 |
|
disass_ppc(monout, mon_use_real_mem ? adr : adr % mon_mem_size, w); |
337 |
|
adr += 4; |
338 |
|
} |
343 |
|
uint8 op = mon_read_byte(adr); |
344 |
|
uint8 lo = mon_read_byte(adr + 1); |
345 |
|
uint8 hi = mon_read_byte(adr + 2); |
346 |
< |
fprintf(monout, "%08lx: ", mon_use_real_mem ? adr : adr % mon_mem_size); |
346 |
> |
fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
347 |
|
adr += disass_6502(monout, mon_use_real_mem ? adr : adr % mon_mem_size, op, lo, hi); |
348 |
|
} |
349 |
|
break; |
350 |
|
|
351 |
|
case CPU_680x0: |
352 |
|
while (adr <= end_adr && !mon_aborted()) { |
353 |
< |
uint16 buf[8]; |
354 |
< |
buf[0] = mon_read_half(adr); |
313 |
< |
buf[1] = mon_read_half(adr + 2); |
314 |
< |
buf[2] = mon_read_half(adr + 4); |
315 |
< |
buf[3] = mon_read_half(adr + 6); |
316 |
< |
buf[4] = mon_read_half(adr + 8); |
317 |
< |
buf[5] = mon_read_half(adr + 10); |
318 |
< |
buf[6] = mon_read_half(adr + 12); |
319 |
< |
buf[7] = mon_read_half(adr + 14); |
320 |
< |
fprintf(monout, "%08lx: ", mon_use_real_mem ? adr : adr % mon_mem_size); |
321 |
< |
adr += disass_68k(monout, mon_use_real_mem ? adr : adr % mon_mem_size, buf); |
353 |
> |
fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
354 |
> |
adr += disass_68k(monout, mon_use_real_mem ? adr : adr % mon_mem_size); |
355 |
|
} |
356 |
|
break; |
357 |
|
|
358 |
< |
case CPU_8080: |
358 |
> |
case CPU_Z80: |
359 |
|
while (adr <= end_adr && !mon_aborted()) { |
360 |
< |
uint8 op = mon_read_byte(adr); |
361 |
< |
uint8 lo = mon_read_byte(adr + 1); |
329 |
< |
uint8 hi = mon_read_byte(adr + 2); |
330 |
< |
fprintf(monout, "%08lx: ", mon_use_real_mem ? adr : adr % mon_mem_size); |
331 |
< |
adr += disass_8080(monout, mon_use_real_mem ? adr : adr % mon_mem_size, op, lo, hi); |
360 |
> |
fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
361 |
> |
adr += disass_z80(monout, mon_use_real_mem ? adr : adr % mon_mem_size); |
362 |
|
} |
363 |
|
break; |
364 |
|
|
365 |
< |
case CPU_80x86: |
365 |
> |
case CPU_MIPS: |
366 |
> |
case CPU_MIPS_LITTLE: |
367 |
|
while (adr <= end_adr && !mon_aborted()) { |
368 |
< |
uint8 buf[16]; |
369 |
< |
for (int i=0; i<16; i++) |
339 |
< |
buf[i] = mon_read_byte(adr + i); |
340 |
< |
fprintf(monout, "%08lx: ", mon_use_real_mem ? adr : adr % mon_mem_size); |
341 |
< |
adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, buf); |
368 |
> |
fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
369 |
> |
adr += disass_mips(monout, mon_use_real_mem ? adr : adr % mon_mem_size, type == CPU_MIPS_LITTLE); |
370 |
|
} |
371 |
|
break; |
372 |
+ |
|
373 |
+ |
case CPU_x86_64: |
374 |
+ |
while (adr <= end_adr && !mon_aborted()) { |
375 |
+ |
fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
376 |
+ |
adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, 64); |
377 |
+ |
} |
378 |
+ |
break; |
379 |
+ |
|
380 |
+ |
case CPU_80x86_32: |
381 |
+ |
while (adr <= end_adr && !mon_aborted()) { |
382 |
+ |
fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
383 |
+ |
adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, 32); |
384 |
+ |
} |
385 |
+ |
break; |
386 |
+ |
|
387 |
+ |
case CPU_80x86_16: |
388 |
+ |
while (adr <= end_adr && !mon_aborted()) { |
389 |
+ |
fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
390 |
+ |
adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, 16); |
391 |
+ |
} |
392 |
|
} |
393 |
|
|
394 |
|
mon_dot_address = adr; |
409 |
|
disassemble(CPU_680x0); |
410 |
|
} |
411 |
|
|
412 |
< |
void disassemble_8080(void) |
412 |
> |
void disassemble_z80(void) |
413 |
|
{ |
414 |
< |
disassemble(CPU_8080); |
414 |
> |
disassemble(CPU_Z80); |
415 |
|
} |
416 |
|
|
417 |
< |
void disassemble_80x86(void) |
417 |
> |
void disassemble_80x86_32(void) |
418 |
|
{ |
419 |
< |
disassemble(CPU_80x86); |
419 |
> |
disassemble(CPU_80x86_32); |
420 |
> |
} |
421 |
> |
|
422 |
> |
void disassemble_80x86_16(void) |
423 |
> |
{ |
424 |
> |
disassemble(CPU_80x86_16); |
425 |
> |
} |
426 |
> |
|
427 |
> |
void disassemble_x86_64(void) |
428 |
> |
{ |
429 |
> |
disassemble(CPU_x86_64); |
430 |
> |
} |
431 |
> |
|
432 |
> |
void disassemble_mips(void) |
433 |
> |
{ |
434 |
> |
disassemble(CPU_MIPS); |
435 |
> |
} |
436 |
> |
|
437 |
> |
void disassemble_mipsel(void) |
438 |
> |
{ |
439 |
> |
disassemble(CPU_MIPS_LITTLE); |
440 |
|
} |
441 |
|
|
442 |
|
|
447 |
|
|
448 |
|
void modify(void) |
449 |
|
{ |
450 |
< |
uint32 adr, len, src_adr = 0; |
451 |
< |
uint8 str[256]; |
450 |
> |
uintptr adr, len, src_adr = 0; |
451 |
> |
uint8 *str; |
452 |
|
|
453 |
|
if (!mon_expression(&adr)) |
454 |
|
return; |
457 |
|
|
458 |
|
while (src_adr < len) |
459 |
|
mon_write_byte(adr++, str[src_adr++]); |
392 |
– |
|
460 |
|
mon_dot_address = adr; |
461 |
+ |
|
462 |
+ |
free(str); |
463 |
|
} |
464 |
|
|
465 |
|
|
470 |
|
|
471 |
|
void fill(void) |
472 |
|
{ |
473 |
< |
uint32 adr, end_adr, len, src_adr = 0; |
474 |
< |
uint8 str[256]; |
473 |
> |
uintptr adr, end_adr, len, src_adr = 0; |
474 |
> |
uint8 *str; |
475 |
|
|
476 |
|
if (!mon_expression(&adr)) |
477 |
|
return; |
482 |
|
|
483 |
|
while (adr <= end_adr) |
484 |
|
mon_write_byte(adr++, str[src_adr++ % len]); |
485 |
+ |
|
486 |
+ |
free(str); |
487 |
|
} |
488 |
|
|
489 |
|
|
494 |
|
|
495 |
|
void transfer(void) |
496 |
|
{ |
497 |
< |
uint32 adr, end_adr, dest; |
497 |
> |
uintptr adr, end_adr, dest; |
498 |
|
int num; |
499 |
|
|
500 |
|
if (!mon_expression(&adr)) |
528 |
|
|
529 |
|
void compare(void) |
530 |
|
{ |
531 |
< |
uint32 adr, end_adr, dest; |
531 |
> |
uintptr adr, end_adr, dest; |
532 |
|
int num = 0; |
533 |
|
|
534 |
|
if (!mon_expression(&adr)) |
544 |
|
|
545 |
|
while (adr <= end_adr && !mon_aborted()) { |
546 |
|
if (mon_read_byte(adr) != mon_read_byte(dest)) { |
547 |
< |
fprintf(monout, "%08lx ", mon_use_real_mem ? adr : adr % mon_mem_size); |
547 |
> |
fprintf(monout, "%0*lx ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
548 |
|
num++; |
549 |
|
if (!(num & 7)) |
550 |
|
fputc('\n', monout); |
565 |
|
|
566 |
|
void hunt(void) |
567 |
|
{ |
568 |
< |
uint32 adr, end_adr, len; |
569 |
< |
uint8 str[256]; |
568 |
> |
uintptr adr, end_adr, len; |
569 |
> |
uint8 *str; |
570 |
|
int num = 0; |
571 |
|
|
572 |
|
if (!mon_expression(&adr)) |
584 |
|
break; |
585 |
|
|
586 |
|
if (i == len) { |
587 |
< |
fprintf(monout, "%08lx ", mon_use_real_mem ? adr : adr % mon_mem_size); |
587 |
> |
fprintf(monout, "%0*lx ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
588 |
|
num++; |
589 |
|
if (num == 1) |
590 |
|
mon_dot_address = adr; |
594 |
|
adr++; |
595 |
|
} |
596 |
|
|
597 |
+ |
free(str); |
598 |
+ |
|
599 |
|
if (num & 7) |
600 |
|
fputc('\n', monout); |
601 |
|
fprintf(monout, "Found %d occurrences\n", num); |
609 |
|
|
610 |
|
void load_data(void) |
611 |
|
{ |
612 |
< |
uint32 start_adr; |
612 |
> |
uintptr start_adr; |
613 |
|
FILE *file; |
614 |
|
int fc; |
615 |
|
|
632 |
|
if (!(file = fopen(mon_string, "rb"))) |
633 |
|
mon_error("Unable to open file"); |
634 |
|
else { |
635 |
< |
uint32 adr = start_adr; |
635 |
> |
uintptr adr = start_adr; |
636 |
|
|
637 |
|
while ((fc = fgetc(file)) != EOF) |
638 |
|
mon_write_byte(adr++, fc); |
639 |
|
fclose(file); |
640 |
|
|
641 |
< |
fprintf(monerr, "%08lx bytes read from %08lx to %08lx\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); |
641 |
> |
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); |
642 |
|
mon_dot_address = adr; |
643 |
|
} |
644 |
|
} |
651 |
|
|
652 |
|
void save_data(void) |
653 |
|
{ |
654 |
< |
uint32 start_adr, size; |
654 |
> |
uintptr start_adr, size; |
655 |
|
FILE *file; |
656 |
|
|
657 |
|
if (!mon_expression(&start_adr)) |
675 |
|
if (!(file = fopen(mon_string, "wb"))) |
676 |
|
mon_error("Unable to create file"); |
677 |
|
else { |
678 |
< |
uint32 adr = start_adr, end_adr = start_adr + size - 1; |
678 |
> |
uintptr adr = start_adr, end_adr = start_adr + size - 1; |
679 |
|
|
680 |
|
while (adr <= end_adr) |
681 |
|
fputc(mon_read_byte(adr++), file); |
682 |
|
fclose(file); |
683 |
|
|
684 |
< |
fprintf(monerr, "%08lx bytes written from %08lx to %08lx\n", size, mon_use_real_mem ? start_adr : start_adr % mon_mem_size, mon_use_real_mem ? end_adr : end_adr % mon_mem_size); |
684 |
> |
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); |
685 |
|
} |
686 |
|
} |