Merge branch 'master' into cmd-casl
[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 /* 実行モード: trace, logical, dump */
19 EXECMODE execmode = {false, false, false};
20
21 /* エラー番号とエラーメッセージ */
22 CERRARRAY cerr[] = {
23     { 201, "execute - out of COMET II memory" },
24     { 202, "SVC input - out of Input memory" },
25     { 203, "SVC output - out of COMET II memory" },
26     { 204, "Program Register (PR) - out of COMET II memory" },
27     { 205, "Stack Pointer (SP) - cannot allocate stack buffer" },
28     { 206, "Address - out of COMET II memory" },
29     { 207, "Stack Pointer (SP) - out of COMET II memory" },
30     { 0, NULL },
31 };
32
33 /* 指定されたファイルからアセンブル結果を読込 */
34 bool loadassemble(char *file) {
35     FILE *fp;
36     if((fp = fopen(file, "r")) == NULL) {
37         perror(file);
38         return false;
39     }
40     endptr = startptr + fread(memory, sizeof(WORD), memsize, fp);
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     reset();
78     startptr = 0;
79     if(loadassemble(argv[optind]) == true) {
80         exec();    /* プログラム実行 */
81     }
82     if(cerrno > 0) {
83         freecerr();
84         exit(-1);
85     }
86     return 0;
87 }