commit 67676821b271e9d3d4605914067704734356e05c
parent 1beb5da6b6262582aa02877989e5788fb888cf1a
Author: Brian Swetland <swetland@frotz.net>
Date: Wed, 4 Apr 2012 20:48:20 -0700
a16: add jsr support, bugfixes, assemble example from dcpu16 spec
Diffstat:
2 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/a16.c b/a16.c
@@ -268,8 +268,16 @@ int assemble_operand(void) {
next();
if (token == tCBRACK) {
return 0x1e;
- } else if ((token >= tA) && (token <= tJ)) {
- return 0x10 | (token & 7);
+ } else if (token == tCOMMA) {
+ u16 n;
+ next();
+ if ((token >= tA) && (token <= tJ)) {
+ n = 0x10 | (token & 7);
+ } else {
+ die("invalid register");
+ }
+ expect(tCBRACK);
+ return n;
} else {
die("invalid operand");
}
@@ -289,6 +297,7 @@ void assemble_binop(int op) {
}
void assemble(const char *fn) {
+ u16 pc, n;
fin = fopen(fn, "r");
filename = fn;
linenumber = 0;
@@ -312,6 +321,11 @@ void assemble(const char *fn) {
case tIFN: case tIFG: case tIFB:
assemble_binop(token - tXXX);
continue;
+ case tJSR:
+ pc = PC++;
+ n = assemble_operand();
+ image[pc] = (n << 10) | 0x0010;
+ continue;
default:
die("unexpected: %s", tnames[token]);
}
diff --git a/tests/example.s b/tests/example.s
@@ -0,0 +1,30 @@
+; DCPU-16 Example from Specification
+; Copyright 2012 Mojang
+
+; Try some basic stuff
+ SET A, 0x30 ; 7c01 0030
+ SET [0x1000], 0x20 ; 7de1 1000 0020
+ SUB A, [0x1000] ; 7803 1000
+ IFN A, 0x10 ; c00d
+ SET PC, crash ; 7dc1 001a [*]
+
+; Do a loopy thing
+ SET I, 10 ; a861
+ SET A, 0x2000 ; 7c01 2000
+:loop
+ SET [0x2000,I], [A] ; 2161 2000
+ SUB I, 1 ; 8463
+ IFN I, 0 ; 806d
+ SET PC, loop ; 7dc1 000d [*]
+
+; Call a subroutine
+ SET X, 0x4 ; 9031
+ JSR testsub ; 7c10 0018 [*]
+ SET PC, crash ; 7dc1 001a [*]
+
+:testsub
+ SHL X, 4 ; 9037
+ SET PC, POP ; 61c1
+
+:crash
+ WORD 0xeee0