6 static BPSLIST *bps[BPSTABSIZE];
9 * @brief comet2monitorのプロンプト
11 static char *monitor_prompt = "(comet2 monitor)";
13 unsigned adrhash(WORD adr)
17 key[0] = malloc_chk(sizeof(HKEY), "adrhash.key");
20 h = hash(1, key, BPSTABSIZE);
29 for(p = bps[adrhash(adr)]; p != NULL; p = p->next) {
42 /* 登録されたラベルを検索。すでに登録されている場合は終了 */
43 if(getbps(adr) == true) {
44 fprintf(stderr, "%04X: Breakpoint is already defined.\n", adr);
48 p = malloc_chk(sizeof(BPSLIST), "bps.next");
52 p->next = bps[h = adrhash(adr)];
63 p = bps[h = adrhash(adr)];
74 for(; p->next != NULL; p = p->next) {
93 fprintf(stdout, "List of breakpoints\n");
94 for(i = 0; i < BPSTABSIZE; i++) {
95 for(p = bps[i]; p != NULL; p = p->next) {
96 fprintf(stdout, "#%04X\n", p->adr);
101 fprintf(stdout, "(No breakpoints.)\n");
105 void freebpslist(BPSLIST *head)
108 for(p = head; p != NULL; p = q) {
117 for(i = 0; i < BPSTABSIZE; i++) {
123 MONARGS *monargstok(const char *str)
125 MONARGS *args = malloc_chk(sizeof(MONARGS), "args");
126 char *p, *q, *r, *sepp; /* pは文字列全体の先頭位置、qはトークンの先頭位置、rは文字の位置 */
133 p = q = r = strdup_chk(str, "argstok.p");
135 while(*q == ' ' || *q == '\t'){
138 sepp = r + strcspn(r, " ");
141 args->argv[++(args->argc)-1] = strdup_chk(q, "args.argv[]");
143 } while(sepc == ' ');
148 MONCMDLINE *monlinetok(const char *line)
152 MONCMDLINE *moncmdl = NULL;
154 if(!line[0] || line[0] == '\n') {
157 p = tokens = strdup_chk(line, "tokens");
158 moncmdl = malloc_chk(sizeof(MONCMDLINE), "moncmdl");
160 moncmdl->cmd = malloc_chk((l = strcspn(p, " \t\n")) + 1, "moncmdl.cmd");
161 strncpy(moncmdl->cmd, p, l);
162 /* コマンドと引数の間の空白をスキップ */
164 while(*p == ' ' || *p == '\t') {
167 /* 引数として、改行までの文字列を取得 */
168 if((l = strcspn(p, "\n")) > 0) {
169 moncmdl->args = monargstok(p);
171 moncmdl->args = malloc_chk(sizeof(MONARGS), "moncmdl.args");
172 moncmdl->args->argc = 0;
178 bool stracmp(char *str1, int str2c, char *str2v[])
184 for(i = 0; i < str2c; i++) {
185 if(strcmp(str1, str2v[i]) == 0) {
192 void mon_break(int argc, char *argv[])
195 if(stracmp(argv[0], 2, (char* []){"l", "list"})) {
197 } else if(stracmp(argv[0], 2, (char* []){"r", "reset"})) {
199 fprintf(stdout, "All breakpoints are deleted.\n");
202 if((w = nh2word(argv[1])) == 0x0) {
203 fprintf(stderr, "%s: address error\n", argv[1]);
206 if(stracmp(argv[0], 2, (char* []){"a", "add"})) {
207 if(addbps(w) == true) {
208 fprintf(stdout, "#%04X: breakpoint added\n", w);
210 fprintf(stdout, "No breakpoint added\n");
212 } else if(stracmp(argv[0], 2, (char* []){"d", "del"})) {
213 if(delbps(w) == true) {
214 fprintf(stdout, "#%04X: breakpoint deleted\n", w);
216 fprintf(stdout, "No breakpoint deleted\n");
218 } else if(stracmp(argv[0], 3, (char* []){"?", "h", "help"})) {
219 fprintf(stdout, "breakpoint manipulate:\n");
220 fprintf(stdout, " b[reak] a[dd] <address>\n");
221 fprintf(stdout, " b[reak] d[el] <address>\n");
222 fprintf(stdout, " b[reak] l[ist]\n");
223 fprintf(stdout, " b[reak] r[eset]\n");
225 fprintf(stderr, "%s: Not breakpoint manipulate command. see `b ?'.\n", argv[0]);
230 void mon_dump(int argc, char *argv[])
233 WORD start = 0, end = 0xFFFF;
234 if(argc > 0 && stracmp(argv[0], 2, (char* []){"a", "auto"})) {
235 execmode.dump = true;
237 } else if(argc > 0 && stracmp(argv[0], 2, (char* []){"no", "noauto"})) {
238 execmode.dump = false;
242 start = execmode.dump_start = nh2word(argv[i++]);
244 end = execmode.dump_end = nh2word(argv[i++]);
246 dumpmemory(start, end);
248 for(j = i; i < argc; j++) {
250 fprintf(stderr, " ");
252 fprintf(stderr, "%s", argv[j]);
254 fprintf(stderr, ": ignored.\n");
259 MONCMDTYPE monitorcmd(char *cmd, MONARGS *args)
261 MONCMDTYPE cmdtype = MONREPEAT;
262 if(stracmp(cmd, 2, (char* []){"a", "assemble"})) {
263 if(args->argc == 0) {
264 fprintf(stderr, "Error: Input file name.\n");
265 } else if(args->argc == 1) {
266 assemble(1, (char* []){args->argv[0]}, 0);
268 assemble(1, (char* []){args->argv[0]}, nh2word(args->argv[1]));
270 } else if(stracmp(cmd, 2, (char* []){"b", "break"})) {
271 mon_break(args->argc, args->argv);
272 } else if(stracmp(cmd, 2, (char* []){"c", "continue"})) {
273 execmode.step = false;
275 } else if(stracmp(cmd, 2, (char* []){"d", "dump"})) {
276 mon_dump(args->argc, args->argv);
277 } else if(stracmp(cmd, 2, (char* []){"l", "load"})) {
278 loadassemble(args->argv[0], nh2word(args->argv[1]));
279 } else if(stracmp(cmd, 2, (char* []){"n", "next"})) {
280 execmode.step = true;
282 } else if(stracmp(cmd, 2, (char* []){"q", "quit"})) {
283 fprintf(stdout, "Quit: COMET II monitor\n");
285 } else if(stracmp(cmd, 2, (char* []){"r", "reverse"})) {
286 if(args->argc == 2) {
287 disassemble_memory(nh2word(args->argv[0]), nh2word(args->argv[1]));
289 } else if(stracmp(cmd, 1, (char* []){"reset"})) {
290 fprintf(stdout, "Reset COMET II.\n");
291 reset(sys->memsize, sys->clocks); /* COMET II仮想マシンのリセット */
292 } else if(stracmp(cmd, 2, (char* []){"t", "trace"})) {
293 if(args->argc > 0 && stracmp(args->argv[0], 2, (char* []){"a", "auto"})) {
294 execmode.logical = false;
295 execmode.trace = true;
296 } else if(args->argc > 0 && stracmp(args->argv[0], 2, (char* []){"no", "noauto"})) {
297 execmode.trace = false;
299 fprintf(stdout, "#%04X: Register::::\n", sys->cpu->pr);
302 } else if(stracmp(cmd, 2, (char* []){"T", "tracelogical"})) {
303 if(args->argc > 0 && stracmp(args->argv[0], 2, (char* []){"a", "auto"})) {
304 execmode.logical = true;
305 execmode.trace = true;
306 } else if(args->argc > 0 && stracmp(args->argv[0], 2, (char* []){"no", "noauto"})) {
307 execmode.trace = false;
309 fprintf(stdout, "#%04X: Register::::\n", sys->cpu->pr);
312 } else if(stracmp(cmd, 3, (char* []){"?", "h", "help"})) {
313 fprintf(stdout, "b[reak] -- Manipulate Breakpoints. See details, `b ?'.\n");
314 fprintf(stdout, "c[ontinue] -- Continue running your program.\n");
315 fprintf(stdout, "d[ump] -- Display memory dump. `d[ump] a[uto]/n[oauto]' set auto/noauto display.\n");
316 fprintf(stdout, "l[oad] -- Load object from a file to the memory. `l[oad] <filepath> <address>' if address is omitted, load to address 0.\n");
317 fprintf(stdout, "n[ext] -- Go next instruction.\n");
318 fprintf(stdout, "q[uit] -- Quit running your program.\n");
319 fprintf(stdout, "reset -- Reset the system.\n");
320 fprintf(stdout, "r[everse] -- Disassemble memory. `r[everse] <start address> <end address>.\n");
321 fprintf(stdout, "s[ave] -- Save object from the memory to a file. `s[ave] <filepath> [<start address1> [<end address>]]' if <start address> and <end address> is omitted, save the whole memory. if <end address> is omitted, save the memory after <start address>.\n");
322 fprintf(stdout, "t[race] -- Display CPU register. `t[race] a[uto]/n[oauto]' set auto/noauto display. \n");
323 fprintf(stdout, "T[race] -- Display CPU register as logical value. `t[race] a[uto]/n[oauto]' set auto/noauto display. \n");
324 fprintf(stdout, "?/h[elp] -- Display this help.\n");
329 void free_moncmdline(MONCMDLINE *moncmdl)
332 assert(moncmdl != NULL);
333 if(moncmdl->args != NULL) {
334 for(i = 0; i < moncmdl->args->argc; i++) {
335 FREE(moncmdl->args->argv[i]);
339 if(moncmdl->cmd != NULL) {
342 if(moncmdl != NULL) {
352 MONCMDTYPE cmdtype = MONREPEAT;
355 fprintf(stdout, "%s ", monitor_prompt);
356 buf = malloc_chk(MONINSIZE + 1, "monitor.buf");
357 fgets(buf, MONINSIZE, stdin);
358 fprintf(stdout, "%s", buf);
362 if((i = strcspn(buf, "\n")) > 0 || buf[0] == '\n') {
365 if((moncmdl = monlinetok(buf)) != NULL) {
366 cmdtype = monitorcmd(moncmdl->cmd, moncmdl->args);
367 free_moncmdline(moncmdl);
370 if(cmdtype == MONQUIT) {
373 free_cmdtable(HASH_CODE);
377 } while(cmdtype == MONREPEAT);