1 |
|
/* |
2 |
|
* mon.cpp - cxmon main program |
3 |
|
* |
4 |
< |
* cxmon (C) 1997-2002 Christian Bauer, Marc Hellwig |
4 |
> |
* cxmon (C) 1997-2003 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 |
52 |
|
#include "mon_lowmem.h" |
53 |
|
|
54 |
|
#ifndef VERSION |
55 |
< |
#define VERSION "2" |
55 |
> |
#define VERSION "3" |
56 |
|
#endif |
57 |
|
|
58 |
|
|
71 |
|
char *mon_args_ptr; |
72 |
|
|
73 |
|
// Current address, value of '.' in expressions |
74 |
< |
uint32 mon_dot_address; |
74 |
> |
uintptr mon_dot_address; |
75 |
|
|
76 |
|
// Current value of ':' in expression |
77 |
|
static uint32 colon_value; |
79 |
|
|
80 |
|
// Scanner variables |
81 |
|
enum Token mon_token; // Last token read |
82 |
< |
uint32 mon_number; // Contains the number if mon_token==T_NUMBER |
82 |
> |
uintptr mon_number; // Contains the number if mon_token==T_NUMBER |
83 |
|
char mon_string[INPUT_LENGTH]; // Contains the string if mon_token==T_STRING |
84 |
|
char mon_name[INPUT_LENGTH]; // Contains the variable name if mon_token==T_NAME |
85 |
|
|
96 |
|
|
97 |
|
|
98 |
|
// List of variables |
99 |
< |
typedef std::map<std::string, uint32> var_map; |
99 |
> |
typedef std::map<std::string, uintptr> var_map; |
100 |
|
static var_map vars; |
101 |
|
|
102 |
|
|
107 |
|
static void read_line(char *prompt); // Scanner |
108 |
|
static char get_char(void); |
109 |
|
static void put_back(char c); |
110 |
< |
static enum Token get_hex_number(uint32 &i); |
111 |
< |
static enum Token get_dec_number(uint32 &i); |
112 |
< |
static enum Token get_char_number(uint32 &i); |
110 |
> |
static enum Token get_hex_number(uintptr &i); |
111 |
> |
static enum Token get_dec_number(uintptr &i); |
112 |
> |
static enum Token get_char_number(uintptr &i); |
113 |
|
static enum Token get_string(char *str); |
114 |
< |
static enum Token get_hex_or_name(uint32 &i, char *name); |
114 |
> |
static enum Token get_hex_or_name(uintptr &i, char *name); |
115 |
|
|
116 |
< |
static bool eor_expr(uint32 *number); // Parser |
117 |
< |
static bool and_expr(uint32 *number); |
118 |
< |
static bool shift_expr(uint32 *number); |
119 |
< |
static bool add_expr(uint32 *number); |
120 |
< |
static bool mul_expr(uint32 *number); |
121 |
< |
static bool factor(uint32 *number); |
116 |
> |
static bool eor_expr(uintptr *number); // Parser |
117 |
> |
static bool and_expr(uintptr *number); |
118 |
> |
static bool shift_expr(uintptr *number); |
119 |
> |
static bool add_expr(uintptr *number); |
120 |
> |
static bool mul_expr(uintptr *number); |
121 |
> |
static bool factor(uintptr *number); |
122 |
|
|
123 |
|
|
124 |
|
/* |
202 |
|
* Access to buffer |
203 |
|
*/ |
204 |
|
|
205 |
< |
uint32 (*mon_read_byte)(uint32 adr); |
205 |
> |
uint32 (*mon_read_byte)(uintptr adr); |
206 |
|
|
207 |
< |
uint32 mon_read_byte_buffer(uint32 adr) |
207 |
> |
uint32 mon_read_byte_buffer(uintptr adr) |
208 |
|
{ |
209 |
|
return mem[adr % mon_mem_size]; |
210 |
|
} |
211 |
|
|
212 |
< |
uint32 mon_read_byte_real(uint32 adr) |
212 |
> |
uint32 mon_read_byte_real(uintptr adr) |
213 |
|
{ |
214 |
|
return *(uint8 *)adr; |
215 |
|
} |
216 |
|
|
217 |
< |
void (*mon_write_byte)(uint32 adr, uint32 b); |
217 |
> |
void (*mon_write_byte)(uintptr adr, uint32 b); |
218 |
|
|
219 |
< |
void mon_write_byte_buffer(uint32 adr, uint32 b) |
219 |
> |
void mon_write_byte_buffer(uintptr adr, uint32 b) |
220 |
|
{ |
221 |
|
mem[adr % mon_mem_size] = b; |
222 |
|
} |
223 |
|
|
224 |
< |
void mon_write_byte_real(uint32 adr, uint32 b) |
224 |
> |
void mon_write_byte_real(uintptr adr, uint32 b) |
225 |
|
{ |
226 |
|
*(uint8 *)adr = b; |
227 |
|
} |
228 |
|
|
229 |
< |
uint32 mon_read_half(uint32 adr) |
229 |
> |
uint32 mon_read_half(uintptr adr) |
230 |
|
{ |
231 |
|
return (mon_read_byte(adr) << 8) | mon_read_byte(adr+1); |
232 |
|
} |
233 |
|
|
234 |
< |
void mon_write_half(uint32 adr, uint32 w) |
234 |
> |
void mon_write_half(uintptr adr, uint32 w) |
235 |
|
{ |
236 |
|
mon_write_byte(adr, w >> 8); |
237 |
|
mon_write_byte(adr+1, w); |
238 |
|
} |
239 |
|
|
240 |
< |
uint32 mon_read_word(uint32 adr) |
240 |
> |
uint32 mon_read_word(uintptr adr) |
241 |
|
{ |
242 |
|
return (mon_read_byte(adr) << 24) | (mon_read_byte(adr+1) << 16) | (mon_read_byte(adr+2) << 8) | mon_read_byte(adr+3); |
243 |
|
} |
244 |
|
|
245 |
< |
void mon_write_word(uint32 adr, uint32 l) |
245 |
> |
void mon_write_word(uintptr adr, uint32 l) |
246 |
|
{ |
247 |
|
mon_write_byte(adr, l >> 24); |
248 |
|
mon_write_byte(adr+1, l >> 16); |
267 |
|
|
268 |
|
line_read = readline(prompt); |
269 |
|
|
270 |
< |
if (line_read && *line_read) |
271 |
< |
add_history(line_read); |
270 |
> |
if (line_read) { |
271 |
> |
|
272 |
> |
if (*line_read) |
273 |
> |
add_history(line_read); |
274 |
> |
|
275 |
> |
strncpy(in_ptr = input, line_read, INPUT_LENGTH); |
276 |
> |
input[INPUT_LENGTH-1] = 0; |
277 |
|
|
278 |
< |
strncpy(in_ptr = input, line_read, INPUT_LENGTH); |
279 |
< |
input[INPUT_LENGTH-1] = 0; |
278 |
> |
} else { |
279 |
> |
|
280 |
> |
// EOF, quit cxmon |
281 |
> |
fprintf(monout, "x\n"); |
282 |
> |
input[0] = 'x'; |
283 |
> |
input[1] = 0; |
284 |
> |
in_ptr = input; |
285 |
> |
} |
286 |
|
#else |
287 |
|
fprintf(monout, prompt); |
288 |
|
fflush(monout); |
396 |
|
} |
397 |
|
} |
398 |
|
|
399 |
< |
static enum Token get_hex_number(uint32 &i) |
399 |
> |
static enum Token get_hex_number(uintptr &i) |
400 |
|
{ |
401 |
|
char c = get_char(); |
402 |
|
|
421 |
|
} |
422 |
|
} |
423 |
|
|
424 |
< |
static enum Token get_dec_number(uint32 &i) |
424 |
> |
static enum Token get_dec_number(uintptr &i) |
425 |
|
{ |
426 |
|
char c = get_char(); |
427 |
|
|
442 |
|
} |
443 |
|
} |
444 |
|
|
445 |
< |
static enum Token get_char_number(uint32 &i) |
445 |
> |
static enum Token get_char_number(uintptr &i) |
446 |
|
{ |
447 |
|
char c; |
448 |
|
|
473 |
|
return T_NULL; |
474 |
|
} |
475 |
|
|
476 |
< |
static enum Token get_hex_or_name(uint32 &i, char *name) |
476 |
> |
static enum Token get_hex_or_name(uintptr &i, char *name) |
477 |
|
{ |
478 |
|
char *old_in_ptr = in_ptr; |
479 |
|
char c; |
501 |
|
* true: OK, false: Error |
502 |
|
*/ |
503 |
|
|
504 |
< |
bool mon_expression(uint32 *number) |
504 |
> |
bool mon_expression(uintptr *number) |
505 |
|
{ |
506 |
< |
uint32 accu, expr; |
506 |
> |
uintptr accu, expr; |
507 |
|
|
508 |
|
if (!eor_expr(&accu)) |
509 |
|
return false; |
529 |
|
* true: OK, false: Error |
530 |
|
*/ |
531 |
|
|
532 |
< |
static bool eor_expr(uint32 *number) |
532 |
> |
static bool eor_expr(uintptr *number) |
533 |
|
{ |
534 |
< |
uint32 accu, expr; |
534 |
> |
uintptr accu, expr; |
535 |
|
|
536 |
|
if (!and_expr(&accu)) |
537 |
|
return false; |
557 |
|
* true: OK, false: Error |
558 |
|
*/ |
559 |
|
|
560 |
< |
static bool and_expr(uint32 *number) |
560 |
> |
static bool and_expr(uintptr *number) |
561 |
|
{ |
562 |
< |
uint32 accu, expr; |
562 |
> |
uintptr accu, expr; |
563 |
|
|
564 |
|
if (!shift_expr(&accu)) |
565 |
|
return false; |
585 |
|
* true: OK, false: Error |
586 |
|
*/ |
587 |
|
|
588 |
< |
static bool shift_expr(uint32 *number) |
588 |
> |
static bool shift_expr(uintptr *number) |
589 |
|
{ |
590 |
< |
uint32 accu, expr; |
590 |
> |
uintptr accu, expr; |
591 |
|
|
592 |
|
if (!add_expr(&accu)) |
593 |
|
return false; |
620 |
|
* true: OK, false: Error |
621 |
|
*/ |
622 |
|
|
623 |
< |
static bool add_expr(uint32 *number) |
623 |
> |
static bool add_expr(uintptr *number) |
624 |
|
{ |
625 |
< |
uint32 accu, expr; |
625 |
> |
uintptr accu, expr; |
626 |
|
|
627 |
|
if (!mul_expr(&accu)) |
628 |
|
return false; |
655 |
|
* true: OK, false: Error |
656 |
|
*/ |
657 |
|
|
658 |
< |
static bool mul_expr(uint32 *number) |
658 |
> |
static bool mul_expr(uintptr *number) |
659 |
|
{ |
660 |
< |
uint32 accu, fact; |
660 |
> |
uintptr accu, fact; |
661 |
|
|
662 |
|
if (!factor(&accu)) |
663 |
|
return false; |
705 |
|
* true: OK, false: Error |
706 |
|
*/ |
707 |
|
|
708 |
< |
static bool factor(uint32 *number) |
708 |
> |
static bool factor(uintptr *number) |
709 |
|
{ |
710 |
|
switch (mon_token) { |
711 |
|
case T_NUMBER: |
804 |
|
if (mon_token == T_ASSIGN) { |
805 |
|
|
806 |
|
// Set variable |
807 |
< |
uint32 value; |
807 |
> |
uintptr value; |
808 |
|
mon_get_token(); |
809 |
|
if (!mon_expression(&value)) |
810 |
|
return; |
874 |
|
|
875 |
|
static void reallocate(void) |
876 |
|
{ |
877 |
< |
uint32 size; |
877 |
> |
uintptr size; |
878 |
|
|
879 |
|
if (mon_use_real_mem) { |
880 |
|
fprintf(monerr, "Cannot reallocate buffer in real mode\n"); |
907 |
|
|
908 |
|
static void apply(int size) |
909 |
|
{ |
910 |
< |
uint32 adr, end_adr, value; |
910 |
> |
uintptr adr, end_adr, value; |
911 |
|
char c; |
912 |
|
|
913 |
|
if (!mon_expression(&adr)) |
921 |
|
return; |
922 |
|
} |
923 |
|
|
924 |
< |
uint32 (*read_func)(uint32 adr); |
925 |
< |
void (*write_func)(uint32 adr, uint32 val); |
924 |
> |
uint32 (*read_func)(uintptr adr); |
925 |
> |
void (*write_func)(uintptr adr, uint32 val); |
926 |
|
switch (size) { |
927 |
|
case 1: |
928 |
|
read_func = mon_read_byte; |
1028 |
|
mon_add_command("d80", disassemble_z80, "d80 [start [end]] Disassemble Z80 code\n"); |
1029 |
|
mon_add_command("d86", disassemble_80x86_32, "d86 [start [end]] Disassemble 80x86 (32-bit) code\n"); |
1030 |
|
mon_add_command("d8086", disassemble_80x86_16, "d8086 [start [end]] Disassemble 80x86 (16-bit) code\n"); |
1031 |
+ |
mon_add_command("d8664", disassemble_x86_64, "d8664 [start [end]] Disassemble x86-64 code\n"); |
1032 |
|
mon_add_command(":", modify, ": start string Modify memory\n"); |
1033 |
|
mon_add_command("f", fill, "f start end string Fill memory\n"); |
1034 |
|
mon_add_command("y", apply_byte, "y[b|h|w] start end expr Apply expression to memory\n"); |
1147 |
|
while (!done) { |
1148 |
|
if (interactive) { |
1149 |
|
char prompt[16]; |
1150 |
< |
sprintf(prompt, "[%08x]-> ", mon_dot_address); |
1150 |
> |
sprintf(prompt, "[%0*lx]-> ", 2 * sizeof(mon_dot_address), mon_dot_address); |
1151 |
|
read_line(prompt); |
1152 |
|
} else { |
1153 |
|
if (argc == 0) { |