--- mon/src/mon.cpp 1999/10/04 19:31:09 1.1 +++ mon/src/mon.cpp 2000/10/08 17:51:58 1.13 @@ -1,27 +1,57 @@ /* - * mon.cpp - Machine language monitor + * mon.cpp - mon main program * - * (C) 1997-1999 Christian Bauer + * mon (C) 1997-2000 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include -#include +#include "sysdeps.h" + #include #include -#include #include #include -#include -#include +#if defined(HAVE_READLINE_H) +extern "C" { +#include +} +#elif defined(HAVE_READLINE_READLINE_H) extern "C" { -extern char *readline(char *prompt); -extern void add_history(char *str); +#include } +#endif + +#if defined(HAVE_HISTORY_H) +extern "C" { +#include +} +#elif defined(HAVE_READLINE_HISTORY_H) +extern "C" { +#include +} +#endif #include "mon.h" #include "mon_cmd.h" +#ifndef VERSION +#define VERSION "2" +#endif + // Buffer we're operating on bool mon_use_real_mem = false; @@ -177,58 +207,52 @@ bool mon_aborted(void) * Access to buffer */ -uint32 mon_read_byte(uint32 adr) +uint32 (*mon_read_byte)(uint32 adr); + +uint32 mon_read_byte_buffer(uint32 adr) { - if (mon_use_real_mem) - return *(uint8 *)adr; - else - return mem[adr % mon_mem_size]; + return mem[adr % mon_mem_size]; } -void mon_write_byte(uint32 adr, uint32 b) +uint32 mon_read_byte_real(uint32 adr) { - if (mon_use_real_mem) - *(uint8 *)adr = b; - else - mem[adr % mon_mem_size] = b; + return *(uint8 *)adr; +} + +void (*mon_write_byte)(uint32 adr, uint32 b); + +void mon_write_byte_buffer(uint32 adr, uint32 b) +{ + mem[adr % mon_mem_size] = b; +} + +void mon_write_byte_real(uint32 adr, uint32 b) +{ + *(uint8 *)adr = b; } uint32 mon_read_half(uint32 adr) { - if (mon_use_real_mem) - return ntohs(*(uint16 *)adr); - else - return mem[adr % mon_mem_size] << 8 | mem[(adr+1) % mon_mem_size]; + return (mon_read_byte(adr) << 8) | mon_read_byte(adr+1); } void mon_write_half(uint32 adr, uint32 w) { - if (mon_use_real_mem) - *(uint16 *)adr = htons(w); - else { - mem[adr % mon_mem_size] = w >> 8; - mem[(adr+1) % mon_mem_size] = w; - } + mon_write_byte(adr, w >> 8); + mon_write_byte(adr+1, w); } uint32 mon_read_word(uint32 adr) { - if (mon_use_real_mem) - return ntohl(*(uint32 *)adr); - else - return mon_read_byte(adr) << 24 | mon_read_byte(adr+1) << 16 | mon_read_byte(adr+2) << 8 | mon_read_byte(adr+3); + return (mon_read_byte(adr) << 24) | (mon_read_byte(adr+1) << 16) | (mon_read_byte(adr+2) << 8) | mon_read_byte(adr+3); } void mon_write_word(uint32 adr, uint32 l) { - if (mon_use_real_mem) - *(uint32 *)adr = htonl(l); - else { - mem[adr % mon_mem_size] = l >> 24; - mem[(adr+1) % mon_mem_size] = l >> 16; - mem[(adr+2) % mon_mem_size] = l >> 8; - mem[(adr+3) % mon_mem_size] = l; - } + mon_write_byte(adr, l >> 24); + mon_write_byte(adr+1, l >> 16); + mon_write_byte(adr+2, l >> 8); + mon_write_byte(adr+3, l); } @@ -238,6 +262,7 @@ void mon_write_word(uint32 adr, uint32 l static void read_line(char *prompt) { +#ifdef HAVE_LIBREADLINE static char *line_read = NULL; if (line_read) { @@ -252,6 +277,14 @@ static void read_line(char *prompt) strncpy(in_ptr = input, line_read, INPUT_LENGTH); input[INPUT_LENGTH-1] = 0; +#else + fprintf(monout, prompt); + fflush(monout); + fgets(in_ptr = input, INPUT_LENGTH, monin); + char *s = strchr(input, '\n'); + if (s != NULL) + *s = 0; +#endif } @@ -809,7 +842,7 @@ static void set_var(void) fprintf(monout, "No variables defined\n"); else for (Variable *v=first_var; v; v=v->next) - fprintf(monout, "%s = %08lx\n", v->name, v->value); + fprintf(monout, "%s = %08x\n", v->name, v->value); } else if (mon_token == T_NAME) { char var_name[256]; @@ -902,7 +935,7 @@ static void reallocate(void) } if (mon_token == T_END) { - fprintf(monerr, "Buffer size: %08lx bytes\n", mon_mem_size); + fprintf(monerr, "Buffer size: %08x bytes\n", mon_mem_size); return; } @@ -914,7 +947,7 @@ static void reallocate(void) } if ((mem = (uint8 *)realloc(mem, size)) != NULL) - fprintf(monerr, "Buffer size: %08lx bytes\n", mon_mem_size = size); + fprintf(monerr, "Buffer size: %08x bytes\n", mon_mem_size = size); else fprintf(monerr, "Unable to reallocate buffer\n"); } @@ -956,6 +989,9 @@ static void apply(int size) read_func = mon_read_word; write_func = mon_write_word; break; + default: + abort(); + break; } while (adr<=end_adr) { @@ -1032,37 +1068,42 @@ void mon_init(void) num_cmds = 0; cmd_help = NULL; - mon_add_command("??", mon_cmd_list, "?? Show list of commands\n"); - mon_add_command("ver", version, "ver Show version\n"); - mon_add_command("?", print_expr, "? expression Calculate expression\n"); - mon_add_command("@", reallocate, "@ [size] Reallocate buffer\n"); - mon_add_command("i", ascii_dump, "i [start [end]] ASCII memory dump\n"); - mon_add_command("m", memory_dump, "m [start [end]] Hex/ASCII memory dump\n"); - mon_add_command("d", disassemble_ppc, "d [start [end]] Disassemble PowerPC code\n"); - mon_add_command("d65", disassemble_6502, "d65 [start [end]] Disassemble 6502 code\n"); - mon_add_command("d68", disassemble_680x0, "d68 [start [end]] Disassemble 680x0 code\n"); - mon_add_command("d80", disassemble_8080, "d80 [start [end]] Disassemble 8080 code\n"); - mon_add_command("d86", disassemble_80x86, "d86 [start [end]] Disassemble 80x86 code\n"); - mon_add_command(":", modify, ": start string Modify memory\n"); - mon_add_command("f", fill, "f start end string Fill memory\n"); - mon_add_command("y", apply_byte, "y[b|h|w] start end expr Apply expression to memory\n"); + mon_add_command("??", mon_cmd_list, "?? Show list of commands\n"); + mon_add_command("ver", version, "ver Show version\n"); + mon_add_command("?", print_expr, "? expression Calculate expression\n"); + mon_add_command("@", reallocate, "@ [size] Reallocate buffer\n"); + mon_add_command("i", ascii_dump, "i [start [end]] ASCII memory dump\n"); + mon_add_command("m", memory_dump, "m [start [end]] Hex/ASCII memory dump\n"); + mon_add_command("b", binary_dump, "b [start [end]] Binary memory dump\n"); + mon_add_command("d", disassemble_ppc, "d [start [end]] Disassemble PowerPC code\n"); + mon_add_command("d65", disassemble_6502, "d65 [start [end]] Disassemble 6502 code\n"); + mon_add_command("d68", disassemble_680x0, "d68 [start [end]] Disassemble 680x0 code\n"); + mon_add_command("d80", disassemble_8080, "d80 [start [end]] Disassemble 8080 code\n"); + mon_add_command("d86", disassemble_80x86_32, "d86 [start [end]] Disassemble 80x86 (32-bit) code\n"); + mon_add_command("d8086", disassemble_80x86_16, "d8086 [start [end]] Disassemble 80x86 (16-bit) code\n"); + mon_add_command(":", modify, ": start string Modify memory\n"); + mon_add_command("f", fill, "f start end string Fill memory\n"); + mon_add_command("y", apply_byte, "y[b|h|w] start end expr Apply expression to memory\n"); mon_add_command("yb", apply_byte, NULL); mon_add_command("yh", apply_half, NULL); mon_add_command("yw", apply_word, NULL); - mon_add_command("t", transfer, "t start end dest Transfer memory\n"); - mon_add_command("c", compare, "c start end dest Compare memory\n"); - mon_add_command("h", help_or_hunt, "h start end string Search for byte string\n"); - mon_add_command("\\", shell_command, "\\ \"command\" Execute shell command\n"); - mon_add_command("ls", mon_exec, "ls [args] List directory contents\n"); - mon_add_command("rm", mon_exec, "rm [args] Remove file(s)\n"); - mon_add_command("cp", mon_exec, "cp [args] Copy file(s)\n"); - mon_add_command("mv", mon_exec, "mv [args] Move file(s)\n"); - mon_add_command("cd", mon_change_dir, "cd directory Change current directory\n"); - mon_add_command("o", redir_output, "o [\"file\"] Redirect output\n"); - mon_add_command("[", load_data, "[ start \"file\" Load data from file\n"); - mon_add_command("]", save_data, "] start size \"file\" Save data to file\n"); - mon_add_command("set", set_var, "set [var[=value]] Set/clear/show variables\n"); - mon_add_command("cv", clear_vars, "cv Clear all variables\n"); + mon_add_command("t", transfer, "t start end dest Transfer memory\n"); + mon_add_command("c", compare, "c start end dest Compare memory\n"); + mon_add_command("h", help_or_hunt, "h start end string Search for byte string\n"); + mon_add_command("\\", shell_command, "\\ \"command\" Execute shell command\n"); + mon_add_command("ls", mon_exec, "ls [args] List directory contents\n"); + mon_add_command("rm", mon_exec, "rm [args] Remove file(s)\n"); + mon_add_command("cp", mon_exec, "cp [args] Copy file(s)\n"); + mon_add_command("mv", mon_exec, "mv [args] Move file(s)\n"); + mon_add_command("cd", mon_change_dir, "cd directory Change current directory\n"); + mon_add_command("o", redir_output, "o [\"file\"] Redirect output\n"); + mon_add_command("[", load_data, "[ start \"file\" Load data from file\n"); + mon_add_command("]", save_data, "] start size \"file\" Save data to file\n"); + mon_add_command("set", set_var, "set [var[=value]] Set/clear/show variables\n"); + mon_add_command("cv", clear_vars, "cv Clear all variables\n"); + + mon_read_byte = NULL; + mon_write_byte = NULL; } @@ -1093,19 +1134,36 @@ void mon(int argc, char **argv) monout = stdout; monerr = stdout; - if (argc) { - // Access real memory if mon was started as "rmon" - char *prgname = argv[0]; - char *lastslash; - if ((lastslash = strrchr(prgname, '/')) != NULL) - prgname = lastslash + 1; - if (strcmp(prgname, "rmon") == 0) + // Make argc/argv point to the actual arguments + if (argc) + argc--; argv++; + + // Parse arguments + mon_macos_mode = false; + mon_use_real_mem = false; + while (argc > 0) { + if (strcmp(argv[0], "-m") == 0) + mon_macos_mode = true; + else if (strcmp(argv[0], "-r") == 0) mon_use_real_mem = true; + else + break; + argc--; argv++; + } + interactive = (argc == 0); - // Make argc/argv point to the actual arguments - argc--; - argv++; - interactive = (argc == 0); + // Set up memory access functions if not supplied by the user + if (mon_read_byte == NULL) { + if (mon_use_real_mem) + mon_read_byte = mon_read_byte_real; + else + mon_read_byte = mon_read_byte_buffer; + } + if (mon_write_byte == NULL) { + if (mon_use_real_mem) + mon_write_byte = mon_write_byte_real; + else + mon_write_byte = mon_write_byte_buffer; } // Allocate buffer @@ -1115,8 +1173,8 @@ void mon(int argc, char **argv) // Print banner if (interactive) - fprintf(monerr, "\n *** mon V%d.%d by Christian Bauer ***\n" - " *** Press 'h' for help ***\n\n", MON_VERSION, MON_REVISION); + fprintf(monerr, "\n *** mon V" VERSION " by Christian Bauer and Marc Hellwig ***\n" + " *** Press 'h' for help ***\n\n"); } init_abort(); @@ -1125,7 +1183,7 @@ void mon(int argc, char **argv) while (!done) { if (interactive) { char prompt[16]; - sprintf(prompt, "[%08lx]-> ", mon_dot_address); + sprintf(prompt, "[%08x]-> ", mon_dot_address); read_line(prompt); } else { if (argc == 0) {