commit 950e054e1852800d754a54459afaa6d75f685318
parent 5f7046b6897415957c6759c755d470ff138bd494
Author: Brian Swetland <swetland@frotz.net>
Date: Wed, 4 Apr 2012 03:54:40 -0700
- program load, tracing, test binary (from spec)
Diffstat:
A | Makefile | | | 6 | ++++++ |
M | dcpu.c | | | 38 | ++++++++++++++++++++++++++++++++++++-- |
A | test.hex | | | 32 | ++++++++++++++++++++++++++++++++ |
3 files changed, 74 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,6 @@
+
+dcpu: dcpu.c
+ gcc -Wall -o dcpu dcpu.c
+
+clean:
+ rm -f dcpu
diff --git a/dcpu.c b/dcpu.c
@@ -7,6 +7,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
+#include <string.h>
+#include <ctype.h>
typedef uint16_t u16;
typedef uint32_t u32;
@@ -77,7 +79,8 @@ void dcpu_step(struct dcpu *d) {
}
return;
default:
- return;
+ fprintf(stderr, "< ILLEGAL OPCODE >\n");
+ exit(0);
}
}
@@ -133,11 +136,42 @@ cond:
d->skip = 0;
return;
}
- d->skip = res;
+ d->skip = !res;
+}
+
+void dumpheader(void) {
+ fprintf(stderr,
+ "PC SP OV SKIP A B C X Y Z I J\n"
+ "---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----\n");
}
+void dumpstate(struct dcpu *d) {
+ fprintf(stderr,
+ "%04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x\n",
+ d->pc, d->sp, d->ov, d->skip,
+ d->r[0], d->r[1], d->r[2], d->r[3], d->r[4], d->r[5], d->r[6], d->r[7]);
+}
+
+void load(struct dcpu *d, FILE *fp) {
+ char buf[128];
+ u16 n = 0;
+ while (!feof(fp) && fgets(buf, 128, fp)) {
+ if (!isalnum(buf[0]))
+ continue;
+ d->m[n++] = strtoul(buf, 0, 16);
+ }
+ fprintf(stderr,"< LOADED %d WORDS >\n", n);
+}
+
int main(int argc, char **argv) {
struct dcpu d;
+ memset(&d, 0, sizeof(d));
+ load(&d, stdin);
+ dumpheader();
+ for (;;) {
+ dumpstate(&d);
+ dcpu_step(&d);
+ }
return 0;
}
diff --git a/test.hex b/test.hex
@@ -0,0 +1,32 @@
+7c01
+0030
+7de1
+1000
+0020
+7803
+1000
+c00d
+7dc1
+001a
+a861
+7c01
+2000
+2161
+2000
+8463
+806d
+7dc1
+000d
+9031
+7c10
+0018
+7dc1
+001a
+9037
+61c1
+7dc1
+001a
+0000
+0000
+0000
+0000