X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fcomet2.c;h=6ab8693732410928b2397f9b03f2c25b49e61305;hp=bfe1634a7d8c91ad0305c25be73d7ab9d7a13a71;hb=df686f409d06eaac1e8909cbe453f256d56ae14f;hpb=ccc3acda4256e10a822e41e84f6c9991271c2f61 diff --git a/src/comet2.c b/src/comet2.c index bfe1634..6ab8693 100644 --- a/src/comet2.c +++ b/src/comet2.c @@ -3,6 +3,7 @@ #define _GNU_SOURCE #include +/* comet2コマンドのオプション */ static struct option longopts[] = { {"trace", no_argument, NULL, 't'}, {"tracearithmetic", no_argument, NULL, 't'}, @@ -14,50 +15,57 @@ static struct option longopts[] = { {0, 0, 0, 0} }; -EXECMODE execmode = {false, false, false}; - -/* エラー番号とエラーメッセージ */ -CERRARRAY cerr[] = { - { 201, "execute - out 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 }, +/* comet2のエラー定義 */ +CERR cerr_comet2[] = { + { 201, "load object file - full of COMET II memory" }, + { 208, "object file is not specified" }, }; +bool addcerrlist_comet2() +{ + return addcerrlist(ARRAYSIZE(cerr_comet2), cerr_comet2); +} /* 指定されたファイルからアセンブル結果を読込 */ -bool inassemble(char *file) { +bool loadassemble(char *file) { FILE *fp; - reset(); + bool status = true; + if((fp = fopen(file, "r")) == NULL) { perror(file); return false; } - fread(memory, sizeof(WORD), memsize, fp); + progprop->end = progprop->start + + fread(memory, sizeof(WORD), memsize-progprop->start, fp); + if(progprop->end == memsize) { + setcerr(201, NULL); /* Load object file - full of COMET II memory */ + fprintf(stderr, "Execute error - %d: %s\n", cerr->num, cerr->msg); + status = false; + } fclose(fp); - return true; + return status; } /* comet2コマンド */ int main(int argc, char *argv[]) { - int opt; - const char *usage = "Usage: %s [-tTdh] [-M ] [-C ] FILE\n"; + int opt, status = 0; + const char *usage = "Usage: %s [-tTdh] [-M ] [-C ] FILE\n"; + /* エラーの初期化 */ + cerr = malloc_chk(sizeof(CERR), "cerr"); + addcerrlist_comet2(); + /* オプションの処理 */ while((opt = getopt_long(argc, argv, "tTdM:C:h", longopts, NULL)) != -1) { switch(opt) { case 't': - (&execmode)->tracemode = true; + execmode.trace = true; break; case 'T': - (&execmode)->tracemode = true; - (&execmode)->logicalmode = true; + execmode.trace = true; + execmode.logical = true; break; case 'd': - (&execmode)->dumpmode = true; + execmode.dump = true; break; case 'M': memsize = atoi(optarg); @@ -73,12 +81,24 @@ int main(int argc, char *argv[]) exit(-1); } } - if(inassemble(argv[optind]) == true) { - exec(); /* プログラム実行 */ - } - if(cerrno > 0) { - freecerr(); + if(argv[optind] == NULL) { + setcerr(208, NULL); /* object file is not specified */ + fprintf(stderr, "comet2 error - %d: %s\n", cerr->num, cerr->msg); exit(-1); } - return 0; + reset(); + progprop->start = 0; + if(loadassemble(argv[optind]) == true) { + create_code_type(); /* 命令と命令タイプがキーのハッシュ表を作成 */ + exec(); /* プログラム実行 */ + free_code_type(); /* 命令と命令タイプがキーのハッシュ表を解放 */ + } + /* COMET II仮想マシンのシャットダウン */ + shutdown(); + if(cerr->num > 0) { + status = -1; + } + /* エラーの解放 */ + freecerr(); + return status; }