--- mon/src/mon.cpp 1999/10/04 19:31:09 1.1 +++ mon/src/mon.cpp 2000/06/27 14:52:44 1.9 @@ -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) { @@ -1038,6 +1074,7 @@ void mon_init(void) 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"); @@ -1063,6 +1100,9 @@ void mon_init(void) 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; } @@ -1108,6 +1148,20 @@ void mon(int argc, char **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 if (!mon_use_real_mem) { mon_mem_size = 0x100000; @@ -1115,8 +1169,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 +1179,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) {