YACASL2
Loading...
Searching...
No Matches
monitor.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <readline/readline.h>
3#include <readline/history.h>
4#include "monitor.h"
5#include "disassemble.h"
6
11
15static char *monitor_prompt = "(comet2 monitor) ";
16
17unsigned adrhash(WORD adr)
18{
19 HKEY *key[1];
20 unsigned h;
21 key[0] = malloc_chk(sizeof(HKEY), "adrhash.key");
22 key[0]->type = INT;
23 key[0]->val.i = adr;
24 h = hash(1, key, BPSTABSIZE);
25 FREE(key[0]);
26 return h;
27}
28
29bool getbps(WORD adr)
30{
31 BPSLIST *p = NULL;
32
33 for(p = bps[adrhash(adr)]; p != NULL; p = p->next) {
34 if(p->adr == adr) {
35 return true;
36 }
37 }
38 return false;
39}
40
41bool addbps(WORD adr)
42{
43 BPSLIST *p = NULL;
44 unsigned h = 0;
45
46 /* 登録されたラベルを検索。すでに登録されている場合は終了 */
47 if(getbps(adr) == true) {
48 fprintf(stderr, "%04X: Breakpoint is already defined.\n", adr);
49 return false;
50 }
51 /* メモリを確保 */
52 p = malloc_chk(sizeof(BPSLIST), "bps.next");
53 /* アドレスを設定 */
54 p->adr = adr;
55 /* ハッシュ表へ追加 */
56 p->next = bps[h = adrhash(adr)];
57 bps[h] = p;
58 return true;
59}
60
61bool delbps(WORD adr)
62{
63 BPSLIST *p = NULL;
64 BPSLIST *q = NULL;
65 unsigned h = 0;
66 bool res = false;
67
68 p = bps[h = adrhash(adr)];
69 if(p != NULL) {
70 if(p->adr == adr) {
71 if(p->next == NULL) {
72 FREE(bps[h]);
73 } else {
74 bps[h] = p->next;
75 FREE(p);
76 }
77 res = true;
78 } else {
79 for(; p->next != NULL; p = p->next) {
80 q = p->next;
81 if(q->adr == adr) {
82 p->next = q->next;
83 FREE(q);
84 res = true;
85 break;
86 }
87 }
88 }
89 }
90 return res;
91}
92
93void listbps()
94{
95 int cnt = 0;
96 BPSLIST *p = NULL;
97
98 fprintf(stdout, "List of breakpoints\n");
99 for(int i = 0; i < BPSTABSIZE; i++) {
100 for(p = bps[i]; p != NULL; p = p->next) {
101 fprintf(stdout, "#%04X\n", p->adr);
102 cnt++;
103 }
104 }
105 if(cnt == 0) {
106 fprintf(stdout, "(No breakpoints.)\n");
107 }
108}
109
111{
112 BPSLIST *p = NULL;
113 BPSLIST *q = NULL;
114 for(p = head; p != NULL; p = q) {
115 q = p->next;
116 FREE(p);
117 }
118}
119
121{
122 for(int i = 0; i < BPSTABSIZE; i++) {
123 freebpslist(bps[i]);
124 bps[i] = NULL;
125 }
126}
127
128MONARGS *monargstok(const char *str)
129{
130 MONARGS *args = malloc_chk(sizeof(MONARGS), "args");
131 char *tok = NULL;
132 char *p = NULL;
133 char sepc = ' ';
134
135 args->argc = 0;
136 if(!str || !str[0]) {
137 return args;
138 }
139 tok = p = strdup_chk(str, "argstok.p");
140 do {
141 int i = strcspn(p, " ");
142 sepc = p[i];
143 args->argv[(args->argc)++] = strndup_chk(p, i, "args->argv[]");
144 p += i + 1;
145 } while(sepc == ' ');
146 FREE(tok);
147 return args;
148}
149
150MONCMDLINE *monlinetok(const char *line)
151{
152 char *tokens = NULL;
153 char *p = NULL;
154 int i = 0;
155 MONCMDLINE *moncmdl = NULL;
156
157 if(!line[0] || line[0] == '\n') {
158 return NULL;
159 }
160 p = tokens = strdup_chk(line, "tokens");
161 /* コメントを削除 */
163 /* 文字列末尾の改行と空白を削除 */
164 strip_end(p);
165
166 moncmdl = malloc_chk(sizeof(MONCMDLINE), "moncmdl");
167 /* コマンドの取得 */
168 i = strcspn(p, " \t\n");
169 moncmdl->cmd = strndup_chk(p, i, "moncmdl->cmd");
170 /* コマンドと引数の間の空白をスキップ */
171 p += i;
172 while(*p == ' ' || *p == '\t') {
173 p++;
174 }
175 /* 引数として、改行までの文字列を取得 */
176 if(strcspn(p, "\n") > 0) {
177 moncmdl->args = monargstok(p);
178 } else {
179 moncmdl->args = malloc_chk(sizeof(MONARGS), "moncmdl.args");
180 moncmdl->args->argc = 0;
181 }
182 FREE(tokens);
183 return moncmdl;
184}
185
186bool stracmp(char *str1, int str2c, char *str2v[])
187{
188 int i;
189 if(str1 == NULL) {
190 return false;
191 }
192 for(i = 0; i < str2c; i++) {
193 if(strcmp(str1, str2v[i]) == 0) {
194 return true;
195 }
196 }
197 return false;
198}
199
200void warn_ignore_arg(int argc, char *argv[])
201{
202 int i;
203 fprintf(stderr, "Info: arguments '");
204 for(i = 0; i < argc; i++) {
205 if(i > 0) {
206 fprintf(stderr, " ");
207 }
208 fprintf(stderr, "%s", argv[i]);
209 }
210 fprintf(stderr, "' are ignored.\n");
211}
212
213void mon_break(int argc, char *argv[])
214{
215 WORD w;
216 int i = 0;
217 if(stracmp(argv[0], 2, (char* []){"l", "list"})) {
218 i++;
219 listbps();
220 } else if(stracmp(argv[0], 2, (char* []){"r", "reset"})) {
221 i++;
222 freebps();
223 fprintf(stdout, "All breakpoints are deleted.\n");
224 } else {
225 if(argc > 1) {
226 if((w = nh2word(argv[1])) == 0x0) {
227 fprintf(stderr, "%s: address error\n", argv[1]);
228 }
229 }
230 if(stracmp(argv[0], 2, (char* []){"a", "add"})) {
231 i += 2;
232 if(addbps(w) == true) {
233 fprintf(stdout, "#%04X: breakpoint added\n", w);
234 } else {
235 fprintf(stdout, "No breakpoint added\n");
236 }
237 } else if(stracmp(argv[0], 2, (char* []){"d", "del"})) {
238 i += 2;
239 if(delbps(w) == true) {
240 fprintf(stdout, "#%04X: breakpoint deleted\n", w);
241 } else {
242 fprintf(stdout, "No breakpoint deleted\n");
243 }
244 } else if(stracmp(argv[0], 3, (char* []){"?", "h", "help"})) {
245 i++;
246 fprintf(stdout, "breakpoint manipulate:\n");
247 fprintf(stdout, " b[reak] a[dd] <address>\n");
248 fprintf(stdout, " b[reak] d[el] <address>\n");
249 fprintf(stdout, " b[reak] l[ist]\n");
250 fprintf(stdout, " b[reak] r[eset]\n");
251 } else {
252 fprintf(stderr, "%s: Not breakpoint manipulate command. see `b ?'.\n", argv[0]);
253 }
254 if(argc > i) {
255 warn_ignore_arg(argc - i, argv + i);
256 }
257 }
258}
259
260void mon_dump(int argc, char *argv[])
261{
262 int i = 0;
263 WORD dump_start = 0, dump_end = 0x40;
264 if(stracmp(argv[0], 2, (char* []){"a", "auto"})) {
265 execmode.dump = true;
266 } else if(stracmp(argv[0], 2, (char* []){"no", "noauto"})) {
267 execmode.dump = false;
268 } else {
269 if(argc > i) {
270 dump_start = nh2word(argv[i++]);
271 if(argc > i) {
272 if(argv[i][0] =='+') {
273 dump_end = dump_start + nh2word(argv[i] + 1);
274 } else {
275 dump_end = nh2word(argv[i]) + 1;
276 }
277 } else {
278 dump_end += dump_start;
279 }
280 }
281 dumpmemory(dump_start, dump_end);
282 execmode.dump_start = dump_start;
283 execmode.dump_end = dump_end;
284 }
285 if(argc > ++i) {
286 warn_ignore_arg(argc - i, argv + i);
287 }
288}
289
291{
292 MONCMDTYPE cmdtype = MONREPEAT;
293 if(stracmp(cmd, 2, (char* []){"a", "assemble"})) {
294 if(args->argc == 0) {
295 fprintf(stderr, "Error: Input file name.\n");
296 } else if(args->argc == 1) {
297 assemble(1, (char* []){args->argv[0]}, 0);
298 } else {
299 assemble(1, (char* []){args->argv[0]}, nh2word(args->argv[1]));
300 }
301 } else if(stracmp(cmd, 2, (char* []){"b", "break"})) {
302 mon_break(args->argc, args->argv);
303 } else if(stracmp(cmd, 2, (char* []){"c", "continue"})) {
304 execmode.step = false;
305 cmdtype = MONNEXT;
306 } else if(stracmp(cmd, 2, (char* []){"d", "dump"})) {
307 mon_dump(args->argc, args->argv);
308 } else if(stracmp(cmd, 2, (char* []){"l", "load"})) {
309 execptr->end = loadassemble(args->argv[0], nh2word(args->argv[1]));
310 } else if(stracmp(cmd, 2, (char* []){"n", "next"})) {
311 execmode.step = true;
312 cmdtype = MONNEXT;
313 } else if(stracmp(cmd, 2, (char* []){"q", "quit"})) {
314 fprintf(stdout, "Quit: COMET II monitor\n");
315 cmdtype = MONQUIT;
316 } else if(stracmp(cmd, 2, (char* []){"r", "reverse"})) {
317 if(args->argc == 2) {
318 disassemble_memory(nh2word(args->argv[0]), nh2word(args->argv[1]));
319 }
320 } else if(stracmp(cmd, 1, (char* []){"reset"})) {
321 fprintf(stdout, "Reset COMET II CPU.\n");
322 comet2_reset(); /* COMET II仮想マシンのCPUのリセット */
323 } else if(stracmp(cmd, 1, (char* []){"resetall"})) {
324 fprintf(stdout, "Reset COMET II CPU and memory.\n");
325 comet2_resetall(); /* COMET II仮想マシンのCPUとメモリのリセット */
326 } else if(stracmp(cmd, 2, (char* []){"t", "trace"})) {
327 if(args->argc > 0 && stracmp(args->argv[0], 2, (char* []){"a", "auto"})) {
328 execmode.logical = false;
329 execmode.trace = true;
330 } else if(args->argc > 0 && stracmp(args->argv[0], 2, (char* []){"no", "noauto"})) {
331 execmode.trace = false;
332 } else {
333 fprintf(stdout, "#%04X: Register::::\n", sys->cpu->pr);
334 dspregister();
335 }
336 } else if(stracmp(cmd, 2, (char* []){"T", "tracelogical"})) {
337 if(args->argc > 0 && stracmp(args->argv[0], 2, (char* []){"a", "auto"})) {
338 execmode.logical = true;
339 execmode.trace = true;
340 } else if(args->argc > 0 && stracmp(args->argv[0], 2, (char* []){"no", "noauto"})) {
341 execmode.trace = false;
342 } else {
343 fprintf(stdout, "#%04X: Register::::\n", sys->cpu->pr);
344 dspregister();
345 }
346 } else if(stracmp(cmd, 3, (char* []){"?", "h", "help"})) {
347 fprintf(stdout, "!<system command> -- Run a system command.\n");
348 fprintf(stdout, "b[reak] -- Manipulate Breakpoints. See details, `b ?'.\n");
349 fprintf(stdout, "c[ontinue] -- Continue running your program.\n");
350 fprintf(stdout, "d[ump] -- Display memory dump. `d[ump] a[uto]/n[oauto]' set auto/noauto display.\n");
351 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");
352 fprintf(stdout, "n[ext] -- Go next instruction.\n");
353 fprintf(stdout, "q[uit] -- Quit running your program.\n");
354 fprintf(stdout, "reset -- Reset COMET II CPU.\n");
355 fprintf(stdout, "resetall -- Reset COMET II CPU and memory.\n");
356 fprintf(stdout, "r[everse] -- Disassemble memory. `r[everse] <start address> <end address>.\n");
357 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");
358 fprintf(stdout, "t[race] -- Display CPU register. `t[race] a[uto]/n[oauto]' set auto/noauto display. \n");
359 fprintf(stdout, "T[race] -- Display CPU register as logical value. `t[race] a[uto]/n[oauto]' set auto/noauto display. \n");
360 fprintf(stdout, "?/h[elp] -- Display this help.\n");
361 }
362 return cmdtype;
363}
364
366{
367 int i;
368 assert(moncmdl != NULL);
369 if(moncmdl->args != NULL) {
370 for(i = 0; i < moncmdl->args->argc; i++) {
371 FREE(moncmdl->args->argv[i]);
372 }
373 FREE(moncmdl->args);
374 }
375 if(moncmdl->cmd != NULL) {
376 FREE(moncmdl->cmd);
377 }
378 if(moncmdl != NULL) {
379 FREE(moncmdl);
380 }
381}
382
384{
385 int stat = 0;
387 freebps();
390 if(cerr->num > 0) {
391 stat = 1;
392 }
393 freecerr();
394 return stat;
395}
396
398{
399 static char *buf = NULL;
400 static char *last_buf = NULL;
401 MONCMDLINE *moncmdl = NULL;
402 MONCMDTYPE cmdtype = MONREPEAT;
403
404 do {
405 buf = readline(monitor_prompt);
406 /* EOFの処理 */
407 if(buf == NULL) {
408 FREE(buf);
409 FREE(last_buf);
410 exit(monquit());
411 }
412 /* 空行(Enterだけ)の場合は、前回のコマンドをリピート */
413 if(buf[0] == '\0') {
414 if(last_buf == NULL) {
415 /* 前回実行したコマンドがなければ何もしない */
416 FREE(buf);
417 fprintf(stdout, ">\n");
418 } else {
419 buf = strdup_chk(last_buf, "monitor.buf_repeat");
420 cmdtype = MONREPEAT;
421 }
422 } else {
423 strip_end(buf); /* 文字列末尾の改行と空白を削除 */
424 /* 履歴(ヒストリ)に追加 */
425 add_history(buf);
426 last_buf = strdup_chk(buf, "monitor.last_buf");
427 }
428 /* 実行コマンドをstdout に出力。ログに残すため */
429 fprintf(stdout, "> %s\n", buf);
430
431 if(buf[0] == '!') {
432 system(buf + 1);
433 } else if((moncmdl = monlinetok(buf)) != NULL) {
434 cmdtype = monitorcmd(moncmdl->cmd, moncmdl->args);
435 free_moncmdline(moncmdl);
436 }
437 if(cmdtype == MONQUIT) {
438 FREE(buf);
439 FREE(last_buf);
440 exit(monquit());
441 }
442 } while(cmdtype == MONREPEAT);
443 FREE(buf);
444 FREE(last_buf);
445}
bool assemble(int filec, char *filev[], WORD adr)
指定された1つまたは複数のファイルを2回アセンブル
Definition assemble.c:727
CERR * cerr
現在のエラー
Definition cerr.c:9
void freecerr()
エラーリストと現在のエラーを解放する
Definition cerr.c:72
#define FREE(ptr)
メモリを解放するマクロ
Definition cmem.h:21
void strip_end(char *s)
文字列の末尾から、改行と空白とタブを削除する
Definition cmem.c:48
char * strndup_chk(const char *s, size_t len, const char *tag)
malloc_chkを実行してメモリを確保し、コピーした文字列の指定した長さの部分を返す
Definition cmem.c:33
void strip_casl2_comment(char *s)
文字列から「'」以降の文字列をCASL IIのコメントとして削除する。「''」の場合は除く
Definition cmem.c:55
void * malloc_chk(size_t size, const char *tag)
mallocを実行し、0で初期化する
Definition cmem.c:3
char * strdup_chk(const char *s, const char *tag)
malloc_chkを実行してメモリを確保し、コピーした文字列を返す
Definition cmem.c:25
void disassemble_memory(WORD start, WORD end)
COMET IIのメモリーを逆アセンブルし、標準出力へ出力する
void dumpmemory(WORD start, WORD end)
COMET IIのメモリを表示する
Definition dump.c:4
void dspregister()
COMET IIのレジスタを表示する
Definition dump.c:38
@ INT
Definition hash.h:9
unsigned hash(int keyc, HKEY *keyv[], int tabsize)
ハッシュ値を取得する
Definition hash.c:3
WORD loadassemble(const char *file, WORD start)
指定されたファイルからアセンブル結果を読み込む
Definition load.c:20
void monitor()
COMET IIモニターを起動する
Definition monitor.c:397
void free_moncmdline(MONCMDLINE *moncmdl)
Definition monitor.c:365
static char * monitor_prompt
comet2monitorのプロンプト
Definition monitor.c:15
MONCMDTYPE monitorcmd(char *cmd, MONARGS *args)
モニターの命令を実行する
Definition monitor.c:290
MONARGS * monargstok(const char *str)
文字列から、モニターの引数を取得する
Definition monitor.c:128
static BPSLIST * bps[BPSTABSIZE]
ブレークポイント表
Definition monitor.c:10
void warn_ignore_arg(int argc, char *argv[])
引数が無視されることを表示する
Definition monitor.c:200
bool getbps(WORD adr)
ブレークポイント表にアドレスがある場合はtrue、ない場合はfalseを返す
Definition monitor.c:29
void listbps()
Definition monitor.c:93
void mon_break(int argc, char *argv[])
Definition monitor.c:213
unsigned adrhash(WORD adr)
アドレスのハッシュ値を返す
Definition monitor.c:17
void freebpslist(BPSLIST *head)
Definition monitor.c:110
bool stracmp(char *str1, int str2c, char *str2v[])
Definition monitor.c:186
void freebps()
ブレークポイント表を解放する
Definition monitor.c:120
bool delbps(WORD adr)
ブレークポイント表からアドレスを削除する
Definition monitor.c:61
MONCMDLINE * monlinetok(const char *line)
行から、モニターの命令と引数を取得する
Definition monitor.c:150
bool addbps(WORD adr)
ブレークポイント表にアドレスを追加する
Definition monitor.c:41
int monquit()
モニター終了時の処理をする
Definition monitor.c:383
void mon_dump(int argc, char *argv[])
Definition monitor.c:260
@ BPSTABSIZE
Definition monitor.h:46
struct _BPSLIST BPSLIST
ブレークポイント表を表すデータ型
MONCMDTYPE
Definition monitor.h:53
@ MONNEXT
Definition monitor.h:55
@ MONQUIT
Definition monitor.h:56
@ MONREPEAT
Definition monitor.h:54
@ HASH_CMDTYPE
Definition struct.h:60
@ HASH_CODE
Definition struct.h:61
void comet2_shutdown()
Definition struct.c:306
EXECPTR * execptr
Definition struct.c:12
void free_cmdtable(CMDTAB_HASH hash)
Definition struct.c:135
SYSTEM * sys
COMET IIの仮想実行マシンシステム
Definition struct.c:7
void comet2_resetall()
Definition struct.c:295
void comet2_reset()
Definition struct.c:286
EXECMODE execmode
実行モード: trace, logical, dump, monitor, step
Definition exec.c:88
struct _BPSLIST * next
Definition monitor.h:38
WORD adr
Definition monitor.h:39
ハッシュ共用体のデータ型
Definition hash.h:15
UTYPE type
Definition hash.h:16
int i
Definition hash.h:19
union HKEY::@132303155052201023056030363273137132157157107260 val
モニター引数を表すデータ型
Definition monitor.h:21
int argc
Definition monitor.h:22
char * argv[MONARGSIZE]
Definition monitor.h:23
モニター命令行を表すデータ型
Definition monitor.h:29
MONARGS * args
Definition monitor.h:31
char * cmd
Definition monitor.h:30
unsigned short WORD
16ビットの数値を表すデータ型
Definition word.h:9
WORD nh2word(const char *str)
10進数または16進数の文字列をWORD値に変換する
Definition word.c:82