diff --git a/Makefile b/Makefile index e5b3e8c..0fc2108 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -OBJS=emul.o emul_inst_exec.o emul_inst_decipher.o emul_reg.o emul_mem.o emul_label.o emul_io_std.o emul_opt.o +OBJS=emul.o emul_inst_exec.o emul_inst_decipher.o emul_reg.o emul_mem.o emul_label.o emul_io_std.o emul_opt.o emul_repl.o PROGRAM=emul CC=gcc CCOPT=-lncurses diff --git a/emul.c b/emul.c index 98bc502..63d1060 100644 --- a/emul.c +++ b/emul.c @@ -1,7 +1,7 @@ #include #include "emul_inst_exec.h" #include "emul_opt.h" -//#include +#include "emul_repl.h" void event_fa(char *arg) { exitcode_t code; @@ -21,6 +21,10 @@ printf("interrupt start? Y/n :"); c = fgetc(stdin); if ( c == 'n' || c == 'N' ) return 0; - else { printf("ok\n"); } + else { + emul_repl_start(); + emul_repl_end(); + } } + return 0; } diff --git a/emul_inst_decipher.c b/emul_inst_decipher.c index e2931fe..fe0d02a 100644 --- a/emul_inst_decipher.c +++ b/emul_inst_decipher.c @@ -69,19 +69,30 @@ }; -#define PSEUDO_COUNT 11 +#define PSEUDO_COUNT 22 emul_pseudo_t pseudo_dict[REG_COUNT] = { { DUMP , ":DUMP" }, + { DUMP , ":dump" }, { MEMCAT , ":MEMCAT" }, + { MEMCAT , ":memcat" }, { MEMCATW , ":MEMCATW" }, + { MEMCATW , ":memcatw" }, { REGCAT , ":REGCAT" }, + { REGCAT , ":regcat" }, { LABEL , ":LABEL" }, + { LABEL , ":label" }, { RUN , ":RUN" }, + { RUN , ":run" }, { STEP , ":STEP" }, + { STEP , ":step" }, { INPUT , ":INPUT" }, + { INPUT , ":input" }, { OUTPUT , ":OUTPUT" }, + { OUTPUT , ":output" }, { CRLF , ":CRLF" }, - { EXIT , ":EXIT" } + { CRLF , ":crlf" }, + { EXIT , ":EXIT" }, + { EXIT , ":exit" } }; @@ -90,6 +101,8 @@ static int program_line_max; // *_load()を呼び出したときにのみ更新 void emul_inst_dec_init() { + program_line_max = 0; + program_line_current = 0; } void emul_inst_dec_set_pc(int pc) { @@ -119,13 +132,13 @@ int emul_inst_dec_load(const char *filename) { FILE *fp; int line; - emul_out_std_debug("Now opening file $s...\n", filename); + emul_out_std_debug("Now opening file %s...\n", filename); if ((fp = fopen(filename, "r")) == NULL) { emul_out_std_err("%s is can not open\n", filename); return 0; } - emul_out_std_debug("Now loading file $s...\n", filename); + emul_out_std_debug("Now loading file %s...\n", filename); program_line_current = 0; line = 0; while ( fgets(program[line], LOAD_LINE_WIDTH_MAX, fp) != NULL ) { @@ -139,12 +152,30 @@ return 1; } +int emul_inst_dec_loadline(const char *line) { + int i; + emul_out_std_debug("Now loading line %s...\n", line); + // 読み込み + strcpy(program[program_line_max], line); + // 読み込んだ場所へPCをセット + emul_reg_set(Spc, program_line_max); + // 最大値をインクリメント + program_line_max++; + // デバック用 + for (i = 0; i < program_line_max; i++) { + emul_out_std_debug("%s\n", program[i]); + } + emul_out_std_debug("Now program line max = %d\n", program_line_max); + return 1; +} + int emul_inst_dec_get_token(char *token) { static char line[LOAD_LINE_WIDTH_MAX] = {0}; static char *current = line; int fShift; int i; + emul_out_std_debug("get_token: start\n"); do { fShift = 0; // 読み込んだ行の先頭が印字可能文字になるようにシフト @@ -160,6 +191,9 @@ current = line; // currentにセット strcpy(current, program[program_line_current]); + emul_out_std_debug("get_token: program_line_current %d\n", program_line_current); + emul_out_std_debug("get_token: program_line_current \"%s\"\n", program[program_line_current]); + emul_out_std_debug("get_token: current = \"%s\"\n", current); // 読み込みを行った場合はシフトフラグをセット fShift = 1; } @@ -167,7 +201,7 @@ // 次のスペース(もしくは'\0')が現れるまで読み込み i = 0; - while ( !isspace(*current) ) { + while ( !isspace(*current) && *current != '\0' ) { token[i] = *current; i++, current++; // インクリメントに注意する } diff --git a/emul_inst_decipher.h b/emul_inst_decipher.h index 94ecdec..73c7273 100644 --- a/emul_inst_decipher.h +++ b/emul_inst_decipher.h @@ -28,6 +28,8 @@ int emul_inst_dec_load(const char *filename); +int emul_inst_dec_loadline(const char *line); + void emul_inst_dec_set_pc(int pc); void emul_inst_dec_update_pc(); diff --git a/emul_inst_exec.c b/emul_inst_exec.c index f1c437e..79312bf 100644 --- a/emul_inst_exec.c +++ b/emul_inst_exec.c @@ -45,6 +45,7 @@ void emul_inst_exec_init() { emul_std_init(); emul_reg_init(); + emul_inst_dec_init(); } void emul_inst_exec_end() { emul_std_end(); @@ -58,6 +59,13 @@ return 1; } +int emul_inst_exec_loadline(char *line) { + // ロードできないなら終了 + if ( emul_inst_dec_loadline(line) == 0 ) return 0; + // できたらラベルも読み込み + //emul_inst_exec_load_label(); + return 1; +} /* オペコード解読処理群 - START */ void emul_inst_exec_add(int r0, int r1, int r2) { @@ -181,9 +189,13 @@ * LAST_CMD [OPTION] */ void emul_inst_exec_pseudo_dump(const char *option) { if ( strcmp(option, "NONE") == 0 ) emul_std_debug(0); + if ( strcmp(option, "none") == 0 ) emul_std_debug(0); if ( strcmp(option, "DEBUG0") == 0 ) emul_std_debug(0); + if ( strcmp(option, "debug0") == 0 ) emul_std_debug(0); if ( strcmp(option, "DEBUG1") == 0 ) emul_std_debug(1); + if ( strcmp(option, "debug1") == 0 ) emul_std_debug(1); if ( strcmp(option, "DEBUG2") == 0 ) emul_std_debug(2); + if ( strcmp(option, "debug2") == 0 ) emul_std_debug(2); emul_out_std_debug("PseudoExec: DUMP option[%s];\n", option); } @@ -350,7 +362,10 @@ int ps_id; int pc; - emul_out_std_debug(">>START STEP!>>\n"); + int temp_op[3]; + char label[LABEL_NAME_LENGTH_MAX]; + + emul_out_std_debug(">>>Start step\n"); // トークンを取得 if ( emul_inst_dec_get_token(token) == 0 ) { @@ -376,8 +391,6 @@ } } - int temp_op[3]; - char label[LABEL_NAME_LENGTH_MAX]; // オペコード実行 switch (op_id) { case ADD : @@ -485,6 +498,7 @@ exitcode_t emul_inst_exec_run() { // step関数を終わるまで呼び出せばよい int exit_code; + emul_out_std_debug(">>>Start run\n"); while ( (exit_code = emul_inst_exec_step()) == Success ) { if (RunMode == Step) { emul_out_std("...Wait for step, please input key enter.\n"); @@ -494,6 +508,7 @@ return Success; } } + emul_out_std_debug("<< static int debug_level = 0; @@ -9,6 +8,8 @@ static WINDOW *win; +WINDOW* emul_getWin() { return win; } + void emul_std_init() { win = initscr(); scrollok(win, TRUE); diff --git a/emul_io_std.h b/emul_io_std.h index 9b1ec34..5d0e348 100644 --- a/emul_io_std.h +++ b/emul_io_std.h @@ -3,9 +3,12 @@ #include #include +#include void emul_std_debug(int level); +WINDOW* emul_getWin(); + void emul_std_init(); void emul_std_end(); diff --git a/emul_repl.c b/emul_repl.c new file mode 100644 index 0000000..017b6a2 --- /dev/null +++ b/emul_repl.c @@ -0,0 +1,47 @@ +#include "emul_repl.h" +#include "emul_inst_exec.h" +#include "emul_io_std.h" +#include +#include +#include + +#define READ_LINE_MAX 64 + +static WINDOW *win; + +void emul_repl_readline(char input[READ_LINE_MAX]) { + printw(">>"); + getnstr(input, READ_LINE_MAX); + //strtok(input, "\n\0"); +} + +void emul_repl_printline(); + +void emul_repl_init() { + emul_inst_exec_init(); +} + +void emul_repl_start() { + char input[READ_LINE_MAX]; + exitcode_t state; + + emul_repl_init(); + // ウィンドウポインタ取得 + win = emul_getWin(); + + while (1) { + emul_repl_readline(input); + emul_inst_exec_loadline(input); + state = emul_inst_exec_run(); + if (state == Failed) { + printw("Abend exit\n"); + } else if (state == Success) { + printw("Sucsess\n"); + break; + } + } +} + +void emul_repl_end() { + emul_inst_exec_end(); +} diff --git a/emul_repl.h b/emul_repl.h new file mode 100644 index 0000000..004dd10 --- /dev/null +++ b/emul_repl.h @@ -0,0 +1,10 @@ +#ifndef ENUL_REPL__ +#define ENUL_REPL__ + +void emul_repl_init(); + +void emul_repl_start(); + +void emul_repl_end(); + +#endif