1 |
|
/* |
2 |
< |
* mon_cmd.cpp - mon standard commands |
2 |
> |
* mon_cmd.cpp - cxmon standard commands |
3 |
|
* |
4 |
< |
* (C) 1997-1999 Christian Bauer |
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 |
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 |
|
*/ |
20 |
|
|
21 |
< |
#include <stdio.h> |
22 |
< |
#include <string.h> |
23 |
< |
#include <unistd.h> |
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" |
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", MON_VERSION, MON_REVISION); |
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 |
|
}; |
321 |
|
|
322 |
|
static void disassemble(CPUType type) |
323 |
|
{ |
324 |
< |
uint32 adr, end_adr; |
324 |
> |
uintptr adr, end_adr; |
325 |
|
|
326 |
|
if (!range_args(&adr, &end_adr, 16 * 4 - 1)) // 16 lines unless end address specified |
327 |
|
return; |
330 |
|
case CPU_PPC: |
331 |
|
while (adr <= end_adr && !mon_aborted()) { |
332 |
|
uint32 w = mon_read_word(adr); |
333 |
< |
fprintf(monout, "%08lx: %08lx\t", mon_use_real_mem ? adr : adr % mon_mem_size, w); |
333 |
> |
fprintf(monout, "%0*lx: %08x\t", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size, w); |
334 |
|
disass_ppc(monout, mon_use_real_mem ? adr : adr % mon_mem_size, w); |
335 |
|
adr += 4; |
336 |
|
} |
341 |
|
uint8 op = mon_read_byte(adr); |
342 |
|
uint8 lo = mon_read_byte(adr + 1); |
343 |
|
uint8 hi = mon_read_byte(adr + 2); |
344 |
< |
fprintf(monout, "%08lx: ", mon_use_real_mem ? adr : adr % mon_mem_size); |
344 |
> |
fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
345 |
|
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 |
< |
uint16 buf[8]; |
352 |
< |
buf[0] = mon_read_half(adr); |
300 |
< |
buf[1] = mon_read_half(adr + 2); |
301 |
< |
buf[2] = mon_read_half(adr + 4); |
302 |
< |
buf[3] = mon_read_half(adr + 6); |
303 |
< |
buf[4] = mon_read_half(adr + 8); |
304 |
< |
buf[5] = mon_read_half(adr + 10); |
305 |
< |
buf[6] = mon_read_half(adr + 12); |
306 |
< |
buf[7] = mon_read_half(adr + 14); |
307 |
< |
fprintf(monout, "%08lx: ", mon_use_real_mem ? adr : adr % mon_mem_size); |
308 |
< |
adr += disass_68k(monout, mon_use_real_mem ? adr : adr % mon_mem_size, buf); |
351 |
> |
fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
352 |
> |
adr += disass_68k(monout, mon_use_real_mem ? adr : adr % mon_mem_size); |
353 |
|
} |
354 |
|
break; |
355 |
|
|
356 |
< |
case CPU_8080: |
356 |
> |
case CPU_Z80: |
357 |
|
while (adr <= end_adr && !mon_aborted()) { |
358 |
< |
uint8 op = mon_read_byte(adr); |
359 |
< |
uint8 lo = mon_read_byte(adr + 1); |
316 |
< |
uint8 hi = mon_read_byte(adr + 2); |
317 |
< |
fprintf(monout, "%08lx: ", mon_use_real_mem ? adr : adr % mon_mem_size); |
318 |
< |
adr += disass_8080(monout, mon_use_real_mem ? adr : adr % mon_mem_size, op, lo, hi); |
358 |
> |
fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
359 |
> |
adr += disass_z80(monout, mon_use_real_mem ? adr : adr % mon_mem_size); |
360 |
|
} |
361 |
|
break; |
362 |
|
|
363 |
< |
case CPU_80x86: |
363 |
> |
case CPU_x86_64: |
364 |
|
while (adr <= end_adr && !mon_aborted()) { |
365 |
< |
uint8 buf[16]; |
366 |
< |
for (int i=0; i<16; i++) |
326 |
< |
buf[i] = mon_read_byte(adr + i); |
327 |
< |
fprintf(monout, "%08lx: ", mon_use_real_mem ? adr : adr % mon_mem_size); |
328 |
< |
adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, buf); |
365 |
> |
fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
366 |
> |
adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, 64); |
367 |
|
} |
368 |
|
break; |
369 |
+ |
|
370 |
+ |
case CPU_80x86_32: |
371 |
+ |
while (adr <= end_adr && !mon_aborted()) { |
372 |
+ |
fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
373 |
+ |
adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, 32); |
374 |
+ |
} |
375 |
+ |
break; |
376 |
+ |
|
377 |
+ |
case CPU_80x86_16: |
378 |
+ |
while (adr <= end_adr && !mon_aborted()) { |
379 |
+ |
fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
380 |
+ |
adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, 16); |
381 |
+ |
} |
382 |
|
} |
383 |
|
|
384 |
|
mon_dot_address = adr; |
399 |
|
disassemble(CPU_680x0); |
400 |
|
} |
401 |
|
|
402 |
< |
void disassemble_8080(void) |
402 |
> |
void disassemble_z80(void) |
403 |
> |
{ |
404 |
> |
disassemble(CPU_Z80); |
405 |
> |
} |
406 |
> |
|
407 |
> |
void disassemble_80x86_32(void) |
408 |
|
{ |
409 |
< |
disassemble(CPU_8080); |
409 |
> |
disassemble(CPU_80x86_32); |
410 |
|
} |
411 |
|
|
412 |
< |
void disassemble_80x86(void) |
412 |
> |
void disassemble_80x86_16(void) |
413 |
|
{ |
414 |
< |
disassemble(CPU_80x86); |
414 |
> |
disassemble(CPU_80x86_16); |
415 |
> |
} |
416 |
> |
|
417 |
> |
void disassemble_x86_64(void) |
418 |
> |
{ |
419 |
> |
disassemble(CPU_x86_64); |
420 |
|
} |
421 |
|
|
422 |
|
|
427 |
|
|
428 |
|
void modify(void) |
429 |
|
{ |
430 |
< |
uint32 adr, len, src_adr = 0; |
431 |
< |
uint8 str[256]; |
430 |
> |
uintptr adr, len, src_adr = 0; |
431 |
> |
uint8 *str; |
432 |
|
|
433 |
|
if (!mon_expression(&adr)) |
434 |
|
return; |
437 |
|
|
438 |
|
while (src_adr < len) |
439 |
|
mon_write_byte(adr++, str[src_adr++]); |
379 |
– |
|
440 |
|
mon_dot_address = adr; |
441 |
+ |
|
442 |
+ |
free(str); |
443 |
|
} |
444 |
|
|
445 |
|
|
450 |
|
|
451 |
|
void fill(void) |
452 |
|
{ |
453 |
< |
uint32 adr, end_adr, len, src_adr = 0; |
454 |
< |
uint8 str[256]; |
453 |
> |
uintptr adr, end_adr, len, src_adr = 0; |
454 |
> |
uint8 *str; |
455 |
|
|
456 |
|
if (!mon_expression(&adr)) |
457 |
|
return; |
462 |
|
|
463 |
|
while (adr <= end_adr) |
464 |
|
mon_write_byte(adr++, str[src_adr++ % len]); |
465 |
+ |
|
466 |
+ |
free(str); |
467 |
|
} |
468 |
|
|
469 |
|
|
474 |
|
|
475 |
|
void transfer(void) |
476 |
|
{ |
477 |
< |
uint32 adr, end_adr, dest; |
477 |
> |
uintptr adr, end_adr, dest; |
478 |
|
int num; |
479 |
|
|
480 |
|
if (!mon_expression(&adr)) |
508 |
|
|
509 |
|
void compare(void) |
510 |
|
{ |
511 |
< |
uint32 adr, end_adr, dest; |
511 |
> |
uintptr adr, end_adr, dest; |
512 |
|
int num = 0; |
513 |
|
|
514 |
|
if (!mon_expression(&adr)) |
524 |
|
|
525 |
|
while (adr <= end_adr && !mon_aborted()) { |
526 |
|
if (mon_read_byte(adr) != mon_read_byte(dest)) { |
527 |
< |
fprintf(monout, "%08lx ", mon_use_real_mem ? adr : adr % mon_mem_size); |
527 |
> |
fprintf(monout, "%0*lx ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
528 |
|
num++; |
529 |
|
if (!(num & 7)) |
530 |
|
fputc('\n', monout); |
545 |
|
|
546 |
|
void hunt(void) |
547 |
|
{ |
548 |
< |
uint32 adr, end_adr, len; |
549 |
< |
uint8 str[256]; |
548 |
> |
uintptr adr, end_adr, len; |
549 |
> |
uint8 *str; |
550 |
|
int num = 0; |
551 |
|
|
552 |
|
if (!mon_expression(&adr)) |
564 |
|
break; |
565 |
|
|
566 |
|
if (i == len) { |
567 |
< |
fprintf(monout, "%08lx ", mon_use_real_mem ? adr : adr % mon_mem_size); |
567 |
> |
fprintf(monout, "%0*lx ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); |
568 |
|
num++; |
569 |
|
if (num == 1) |
570 |
|
mon_dot_address = adr; |
574 |
|
adr++; |
575 |
|
} |
576 |
|
|
577 |
+ |
free(str); |
578 |
+ |
|
579 |
|
if (num & 7) |
580 |
|
fputc('\n', monout); |
581 |
|
fprintf(monout, "Found %d occurrences\n", num); |
589 |
|
|
590 |
|
void load_data(void) |
591 |
|
{ |
592 |
< |
uint32 start_adr; |
592 |
> |
uintptr start_adr; |
593 |
|
FILE *file; |
594 |
|
int fc; |
595 |
|
|
612 |
|
if (!(file = fopen(mon_string, "rb"))) |
613 |
|
mon_error("Unable to open file"); |
614 |
|
else { |
615 |
< |
uint32 adr = start_adr; |
615 |
> |
uintptr adr = start_adr; |
616 |
|
|
617 |
|
while ((fc = fgetc(file)) != EOF) |
618 |
|
mon_write_byte(adr++, fc); |
619 |
|
fclose(file); |
620 |
|
|
621 |
< |
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); |
621 |
> |
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 |
|
mon_dot_address = adr; |
623 |
|
} |
624 |
|
} |
631 |
|
|
632 |
|
void save_data(void) |
633 |
|
{ |
634 |
< |
uint32 start_adr, size; |
634 |
> |
uintptr start_adr, size; |
635 |
|
FILE *file; |
636 |
|
|
637 |
|
if (!mon_expression(&start_adr)) |
655 |
|
if (!(file = fopen(mon_string, "wb"))) |
656 |
|
mon_error("Unable to create file"); |
657 |
|
else { |
658 |
< |
uint32 adr = start_adr, end_adr = start_adr + size - 1; |
658 |
> |
uintptr adr = start_adr, end_adr = start_adr + size - 1; |
659 |
|
|
660 |
|
while (adr <= end_adr) |
661 |
|
fputc(mon_read_byte(adr++), file); |
662 |
|
fclose(file); |
663 |
|
|
664 |
< |
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); |
664 |
> |
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 |
|
} |
666 |
|
} |