実行時に汎用レジスタの指定が不正だった場合のエラー「209」を追加
[YACASL2.git] / src / exec.c
index 4bbe350..887d9fe 100644 (file)
@@ -16,6 +16,7 @@ static CERR cerr_exec[] = {
     { 205, "Stack Pointer (SP) - cannot allocate stack buffer" },
     { 206, "Address - out of COMET II memory" },
     { 207, "Stack Pointer (SP) - out of COMET II memory" },
+    { 209, "not GR in operand x" },
 };
 
 /**
@@ -356,15 +357,19 @@ bool exec()
             sprintf(errpr, "PR:#%04X", sys->cpu->pr);
             setcerr(204, errpr);    /* Program Register (PR) - out of COMET II memory */
         }
+        /* スタック領域を確保できない場合はエラー */
+        else if(sys->cpu->sp <= prog->end) {
+            sprintf(errpr, "PR:#%04X", sys->cpu->pr);
+            setcerr(205, errpr);    /* Stack Pointer (SP) - cannot allocate stack buffer */
+        }
         /* スタック領域のアドレスが主記憶の範囲外の場合はエラー */
-        if(sys->cpu->sp > sys->memsize) {
+        else if(sys->cpu->sp > sys->memsize) {
             sprintf(errpr, "PR:#%04X", sys->cpu->pr);
             setcerr(207, errpr);    /* Stack Pointer (SP) - out of COMET II memory */
         }
-        /* スタック領域を確保できない場合はエラー */
-        if(sys->cpu->sp <= prog->end) {
-            sprintf(errpr, "PR:#%04X", sys->cpu->pr);
-            setcerr(205, errpr);    /* Stack Pointer (SP) - cannot allocate stack buffer */
+        /* エラー発生時は終了 */
+        if(cerr->num > 0) {
+            goto execerr;
         }
         /* 命令の取り出し */
         op = sys->memory[sys->cpu->pr] & 0xFF00;
@@ -372,10 +377,6 @@ bool exec()
         cmdtype = getcmdtype(op);
         r_r1 = (sys->memory[sys->cpu->pr] >> 4) & 0xF;
         x_r2 = sys->memory[sys->cpu->pr] & 0xF;
-        /* エラー発生時は終了 */
-        if(cerr->num > 0) {
-            goto execerr;
-        }
         /* traceオプション指定時、レジスタを出力 */
         if(execmode.trace){
             fprintf(stdout, "#%04X: Register::::\n", sys->cpu->pr);
@@ -386,18 +387,28 @@ bool exec()
             fprintf(stdout, "#%04X: Memory::::\n", sys->cpu->pr);
             dumpmemory();
         }
-        /* どちらかのオプション指定時、改行を出力 */
+        /* traceまたはdumpオプション指定時、改行を出力 */
         if(execmode.dump || execmode.trace) {
             fprintf(stdout, "\n");
         }
         sys->cpu->pr++;
         /* オペランドの取り出し */
         if(cmdtype == R1_R2) {
-            assert(x_r2 < GRSIZE);
+            /* オペランドの数値が汎用レジスタの範囲外の場合はエラー */
+            if(x_r2 > GRSIZE - 1) {
+                sprintf(errpr, "PR:#%04X", sys->cpu->pr-1);
+                setcerr(209, errpr);    /* not GR in operand x */
+                goto execerr;
+            }
             val = sys->cpu->gr[x_r2];
         }
         else if(cmdtype ==  R_ADR_X || cmdtype == R_ADR_X_ || cmdtype == ADR_X) {
-            assert(x_r2 < GRSIZE);
+            /* オペランドの数値が汎用レジスタの範囲外の場合はエラー */
+            if(x_r2 > GRSIZE - 1) {
+                sprintf(errpr, "PR:#%04X", sys->cpu->pr-1);
+                setcerr(209, errpr);    /* not GR in operand x */
+                goto execerr;
+            }
             /* 実効アドレス(値または値が示す番地)を取得  */
             val = sys->memory[sys->cpu->pr++];
             /* 指標アドレスを加算  */
@@ -544,5 +555,6 @@ bool exec()
     return true;
 execerr:
     fprintf(stderr, "Execute error - %d: %s\n", cerr->num, cerr->msg);
+    cerr->msg = NULL;           /* Ubuntu 10.04 PPCでcerr.msg開放時にSegmentation Faultが発生する現象を回避するため */
     return false;
 }