commit 75836878bcd0d2e2f4e80655f4b6db546b4a958e
parent 34128884c23479e29345aef533e6702f7043771d
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Tue, 15 Nov 2022 20:51:29 -0500
app/uxn: add some stuff until a CC bug is hit
Diffstat:
4 files changed, 33 insertions(+), 17 deletions(-)
diff --git a/fs/app/uxn/vm.c b/fs/app/uxn/vm.c
@@ -23,8 +23,11 @@ typedef void (*VMOP) ();
// TODO: for debugging purposes during development, we sometimes omit "static".
unsigned char ram[$10000];
// TODO: allow single line decl
-Stack wst;
+static Stack wst;
static Stack rst;
+// TODO: under i386, having a global struct pointer doesn't work and corrupts
+// memory all around.
+// static Stack *s;
static Device dev[$10];
static unsigned short pc; // Program Counter
@@ -33,11 +36,13 @@ static void error(int code) {
abort();
}
-void push8(Stack *s, unsigned char val) {
+void push8(unsigned char val) {
+ Stack *s = &wst;
if (s->ptr == $ff) { error(2); } else { s->dat[s->ptr++] = val; }
}
-void push16(Stack *s, unsigned short val) {
+void push(unsigned short val) {
+ Stack *s = &wst;
if (s->ptr >= $fe) { error(2); } else {
s->dat[s->ptr] = val >> 8;
s->dat[s->ptr+1] = val;
@@ -45,12 +50,14 @@ void push16(Stack *s, unsigned short val) {
}
}
-unsigned char pop8(Stack *s) {
+unsigned char pop8() {
+ Stack *s = &wst;
if (!s->ptr) { error(0); } else { return s->dat[--s->ptr]; }
}
-unsigned short pop16(Stack *s) {
+unsigned short pop16() {
unsigned short val;
+ Stack *s = &wst;
if (s->ptr <= 1) { error(0); } else {
s->ptr -= 2;
val = s->dat[s->ptr] << 8;
@@ -81,21 +88,28 @@ unsigned short peek16(unsigned short addr) {
/* Operations */
static void _err() { error(42); }
-static void LIT() { push8(&wst, peek8(pc++)); }
+static void LIT() { push8(peek8(pc++)); }
+static void INC() { push8(pop8()+1); }
+static void DUP() { unsigned char x = pop8(); push8(x); push8(x); }
+// TODO: this doesn't work under i386
+static void ADD() { push8(pop8() + pop8()); }
static VMOP ops[$20] = {
- LIT, _err, _err, _err, _err, _err, _err, _err,
+ LIT, INC, _err, _err, _err, _err, DUP, _err,
_err, _err, _err, _err, _err, _err, _err, _err,
_err, _err, _err, _err, _err, _err, _err, _err,
- _err, _err, _err, _err, _err, _err, _err, _err};
+ ADD, _err, _err, _err, _err, _err, _err, _err};
-void uxn_exec1() {
- unsigned char op = peek8(pc++);
- op &= $1f;
- ops[op]();
+void uxn_exec() {
+ unsigned char op;
+ while (op = peek8(pc++)) {
+ op &= $1f;
+ ops[op]();
+ }
}
void uxn_init() {
- pc = 0; // we'll init it to $100 later
+ pc = $100;
wst.ptr = 0;
rst.ptr = 0;
+ // s = &wst;
}
diff --git a/fs/tests/app/uxn/dummy.bin b/fs/tests/app/uxn/dummy.bin
Binary files differ.
diff --git a/fs/tests/app/uxn/dummy.tal b/fs/tests/app/uxn/dummy.tal
@@ -0,0 +1,2 @@
+|0100
+#2a DUP INC ADD BRK
diff --git a/fs/tests/app/uxn/vm.fs b/fs/tests/app/uxn/vm.fs
@@ -2,10 +2,10 @@
?f<< /app/uxn/vm.fs
testbegin
\ Testing uxn VM
-\ A dummy handcrafted ROM with a 42 literal in it, followed by BRK
-ram S" /tests/app/uxn/dummy.bin" curpath :find# Path :open
+\ Compiled from dummy.tal with uxn's official assembler
+ram $100 + S" /tests/app/uxn/dummy.bin" curpath :find# Path :open
tuck File :readall File :close
uxn_init
-uxn_exec1
-wst pop8 42 #eq
+uxn_exec
+pop8 85 #eq
testend