-#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},
};
-/* 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 loadassemble(char *file) {
- FILE *fp;
- bool status = true;
-
- if((fp = fopen(file, "r")) == NULL) {
- perror(file);
- return false;
- }
- 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 status;
-}
-
-/* comet2コマンド */
+/**
+ * comet2コマンドのメイン
+ */
int main(int argc, char *argv[])
{
- int opt, status = 0;
+ int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS;
+ int opt, stat = 0;
const char *usage = "Usage: %s [-tTdh] [-M <MEMORYSIZE>] [-C <CLOCKS>] FILE\n";
- /* エラーの初期化 */
- cerr = malloc_chk(sizeof(CERR), "cerr");
- addcerrlist_comet2();
+ cerr_init();
+ addcerrlist_load();
+ addcerrlist_exec();
+
/* オプションの処理 */
while((opt = getopt_long(argc, argv, "tTdM:C:h", longopts, NULL)) != -1) {
switch(opt) {
return 0;
case '?':
fprintf(stderr, usage, argv[0]);
- exit(-1);
+ exit(1);
}
}
if(argv[optind] == NULL) {
- setcerr(208, NULL); /* object file is not specified */
+ setcerr(211, NULL); /* object file not specified */
fprintf(stderr, "comet2 error - %d: %s\n", cerr->num, cerr->msg);
- exit(-1);
+ exit(1);
}
- reset();
- progprop->start = 0;
+ /* COMET II仮想マシンのリセット */
+ reset(memsize, clocks);
+ execptr->start = 0;
if(loadassemble(argv[optind]) == true) {
- create_code_type(); /* 命令と命令タイプがキーのハッシュ表を作成 */
exec(); /* プログラム実行 */
- free_code_type(); /* 命令と命令タイプがキーのハッシュ表を解放 */
}
/* COMET II仮想マシンのシャットダウン */
shutdown();
- if(cerr->num > 0) {
- status = -1;
- }
+ stat = (cerr->num == 0) ? 0 : -1;
/* エラーの解放 */
freecerr();
- return status;
+ return stat;
}