e51b0e0582894781665cd8789f6ed13ac1d4832f
[YACASL2.git] / src / comet2.c
1 #include "casl2.h"
2 #include "exec.h"
3 #define _GNU_SOURCE
4 #include <getopt.h>
5
6 /* comet2コマンドのオプション */
7 static struct option longopts[] = {
8     {"trace", no_argument, NULL, 't'},
9     {"tracearithmetic", no_argument, NULL, 't'},
10     {"tracelogical", no_argument, NULL, 'T'},
11     {"dump", no_argument, NULL, 'd'},
12     {"memorysize", required_argument, NULL, 'M'},
13     {"clocks", required_argument, NULL, 'C'},
14     {"help", no_argument, NULL, 'h'},
15     {0, 0, 0, 0}
16 };
17
18 /* エラー番号とエラーメッセージ */
19 CERRARRAY cerr_comet2[] = {
20     { 201, "Load object file - full of COMET II memory" },
21     { 202, "SVC input - out of Input memory" },
22     { 203, "SVC output - out of COMET II memory" },
23     { 204, "Program Register (PR) - out of COMET II memory" },
24     { 205, "Stack Pointer (SP) - cannot allocate stack buffer" },
25     { 206, "Address - out of COMET II memory" },
26     { 207, "Stack Pointer (SP) - out of COMET II memory" },
27 };
28
29 /* 指定されたファイルからアセンブル結果を読込 */
30 bool loadassemble(char *file) {
31     FILE *fp;
32     if((fp = fopen(file, "r")) == NULL) {
33         perror(file);
34         return false;
35     }
36     if((endptr = startptr + fread(memory, sizeof(WORD), memsize-startptr, fp)) == memsize) {
37         setcerr(201, NULL);    /* Load object file - full of COMET II memory */
38         fprintf(stderr, "Execute error - %d: %s\n", cerrno, cerrmsg);
39         return false;
40     }
41     fclose(fp);
42     return true;
43 }
44
45 /* comet2コマンド */
46 int main(int argc, char *argv[])
47 {
48     int opt;
49     const char *usage = "Usage: %s [-tTdh] [-M <memorysize>] [-C <clocks>] FILE\n";
50
51     while((opt = getopt_long(argc, argv, "tTdM:C:h", longopts, NULL)) != -1) {
52         switch(opt) {
53         case 't':
54             execmode.trace = true;
55             break;
56         case 'T':
57             execmode.trace = true;
58             execmode.logical = true;
59             break;
60         case 'd':
61             execmode.dump = true;
62             break;
63         case 'M':
64             memsize = atoi(optarg);
65             break;
66         case 'C':
67             clocks = atoi(optarg);
68             break;
69         case 'h':
70             fprintf(stdout, usage, argv[0]);
71             return 0;
72         case '?':
73             fprintf(stderr, usage, argv[0]);
74             exit(-1);
75         }
76     }
77     /* エラーリストにerr_comet2を追加 */
78     addcerrlist(ARRAYSIZE(cerr_comet2), cerr_comet2);
79     reset();
80     startptr = 0;
81     if(loadassemble(argv[optind]) == true) {
82         exec();    /* プログラム実行 */
83     }
84     if(cerrno > 0) {
85         freecerr();
86         exit(-1);
87     }
88     return 0;
89 }