From 738eeba229e4087da2b6a42154f6b2ed16c80c5c Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Thu, 19 Apr 2012 12:09:30 -0700 Subject: [PATCH] removed checks for setting literal operands 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 | 1 + dcpu16.c | 80 ++++++++++++++++++++------------------------------------ 2 files changed, 29 insertions(+), 52 deletions(-) diff --git a/Makefile b/Makefile index 1bc867c..fd05c43 100644 --- 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 diff --git a/dcpu16.c b/dcpu16.c index 772aec8..7d7456a 100644 --- 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) { -- 2.43.2