-#include "casl2.h"
-#include "exec.h"
+#include <stdio.h>
+#include <stdlib.h>
#define _GNU_SOURCE
#include <getopt.h>
-/* comet2コマンドのオプション */
+#include "exec.h"
+#include "cmem.h"
+#include "cerr.h"
+
+/**
+ * comet2コマンドのオプション
+ */
static struct option longopts[] = {
{"trace", no_argument, NULL, 't'},
{"tracearithmetic", no_argument, NULL, 't'},
{"memorysize", required_argument, NULL, 'M'},
{"clocks", required_argument, NULL, 'C'},
{"help", no_argument, NULL, 'h'},
- {0, 0, 0, 0}
+ {0, 0, 0, 0},
};
-/* エラー番号とエラーメッセージ */
-CERRARRAY cerr[] = {
- { 201, "Load object file - full of COMET II memory" },
- { 202, "SVC input - out of Input memory" },
- { 203, "SVC output - out of COMET II memory" },
- { 204, "Program Register (PR) - out of COMET II memory" },
- { 205, "Stack Pointer (SP) - cannot allocate stack buffer" },
- { 206, "Address - out of COMET II memory" },
- { 207, "Stack Pointer (SP) - out of COMET II memory" },
- { 0, NULL },
-};
-
-/* 指定されたファイルからアセンブル結果を読込 */
-bool loadassemble(char *file) {
- FILE *fp;
- if((fp = fopen(file, "r")) == NULL) {
- perror(file);
- return false;
- }
- if((endptr = startptr + fread(memory, sizeof(WORD), memsize-startptr, fp)) == memsize) {
- setcerr(201, NULL); /* Load object file - full of COMET II memory */
- fprintf(stderr, "Execute error - %d: %s\n", cerrno, cerrmsg);
- return false;
- }
- fclose(fp);
- return true;
-}
-
-/* comet2コマンド */
+/**
+ * comet2コマンドのメイン
+ */
int main(int argc, char *argv[])
{
- int opt;
- const char *usage = "Usage: %s [-tTdh] [-M <memorysize>] [-C <clocks>] FILE\n";
+ int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS;
+ int opt, stat = 0;
+ const char *usage = "Usage: %s [-tTdh] [-M <MEMORYSIZE>] [-C <CLOCKS>] FILE\n";
+ cerr_init();
+ addcerrlist_load();
+ addcerrlist_exec();
+
+ /* オプションの処理 */
while((opt = getopt_long(argc, argv, "tTdM:C:h", longopts, NULL)) != -1) {
switch(opt) {
case 't':
- (&execmode)->trace = true;
+ execmode.trace = true;
break;
case 'T':
- (&execmode)->trace = true;
- (&execmode)->logical = true;
+ execmode.trace = true;
+ execmode.logical = true;
break;
case 'd':
- (&execmode)->dump = true;
+ execmode.dump = true;
break;
case 'M':
memsize = atoi(optarg);
return 0;
case '?':
fprintf(stderr, usage, argv[0]);
- exit(-1);
+ exit(1);
}
}
- reset();
- startptr = 0;
- if(loadassemble(argv[optind]) == true) {
- exec(); /* プログラム実行 */
+ if(argv[optind] == NULL) {
+ setcerr(211, NULL); /* object file not specified */
+ fprintf(stderr, "comet2 error - %d: %s\n", cerr->num, cerr->msg);
+ exit(1);
}
- if(cerrno > 0) {
- freecerr();
- exit(-1);
+ /* COMET II仮想マシンのリセット */
+ reset(memsize, clocks);
+ execptr->start = 0;
+ if(loadassemble(argv[optind]) == true) {
+ exec(); /* プログラム実行 */
}
- return 0;
+ /* COMET II仮想マシンのシャットダウン */
+ shutdown();
+ stat = (cerr->num == 0) ? 0 : -1;
+ /* エラーの解放 */
+ freecerr();
+ return stat;
}