--- mon/src/mon_cmd.cpp 2000/04/24 13:10:17 1.5 +++ mon/src/mon_cmd.cpp 2007/01/21 17:32:05 1.19 @@ -1,7 +1,7 @@ /* - * mon_cmd.cpp - mon standard commands + * mon_cmd.cpp - cxmon standard commands * - * mon (C) 1997-1999 Christian Bauer, Marc Hellwig + * cxmon (C) 1997-2004 Christian Bauer, Marc Hellwig * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,11 +24,11 @@ #include "mon.h" #include "mon_cmd.h" -#include "mon_ppc.h" -#include "mon_68k.h" -#include "mon_x86.h" -#include "mon_6502.h" -#include "mon_8080.h" +#include "mon_disass.h" + +#ifndef VERSION +#define VERSION "3" +#endif /* @@ -40,7 +40,7 @@ * true: OK, false: Error */ -static bool range_args(uint32 *adr, uint32 *end_adr, uint32 def_range) +static bool range_args(uintptr *adr, uintptr *end_adr, uint32 def_range) { *adr = mon_dot_address; *end_adr = mon_dot_address + def_range; @@ -67,10 +67,12 @@ static bool range_args(uint32 *adr, uint * byte_string = (expression | STRING) {COMMA (expression | STRING)} END */ -static bool byte_string(uint8 *s, uint32 &len) +static bool byte_string(uint8 *&str, uintptr &len) { - uint32 value; + uintptr value; + static const int GRANULARITY = 16; // must be a power of 2 + str = NULL; len = 0; goto start; @@ -80,21 +82,27 @@ static bool byte_string(uint8 *s, uint32 start: if (mon_token == T_STRING) { - uint8 *p = (uint8 *)mon_string; - while ((*s++ = *p++) != 0) ; - s--; - len += strlen(mon_string); + unsigned n = strlen(mon_string); + str = (uint8 *)realloc(str, (len + n - 1 + GRANULARITY) & ~(GRANULARITY - 1)); + memcpy(str + len, mon_string, n); + len += n; mon_get_token(); } else if (mon_expression(&value)) { - *s++ = value; + str = (uint8 *)realloc(str, (len + GRANULARITY) & ~(GRANULARITY - 1)); + str[len] = value; len++; - } else + } else { + if (str) + free(str); return false; + } - } else if (mon_token == T_END) + } else if (mon_token == T_END) { return true; - else { + } else { mon_error("',' expected"); + if (str) + free(str); return false; } } @@ -118,7 +126,7 @@ static inline uint8 char2print(uint8 c) void version(void) { - fprintf(monout, "mon V" VERSION "\n"); + fprintf(monout, "cxmon V" VERSION "\n"); } @@ -161,7 +169,7 @@ void redir_output(void) void print_expr(void) { - uint32 val; + uintptr val; if (!mon_expression(&val)) return; @@ -213,7 +221,7 @@ void shell_command(void) void memory_dump(void) { - uint32 adr, end_adr; + uintptr adr, end_adr; uint8 mem[MEMDUMP_BPL + 1]; mem[MEMDUMP_BPL] = 0; @@ -222,7 +230,7 @@ void memory_dump(void) return; while (adr <= end_adr && !mon_aborted()) { - fprintf(monout, "%08x:", mon_use_real_mem ? adr: adr % mon_mem_size); + fprintf(monout, "%0*lx:", int(2 * sizeof(adr)), mon_use_real_mem ? adr: adr % mon_mem_size); for (int i=0; i>=1, i++) str[i] = (b & m) ? '*' : '.'; @@ -298,19 +306,22 @@ void binary_dump(void) * d68 [start [end]] * d80 [start [end]] * d86 [start [end]] + * d8086 [start [end]] */ enum CPUType { CPU_PPC, CPU_6502, CPU_680x0, - CPU_8080, - CPU_80x86 + CPU_Z80, + CPU_80x86_32, + CPU_80x86_16, + CPU_x86_64 }; static void disassemble(CPUType type) { - uint32 adr, end_adr; + uintptr adr, end_adr; if (!range_args(&adr, &end_adr, 16 * 4 - 1)) // 16 lines unless end address specified return; @@ -319,7 +330,7 @@ static void disassemble(CPUType type) case CPU_PPC: while (adr <= end_adr && !mon_aborted()) { uint32 w = mon_read_word(adr); - fprintf(monout, "%08x: %08x\t", mon_use_real_mem ? adr : adr % mon_mem_size, w); + fprintf(monout, "%0*lx: %08x\t", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size, w); disass_ppc(monout, mon_use_real_mem ? adr : adr % mon_mem_size, w); adr += 4; } @@ -330,46 +341,44 @@ static void disassemble(CPUType type) uint8 op = mon_read_byte(adr); uint8 lo = mon_read_byte(adr + 1); uint8 hi = mon_read_byte(adr + 2); - fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size); + fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); adr += disass_6502(monout, mon_use_real_mem ? adr : adr % mon_mem_size, op, lo, hi); } break; case CPU_680x0: while (adr <= end_adr && !mon_aborted()) { - uint16 buf[8]; - buf[0] = mon_read_half(adr); - buf[1] = mon_read_half(adr + 2); - buf[2] = mon_read_half(adr + 4); - buf[3] = mon_read_half(adr + 6); - buf[4] = mon_read_half(adr + 8); - buf[5] = mon_read_half(adr + 10); - buf[6] = mon_read_half(adr + 12); - buf[7] = mon_read_half(adr + 14); - fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size); - adr += disass_68k(monout, mon_use_real_mem ? adr : adr % mon_mem_size, buf); + fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); + adr += disass_68k(monout, mon_use_real_mem ? adr : adr % mon_mem_size); } break; - case CPU_8080: + case CPU_Z80: while (adr <= end_adr && !mon_aborted()) { - uint8 op = mon_read_byte(adr); - uint8 lo = mon_read_byte(adr + 1); - uint8 hi = mon_read_byte(adr + 2); - fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size); - adr += disass_8080(monout, mon_use_real_mem ? adr : adr % mon_mem_size, op, lo, hi); + fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); + adr += disass_z80(monout, mon_use_real_mem ? adr : adr % mon_mem_size); } break; - case CPU_80x86: + case CPU_x86_64: while (adr <= end_adr && !mon_aborted()) { - uint8 buf[16]; - for (int i=0; i<16; i++) - buf[i] = mon_read_byte(adr + i); - fprintf(monout, "%08x: ", mon_use_real_mem ? adr : adr % mon_mem_size); - adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, buf); + fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); + adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, 64); } break; + + case CPU_80x86_32: + while (adr <= end_adr && !mon_aborted()) { + fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); + adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, 32); + } + break; + + case CPU_80x86_16: + while (adr <= end_adr && !mon_aborted()) { + fprintf(monout, "%0*lx: ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); + adr += disass_x86(monout, mon_use_real_mem ? adr : adr % mon_mem_size, 16); + } } mon_dot_address = adr; @@ -390,14 +399,24 @@ void disassemble_680x0(void) disassemble(CPU_680x0); } -void disassemble_8080(void) +void disassemble_z80(void) +{ + disassemble(CPU_Z80); +} + +void disassemble_80x86_32(void) { - disassemble(CPU_8080); + disassemble(CPU_80x86_32); } -void disassemble_80x86(void) +void disassemble_80x86_16(void) { - disassemble(CPU_80x86); + disassemble(CPU_80x86_16); +} + +void disassemble_x86_64(void) +{ + disassemble(CPU_x86_64); } @@ -408,8 +427,8 @@ void disassemble_80x86(void) void modify(void) { - uint32 adr, len, src_adr = 0; - uint8 str[256]; + uintptr adr, len, src_adr = 0; + uint8 *str; if (!mon_expression(&adr)) return; @@ -418,8 +437,9 @@ void modify(void) while (src_adr < len) mon_write_byte(adr++, str[src_adr++]); - mon_dot_address = adr; + + free(str); } @@ -430,8 +450,8 @@ void modify(void) void fill(void) { - uint32 adr, end_adr, len, src_adr = 0; - uint8 str[256]; + uintptr adr, end_adr, len, src_adr = 0; + uint8 *str; if (!mon_expression(&adr)) return; @@ -442,6 +462,8 @@ void fill(void) while (adr <= end_adr) mon_write_byte(adr++, str[src_adr++ % len]); + + free(str); } @@ -452,7 +474,7 @@ void fill(void) void transfer(void) { - uint32 adr, end_adr, dest; + uintptr adr, end_adr, dest; int num; if (!mon_expression(&adr)) @@ -486,7 +508,7 @@ void transfer(void) void compare(void) { - uint32 adr, end_adr, dest; + uintptr adr, end_adr, dest; int num = 0; if (!mon_expression(&adr)) @@ -502,7 +524,7 @@ void compare(void) while (adr <= end_adr && !mon_aborted()) { if (mon_read_byte(adr) != mon_read_byte(dest)) { - fprintf(monout, "%08x ", mon_use_real_mem ? adr : adr % mon_mem_size); + fprintf(monout, "%0*lx ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); num++; if (!(num & 7)) fputc('\n', monout); @@ -523,8 +545,8 @@ void compare(void) void hunt(void) { - uint32 adr, end_adr, len; - uint8 str[256]; + uintptr adr, end_adr, len; + uint8 *str; int num = 0; if (!mon_expression(&adr)) @@ -542,7 +564,7 @@ void hunt(void) break; if (i == len) { - fprintf(monout, "%08x ", mon_use_real_mem ? adr : adr % mon_mem_size); + fprintf(monout, "%0*lx ", int(2 * sizeof(adr)), mon_use_real_mem ? adr : adr % mon_mem_size); num++; if (num == 1) mon_dot_address = adr; @@ -552,6 +574,8 @@ void hunt(void) adr++; } + free(str); + if (num & 7) fputc('\n', monout); fprintf(monout, "Found %d occurrences\n", num); @@ -565,7 +589,7 @@ void hunt(void) void load_data(void) { - uint32 start_adr; + uintptr start_adr; FILE *file; int fc; @@ -588,13 +612,13 @@ void load_data(void) if (!(file = fopen(mon_string, "rb"))) mon_error("Unable to open file"); else { - uint32 adr = start_adr; + uintptr adr = start_adr; while ((fc = fgetc(file)) != EOF) mon_write_byte(adr++, fc); fclose(file); - 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); + 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); mon_dot_address = adr; } } @@ -607,7 +631,7 @@ void load_data(void) void save_data(void) { - uint32 start_adr, size; + uintptr start_adr, size; FILE *file; if (!mon_expression(&start_adr)) @@ -631,12 +655,12 @@ void save_data(void) if (!(file = fopen(mon_string, "wb"))) mon_error("Unable to create file"); else { - uint32 adr = start_adr, end_adr = start_adr + size - 1; + uintptr adr = start_adr, end_adr = start_adr + size - 1; while (adr <= end_adr) fputc(mon_read_byte(adr++), file); fclose(file); - 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); + 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); } }