commit 220c3676e5e73e13a0e2aafaea06f873d8a12d2b
parent de22c37e07b4f873d795c54ec3a990ba7f355a74
Author: neauoire <aliceffekt@gmail.com>
Date:   Tue,  9 Feb 2021 09:00:27 -0800
Moved dev code out of uxn
Diffstat:
3 files changed, 21 insertions(+), 17 deletions(-)
diff --git a/cli.c b/cli.c
@@ -21,13 +21,18 @@ error(char *msg, const char *err)
 }
 
 void
-console_onread(void)
+console_onread(Uint8 *b)
 {
+	(void)b;
 }
 
 void
-console_onwrite(void)
+console_onwrite(Uint8 *b)
 {
+	if(b) {
+		printf("%c", *b);
+		*b = 0x00;
+	}
 }
 
 void
diff --git a/uxn.c b/uxn.c
@@ -114,15 +114,12 @@ lituxn(Uxn *u, Uint8 instr)
 }
 
 int
-devuxn(Uxn *u) /* experimental */
+devuxn(Uxn *u)
 {
 	int i;
 	for(i = 0; i < u->devices; ++i) {
-		Uint16 addr = u->dev[i].w;
-		if(u->ram.dat[addr]) {
-			printf("%c", u->ram.dat[addr]);
-			u->ram.dat[addr] = 0;
-		}
+		u->dev[i].wfn(&u->ram.dat[u->dev[i].w]);
+		u->dev[i].rfn(&u->ram.dat[u->dev[i].r]);
 	}
 	return 1;
 }
@@ -147,10 +144,8 @@ opcuxn(Uxn *u, Uint8 instr)
 }
 
 int
-parse(Uxn *u) /* TODO: Rename */
+stepuxn(Uxn *u, Uint8 instr)
 {
-	Uint8 instr = u->ram.dat[u->ram.ptr++];
-	u->counter++;
 	if(u->literal > 0)
 		return lituxn(u, instr);
 	else
@@ -162,8 +157,12 @@ evaluxn(Uxn *u, Uint16 vec)
 {
 	u->ram.ptr = vec;
 	setflag(&u->status, FLAG_HALT, 0);
-	while(!(u->status & FLAG_HALT) && parse(u))
-		;
+	while(!(u->status & FLAG_HALT)) {
+		Uint8 instr = u->ram.dat[u->ram.ptr++];
+		if(!stepuxn(u, instr))
+			return 0;
+		u->counter++;
+	}
 	return 1;
 }
 
@@ -198,7 +197,7 @@ loaduxn(Uxn *u, char *filepath)
 /* to start: evaluxn(u, u->vreset); */
 
 int
-portuxn(Uxn *u, Uint16 r, Uint16 w, void (*onread)(), void (*onwrite)())
+portuxn(Uxn *u, Uint16 r, Uint16 w, void (*onread)(Uint8 *), void (*onwrite)(Uint8 *))
 {
 	Device *d = &u->dev[u->devices++];
 	d->r = r;
diff --git a/uxn.h b/uxn.h
@@ -36,8 +36,8 @@ typedef struct {
 
 typedef struct {
 	Uint16 r, w;
-	void (*rfn)(void);
-	void (*wfn)(void);
+	void (*rfn)(Uint8 *);
+	void (*wfn)(Uint8 *);
 } Device;
 
 typedef struct {
@@ -54,4 +54,4 @@ int getflag(Uint8 *status, char flag);
 int loaduxn(Uxn *c, char *filepath);
 int bootuxn(Uxn *c);
 int evaluxn(Uxn *u, Uint16 vec);
-int portuxn(Uxn *u, Uint16 r, Uint16 w, void (*onread)(), void (*onwrite)());
+int portuxn(Uxn *u, Uint16 r, Uint16 w, void (*onread)(Uint8 *), void (*onwrite)(Uint8 *));