commit 802aa51d54fa290fe6681ca6ae5dbbebc1611783
parent f334086721e222d88c7a7271fc1f256fe4ecd1e7
Author: Andrew Alderwick <andrew@alderwick.co.uk>
Date: Sun, 27 Jun 2021 21:45:50 +0100
Added Console/vector implementation with example
Diffstat:
4 files changed, 47 insertions(+), 15 deletions(-)
diff --git a/projects/examples/blank.tal b/projects/examples/blank.tal
@@ -10,8 +10,8 @@
( devices )
-|00 @System [ &vector $2 &wst $1 &rst $1 &pad $4 &r $2 &g $2 &b $2 ]
-|10 @Console [ &pad $8 &char $1 &byte $1 &short $2 &string $2 ]
+|00 @System [ &vector $2 &wst $1 &rst $1 &pad $4 &r $2 &g $2 &b $2 ]
+|10 @Console [ &vector $2 &pad $6 &char $1 &byte $1 &short $2 &string $2 ]
|20 @Screen [ &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &color $1 ]
|30 @Audio0 [ &vector $2 &position $2 &output $1 &pad $3 &adsr $2 &length $2 &addr $2 &volume $1 &pitch $1 ]
|40 @Audio1 [ &vector $2 &position $2 &output $1 &pad $3 &adsr $2 &length $2 &addr $2 &volume $1 &pitch $1 ]
diff --git a/projects/examples/devices/console.echo.tal b/projects/examples/devices/console.echo.tal
@@ -0,0 +1,14 @@
+( dev/console )
+
+|10 @Console [ &vector $2 &pad $6 &char $1 &byte $1 &short $2 &string $2 ]
+
+( init )
+
+|0100 ( -> )
+ ;on-stdin .Console/vector DEO2
+BRK
+
+@on-stdin ( -> )
+ .Console/char DEI .Console/byte DEO
+ #0a .Console/char DEO
+BRK
+\ No newline at end of file
diff --git a/src/uxncli.c b/src/uxncli.c
@@ -1,5 +1,7 @@
#include <stdio.h>
#include <time.h>
+#include <unistd.h>
+#include <string.h>
#include "uxn.h"
/*
@@ -15,6 +17,8 @@ WITH REGARD TO THIS SOFTWARE.
#pragma mark - Core
+static Device *devconsole;
+
int
error(char *msg, const char *err)
{
@@ -41,14 +45,16 @@ printstack(Stack *s)
void
console_talk(Device *d, Uint8 b0, Uint8 w)
{
+ char buffer[7], *p = buffer;
+ int len = 0;
if(!w) return;
switch(b0) {
- case 0x8: printf("%c", d->dat[0x8]); break;
- case 0x9: printf("0x%02x", d->dat[0x9]); break;
- case 0xb: printf("0x%04x", mempeek16(d->dat, 0xa)); break;
- case 0xd: printf("%s", &d->mem[mempeek16(d->dat, 0xc)]); break;
+ case 0x8: len = 1, p = (char *)&d->dat[0x8]; break;
+ case 0x9: len = sprintf(p, "0x%02x", d->dat[0x9]); break;
+ case 0xb: len = sprintf(p, "0x%04x", mempeek16(d->dat, 0xa)); break;
+ case 0xd: len = strlen(p = (char *)&d->mem[mempeek16(d->dat, 0xc)]); break;
}
- fflush(stdout);
+ if(len) write(1, p, len);
}
void
@@ -106,6 +112,9 @@ start(Uxn *u)
{
if(!evaluxn(u, PAGE_PROGRAM))
return error("Reset", "Failed");
+ while(mempeek16(devconsole->dat, 0))
+ while(read(0, &devconsole->dat[0x8], 1) > 0)
+ evaluxn(u, mempeek16(devconsole->dat, 0));
return 1;
}
@@ -122,7 +131,7 @@ main(int argc, char **argv)
return error("Load", "Failed");
portuxn(&u, 0x0, "empty", nil_talk);
- portuxn(&u, 0x1, "console", console_talk);
+ devconsole = portuxn(&u, 0x1, "console", console_talk);
portuxn(&u, 0x2, "empty", nil_talk);
portuxn(&u, 0x3, "empty", nil_talk);
portuxn(&u, 0x4, "empty", nil_talk);
diff --git a/src/uxnemu.c b/src/uxnemu.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <unistd.h>
#include <time.h>
+#include <fcntl.h>
#include "uxn.h"
#pragma GCC diagnostic push
@@ -28,7 +29,7 @@ static SDL_Texture *fgTexture, *bgTexture;
static SDL_Rect gRect;
static Ppu ppu;
static Apu apu[POLYPHONY];
-static Device *devscreen, *devmouse, *devctrl, *devaudio0;
+static Device *devscreen, *devmouse, *devctrl, *devaudio0, *devconsole;
#define PAD 16
@@ -234,14 +235,16 @@ system_talk(Device *d, Uint8 b0, Uint8 w)
void
console_talk(Device *d, Uint8 b0, Uint8 w)
{
+ char buffer[7], *p = buffer;
+ int len = 0;
if(!w) return;
switch(b0) {
- case 0x8: write(1, &d->dat[0x8], 1); break;
- case 0x9: fprintf(stdout, "0x%02x", d->dat[0x9]); break;
- case 0xb: fprintf(stdout, "0x%04x", mempeek16(d->dat, 0xa)); break;
- case 0xd: fprintf(stdout, "%s", &d->mem[mempeek16(d->dat, 0xc)]); break;
+ case 0x8: len = 1, p = (char *)&d->dat[0x8]; break;
+ case 0x9: len = sprintf(p, "0x%02x", d->dat[0x9]); break;
+ case 0xb: len = sprintf(p, "0x%04x", mempeek16(d->dat, 0xa)); break;
+ case 0xd: len = strlen(p = (char *)&d->mem[mempeek16(d->dat, 0xc)]); break;
}
- fflush(stdout);
+ if(len) write(1, p, len);
}
void
@@ -374,6 +377,8 @@ start(Uxn *u)
break;
}
}
+ while(read(0, &devconsole->dat[0x8], 1) > 0)
+ evaluxn(u, mempeek16(devconsole->dat, 0));
evaluxn(u, mempeek16(devscreen->dat, 0));
if(reqdraw)
redraw(u);
@@ -391,6 +396,9 @@ main(int argc, char **argv)
Uxn u;
zoom = 2;
+ /* set stdin nonblocking */
+ fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK);
+
if(argc < 2)
return error("Input", "Missing");
if(!bootuxn(&u))
@@ -401,7 +409,7 @@ main(int argc, char **argv)
return error("Init", "Failed");
portuxn(&u, 0x0, "system", system_talk);
- portuxn(&u, 0x1, "console", console_talk);
+ devconsole = portuxn(&u, 0x1, "console", console_talk);
devscreen = portuxn(&u, 0x2, "screen", screen_talk);
devaudio0 = portuxn(&u, 0x3, "audio0", audio_talk);
portuxn(&u, 0x4, "audio1", audio_talk);