X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fcomet2.c;h=f358ec110a5a80153fd931768c65431b814bc23e;hp=e51b0e0582894781665cd8789f6ed13ac1d4832f;hb=HEAD;hpb=3ea92ba68cb320156d5bc7fe92f5e5fa0c150635 diff --git a/src/comet2.c b/src/comet2.c index e51b0e0..f358ec1 100644 --- a/src/comet2.c +++ b/src/comet2.c @@ -1,54 +1,49 @@ -#include "casl2.h" +#include "package.h" #include "exec.h" -#define _GNU_SOURCE -#include +#include "load.h" -/* comet2コマンドのオプション */ +/** + * comet2コマンドのオプション + */ static struct option longopts[] = { {"trace", no_argument, NULL, 't'}, {"tracearithmetic", no_argument, NULL, 't'}, {"tracelogical", no_argument, NULL, 'T'}, {"dump", no_argument, NULL, 'd'}, + {"monitor", no_argument, NULL, 'm'}, {"memorysize", required_argument, NULL, 'M'}, {"clocks", required_argument, NULL, 'C'}, + {"version", no_argument, NULL, 'v' }, {"help", no_argument, NULL, 'h'}, - {0, 0, 0, 0} + {0, 0, 0, 0}, }; -/* エラー番号とエラーメッセージ */ -CERRARRAY cerr_comet2[] = { - { 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" }, -}; - -/* 指定されたファイルからアセンブル結果を読込 */ -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コマンド */ +/** + * @brief comet2コマンドのメイン + * + * @return 正常終了時は0、異常終了時は1 + * + * @param argc コマンドライン引数の数 + * @param *argv[] コマンドライン引数の配列 + */ int main(int argc, char *argv[]) { - int opt; - const char *usage = "Usage: %s [-tTdh] [-M ] [-C ] FILE\n"; + int memsize = DEFAULT_MEMSIZE; + int clocks = DEFAULT_CLOCKS; + int opt = 0; + int stat = 0; + const char *version = PACKAGE_VERSION; + const char *cmdversion = "comet2 of YACASL2 version %s\n"; + const char *usage = "Usage: %s [-tTdmvh] [-M ] [-C ] FILE\n"; + + /* エラーの定義 */ + cerr_init(); + addcerrlist_load(); + addcerrlist_exec(); - while((opt = getopt_long(argc, argv, "tTdM:C:h", longopts, NULL)) != -1) { + /* オプションの処理 */ + while((opt = getopt_long(argc, argv, "tTdmM:C:vh", longopts, NULL)) != -1) { switch(opt) { case 't': execmode.trace = true; @@ -60,30 +55,43 @@ int main(int argc, char *argv[]) case 'd': execmode.dump = true; break; + case 'm': + execmode.monitor = true; + break; case 'M': memsize = atoi(optarg); break; case 'C': clocks = atoi(optarg); break; + case 'v': + fprintf(stdout, cmdversion, version); + goto comet2fin; case 'h': fprintf(stdout, usage, argv[0]); - return 0; + goto comet2fin; case '?': fprintf(stderr, usage, argv[0]); - exit(-1); + setcerr(212, ""); /* invalid option */ + goto comet2fin; } } - /* エラーリストにerr_comet2を追加 */ - addcerrlist(ARRAYSIZE(cerr_comet2), cerr_comet2); - reset(); - startptr = 0; - if(loadassemble(argv[optind]) == true) { - exec(); /* プログラム実行 */ + if(argv[optind] == NULL) { + setcerr(211, ""); /* object file not specified */ + fprintf(stderr, "comet2 error - %d: %s\n", cerr->num, cerr->msg); + goto comet2fin; + } + reset(memsize, clocks); /* COMET II仮想マシンのリセット */ + execptr->start = 0; + execptr->end = loadassemble(argv[optind], execptr->start); + if(execptr->end > 0 && cerr->num == 0) { + exec(); /* プログラム実行 */ } - if(cerrno > 0) { - freecerr(); - exit(-1); + shutdown(); /* COMET II仮想マシンのシャットダウン */ +comet2fin: + if(cerr->num > 0) { + stat = 1; } - return 0; + freecerr(); /* エラーの解放 */ + return stat; }