From: j8takagi Date: Mon, 8 Jun 2026 11:16:35 +0000 (+0900) Subject: コマンド実行時のクロック周波数とメモリサイズの指定時にチェックするように X-Git-Tag: v0.5p48 X-Git-Url: https://j8takagi.net/gitweb?a=commitdiff_plain;h=41e630877477f8416799b85e0db7ab776fae6a5a;p=yacasl2.git コマンド実行時のクロック周波数とメモリサイズの指定時にチェックするように --- diff --git a/VERSION b/VERSION index f148788..f3d98b8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v0.5p47 +v0.5p48 diff --git a/as/sample/test.casl b/as/sample/test.casl new file mode 100644 index 0000000..5356eba --- /dev/null +++ b/as/sample/test.casl @@ -0,0 +1,3 @@ +TEST START + RET + END diff --git a/include/cmem.h b/include/cmem.h index f8ccf50..622f4d3 100644 --- a/include/cmem.h +++ b/include/cmem.h @@ -6,6 +6,7 @@ #include #include #include +#include /** * @brief 配列のサイズを返すマクロ @@ -21,6 +22,17 @@ #define FREE(ptr) {free(ptr); ptr = NULL;} #endif +/** + * @brief 数値文字列が特定の範囲の数値かチェックし、正の場合は変換した数値、不正の場合は0を返す + * + * @param str 数値文字列 + * @param min 範囲最小値 + * @param min 範囲最大値 + * @param tag エラーメッセージなどで表示される名前 + * + */ +long str2l_range(const char *str, long min, long max, const char *name); + /** * @brief mallocを実行し、0で初期化する * diff --git a/include/exec.h b/include/exec.h index 9a8a45b..855efee 100644 --- a/include/exec.h +++ b/include/exec.h @@ -14,6 +14,15 @@ enum { INSIZE = 256 /** MAX_CLOCKS) { + val = MAX_CLOCKS; + fprintf(stderr, "Info - %s: Clock frequency exceeds maximum. Set to %ld\n", str, val); + } + return (CLOCK)val; +} + char *pr2str(WORD pr) { char *str = malloc_chk(CERRSTRSIZE + 1, "pr2str.pr"); @@ -110,7 +121,8 @@ void svcin() break; } if(sys->cpu->gr[1] + i > execptr->end) { - setcerr(208, ""); /* SVC input - memory overflow */ + setcerr + (208, ""); /* SVC input - memory overflow */ break; } sys->memory[sys->cpu->gr[1] + i] = buf[i]; @@ -672,8 +684,8 @@ void svc() void exec() { - clock_t clock_begin = 0; - clock_t clock_end = 0; + CLOCK clock_begin = 0; + CLOCK clock_end = 0; void (*cmdptr)() = NULL; char *s = NULL; const char *monmsg = "COMET II machine code monitor. Type ? for help.\n"; diff --git a/src/load.c b/src/load.c index 5ee775d..8eb83e1 100644 --- a/src/load.c +++ b/src/load.c @@ -30,6 +30,7 @@ WORD loadassemble(const char *file, WORD start) end = start + fread(sys->memory + start, sizeof(WORD), sys->memsize - start, fp); if(end == sys->memsize) { setcerr(210, file); /* load - memory overflow */ + fprintf(stderr, "Load error - %d: %s\n", cerr->num, cerr->msg); } fclose(fp); return end; diff --git a/src/struct.c b/src/struct.c index 444d849..086c286 100644 --- a/src/struct.c +++ b/src/struct.c @@ -1,3 +1,4 @@ +#include #include "struct.h" #include "exec.h" @@ -82,6 +83,16 @@ unsigned hash_cmdtype(const char *cmd, CMDTYPE type); */ unsigned hash_code(WORD code); +/** + * CPUのリセット + */ +void cpu_reset(); + +/** + * メモリのリセット + */ +void memory_reset(); + /** * 命令の名前とタイプからハッシュ値を生成する */ @@ -247,39 +258,53 @@ char *grstr(WORD word) return str; } -void cpu_reset() { - sys->cpu = malloc_chk(sizeof(CPU), "cpu"); - for(int i = 0; i < GRSIZE; i++) { /* 汎用レジスタ */ - sys->cpu->gr[i] = 0x0; - } - sys->cpu->sp = sys->memsize; /* スタックポインタ */ - sys->cpu->pr = 0x0; /* プログラムレジスタ */ - sys->cpu->fr = 0x0; /* フラグレジスタ */ -} - -void memory_reset() { +WORD memsize_str2word(const char *str) { + return (WORD)str2l_range(str, 1, MAX_MEMSIZE, "Memory Size"); } - /** * COMET II仮想マシンの初期化 */ -void comet2_init(int memsize, int clocks) +void comet2_init(WORD memsize, CLOCK clocks) { sys = malloc_chk(sizeof(SYSTEM), "sys"); /* メモリサイズを設定 */ + assert(0 < memsize && memsize <= MAX_MEMSIZE-1); sys->memsize = memsize; /* クロック周波数を設定 */ + assert(0 < clocks && clocks <= MAX_CLOCKS); sys->clocks = clocks; - /* メモリ領域の確保 */ - sys->memory = calloc_chk(sys->memsize, sizeof(WORD), "memory"); - /* CPUをクリア */ + /* CPU領域の確保 */ + sys->cpu = malloc_chk(sizeof(CPU), "comet2_init.cpu"); + /* CPUをリセット */ cpu_reset(); + /* メモリ領域の確保 */ + sys->memory = calloc_chk(sys->memsize, sizeof(WORD), "comet2_init.memory"); + /* メモリをリセット */ + memory_reset(); /* CASL2プログラムの開始と終了のアドレスを初期化 */ execptr = malloc_chk(sizeof(EXECPTR), "execptr"); execptr->stop = false; } + +void cpu_reset() { + /* 汎用レジスタ */ + for(int i = 0; i < GRSIZE; i++) { + sys->cpu->gr[i] = 0x0; + } + sys->cpu->sp = sys->memsize; /* スタックポインタ */ + sys->cpu->pr = 0x0; /* プログラムレジスタ */ + sys->cpu->fr = 0x0; /* フラグレジスタ */ +} + +/** + * メモリのリセット + */ +void memory_reset() { + memset(sys->memory, 0, sys->memsize * sizeof(WORD)); +} + /** * COMET II仮想マシンのCPUリセット */ @@ -297,7 +322,7 @@ void comet2_resetall() /* CPUをリセット */ cpu_reset(); /* メモリをリセット */ - memset(sys->memory, 0, sys->memsize * sizeof(WORD)); + memory_reset(); } /** diff --git a/test/system/casl2_err/err_202_memsize1/0.txt b/test/system/casl2_err/err_202_memsize1/0.txt new file mode 100644 index 0000000..ccbdff3 --- /dev/null +++ b/test/system/casl2_err/err_202_memsize1/0.txt @@ -0,0 +1 @@ +Execute error - 202: PR:#0000: Stack Pointer (SP) - stack overflow diff --git a/test/system/casl2_err/err_202_memsize1/Makefile b/test/system/casl2_err/err_202_memsize1/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_err/err_202_memsize1/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_err/err_202_memsize1/cmd b/test/system/casl2_err/err_202_memsize1/cmd new file mode 100755 index 0000000..bbe9813 --- /dev/null +++ b/test/system/casl2_err/err_202_memsize1/cmd @@ -0,0 +1 @@ +../../../../casl2 -M1 ../../../../as/sample/test.casl diff --git a/test/system/casl2_err/err_optC_-1/0.txt b/test/system/casl2_err/err_optC_-1/0.txt new file mode 100644 index 0000000..62e9f44 --- /dev/null +++ b/test/system/casl2_err/err_optC_-1/0.txt @@ -0,0 +1 @@ +-1: Clock out of range: 1 - diff --git a/test/system/casl2_err/err_optC_-1/Makefile b/test/system/casl2_err/err_optC_-1/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_err/err_optC_-1/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_err/err_optC_-1/cmd b/test/system/casl2_err/err_optC_-1/cmd new file mode 100755 index 0000000..fad578a --- /dev/null +++ b/test/system/casl2_err/err_optC_-1/cmd @@ -0,0 +1 @@ +../../../../casl2 -C -1 ../../../../as/sample/test.casl diff --git a/test/system/casl2_err/err_optC_0/0.txt b/test/system/casl2_err/err_optC_0/0.txt new file mode 100644 index 0000000..9604e87 --- /dev/null +++ b/test/system/casl2_err/err_optC_0/0.txt @@ -0,0 +1 @@ +0: Clock out of range: 1 - diff --git a/test/system/casl2_err/err_optC_0/Makefile b/test/system/casl2_err/err_optC_0/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_err/err_optC_0/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_err/err_optC_0/cmd b/test/system/casl2_err/err_optC_0/cmd new file mode 100755 index 0000000..4358be2 --- /dev/null +++ b/test/system/casl2_err/err_optC_0/cmd @@ -0,0 +1 @@ +../../../../casl2 -C 0 ../../../../as/sample/test.casl diff --git a/test/system/casl2_err/err_optC_1.5/0.txt b/test/system/casl2_err/err_optC_1.5/0.txt new file mode 100644 index 0000000..518625d --- /dev/null +++ b/test/system/casl2_err/err_optC_1.5/0.txt @@ -0,0 +1 @@ +Clock: `1.5' is not integer. diff --git a/test/system/casl2_err/err_optC_1.5/Makefile b/test/system/casl2_err/err_optC_1.5/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_err/err_optC_1.5/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_err/err_optC_1.5/cmd b/test/system/casl2_err/err_optC_1.5/cmd new file mode 100755 index 0000000..e1ee190 --- /dev/null +++ b/test/system/casl2_err/err_optC_1.5/cmd @@ -0,0 +1 @@ +../../../../casl2 -C 1.5 ../../../../as/sample/test.casl diff --git a/test/system/casl2_err/err_optC_abc/0.txt b/test/system/casl2_err/err_optC_abc/0.txt new file mode 100644 index 0000000..72497c4 --- /dev/null +++ b/test/system/casl2_err/err_optC_abc/0.txt @@ -0,0 +1 @@ +Clock: Not specified. diff --git a/test/system/casl2_err/err_optC_abc/Makefile b/test/system/casl2_err/err_optC_abc/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_err/err_optC_abc/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_err/err_optC_abc/cmd b/test/system/casl2_err/err_optC_abc/cmd new file mode 100755 index 0000000..7a352ab --- /dev/null +++ b/test/system/casl2_err/err_optC_abc/cmd @@ -0,0 +1 @@ +../../../../casl2 -C abc ../../../../as/sample/test.casl diff --git a/test/system/casl2_err/err_optM_-1/0.txt b/test/system/casl2_err/err_optM_-1/0.txt new file mode 100644 index 0000000..6a8178f --- /dev/null +++ b/test/system/casl2_err/err_optM_-1/0.txt @@ -0,0 +1 @@ +-1: Memory Size out of range: 1 - 65536 diff --git a/test/system/casl2_err/err_optM_-1/Makefile b/test/system/casl2_err/err_optM_-1/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_err/err_optM_-1/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_err/err_optM_-1/cmd b/test/system/casl2_err/err_optM_-1/cmd new file mode 100755 index 0000000..651193b --- /dev/null +++ b/test/system/casl2_err/err_optM_-1/cmd @@ -0,0 +1 @@ +../../../../casl2 -M -1 ../../../../as/sample/test.casl diff --git a/test/system/casl2_err/err_optM_0/0.txt b/test/system/casl2_err/err_optM_0/0.txt new file mode 100644 index 0000000..e3918ee --- /dev/null +++ b/test/system/casl2_err/err_optM_0/0.txt @@ -0,0 +1 @@ +0: Memory Size out of range: 1 - 65536 diff --git a/test/system/casl2_err/err_optM_0/Makefile b/test/system/casl2_err/err_optM_0/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_err/err_optM_0/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_err/err_optM_0/cmd b/test/system/casl2_err/err_optM_0/cmd new file mode 100755 index 0000000..d7d0649 --- /dev/null +++ b/test/system/casl2_err/err_optM_0/cmd @@ -0,0 +1 @@ +../../../../casl2 -M 0 ../../../../as/sample/test.casl diff --git a/test/system/casl2_err/err_optM_1.5/0.txt b/test/system/casl2_err/err_optM_1.5/0.txt new file mode 100644 index 0000000..eb4eb00 --- /dev/null +++ b/test/system/casl2_err/err_optM_1.5/0.txt @@ -0,0 +1 @@ +Memory Size: `1.5' is not integer. diff --git a/test/system/casl2_err/err_optM_1.5/Makefile b/test/system/casl2_err/err_optM_1.5/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_err/err_optM_1.5/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_err/err_optM_1.5/cmd b/test/system/casl2_err/err_optM_1.5/cmd new file mode 100755 index 0000000..98d338a --- /dev/null +++ b/test/system/casl2_err/err_optM_1.5/cmd @@ -0,0 +1 @@ +../../../../casl2 -M 1.5 ../../../../as/sample/test.casl diff --git a/test/system/casl2_err/err_optM_65537/0.txt b/test/system/casl2_err/err_optM_65537/0.txt new file mode 100644 index 0000000..4f53f89 --- /dev/null +++ b/test/system/casl2_err/err_optM_65537/0.txt @@ -0,0 +1 @@ +65537: Memory Size out of range: 1 - 65536 diff --git a/test/system/casl2_err/err_optM_65537/Makefile b/test/system/casl2_err/err_optM_65537/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_err/err_optM_65537/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_err/err_optM_65537/cmd b/test/system/casl2_err/err_optM_65537/cmd new file mode 100755 index 0000000..8146306 --- /dev/null +++ b/test/system/casl2_err/err_optM_65537/cmd @@ -0,0 +1 @@ +../../../../casl2 -M 65537 ../../../../as/sample/test.casl diff --git a/test/system/casl2_err/err_optM_abc/0.txt b/test/system/casl2_err/err_optM_abc/0.txt new file mode 100644 index 0000000..c8e767b --- /dev/null +++ b/test/system/casl2_err/err_optM_abc/0.txt @@ -0,0 +1 @@ +Memory Size: Not specified. diff --git a/test/system/casl2_err/err_optM_abc/Makefile b/test/system/casl2_err/err_optM_abc/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_err/err_optM_abc/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_err/err_optM_abc/cmd b/test/system/casl2_err/err_optM_abc/cmd new file mode 100755 index 0000000..03992f0 --- /dev/null +++ b/test/system/casl2_err/err_optM_abc/cmd @@ -0,0 +1 @@ +../../../../casl2 -M abc ../../../../as/sample/test.casl diff --git a/test/system/casl2_opt/opt_C_1/0.txt b/test/system/casl2_opt/opt_C_1/0.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/system/casl2_opt/opt_C_1/Makefile b/test/system/casl2_opt/opt_C_1/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_opt/opt_C_1/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_opt/opt_C_1/cmd b/test/system/casl2_opt/opt_C_1/cmd new file mode 100755 index 0000000..01fea54 --- /dev/null +++ b/test/system/casl2_opt/opt_C_1/cmd @@ -0,0 +1 @@ +../../../../casl2 -C 1 ../../../../as/sample/test.casl diff --git a/test/system/casl2_opt/opt_C_1000/0.txt b/test/system/casl2_opt/opt_C_1000/0.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/system/casl2_opt/opt_C_1000/Makefile b/test/system/casl2_opt/opt_C_1000/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_opt/opt_C_1000/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_opt/opt_C_1000/cmd b/test/system/casl2_opt/opt_C_1000/cmd new file mode 100755 index 0000000..1d7d948 --- /dev/null +++ b/test/system/casl2_opt/opt_C_1000/cmd @@ -0,0 +1 @@ +../../../../casl2 -C 1000 ../../../../as/sample/test.casl diff --git a/test/system/casl2_opt/opt_C_1000000/0.txt b/test/system/casl2_opt/opt_C_1000000/0.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/system/casl2_opt/opt_C_1000000/Makefile b/test/system/casl2_opt/opt_C_1000000/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_opt/opt_C_1000000/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_opt/opt_C_1000000/cmd b/test/system/casl2_opt/opt_C_1000000/cmd new file mode 100755 index 0000000..a50f9d0 --- /dev/null +++ b/test/system/casl2_opt/opt_C_1000000/cmd @@ -0,0 +1 @@ +../../../../casl2 -M 65535 ../../../../as/sample/test.casl diff --git a/test/system/casl2_opt/opt_C_1000001/0.txt b/test/system/casl2_opt/opt_C_1000001/0.txt new file mode 100644 index 0000000..189a126 --- /dev/null +++ b/test/system/casl2_opt/opt_C_1000001/0.txt @@ -0,0 +1 @@ +Info - 1000001: Clock frequency exceeds maximum. Set to 1000000 diff --git a/test/system/casl2_opt/opt_C_1000001/Makefile b/test/system/casl2_opt/opt_C_1000001/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_opt/opt_C_1000001/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_opt/opt_C_1000001/cmd b/test/system/casl2_opt/opt_C_1000001/cmd new file mode 100755 index 0000000..1585b05 --- /dev/null +++ b/test/system/casl2_opt/opt_C_1000001/cmd @@ -0,0 +1 @@ +../../../../casl2 -C 1000001 ../../../../as/sample/test.casl diff --git a/test/system/casl2_opt/opt_M_2/0.txt b/test/system/casl2_opt/opt_M_2/0.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/system/casl2_opt/opt_M_2/Makefile b/test/system/casl2_opt/opt_M_2/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_opt/opt_M_2/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_opt/opt_M_2/cmd b/test/system/casl2_opt/opt_M_2/cmd new file mode 100755 index 0000000..244f3c0 --- /dev/null +++ b/test/system/casl2_opt/opt_M_2/cmd @@ -0,0 +1 @@ +../../../../casl2 -M 2 ../../../../as/sample/test.casl diff --git a/test/system/casl2_opt/opt_M_512/0.txt b/test/system/casl2_opt/opt_M_512/0.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/system/casl2_opt/opt_M_512/Makefile b/test/system/casl2_opt/opt_M_512/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_opt/opt_M_512/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_opt/opt_M_512/cmd b/test/system/casl2_opt/opt_M_512/cmd new file mode 100755 index 0000000..6566ccf --- /dev/null +++ b/test/system/casl2_opt/opt_M_512/cmd @@ -0,0 +1 @@ +../../../../casl2 -M 512 ../../../../as/sample/test.casl diff --git a/test/system/casl2_opt/opt_M_65535/0.txt b/test/system/casl2_opt/opt_M_65535/0.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/system/casl2_opt/opt_M_65535/Makefile b/test/system/casl2_opt/opt_M_65535/Makefile new file mode 100644 index 0000000..b6dac59 --- /dev/null +++ b/test/system/casl2_opt/opt_M_65535/Makefile @@ -0,0 +1,2 @@ +include ../Define.mk +include ../Test.mk diff --git a/test/system/casl2_opt/opt_M_65535/cmd b/test/system/casl2_opt/opt_M_65535/cmd new file mode 100755 index 0000000..a50f9d0 --- /dev/null +++ b/test/system/casl2_opt/opt_M_65535/cmd @@ -0,0 +1 @@ +../../../../casl2 -M 65535 ../../../../as/sample/test.casl