removed checks for setting literal operands
authorJustin Wind <justin.wind@gmail.com>
Thu, 19 Apr 2012 19:09:30 +0000 (12:09 -0700)
committerJustin Wind <justin.wind@gmail.com>
Thu, 19 Apr 2012 19:09:30 +0000 (12:09 -0700)
opcodes just set the scratch register the literal is stored in, causing no harm nor later effect.
also added _XOPEN_SOURCE=600 define to Makefile, now compiles cleanly on both darwin and linux.

Makefile
dcpu16.c

index 1bc867cc92eceae4da88a337929befad75c7cdf1..fd05c43b8d484609d68e7835d5512bf4629e4f60 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,6 +8,7 @@ endif
 PROGRAMS = as-dcpu16 vm-dcpu16
 SOURCES = common.c dcpu16.c as-dcpu16.c vm-dcpu16.c
 
+CPPFLAGS = -D_XOPEN_SOURCE=600
 CFLAGS = -g -Wall -Wextra -pedantic -std=c99
 LDFLAGS = -lreadline
 
index 772aec84917cd407884f7a281a6e0ed3be547d29..7d7456a828a71b96a135457315e7abe4c4585aba 100644 (file)
--- a/dcpu16.c
+++ b/dcpu16.c
@@ -25,7 +25,6 @@
  *
  *  TODO
  *    change api to print into buffers rather than stdio
- *    drop checks for assigning to literals -- it won't affect anything anyhow
  *    refactor opcode functiontables into switch statements
  */
 
@@ -34,7 +33,7 @@ static const char * const src_id_ = "$Id$";
 #define OPCODE_BASIC_BITS   4
 #define OPCODE_OPERAND_BITS 6
 
-static const char regnames_[] = "ABCXYZIJ";
+static const char * const regnames_ = "ABCXYZIJ";
 
 /* some default warning and debug reporting functions, which can be overridden by clients */
 #define WARN(...) do { if (warn_cb_) warn_cb_(__VA_ARGS__); } while (0)
@@ -296,8 +295,8 @@ static const struct opcode_entry opcode_nbi_entries[] = {
 /* basic opcodes */
 
 /*
-    N.B. the following function does not decode values, as the nbi
-    instructions only have one operand.
+    N.B. the following function does not decode values, (thus does not advance pc &c)
+    Decoding is handled by the opcode functions it calls.
 */
 OP_IMPL(_nbi_) {
     /* non-basic instruction */
@@ -320,14 +319,16 @@ OP_IMPL(set) {
     /* sets a to b */
 
     ACCT_R(ev_b_addr);
-    ACCT_W(ev_a_addr);
 
-    /* only set non-literal target */
-    if (!lit_a) {
-        *a = *b;
-    }
+    /*
+        if a is a literal, it's aimed at a scratch register,
+        so it's fine to update, as it won't have any effect.
+     */
+    *a = *b;
 
     d->cycle += 1;
+
+    ACCT_W(ev_a_addr);
 }
 
 OP_IMPL(add) {
@@ -338,9 +339,7 @@ OP_IMPL(add) {
     ACCT_R(ev_b_addr);
     ACCT_R(ev_a_addr);
 
-    if (!lit_a) {
-        *a = acc;
-    }
+    *a = acc;
     d->o = (acc > 0xffff);
 
     d->cycle += 2;
@@ -356,10 +355,9 @@ OP_IMPL(sub) {
     ACCT_R(ev_b_addr);
     ACCT_R(ev_a_addr);
 
-    if (!lit_a) {
-        *a = acc;
-    }
+    *a = acc;
     d->o = (acc > 0xffff);
+
     d->cycle += 2;
 
     ACCT_W(ev_a_addr);
@@ -373,10 +371,9 @@ OP_IMPL(mul) {
     ACCT_R(ev_b_addr);
     ACCT_R(ev_a_addr);
 
-    if (!lit_a) {
-        *a = acc;
-    }
+    *a = acc;
     d->o = acc >> 16;
+
     d->cycle += 2;
 
     ACCT_W(ev_a_addr);
@@ -390,19 +387,11 @@ OP_IMPL(div) {
     ACCT_R(ev_a_addr);
 
     if (*b == 0) {
-        if (!lit_a) {
-            *a = 0;
-        }
+        *a = 0;
         d->o = 0;
     } else {
-        unsigned int acc = *a / *b;
-
-        if (!lit_a) {
-            *a = acc;
-        }
-
-        acc = (*a << 16) / *b;
-        d->o = acc;
+        *a = *a / *b;
+        d->o = (*a << 16) / *b;
     }
 
     d->cycle += 3;
@@ -418,13 +407,9 @@ OP_IMPL(mod) {
     ACCT_R(ev_a_addr);
 
     if (*b == 0) {
-        if (!lit_a) {
-            *a = 0;
-        }
+        *a = 0;
     } else {
-        if (!lit_a) {
-            *a = *a % *b;
-        }
+        *a = *a % *b;
     }
 
     d->cycle += 3;
@@ -440,9 +425,8 @@ OP_IMPL(shl) {
     ACCT_R(ev_b_addr);
     ACCT_R(ev_a_addr);
 
-    if (!lit_a) {
-        *a = acc;
-    }
+    *a = acc;
+
     d->o = acc >> 16;
 
     d->cycle += 2;
@@ -458,9 +442,7 @@ OP_IMPL(shr) {
     ACCT_R(ev_b_addr);
     ACCT_R(ev_a_addr);
 
-    if (!lit_a) {
-        *a = acc;
-    }
+    *a = acc;
     d->o = (*a << 16) >> *b;
 
     d->cycle += 2;
@@ -475,9 +457,7 @@ OP_IMPL(and) {
     ACCT_R(ev_b_addr);
     ACCT_R(ev_a_addr);
 
-    if (!lit_a) {
-        *a = *a & *b;
-    }
+    *a = *a & *b;
 
     d->cycle += 1;
 
@@ -491,9 +471,7 @@ OP_IMPL(bor) {
     ACCT_R(ev_b_addr);
     ACCT_R(ev_a_addr);
 
-    if (!lit_a) {
-        *a = *a | *b;
-    }
+    *a = *a | *b;
 
     d->cycle += 1;
 
@@ -507,13 +485,11 @@ OP_IMPL(xor) {
     ACCT_R(ev_b_addr);
     ACCT_R(ev_a_addr);
 
-    if (!lit_a) {
-        *a = *a ^ *b;
-    }
-
-    ACCT_W(ev_a_addr);
+    *a = *a ^ *b;
 
     d->cycle += 1;
+
+    ACCT_W(ev_a_addr);
 }
 
 OP_IMPL(ife) {