}
}
+void disassemble_cmd_adr_x(CMDTYPE cmdtype, const char *cmdname, WORD word, WORD adr, WORD loc)
+{
+ WORD x;
+ char *g;
+ fprintf(stdout, "\t%s\t", cmdname);
+ if(cmdtype == R_ADR_X) {
+ fprintf(stdout, "%s,", g = grstr((word & 0x00F0) >> 4));
+ FREE(g);
+ }
+ fprintf(stdout, "#%04X", adr);
+ if((x = (word & 0x000F)) != 0) {
+ fprintf(stdout, ",%s", g = grstr(x));
+ FREE(g);
+ }
+ fprintf(stdout, "\t\t\t\t; #%04X: #%04X #%04X", loc, word, adr);
+}
+
+void disassemble_cmd_r(CMDTYPE cmdtype, const char *cmdname, WORD word, WORD loc)
+{
+ char *g, *g1, *g2;
+ fprintf(stdout, "\t%s", cmdname);
+ if(cmdtype == R1_R2) {
+ g1 = grstr((word & 0x00F0) >> 4);
+ g2 = grstr(word & 0x000F);
+ fprintf(stdout, "\t%s,%s", g1, g2);
+ FREE(g1);
+ FREE(g2);
+ } else if(cmdtype == R_) {
+ g = grstr((word & 0x00F0) >> 4);
+ fprintf(stdout, "\t%s", g);
+ FREE(g);
+ }
+ fprintf(stdout, "\t\t\t\t; #%04X: #%04X", loc, word);
+}
+
bool disassemble_file(const char *file)
{
bool stat = true;
FILE *fp;
- WORD i = 0, w, cmd, x, adr;
+ WORD i = 0, w, cmd, adr;
CMDTYPE cmdtype = 0;
- char *cmdname, *g, *g1, *g2;
+ char *cmdname;
assert(file != NULL);
if((fp = fopen(file, "rb")) == NULL) {
create_code_cmdtype(); /* 命令のコードとタイプがキーのハッシュ表を作成 */
fprintf(stdout, "MAIN\tSTART\n");
- for(; ;) {
- fread(&w, sizeof(WORD), 1, fp);
- if(feof(fp)) {
- break;
- }
+ for(fread(&w, sizeof(WORD), 1, fp); !feof(fp); fread(&w, sizeof(WORD), 1, fp)) {
cmd = w & 0xFF00;
cmdname = getcmdname(cmd);
cmdtype = getcmdtype(cmd);
- if(cmd == 0xFF00 || (w != 0 && cmd == 0x0000)) {
+ if(cmd == 0xFF00 || (w != 0 && cmd == 0)) {
fprintf(stdout, "\tDC\t%d\t\t\t\t; #%04X: #%04X :: ", w, i++, w);
print_dumpword(w, true);
} else if(cmdtype == R_ADR_X || cmdtype == ADR_X) {
fread(&adr, sizeof(WORD), 1, fp);
- fprintf(stdout, "\t%s\t", cmdname);
- if(cmdtype == R_ADR_X) {
- g = grstr((w & 0x00F0) >> 4);
- fprintf(stdout, "%s,", g);
- FREE(g);
- }
- fprintf(stdout, "#%04X", adr);
- if((x = w & 0x000F) != 0) {
- fprintf(stdout, ",%s", g = grstr(x));
- FREE(g);
- }
- fprintf(stdout, "\t\t\t\t; #%04X: #%04X #%04X", i, w, adr);
+ disassemble_cmd_adr_x(cmdtype, cmdname, w, adr, i);
i += 2;
} else {
- fprintf(stdout, "\t%s", cmdname);
- if(cmdtype == R1_R2) {
- g1 = grstr((w & 0x00F0) >> 4);
- g2 = grstr(w & 0x000F);
- fprintf(stdout, "\t%s,%s", g1, g2);
- FREE(g1);
- FREE(g2);
- } else if(cmdtype == R_) {
- g = grstr((w & 0x00F0) >> 4);
- fprintf(stdout, "\t%s", g);
- FREE(g);
- }
- fprintf(stdout, "\t\t\t\t; #%04X: #%04X", i++, w);
+ disassemble_cmd_r(cmdtype, cmdname, w, i++);
}
fprintf(stdout, "\n");
}
fclose(fp);
return stat;
}
+
+void disassemble_memory(WORD start, WORD end)
+{
+ WORD i, w, cmd, adr;
+ CMDTYPE cmdtype = 0;
+ char *cmdname;
+
+ for(i = start; i <= end; i++) {
+ w = sys->memory[i];
+ cmd = w & 0xFF00;
+ cmdname = getcmdname(cmd);
+ cmdtype = getcmdtype(cmd);
+ if(cmd == 0xFF00 || (w != 0 && cmd == 0)) {
+ fprintf(stdout, "\tDC\t%d\t\t\t\t; #%04X: #%04X :: ", w, i, w);
+ print_dumpword(w, true);
+ } else if(cmdtype == R_ADR_X || cmdtype == ADR_X) {
+ adr = sys->memory[i+1];
+ disassemble_cmd_adr_x(cmdtype, cmdname, w, adr, i++);
+ } else {
+ disassemble_cmd_r(cmdtype, cmdname, w, i);
+ }
+ fprintf(stdout, "\n");
+ }
+}
}
}
-bool monitorcmd(char *cmd, MONARGS *args)
+MONCMDTYPE monitorcmd(char *cmd, MONARGS *args)
{
- bool next = false;
+ MONCMDTYPE cmdtype = MONREPEAT;
if(stracmp(cmd, 2, (char* []){"s", "step"})) {
execmode.step = true;
- next = true;
+ cmdtype = MONNEXT;
} else if(stracmp(cmd, 2, (char* []){"b", "break"})) {
mon_break(args->argc, args->argv);
} else if(stracmp(cmd, 2, (char* []){"c", "continue"})) {
execmode.step = false;
- next = true;
+ cmdtype = MONNEXT;
} else if(stracmp(cmd, 2, (char* []){"t", "trace"})) {
if(args->argc > 0 && stracmp(args->argv[0], 2, (char* []){"a", "auto"})) {
execmode.logical = false;
execmode.dump = true;
} else if(args->argc > 0 && stracmp(args->argv[0], 2, (char* []){"no", "noauto"})) {
execmode.dump = false;
- } else {
- dumpmemory();
+ } else if(args->argc == 1) {
+ dumpmemory(nh2word(args->argv[0]), 0xFFFF);
+ } else if(args->argc > 1) {
+ dumpmemory(nh2word(args->argv[0]), nh2word(args->argv[1]));
+ }
+ } else if(stracmp(cmd, 2, (char* []){"r", "reverse"})) {
+ if(args->argc == 2) {
+ disassemble_memory(nh2word(args->argv[0]), nh2word(args->argv[1]));
}
+ } else if(stracmp(cmd, 2, (char* []){"q", "quit"})) {
+ fprintf(stdout, "Quit: COMET II monitor\n");
+ cmdtype = MONQUIT;
} else if(stracmp(cmd, 3, (char* []){"?", "h", "help"})) {
fprintf(stdout, "b[reak] -- Manipulate Breakpoints. Details in `b ?'.\n");
fprintf(stdout, "s[tep] -- Step by step running your program until next interaction.\n");
fprintf(stdout, "d[ump] -- Display memory dump. `d[ump] a[uto]/n[oauto]' set auto/noauto display.\n");
fprintf(stdout, "?/h[elp] -- Display this help.\n");
}
- return next;
+ return cmdtype;
}
void free_moncmdline(MONCMDLINE *moncmdl)
{
char *buf, *p;
MONCMDLINE *moncmdl;
- bool next = false;
+ MONCMDTYPE cmdtype = MONREPEAT;
do {
fprintf(stdout, "COMET II (Type ? for help) > ");
buf = malloc_chk(MONINSIZE + 1, "monitor.buf");
*p = '\0';
}
if((moncmdl = monlinetok(buf)) != NULL) {
- next = monitorcmd(moncmdl->cmd, moncmdl->args);
+ cmdtype = monitorcmd(moncmdl->cmd, moncmdl->args);
free_moncmdline(moncmdl);
}
FREE(buf);
- } while(next == false);
+ if(cmdtype == MONQUIT) {
+ shutdown();
+ freebps();
+ free_code_cmdtype();
+ freecerr();
+ exit(0);
+ }
+ } while(cmdtype == MONREPEAT);
}